[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
https://github.com/wsmoses edited https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From ee967e7cd00f3943494ca04576325c2f28f2c614 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [Clang] Add codegen option to add passbuilder callback functions --- clang/examples/CMakeLists.txt | 1 + .../LLVMPrintFunctionNames/CMakeLists.txt | 23 ++ .../LLVMPrintFunctionNames.cpp| 78 +++ .../LLVMPrintFunctionNames.exports| 0 clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 2 + clang/test/CMakeLists.txt | 1 + clang/test/Frontend/llvmplugins.c | 5 ++ 8 files changed, 116 insertions(+) create mode 100644 clang/examples/LLVMPrintFunctionNames/CMakeLists.txt create mode 100644 clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp create mode 100644 clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.exports create mode 100644 clang/test/Frontend/llvmplugins.c diff --git a/clang/examples/CMakeLists.txt b/clang/examples/CMakeLists.txt index 8be327bcdbb9d63..2396ecac16b2dc6 100644 --- a/clang/examples/CMakeLists.txt +++ b/clang/examples/CMakeLists.txt @@ -4,6 +4,7 @@ if(NOT CLANG_BUILD_EXAMPLES) endif() if(CLANG_PLUGIN_SUPPORT) + add_subdirectory(LLVMPrintFunctionNames) add_subdirectory(PrintFunctionNames) add_subdirectory(AnnotateFunctions) add_subdirectory(Attribute) diff --git a/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt b/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt new file mode 100644 index 000..7c9a1e65b66524a --- /dev/null +++ b/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt @@ -0,0 +1,23 @@ +# If we don't need RTTI or EH, there's no reason to export anything +# from the plugin. +if( NOT MSVC ) # MSVC mangles symbols differently, and +# PrintLLVMFunctionNames.export contains C++ symbols. + if( NOT LLVM_REQUIRES_RTTI ) +if( NOT LLVM_REQUIRES_EH ) + set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/LLVMPrintFunctionNames.exports) +endif() + endif() +endif() + +add_llvm_library(LLVMPrintFunctionNames MODULE LLVMPrintFunctionNames.cpp PLUGIN_TOOL clang) + +if(WIN32 OR CYGWIN) + set(LLVM_LINK_COMPONENTS +Support + ) + clang_target_link_libraries(LLVMPrintFunctionNames PRIVATE +clangAST +clangBasic +clangFrontend +) +endif() diff --git a/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp b/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp new file mode 100644 index 000..739740cecdf47d2 --- /dev/null +++ b/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp @@ -0,0 +1,78 @@ +//===- LLVMPrintFunctionNames.cpp +//-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Example clang plugin which simply prints the names of all the functions +// within the generated LLVM code. +// +//===--===// + +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendPluginRegistry.h" +#include "clang/Sema/Sema.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Passes/OptimizationLevel.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Support/raw_ostream.h" +using namespace clang; + +namespace { + +class PrintPass final : public llvm::AnalysisInfoMixin { + friend struct llvm::AnalysisInfoMixin; + +private: + static llvm::AnalysisKey key; + +public: + using Result = llvm::PreservedAnalyses; + + Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { +for (auto &F : M) + llvm::outs() << "[PrintPass] Found function: " << F.getName() << "\n"; +return llvm::PreservedAnalyses::all(); + } + static bool isRequired() { return true; } +}; + +void PrintCallback(llvm::PassBuilder &PB) { + PB.registerPipelineStartEPCallback( + [](llvm::ModulePassManager &MPM, llvm::OptimizationLevel) { +MPM.addPass(PrintPass()); + }); +} + +class LLVMPrintFunctionsConsumer : public ASTConsumer { +public: + LLVMPrintFunctionsConsumer(CompilerInstance &Instance) { +Instance.getCodeGenOpts().PassBuilderCallbacks.push_back(PrintCallback); + } +}; + +class LLVMPrintFunctionNamesAction : public PluginASTAction { +protected: + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef) override { +return std::make_unique(CI); + } + bool ParseArgs(const CompilerInst
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From ff6e93b1f9d57a73be1407f515e5c9c58bbd5b4c Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [Clang] Add codegen option to add passbuilder callback functions --- clang/examples/CMakeLists.txt | 1 + .../LLVMPrintFunctionNames/CMakeLists.txt | 23 ++ .../LLVMPrintFunctionNames.cpp| 78 +++ .../LLVMPrintFunctionNames.exports| 0 clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 2 + clang/test/CMakeLists.txt | 1 + clang/test/Frontend/llvmplugins.c | 5 ++ 8 files changed, 116 insertions(+) create mode 100644 clang/examples/LLVMPrintFunctionNames/CMakeLists.txt create mode 100644 clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp create mode 100644 clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.exports create mode 100644 clang/test/Frontend/llvmplugins.c diff --git a/clang/examples/CMakeLists.txt b/clang/examples/CMakeLists.txt index 8be327bcdbb9d63..2396ecac16b2dc6 100644 --- a/clang/examples/CMakeLists.txt +++ b/clang/examples/CMakeLists.txt @@ -4,6 +4,7 @@ if(NOT CLANG_BUILD_EXAMPLES) endif() if(CLANG_PLUGIN_SUPPORT) + add_subdirectory(LLVMPrintFunctionNames) add_subdirectory(PrintFunctionNames) add_subdirectory(AnnotateFunctions) add_subdirectory(Attribute) diff --git a/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt b/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt new file mode 100644 index 000..61e7a7842e2f3e2 --- /dev/null +++ b/clang/examples/LLVMPrintFunctionNames/CMakeLists.txt @@ -0,0 +1,23 @@ +# If we don't need RTTI or EH, there's no reason to export anything +# from the plugin. +if(NOT MSVC) # MSVC mangles symbols differently, and +# PrintLLVMFunctionNames.export contains C++ symbols. + if(NOT LLVM_REQUIRES_RTTI) +if(NOT LLVM_REQUIRES_EH) + set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/LLVMPrintFunctionNames.exports) +endif() + endif() +endif() + +add_llvm_library(LLVMPrintFunctionNames MODULE LLVMPrintFunctionNames.cpp PLUGIN_TOOL clang) + +if(WIN32 OR CYGWIN) + set(LLVM_LINK_COMPONENTS +Support + ) + clang_target_link_libraries(LLVMPrintFunctionNames PRIVATE +clangAST +clangBasic +clangFrontend +) +endif() diff --git a/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp b/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp new file mode 100644 index 000..739740cecdf47d2 --- /dev/null +++ b/clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp @@ -0,0 +1,78 @@ +//===- LLVMPrintFunctionNames.cpp +//-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Example clang plugin which simply prints the names of all the functions +// within the generated LLVM code. +// +//===--===// + +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendPluginRegistry.h" +#include "clang/Sema/Sema.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Passes/OptimizationLevel.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Support/raw_ostream.h" +using namespace clang; + +namespace { + +class PrintPass final : public llvm::AnalysisInfoMixin { + friend struct llvm::AnalysisInfoMixin; + +private: + static llvm::AnalysisKey key; + +public: + using Result = llvm::PreservedAnalyses; + + Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { +for (auto &F : M) + llvm::outs() << "[PrintPass] Found function: " << F.getName() << "\n"; +return llvm::PreservedAnalyses::all(); + } + static bool isRequired() { return true; } +}; + +void PrintCallback(llvm::PassBuilder &PB) { + PB.registerPipelineStartEPCallback( + [](llvm::ModulePassManager &MPM, llvm::OptimizationLevel) { +MPM.addPass(PrintPass()); + }); +} + +class LLVMPrintFunctionsConsumer : public ASTConsumer { +public: + LLVMPrintFunctionsConsumer(CompilerInstance &Instance) { +Instance.getCodeGenOpts().PassBuilderCallbacks.push_back(PrintCallback); + } +}; + +class LLVMPrintFunctionNamesAction : public PluginASTAction { +protected: + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef) override { +return std::make_unique(CI); + } + bool ParseArgs(const CompilerInstance &
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
https://github.com/wsmoses closed https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses created https://github.com/llvm/llvm-project/pull/71894 As discussed in [this RFC](https://discourse.llvm.org/t/rfc-attribute-no-sizeof/74695) this PR implements a. new user-level sizeless attribute. This prevents types or variables marked with this attribute from having sizeof or alignof taken, including of struct or other types which contain sizeless types. >From fb96061f85585e15b0a15d7b89ecf93b5dd4e3f2 Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 clang/include/clang/Basic/AttrDocs.td | 31 +++ clang/include/clang/Sema/Sema.h | 20 +- clang/lib/AST/Type.cpp| 55 ++- clang/lib/AST/TypePrinter.cpp | 1 + clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 6 +-- clang/lib/Sema/SemaType.cpp | 29 -- clang/test/Sema/sizeless.c| 22 +++ 9 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..2675aaad5a96a45 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..db1e00a2747f875 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,13 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, unsigned DiagID, +const Ts &... Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2578,13 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, +const Ts &... Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::No
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/71894 >From 3e23febb464791a4b24ec73476ef988d2e98ca18 Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 31 + clang/include/clang/Sema/Sema.h | 22 +- clang/lib/AST/Type.cpp| 63 ++- clang/lib/AST/TypePrinter.cpp | 3 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 6 +-- clang/lib/Sema/SemaType.cpp | 28 ++-- clang/test/Sema/sizeless.c| 22 ++ 9 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..2675aaad5a96a45 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..3e6988759e19a00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,14 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, + unsigned DiagID, const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2579,14 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, + const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c8e452e2feab0bf..a142c29c5c579e6 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -43,6 +43,7 @@ #include "llvm/ADT/APSInt.h" #include "llv
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/71894 >From 4de8e238b6680540f4bc884c66430eb2974512c2 Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 31 + clang/include/clang/Sema/Sema.h | 22 +- clang/lib/AST/Type.cpp| 63 ++- clang/lib/AST/TypePrinter.cpp | 3 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 24 +- clang/lib/Sema/SemaType.cpp | 28 ++-- clang/test/Sema/sizeless.c| 22 ++ 9 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..2675aaad5a96a45 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..3e6988759e19a00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,14 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, + unsigned DiagID, const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2579,14 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, + const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c8e452e2feab0bf..a142c29c5c579e6 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -43,6 +43,7 @@ #include "llvm/ADT/APSInt.h" #incl
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/71894 >From 4de8e238b6680540f4bc884c66430eb2974512c2 Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH 1/2] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 31 + clang/include/clang/Sema/Sema.h | 22 +- clang/lib/AST/Type.cpp| 63 ++- clang/lib/AST/TypePrinter.cpp | 3 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 24 +- clang/lib/Sema/SemaType.cpp | 28 ++-- clang/test/Sema/sizeless.c| 22 ++ 9 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..2675aaad5a96a45 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..3e6988759e19a00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,14 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, + unsigned DiagID, const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2579,14 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, + const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c8e452e2feab0bf..a142c29c5c579e6 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -43,6 +43,7 @@ #include "llvm/ADT/APSInt.h" #
[clang] [Clang] add user-level sizeless attribute (PR #71894)
@@ -2400,7 +2402,66 @@ bool Type::isWebAssemblyTableType() const { return false; } -bool Type::isSizelessType() const { return isSizelessBuiltinType(); } +bool Type::isSizelessType() const { wsmoses wrote: isIncompleteType has a recursive descent open to suggestions and/or renaming it though. https://github.com/llvm/llvm-project/pull/71894 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/71894 >From 5feb493573e1809ddf450948bdd3c735bb4b6ffd Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 31 + clang/include/clang/Sema/Sema.h | 22 +- clang/lib/AST/Type.cpp| 63 ++- clang/lib/AST/TypePrinter.cpp | 3 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 24 +- clang/lib/Sema/SemaType.cpp | 28 ++-- clang/test/Sema/sizeless.c| 22 ++ 9 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..2675aaad5a96a45 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..3e6988759e19a00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,14 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, + unsigned DiagID, const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2579,14 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, + const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c8e452e2feab0bf..f0e8bde91332a08 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -43,6 +43,7 @@ #include "llvm/ADT/APSInt.h" #incl
[clang] [Clang] add user-level sizeless attribute (PR #71894)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/71894 >From 9202857d0100bf8a29c43472a3ce531d9afa4a2b Mon Sep 17 00:00:00 2001 From: Billy Moses Date: Thu, 9 Nov 2023 02:17:05 + Subject: [PATCH] [Clang] add user-level sizeless attribute --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 31 + clang/include/clang/Sema/Sema.h | 22 +- clang/lib/AST/Type.cpp| 63 ++- clang/lib/AST/TypePrinter.cpp | 3 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaExpr.cpp | 24 +- clang/lib/Sema/SemaType.cpp | 28 ++-- clang/test/Sema/sizeless.c| 22 ++ 9 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 clang/test/Sema/sizeless.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 60b54c155e5..591762c8be5a705 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4285,3 +4285,10 @@ def PreferredType: InheritableAttr { let Args = [TypeArgument<"Type", 1>]; let Documentation = [PreferredTypeDocumentation]; } + +def SizelessType : DeclOrTypeAttr { + let Spellings = [Clang<"sizeless">]; + let Documentation = [SizelessTypeDocs]; + let HasCustomParsing = 1; +} + diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 05703df2129f612..25500689eb4f124 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7095,6 +7095,37 @@ neither type checking rules, nor runtime semantics. In particular: }]; } +def SizelessTypeDocs : Documentation { + let Category = DocCatType; + let Heading = "sizeless"; + let Content = [{ +This attribute is used to on types to forbid the use of the sizeof operation. +This may be useful for code with scalable vectors, which may forbid the use +of sizeof on that platform already. This attribute enables more consistent +behavior by forbidding sizeof on all platforms. + +The attribute takes no arguments. + +For example: + +.. code-block:: c++ + + int* [[clang::sizeless]] f(); + +The attribute does not have any effect on the semantics of the type system, +neither type checking rules, nor runtime semantics. In particular: + +- ``std::is_same`` is true for all types + ``T``. + +- It is not permissible for overloaded functions or template specializations + to differ merely by an ``sizeless`` attribute. + +- The presence of an ``sizeless`` attribute will not affect name + mangling. + }]; +} + def WeakDocs : Documentation { let Category = DocCatDecl; let Content = [{ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a8c41492b61ac4c..3e6988759e19a00 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2275,9 +2275,13 @@ class Sema final { Normal, /// Relax the normal rules for complete types so that they include -/// sizeless built-in types. +/// sizeless built-in or sizeless user types. AcceptSizeless, +/// Relax the normal rules for complete types so that they include +/// sizeless user types, but not sizeless builtin types. +AcceptUserSizeless, + // FIXME: Eventually we should flip the default to Normal and opt in // to AcceptSizeless rather than opt out of it. Default = AcceptSizeless @@ -2539,6 +2543,14 @@ class Sema final { bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteType(Loc, T, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofType(SourceLocation Loc, QualType T, + unsigned DiagID, const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser); } @@ -2567,6 +2579,14 @@ class Sema final { bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID, const Ts &... Args) { SizelessTypeDiagnoser Diagnoser(DiagID, Args...); +return RequireCompleteExprType(E, CompleteTypeKind::AcceptUserSizeless, + Diagnoser); + } + + template + bool RequireCompleteSizeofExprType(Expr *E, unsigned DiagID, + const Ts &...Args) { +SizelessTypeDiagnoser Diagnoser(DiagID, Args...); return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c8e452e2feab0bf..03354a36bc4ec5c 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -43,6 +43,7 @@ #include "llvm/ADT/APSInt.h" #inclu
[llvm] [clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
wsmoses wrote: Let me fork off the clang part off into a separate PR from the codegenoption. For context, I'm in an environment where I can't modify the LLVM build files (but I can import them), but want to produce the modified clang, without maintaining a separate fork just to add the pass. https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From bb04e157dcd667b9baf261f07f8081a52c5481a0 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [Clang] Add codegen option to add passbuilder callback functions --- clang/include/clang/Basic/CodeGenOptions.h | 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 +++ 2 files changed, 9 insertions(+) diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
https://github.com/wsmoses edited https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
wsmoses wrote: @efriedma-quic I've removed the non-options stuff, please review it at your convenience. Let me see if I can make the other stuff work in a different way. https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add codegen option to add passbuilder callback functions (PR #70171)
wsmoses wrote: bump @efriedma-quic for reviewing this PR https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses created https://github.com/llvm/llvm-project/pull/83059 This PR adds functionality for specifying an LLVM function attribute within clang. This is necessary for transfering information from C++ into LLVM which doens't have a C++-level attribute. This functionality has been demonstrated to be helpful in a variety of ways, such as LLVM HTO. However, this attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. >From 0afeea9dcc8b02ade26e6ec0652ae1fa6ec33a7c Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 8 clang/lib/CodeGen/CodeGenFunction.cpp | 10 ++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 52 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..cb960bf7a3af6f 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,14 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for(auto Attr: TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + auto Attr = llvm::Attribute::parseAttr(getLLVMContext(), AttributeAndValue.first, AttributeAndValue.second); + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..1d8031c1a27900 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,16 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for(auto Attr: TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + auto Attr = llvm::Attribute::parseAttr(getLLVMContext(), AttributeAndValue.first, AttributeAndValue.second); + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr-llvmfn.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/83059 >From b6ab7f38406cd51670bd2a1253142243cf80a433 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 9 + clang/lib/CodeGen/CodeGenFunction.cpp | 11 +++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 54 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..0d6e1cb5124037 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,15 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + auto Attr = llvm::Attribute::parseAttr( + getLLVMContext(), AttributeAndValue.first, AttributeAndValue.second); + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..86ada81facb5e3 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,17 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + auto Attr = llvm::Attribute::parseAttr( + getLLVMContext(), AttributeAndValue.first, AttributeAndValue.second); + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr-llvmfn.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - | FileCheck %s + +void t1() __attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); + +void t1() +{ +} + +void t2(); + +void t3() { + t2() attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); +} + ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
@@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - | FileCheck %s + +void t1() __attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); + +void t1() +{ +} + +void t2(); + +void t3() { + t2() attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); +} + wsmoses wrote: Yeah sorry, should've marked this draft (still rebuilding to add the correct check lines, but wanted to open PR for visibility and comment from stakeholders re design) https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/83059 >From c01b559836ca62648c5f95a6441888514347a1ea Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 16 clang/lib/CodeGen/CodeGenFunction.cpp | 19 +++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 69 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..4725dce607eee9 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,22 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..5d0edd4bbf83c7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,25 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for (auto Attr : + template TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr-llv
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/83059 >From c01b559836ca62648c5f95a6441888514347a1ea Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH 1/2] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 16 clang/lib/CodeGen/CodeGenFunction.cpp | 19 +++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 69 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..4725dce607eee9 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,22 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..5d0edd4bbf83c7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,25 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for (auto Attr : + template TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/83059 >From c01b559836ca62648c5f95a6441888514347a1ea Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH 1/2] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 16 clang/lib/CodeGen/CodeGenFunction.cpp | 19 +++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 69 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..4725dce607eee9 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,22 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..5d0edd4bbf83c7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,25 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for (auto Attr : + template TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
@@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - | FileCheck %s + +void t1() __attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); + +void t1() +{ +} + +void t2(); + +void t3() { + t2() attribute__((llvm_fn_attr("custom_attr", "custom_value"), llvm_fn_attr("second_attr", "second_value"))); +} + wsmoses wrote: @jdoerfert okay tests added and appear working, ready for review https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/83059 >From c01b559836ca62648c5f95a6441888514347a1ea Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Mon, 26 Feb 2024 16:17:55 -0500 Subject: [PATCH 1/2] [Clang][HTO] Add clang attribute for propagating llvm-level information --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 13 + clang/lib/CodeGen/CGCall.cpp | 16 clang/lib/CodeGen/CodeGenFunction.cpp | 19 +++ clang/test/CodeGen/attr-llvmfn.c | 14 ++ 5 files changed, 69 insertions(+) create mode 100644 clang/test/CodeGen/attr-llvmfn.c diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fa191c7378dba4..044f4fada3590f 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2211,6 +2211,13 @@ def AllocAlign : InheritableAttr { let Documentation = [AllocAlignDocs]; } +def LLVMFuncAttr : InheritableAttr { + let Spellings = [Clang<"llvm_fn_attr">]; + let Args = [StringArgument<"LLVMAttrName">, StringArgument<"LLVMAttrValue">]; + let Documentation = [LLVMFuncAttrDocs]; + let InheritEvenIfAlreadyPresent = 1; +} + def NoReturn : InheritableAttr { let Spellings = [GCC<"noreturn">, Declspec<"noreturn">]; // FIXME: Does GCC allow this on the function instead? diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b96fbddd51154c..3f93bb6d6fc648 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -781,6 +781,19 @@ pointer is not sufficiently aligned. }]; } +def LLVMFuncAttrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use ``__attribute__((llvm_fn_attr(attr_name, attr_value)))`` on a function to specify an LLVM function attribute that will be added to this function. + +Note that uses of this attribute are highly compiler specific as the meaning and availability of LLVM attributes may change between compiler versions. + +This attribute is only intended for advanced users who need to specify specific information only available in LLVM attributes. Use of attributes which are not tied to a specific version of Clang (e.g. pure) should be preferred when available. + +The first arugment is a string representing the name of the LLVM attribute to be applied and the second arugment is a string representing its value. + }]; +} + def EnableIfDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index d05cf1c6e1814e..4725dce607eee9 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2465,6 +2465,22 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (TargetDecl->hasAttr()) FuncAttrs.addAttribute("aarch64_pstate_sm_body"); + +for (auto Attr : TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + FuncAttrs.addAttribute(Attr); +} } // Attach "no-builtins" attributes to: diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1ad905078d349c..5d0edd4bbf83c7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -831,6 +831,25 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; + if (D) { +for (auto Attr : + template TargetDecl->specific_attrs()) { + auto name = Attr->getLLVMAttrName(); + auto value = Attr->getLLVMAttrValue(); + + Attribute Attr; + auto EnumAttr = llvm::Attribute::getAttrKindFromName(name); + if (EnumAttr == llvm::Attribute::None) +Attr = llvm::Attribute::get(getLLVMContext(), name, value); + else { +assert(value.size() == 0 && + "enum attribute does not support value yet"); +Attr = llvm::Attribute::get(getLLVMContext(), EnumAttr); + } + Fn->addFnAttr(Attr); +} + } + // Apply xray attributes to the function (as a string, for now) bool AlwaysXRayAttr = false; if (const auto *XRayAttr = D ? D->getAttr() : nullptr) { diff --git a/clang/test/CodeGen/attr-llvmfn.c b/clang/test/CodeGen/attr-llvmfn.c new file mode 100644 index 00..72f14a726f795e --- /dev/null +++ b/clang/test/CodeGen/attr
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
wsmoses wrote: @AaronBallman @erichkeane, do you have any suggestions for paths forward, for use cases where it is guaranteed that the attribute is valid and the user (or perhaps more specifically, another Clang-tool) needs to provide information to LLVM through Clang AST/source. For example, I have a clang plugin that should apply specific LLVM (string) attributes to functions. Unfortunately, without a way to modify codegen in the clang plugin, this prevents this workflow from working without significant hacks. Presently those "hacks" are essentially making a new global variable that takes the function and an LLVM pass that parses those globals into attributes -- which is quite poor, and has issues for templates, etc. In the case of the original research study that inspired this (HTO), we built an experimental LTO replacement tool that emitted headers that contained the derived LLVM attributes that were missing for the source and found significant speedup as a result. This has even more of the implementation detail requirement that you mention, but again has the guarantee that it is emitted by the same clang. Would this be permissible as a hidden attribute, or perhaps it takes an additional argument for the LLVM version, and errs if the version doesn't match the present compiler? https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
wsmoses wrote: As another option, what if this always emits string attributes prefixed with "clang_". And any other tools that semantic assurances for what they do with it. Thus the attribute won't be tied to any LLVM attributes. https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
wsmoses wrote: So my understanding was that the annotate attribute didn't modify codegen (and thus LLVM string attributes), but perhaps I didn't use it properly. Is there any reference for that? https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses closed https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
wsmoses wrote: Okay, the AnnotateAttr doesn't create LLVM string attributes, it creates LLVM metadata, but that should sufice, closing. https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
wsmoses wrote: Hm actually reopening, for some reason the metadata isn't being pushed at O0. https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][HTO] Add clang attribute for propagating llvm-level information (PR #83059)
https://github.com/wsmoses reopened https://github.com/llvm/llvm-project/pull/83059 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses created https://github.com/llvm/llvm-project/pull/70171 Adding passbuilder callbacks presently can be done one of two ways: * a shared library plugin * modifying the extensions list (and thus extensions.def) This prevents the use of such functionality for static builds on build systems that cannot be modified, as well as prevents such functionality from being usable when using llvm/clang as a library. This creates a third mechanism for adding these callbacks, an explicit list of said callbacks. It's otherwise identical to the plugin approach, except now instead of the list of plugins each being queried for the callback function, this just runs the callback function. >From d77dd6718c0e5148d3d818717de3b6e7779cd3ac Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/lib/CodeGen/BackendUtil.cpp | 3 +++ llvm/include/llvm/Passes/PassBuilder.h | 5 + llvm/lib/LTO/LTOBackend.cpp| 3 +++ llvm/tools/opt/NewPMDriver.cpp | 6 ++ 4 files changed, 17 insertions(+) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..8dbe4d942df278d 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -909,6 +909,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Register the target library analysis directly and give it a customized // preset TLI. diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h index 2c7ceda7998eda1..d018dd1e69166f0 100644 --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -739,6 +739,11 @@ bool parseAnalysisUtilityPasses( return false; } + +//! List of pass builder callbacks to be applied, in addition to those imported +//! from plugins or LLVM extensions. +extern SmallVector> +ListRegisterPassBuilderCallbacks; } #endif diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index ccc4276e36dacf0..28aabf8bdebb4d6 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -187,6 +187,9 @@ static void RegisterPassPlugins(ArrayRef PassPlugins, #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Load requested pass plugins and let them register pass builder callbacks for (auto &PluginFN : PassPlugins) { diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp index 6ae3f87099afd64..595bc4acddee6c4 100644 --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -431,6 +431,9 @@ bool llvm::runPassPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. @@ -546,3 +549,6 @@ void llvm::printPasses(raw_ostream &OS) { PassBuilder PB; PB.printPassNames(OS); } + +llvm::SmallVector> +ListRegisterPassBuilderCallbacks; \ No newline at end of file ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From 9ccb8b7bff8adc03bf027766005762b350a803bc Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/lib/CodeGen/BackendUtil.cpp | 3 +++ llvm/include/llvm/Passes/PassBuilder.h | 5 + llvm/lib/LTO/LTOBackend.cpp| 3 +++ llvm/lib/Passes/PassBuilder.cpp| 3 +++ llvm/tools/opt/NewPMDriver.cpp | 5 - 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..8dbe4d942df278d 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -909,6 +909,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Register the target library analysis directly and give it a customized // preset TLI. diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h index 2c7ceda7998eda1..d018dd1e69166f0 100644 --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -739,6 +739,11 @@ bool parseAnalysisUtilityPasses( return false; } + +//! List of pass builder callbacks to be applied, in addition to those imported +//! from plugins or LLVM extensions. +extern SmallVector> +ListRegisterPassBuilderCallbacks; } #endif diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index ccc4276e36dacf0..28aabf8bdebb4d6 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -187,6 +187,9 @@ static void RegisterPassPlugins(ArrayRef PassPlugins, #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Load requested pass plugins and let them register pass builder callbacks for (auto &PluginFN : PassPlugins) { diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index fde759026e5d780..ff63988566b8649 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -2099,3 +2099,6 @@ void PassBuilder::registerParseTopLevelPipelineCallback( &C) { TopLevelPipelineParsingCallbacks.push_back(C); } + +llvm::SmallVector> +ListRegisterPassBuilderCallbacks; \ No newline at end of file diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp index 6ae3f87099afd64..b6b73f8d0ec7842 100644 --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -431,6 +431,9 @@ bool llvm::runPassPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. @@ -545,4 +548,4 @@ bool llvm::runPassPipeline( void llvm::printPasses(raw_ostream &OS) { PassBuilder PB; PB.printPassNames(OS); -} +} \ No newline at end of file ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From 8b0e0939ea51768b9e1257e816556d245c91134b Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/lib/CodeGen/BackendUtil.cpp | 3 +++ llvm/include/llvm/Passes/PassBuilder.h | 5 + llvm/lib/LTO/LTOBackend.cpp| 3 +++ llvm/lib/Passes/PassBuilder.cpp| 3 +++ llvm/tools/opt/NewPMDriver.cpp | 3 +++ 5 files changed, 17 insertions(+) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..8dbe4d942df278d 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -909,6 +909,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Register the target library analysis directly and give it a customized // preset TLI. diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h index 2c7ceda7998eda1..d018dd1e69166f0 100644 --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -739,6 +739,11 @@ bool parseAnalysisUtilityPasses( return false; } + +//! List of pass builder callbacks to be applied, in addition to those imported +//! from plugins or LLVM extensions. +extern SmallVector> +ListRegisterPassBuilderCallbacks; } #endif diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index ccc4276e36dacf0..28aabf8bdebb4d6 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -187,6 +187,9 @@ static void RegisterPassPlugins(ArrayRef PassPlugins, #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Load requested pass plugins and let them register pass builder callbacks for (auto &PluginFN : PassPlugins) { diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index fde759026e5d780..ff63988566b8649 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -2099,3 +2099,6 @@ void PassBuilder::registerParseTopLevelPipelineCallback( &C) { TopLevelPipelineParsingCallbacks.push_back(C); } + +llvm::SmallVector> +ListRegisterPassBuilderCallbacks; \ No newline at end of file diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp index 6ae3f87099afd64..f072120d7461d4e 100644 --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -431,6 +431,9 @@ bool llvm::runPassPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From fca61de1d6cf5fe68cfb51bfe5ca73d5d3948ba5 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/lib/CodeGen/BackendUtil.cpp | 3 +++ llvm/include/llvm/Passes/PassBuilder.h | 5 + llvm/lib/LTO/LTOBackend.cpp| 3 +++ llvm/lib/Passes/PassBuilder.cpp| 3 +++ llvm/tools/opt/NewPMDriver.cpp | 3 +++ 5 files changed, 17 insertions(+) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..8dbe4d942df278d 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -909,6 +909,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Register the target library analysis directly and give it a customized // preset TLI. diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h index 2c7ceda7998eda1..d018dd1e69166f0 100644 --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -739,6 +739,11 @@ bool parseAnalysisUtilityPasses( return false; } + +//! List of pass builder callbacks to be applied, in addition to those imported +//! from plugins or LLVM extensions. +extern SmallVector> +ListRegisterPassBuilderCallbacks; } #endif diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index ccc4276e36dacf0..28aabf8bdebb4d6 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -187,6 +187,9 @@ static void RegisterPassPlugins(ArrayRef PassPlugins, #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Load requested pass plugins and let them register pass builder callbacks for (auto &PluginFN : PassPlugins) { diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index fde759026e5d780..21a7ecb88e96418 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -2099,3 +2099,6 @@ void PassBuilder::registerParseTopLevelPipelineCallback( &C) { TopLevelPipelineParsingCallbacks.push_back(C); } + +llvm::SmallVector> +ListRegisterPassBuilderCallbacks; diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp index 6ae3f87099afd64..f072120d7461d4e 100644 --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -431,6 +431,9 @@ bool llvm::runPassPipeline( #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" + for (auto PassCallback : ListRegisterPassBuilderCallbacks) { +PassCallback(PB); + } // Specially handle the alias analysis manager so that we can register // a custom pipeline of AA passes with it. ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
wsmoses wrote: > acks presently can be done one of two ways: > > * a shared library plugin > * modifying the extensions list (and thus extensions.def) > > This prevents the use of such functionality for static builds on build > systems that cannot be modified, as well as prevents such functionality from > being usable when using llvm/clang as a library. > > This creates a third mechanism for adding these callbacks, an explicit list > of said callbacks. It's otherwise identical to the plugin approach, except > now instead of the list of plugins each being queried for the callback f Will add later today https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From ad9ed0260b1288efc3e8c7794f4de4592657474b Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 + clang/test/DriverPlugin/plugintest.c | 3 + clang/tools/CMakeLists.txt| 1 + .../driver-static-plugin-test/CMakeLists.txt | 59 + .../driver-static-plugin-test/helloplugin.cpp | 66 +++ clang/tools/driver/cc1_main.cpp | 9 +++ llvm/include/llvm/Passes/PassBuilder.h| 5 ++ 8 files changed, 152 insertions(+) create mode 100644 clang/test/DriverPlugin/plugintest.c create mode 100644 clang/tools/driver-static-plugin-test/CMakeLists.txt create mode 100644 clang/tools/driver-static-plugin-test/helloplugin.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/clang/test/DriverPlugin/plugintest.c b/clang/test/DriverPlugin/plugintest.c new file mode 100644 index 000..cb96749ef8f2888 --- /dev/null +++ b/clang/test/DriverPlugin/plugintest.c @@ -0,0 +1,3 @@ +// RUN: clang-hello-plugin %s -o /dev/null -S | FileCheck %s + +int myfunction() { return 0; } diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt index f60db6ef0ba3454..69c8cd413a6293b 100644 --- a/clang/tools/CMakeLists.txt +++ b/clang/tools/CMakeLists.txt @@ -2,6 +2,7 @@ create_subdirectory_options(CLANG TOOL) add_clang_subdirectory(diagtool) add_clang_subdirectory(driver) +add_clang_subdirectory(driver-static-plugin-test) add_clang_subdirectory(apinotes-test) add_clang_subdirectory(clang-diff) add_clang_subdirectory(clang-format) diff --git a/clang/tools/driver-static-plugin-test/CMakeLists.txt b/clang/tools/driver-static-plugin-test/CMakeLists.txt new file mode 100644 index 000..61891ff7eb9e95b --- /dev/null +++ b/clang/tools/driver-static-plugin-test/CMakeLists.txt @@ -0,0 +1,59 @@ +set( LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + Analysis + CodeGen + Core + IPO + AggressiveInstCombine + InstCombine + Instrumentation + MC + MCParser + ObjCARCOpts + Option + ScalarOpts + Support + TargetParser + TransformUtils + Vectorize + ) + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + set(support_plugins SUPPORT_PLUGINS) +endif() + +add_clang_tool(clang-hello-plugin + ../driver/driver.cpp + ../driver/cc1_main.cpp + ../driver/cc1as_main.cpp + ../driver/cc1gen_reproducer_main.cpp + helloplugin.cpp + DEPENDS + intrinsics_gen + ${support_plugins} + GENERATE_DRIVER + ) + +clang_target_link_libraries(clang-hello-plugin + PRIVATE + clangBasic + clangCodeGen + clangDriver + clangFrontend + clangFrontendTool + clangSerialization + ) + +if(WIN32 AND NOT CYGWIN) + # Prevent versioning if the buildhost is targeting for Win32. +else() + set_target_properties(clang-hello-plugin PROPERTIES VERSION ${CLANG_EXECUTABLE_VERSION}) +endif() + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + export_executable_symbols_for_plugins(clang-hello-plugin) +endif() + +add_dependencies(clang-hello-plugin clang-resource-headers) diff --git a/clang/tools/driver-static-plugin-test/helloplugin.cpp b/clang/tools/driver-static-plugin-test/helloplugin.cpp new file mode 100644 index 000..52551d12af0a350 --- /dev/null +++ b/clang/tools/driver-static-plugin-test/
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
wsmoses wrote: @jdoerfert added the example test @efriedma-quic I've reworked the mechanism to not create a global variable within LLVM. Here this only applies to clang and enables two things: 1) Users of clang as a library can add a custom passbuilder callback by adding to codegenoptions 2) Those who want to modify clang codegen passes can statically link the requisite code (like in the example) to enable their functionality, without forking clang. This still requires a global variable, but here it is limited to the clang driver itself, so it should not create any version conflicts since the clang driver should not load a second clang driver. https://github.com/llvm/llvm-project/pull/70171 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From 5119368d060c886c6cb9c4209169604d191e6b4c Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 + clang/test/DriverPlugin/plugintest.c | 6 ++ clang/tools/CMakeLists.txt| 1 + .../driver-static-plugin-test/CMakeLists.txt | 59 + .../driver-static-plugin-test/helloplugin.cpp | 66 +++ clang/tools/driver/cc1_main.cpp | 9 +++ llvm/include/llvm/Passes/PassBuilder.h| 5 ++ 8 files changed, 155 insertions(+) create mode 100644 clang/test/DriverPlugin/plugintest.c create mode 100644 clang/tools/driver-static-plugin-test/CMakeLists.txt create mode 100644 clang/tools/driver-static-plugin-test/helloplugin.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/clang/test/DriverPlugin/plugintest.c b/clang/test/DriverPlugin/plugintest.c new file mode 100644 index 000..060eb43e10baef3 --- /dev/null +++ b/clang/test/DriverPlugin/plugintest.c @@ -0,0 +1,6 @@ +// RUN: clang-hello-plugin %s -o /dev/null -S | FileCheck %s + +int myfunction() { return 0; } + +// CHECK: [HelloPass] Found function: myfunction + diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt index f60db6ef0ba3454..69c8cd413a6293b 100644 --- a/clang/tools/CMakeLists.txt +++ b/clang/tools/CMakeLists.txt @@ -2,6 +2,7 @@ create_subdirectory_options(CLANG TOOL) add_clang_subdirectory(diagtool) add_clang_subdirectory(driver) +add_clang_subdirectory(driver-static-plugin-test) add_clang_subdirectory(apinotes-test) add_clang_subdirectory(clang-diff) add_clang_subdirectory(clang-format) diff --git a/clang/tools/driver-static-plugin-test/CMakeLists.txt b/clang/tools/driver-static-plugin-test/CMakeLists.txt new file mode 100644 index 000..61891ff7eb9e95b --- /dev/null +++ b/clang/tools/driver-static-plugin-test/CMakeLists.txt @@ -0,0 +1,59 @@ +set( LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + Analysis + CodeGen + Core + IPO + AggressiveInstCombine + InstCombine + Instrumentation + MC + MCParser + ObjCARCOpts + Option + ScalarOpts + Support + TargetParser + TransformUtils + Vectorize + ) + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + set(support_plugins SUPPORT_PLUGINS) +endif() + +add_clang_tool(clang-hello-plugin + ../driver/driver.cpp + ../driver/cc1_main.cpp + ../driver/cc1as_main.cpp + ../driver/cc1gen_reproducer_main.cpp + helloplugin.cpp + DEPENDS + intrinsics_gen + ${support_plugins} + GENERATE_DRIVER + ) + +clang_target_link_libraries(clang-hello-plugin + PRIVATE + clangBasic + clangCodeGen + clangDriver + clangFrontend + clangFrontendTool + clangSerialization + ) + +if(WIN32 AND NOT CYGWIN) + # Prevent versioning if the buildhost is targeting for Win32. +else() + set_target_properties(clang-hello-plugin PROPERTIES VERSION ${CLANG_EXECUTABLE_VERSION}) +endif() + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + export_executable_symbols_for_plugins(clang-hello-plugin) +endif() + +add_dependencies(clang-hello-plugin clang-resource-headers) diff --git a/clang/tools/driver-static-plugin-test/helloplugin.cpp b/clang/tools/driver-static-plugin-test/helloplugin.cpp new file mode 100644 index 000..52551d12af0a350 ---
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From 227a494be06d8cb3b590c78f52f717bd781a8f0e Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 + clang/test/CMakeLists.txt | 1 + clang/test/DriverPlugin/plugintest.c | 6 ++ clang/tools/CMakeLists.txt| 1 + .../clang-hello-plugin-test/CMakeLists.txt| 59 + .../clang-hello-plugin-test/helloplugin.cpp | 66 +++ clang/tools/driver/cc1_main.cpp | 9 +++ llvm/include/llvm/Passes/PassBuilder.h| 5 ++ 9 files changed, 156 insertions(+) create mode 100644 clang/test/DriverPlugin/plugintest.c create mode 100644 clang/tools/clang-hello-plugin-test/CMakeLists.txt create mode 100644 clang/tools/clang-hello-plugin-test/helloplugin.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt index 31b494f39cce577..a64dfe467c43abc 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -62,6 +62,7 @@ list(APPEND CLANG_TEST_DEPS apinotes-test c-index-test clang + clang-hello-plugin-test clang-fuzzer-dictionary clang-resource-headers clang-format diff --git a/clang/test/DriverPlugin/plugintest.c b/clang/test/DriverPlugin/plugintest.c new file mode 100644 index 000..e80866f0d1dd2fe --- /dev/null +++ b/clang/test/DriverPlugin/plugintest.c @@ -0,0 +1,6 @@ +// RUN: clang-hello-plugin-test %s -o /dev/null -S | FileCheck %s + +int myfunction() { return 0; } + +// CHECK: [HelloPass] Found function: myfunction + diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt index f60db6ef0ba3454..9742e2a155a0d30 100644 --- a/clang/tools/CMakeLists.txt +++ b/clang/tools/CMakeLists.txt @@ -2,6 +2,7 @@ create_subdirectory_options(CLANG TOOL) add_clang_subdirectory(diagtool) add_clang_subdirectory(driver) +add_clang_subdirectory(clang-hello-plugin-test) add_clang_subdirectory(apinotes-test) add_clang_subdirectory(clang-diff) add_clang_subdirectory(clang-format) diff --git a/clang/tools/clang-hello-plugin-test/CMakeLists.txt b/clang/tools/clang-hello-plugin-test/CMakeLists.txt new file mode 100644 index 000..2b951677542c217 --- /dev/null +++ b/clang/tools/clang-hello-plugin-test/CMakeLists.txt @@ -0,0 +1,59 @@ +set( LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + Analysis + CodeGen + Core + IPO + AggressiveInstCombine + InstCombine + Instrumentation + MC + MCParser + ObjCARCOpts + Option + ScalarOpts + Support + TargetParser + TransformUtils + Vectorize + ) + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + set(support_plugins SUPPORT_PLUGINS) +endif() + +add_clang_tool(clang-hello-plugin-test + ../driver/driver.cpp + ../driver/cc1_main.cpp + ../driver/cc1as_main.cpp + ../driver/cc1gen_reproducer_main.cpp + helloplugin.cpp + DEPENDS + intrinsics_gen + ${support_plugins} + GENERATE_DRIVER + ) + +clang_target_link_libraries(clang-hello-plugin-test + PRIVATE + clangBasic + clangCodeGen + clangDriver + clangFrontend + clangFrontendTool + clangSerialization + ) + +if(WIN32 AND NOT CYGWIN) + # Prevent versioning if the buildhost is targeting for Win32. +else() + set_target_properties(clang-hello-plugin-test PRO
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From a49b8e2da32c1be06ea956198ba0d01006d64c92 Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 + clang/test/CMakeLists.txt | 1 + clang/test/DriverPlugin/plugintest.c | 6 ++ clang/tools/CMakeLists.txt| 1 + .../clang-hello-plugin-test/CMakeLists.txt| 59 + .../clang-hello-plugin-test/helloplugin.cpp | 66 +++ clang/tools/driver/cc1_main.cpp | 9 +++ llvm/include/llvm/Passes/PassBuilder.h| 5 ++ 9 files changed, 156 insertions(+) create mode 100644 clang/test/DriverPlugin/plugintest.c create mode 100644 clang/tools/clang-hello-plugin-test/CMakeLists.txt create mode 100644 clang/tools/clang-hello-plugin-test/helloplugin.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt index 31b494f39cce577..a64dfe467c43abc 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -62,6 +62,7 @@ list(APPEND CLANG_TEST_DEPS apinotes-test c-index-test clang + clang-hello-plugin-test clang-fuzzer-dictionary clang-resource-headers clang-format diff --git a/clang/test/DriverPlugin/plugintest.c b/clang/test/DriverPlugin/plugintest.c new file mode 100644 index 000..e80866f0d1dd2fe --- /dev/null +++ b/clang/test/DriverPlugin/plugintest.c @@ -0,0 +1,6 @@ +// RUN: clang-hello-plugin-test %s -o /dev/null -S | FileCheck %s + +int myfunction() { return 0; } + +// CHECK: [HelloPass] Found function: myfunction + diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt index f60db6ef0ba3454..9742e2a155a0d30 100644 --- a/clang/tools/CMakeLists.txt +++ b/clang/tools/CMakeLists.txt @@ -2,6 +2,7 @@ create_subdirectory_options(CLANG TOOL) add_clang_subdirectory(diagtool) add_clang_subdirectory(driver) +add_clang_subdirectory(clang-hello-plugin-test) add_clang_subdirectory(apinotes-test) add_clang_subdirectory(clang-diff) add_clang_subdirectory(clang-format) diff --git a/clang/tools/clang-hello-plugin-test/CMakeLists.txt b/clang/tools/clang-hello-plugin-test/CMakeLists.txt new file mode 100644 index 000..2b951677542c217 --- /dev/null +++ b/clang/tools/clang-hello-plugin-test/CMakeLists.txt @@ -0,0 +1,59 @@ +set( LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + Analysis + CodeGen + Core + IPO + AggressiveInstCombine + InstCombine + Instrumentation + MC + MCParser + ObjCARCOpts + Option + ScalarOpts + Support + TargetParser + TransformUtils + Vectorize + ) + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + set(support_plugins SUPPORT_PLUGINS) +endif() + +add_clang_tool(clang-hello-plugin-test + ../driver/driver.cpp + ../driver/cc1_main.cpp + ../driver/cc1as_main.cpp + ../driver/cc1gen_reproducer_main.cpp + helloplugin.cpp + DEPENDS + intrinsics_gen + ${support_plugins} + GENERATE_DRIVER + ) + +clang_target_link_libraries(clang-hello-plugin-test + PRIVATE + clangBasic + clangCodeGen + clangDriver + clangFrontend + clangFrontendTool + clangSerialization + ) + +if(WIN32 AND NOT CYGWIN) + # Prevent versioning if the buildhost is targeting for Win32. +else() + set_target_properties(clang-hello-plugin-test PRO
[clang] [PassBuilder] Add a mechanism for adding passbuilder callbacks for static builds (PR #70171)
https://github.com/wsmoses updated https://github.com/llvm/llvm-project/pull/70171 >From 547d498662c3b8da2cb87abe9f5c13b39123d90f Mon Sep 17 00:00:00 2001 From: "William S. Moses" Date: Wed, 25 Oct 2023 02:10:32 -0500 Subject: [PATCH] [PassBuilder] Add a mechanism for adding passbuilder callbacks without an extension mechanism or dlopen of plugin --- clang/include/clang/Basic/CodeGenOptions.h| 6 ++ clang/lib/CodeGen/BackendUtil.cpp | 3 + clang/test/CMakeLists.txt | 1 + clang/test/DriverPlugin/plugintest.c | 6 ++ clang/tools/CMakeLists.txt| 1 + .../clang-hello-plugin-test/CMakeLists.txt| 59 + .../clang-hello-plugin-test/helloplugin.cpp | 66 +++ clang/tools/driver/cc1_main.cpp | 9 +++ llvm/include/llvm/Passes/PassBuilder.h| 5 ++ 9 files changed, 156 insertions(+) create mode 100644 clang/test/DriverPlugin/plugintest.c create mode 100644 clang/tools/clang-hello-plugin-test/CMakeLists.txt create mode 100644 clang/tools/clang-hello-plugin-test/helloplugin.cpp diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 12be4e0025a7054..c8e2544f891cc2b 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -26,6 +26,9 @@ #include #include +namespace llvm { +class PassBuilder; +} namespace clang { /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure @@ -408,6 +411,9 @@ class CodeGenOptions : public CodeGenOptionsBase { /// List of dynamic shared object files to be loaded as pass plugins. std::vector PassPlugins; + /// List of pass builder callbacks. + std::vector> PassBuilderCallbacks; + /// Path to allowlist file specifying which objects /// (files, functions) should exclusively be instrumented /// by sanitizer coverage pass. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 70accce456d3c07..a8db38fce6425fb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -906,6 +906,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline( << PluginFN << toString(PassPlugin.takeError()); } } + for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks) { +PassCallback(PB); + } #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt index 31b494f39cce577..a64dfe467c43abc 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -62,6 +62,7 @@ list(APPEND CLANG_TEST_DEPS apinotes-test c-index-test clang + clang-hello-plugin-test clang-fuzzer-dictionary clang-resource-headers clang-format diff --git a/clang/test/DriverPlugin/plugintest.c b/clang/test/DriverPlugin/plugintest.c new file mode 100644 index 000..e80866f0d1dd2fe --- /dev/null +++ b/clang/test/DriverPlugin/plugintest.c @@ -0,0 +1,6 @@ +// RUN: clang-hello-plugin-test %s -o /dev/null -S | FileCheck %s + +int myfunction() { return 0; } + +// CHECK: [HelloPass] Found function: myfunction + diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt index f60db6ef0ba3454..9742e2a155a0d30 100644 --- a/clang/tools/CMakeLists.txt +++ b/clang/tools/CMakeLists.txt @@ -2,6 +2,7 @@ create_subdirectory_options(CLANG TOOL) add_clang_subdirectory(diagtool) add_clang_subdirectory(driver) +add_clang_subdirectory(clang-hello-plugin-test) add_clang_subdirectory(apinotes-test) add_clang_subdirectory(clang-diff) add_clang_subdirectory(clang-format) diff --git a/clang/tools/clang-hello-plugin-test/CMakeLists.txt b/clang/tools/clang-hello-plugin-test/CMakeLists.txt new file mode 100644 index 000..2b951677542c217 --- /dev/null +++ b/clang/tools/clang-hello-plugin-test/CMakeLists.txt @@ -0,0 +1,59 @@ +set( LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + Analysis + CodeGen + Core + IPO + AggressiveInstCombine + InstCombine + Instrumentation + MC + MCParser + ObjCARCOpts + Option + ScalarOpts + Support + TargetParser + TransformUtils + Vectorize + ) + +# Support plugins. +if(CLANG_PLUGIN_SUPPORT) + set(support_plugins SUPPORT_PLUGINS) +endif() + +add_clang_tool(clang-hello-plugin-test + ../driver/driver.cpp + ../driver/cc1_main.cpp + ../driver/cc1as_main.cpp + ../driver/cc1gen_reproducer_main.cpp + helloplugin.cpp + DEPENDS + intrinsics_gen + ${support_plugins} + GENERATE_DRIVER + ) + +clang_target_link_libraries(clang-hello-plugin-test + PRIVATE + clangBasic + clangCodeGen + clangDriver + clangFrontend + clangFrontendTool + clangSerialization + ) + +if(WIN32 AND NOT CYGWIN) + # Prevent versioning if the buildhost is targeting for Win32. +else() + set_target_properties(clang-hello-plugin-test PRO