Skip to content

Commit 67f06b3

Browse files
authored
Merge pull request #37 from sampsyo/modernize
Modernize for LLVM 17
2 parents c812e63 + e32353e commit 67f06b3

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.6)
22
project(Skeleton)
33

4-
# support C++14 features used by LLVM 10.0.0
5-
set(CMAKE_CXX_STANDARD 14)
4+
# LLVM uses C++17.
5+
set(CMAKE_CXX_STANDARD 17)
66

7+
# Load LLVMConfig.cmake. If this fails, consider setting `LLVM_DIR` to point
8+
# to your LLVM installation's `lib/cmake/llvm` directory.
79
find_package(LLVM REQUIRED CONFIG)
10+
11+
# Include the part of LLVM's CMake libraries that defines
12+
# `add_llvm_pass_plugin`.
13+
include(AddLLVM)
14+
15+
# Use LLVM's preprocessor definitions, include directories, and library search
16+
# paths.
817
add_definitions(${LLVM_DEFINITIONS})
918
include_directories(${LLVM_INCLUDE_DIRS})
1019
link_directories(${LLVM_LIBRARY_DIRS})
1120

12-
add_subdirectory(skeleton) # Use your pass name here.
21+
# Our pass lives in this subdirectory.
22+
add_subdirectory(skeleton)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# llvm-pass-skeleton
22

33
A completely useless LLVM pass.
4-
It's for LLVM 14.
4+
It's for LLVM 17.
55

66
Build:
77

@@ -14,4 +14,4 @@ Build:
1414

1515
Run:
1616

17-
$ clang -flegacy-pass-manager -Xclang -load -Xclang build/skeleton/libSkeletonPass.* something.c
17+
$ clang -fpass-plugin=`ls build/skeleton/SkeletonPass.*` something.c

skeleton/CMakeLists.txt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
add_library(SkeletonPass MODULE
1+
add_llvm_pass_plugin(SkeletonPass
22
# List your source files here.
33
Skeleton.cpp
44
)
5-
6-
# Use C++11 to compile our pass (i.e., supply -std=c++11).
7-
target_compile_features(SkeletonPass PRIVATE cxx_range_for cxx_auto_type)
8-
9-
# LLVM is (typically) built with no C++ RTTI. We need to match that;
10-
# otherwise, we'll get linker errors about missing RTTI data.
11-
set_target_properties(SkeletonPass PROPERTIES
12-
COMPILE_FLAGS "-fno-rtti"
13-
)
14-
15-
# Get proper shared-library behavior (where symbols are not necessarily
16-
# resolved when the shared library is linked) on OS X.
17-
if(APPLE)
18-
set_target_properties(SkeletonPass PROPERTIES
19-
LINK_FLAGS "-undefined dynamic_lookup"
20-
)
21-
endif(APPLE)

skeleton/Skeleton.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
#include "llvm/Pass.h"
2-
#include "llvm/IR/Function.h"
2+
#include "llvm/Passes/PassBuilder.h"
3+
#include "llvm/Passes/PassPlugin.h"
34
#include "llvm/Support/raw_ostream.h"
4-
#include "llvm/IR/LegacyPassManager.h"
5-
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
5+
66
using namespace llvm;
77

88
namespace {
9-
struct SkeletonPass : public FunctionPass {
10-
static char ID;
11-
SkeletonPass() : FunctionPass(ID) {}
129

13-
virtual bool runOnFunction(Function &F) {
14-
errs() << "I saw a function called " << F.getName() << "!\n";
15-
return false;
16-
}
17-
};
18-
}
10+
struct SkeletonPass : public PassInfoMixin<SkeletonPass> {
11+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
12+
for (auto &F : M.functions()) {
13+
errs() << "I saw a function called " << F.getName() << "!\n";
14+
}
15+
return PreservedAnalyses::all();
16+
};
17+
};
1918

20-
char SkeletonPass::ID = 0;
19+
}
2120

22-
// Automatically enable the pass.
23-
// http://adriansampson.net/blog/clangpass.html
24-
static void registerSkeletonPass(const PassManagerBuilder &,
25-
legacy::PassManagerBase &PM) {
26-
PM.add(new SkeletonPass());
21+
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
22+
llvmGetPassPluginInfo() {
23+
return {
24+
.APIVersion = LLVM_PLUGIN_API_VERSION,
25+
.PluginName = "Skeleton pass",
26+
.PluginVersion = "v0.1",
27+
.RegisterPassBuilderCallbacks = [](PassBuilder &PB) {
28+
PB.registerPipelineStartEPCallback(
29+
[](ModulePassManager &MPM, OptimizationLevel Level) {
30+
MPM.addPass(SkeletonPass());
31+
});
32+
}
33+
};
2734
}
28-
static RegisterStandardPasses
29-
RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
30-
registerSkeletonPass);

0 commit comments

Comments
 (0)