-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PassBuilder] Add a mechanism for adding passbuilder callbacks withou…
…t an extension mechanism or dlopen of plugin
- Loading branch information
Showing
9 changed files
with
156 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
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,6 @@ | ||
// RUN: clang-hello-plugin-test %s -o /dev/null -S | FileCheck %s | ||
|
||
int myfunction() { return 0; } | ||
|
||
// CHECK: [HelloPass] Found function: myfunction | ||
|
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,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 PROPERTIES VERSION ${CLANG_EXECUTABLE_VERSION}) | ||
endif() | ||
|
||
# Support plugins. | ||
if(CLANG_PLUGIN_SUPPORT) | ||
export_executable_symbols_for_plugins(clang-hello-plugin-test) | ||
endif() | ||
|
||
add_dependencies(clang-hello-plugin-test clang-resource-headers) |
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,66 @@ | ||
//===-- helloplugin.cpp - Hello World test static link plugin ------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This is a test/example mechanism for statically adding functionality to Clang | ||
// Codegen without requiring a fork of Clang. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Support/LLVMDriver.h" | ||
|
||
#include <functional> | ||
|
||
using namespace llvm; | ||
|
||
extern SmallVector<std::function<void(llvm::PassBuilder &)>> | ||
PassBuilderCallbacks; | ||
|
||
class StaticPlugin { | ||
public: | ||
StaticPlugin(std::function<void(llvm::PassBuilder &)> f) { | ||
PassBuilderCallbacks.push_back(f); | ||
} | ||
}; | ||
|
||
class HelloPass final : public llvm::AnalysisInfoMixin<HelloPass> { | ||
friend struct llvm::AnalysisInfoMixin<HelloPass>; | ||
|
||
private: | ||
static llvm::AnalysisKey Key; | ||
|
||
public: | ||
using Result = llvm::PreservedAnalyses; | ||
|
||
Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { | ||
for (auto &F : M) | ||
llvm::outs() << "[HelloPass] Found function: " << F.getName() << "\n"; | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
static bool isRequired() { return true; } | ||
}; | ||
|
||
void HelloCallback(llvm::PassBuilder &PB) { | ||
PB.registerPipelineStartEPCallback( | ||
[](ModulePassManager &MPM, OptimizationLevel) { | ||
MPM.addPass(HelloPass()); | ||
}); | ||
} | ||
|
||
StaticPlugin P(HelloCallback); | ||
|
||
extern int clang_main(int Argc, char **Argv, | ||
const llvm::ToolContext &ToolContext); | ||
|
||
int clang_hello_plugin_test_main(int Argc, char **Argv, | ||
const llvm::ToolContext &ToolContext) { | ||
return clang_main(Argc, Argv, ToolContext); | ||
} |
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