-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Clang] Add codegen option to add passbuilder callback functions (#70171
- Loading branch information
Showing
8 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
78 changes: 78 additions & 0 deletions
78
clang/examples/LLVMPrintFunctionNames/LLVMPrintFunctionNames.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PrintPass> { | ||
friend struct llvm::AnalysisInfoMixin<PrintPass>; | ||
|
||
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<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, | ||
llvm::StringRef) override { | ||
return std::make_unique<LLVMPrintFunctionsConsumer>(CI); | ||
} | ||
bool ParseArgs(const CompilerInstance &, | ||
const std::vector<std::string> &) override { | ||
return true; | ||
} | ||
PluginASTAction::ActionType getActionType() override { | ||
return AddBeforeMainAction; | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
static FrontendPluginRegistry::Add<LLVMPrintFunctionNamesAction> | ||
X("llvm-print-fns", "print function names, llvm level"); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// RUN: %clang_cc1 -load %llvmshlibdir/LLVMPrintFunctionNames%pluginext -S -o /dev/null -emit-llvm %s 2>&1 | FileCheck %s | ||
// REQUIRES: plugins, examples | ||
|
||
// CHECK: [PrintPass] Found function: x | ||
int x(int y){ return y; } |