Skip to content

Commit da71889

Browse files
Merge pull request #9 from flopsreallygotit/dev
Add different LLVM versions compatibility
2 parents 1212485 + 05567be commit da71889

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
set(LLVM_VERSION 14)
2-
3-
set(CMAKE_C_COMPILER clang-${LLVM_VERSION})
4-
set(CMAKE_CXX_COMPILER clang++-${LLVM_VERSION})
5-
61
cmake_minimum_required(VERSION 3.22)
7-
project(log-pass)
2+
project(LogPass)
83

94
set(CMAKE_CXX_STANDARD 23)
105

116
find_package(LLVM ${LLVM_VERSION} REQUIRED CONFIG)
7+
find_package(Clang ${LLVM_VERSION} REQUIRED)
8+
9+
# Check for Clang compiler
10+
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
11+
message(FATAL_ERROR "This project requires Clang for compilation.\n"
12+
"Detected C compiler: ${CMAKE_C_COMPILER_ID}\n"
13+
"Detected C++ compiler: ${CMAKE_CXX_COMPILER_ID}")
14+
endif()
1215

1316
include(FetchContent)
1417
FetchContent_Declare(
@@ -18,8 +21,10 @@ FetchContent_Declare(
1821
FetchContent_MakeAvailable(googletest)
1922

2023
add_definitions(${LLVM_DEFINITIONS})
24+
add_definitions(${CLANG_DEFINITIONS})
2125

2226
include_directories(${LLVM_INCLUDE_DIRS})
27+
include_directories(${CLANG_INCLUDE_DIRS})
2328
link_directories(${LLVM_LIBRARY_DIRS})
2429

2530
add_subdirectory(lib)

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# LogPass
22

3-
LLVM Pass that logs all IR info and collects code coverage. The project is under development.
3+
LogPass provides compiler wrapper and pass library that logs function traces
44

55
# Dependencies
66

77
You can install dependencies on Debian/Ubuntu with following command:
88

99
```bash
10-
sudo apt install clang-14 llvm-14-dev cmake make
10+
sudo apt install clang-16 llvm-16-dev cmake make
1111
```
1212

1313
## Quick Start
@@ -17,7 +17,7 @@ To build pass library write:
1717
```bash
1818
mkdir build
1919
cd build
20-
cmake ..
20+
cmake .. -DCMAKE_C_COMPILER=clang-16 -DCMAKE_CXX_COMPILER=clang++-16
2121
make -j
2222
```
2323

@@ -30,4 +30,7 @@ CC=/path/to/loggercc
3030
CXX=/path/to/loggercxx
3131

3232
cmake / make / ninja / ...
33+
./a.out
3334
```
35+
36+
You'll receive `logger.log` file with function trace.

lib/log_pass.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
#include "llvm/IR/PassManager.h"
1010
#include "llvm/IR/Instruction.h"
1111
#include "llvm/IR/Instructions.h"
12-
#include "llvm/Support/Casting.h"
1312
#include "llvm/IR/DerivedTypes.h"
1413

1514
#include "llvm/Passes/PassPlugin.h"
1615
#include "llvm/Passes/PassBuilder.h"
1716

1817
#include "llvm/Transforms/Utils/ModuleUtils.h"
1918

19+
#if LLVM_VERSION_MAJOR < 17
20+
#include "llvm/InitializePasses.h"
21+
#endif
22+
2023
namespace {
2124
using namespace llvm;
2225

@@ -71,13 +74,14 @@ namespace {
7174
IRBuilder<> builder(llvm_context);
7275

7376
Type *llvm_void_t = Type::getVoidTy(llvm_context);
74-
Type *llvm_str_t = Type::getInt8PtrTy(llvm_context);
75-
76-
FunctionType *log_init_function_t = FunctionType::get(llvm_void_t, false);
77-
FunctionCallee log_init_function = module.getOrInsertFunction(log_init_function_name, log_init_function_t);
77+
#if LLVM_VERSION_MAJOR < 16
78+
Type *llvm_str_t = Type::getInt8PtrTy(llvm_context);
79+
#else
80+
Type *llvm_str_t = builder.getPtrTy();
81+
#endif
7882

79-
FunctionType *log_call_function_t = FunctionType::get(llvm_void_t, {llvm_str_t}, false);
80-
FunctionCallee log_call_function = module.getOrInsertFunction(log_call_function_name, log_call_function_t);
83+
FunctionCallee log_init_function = module.getOrInsertFunction(log_init_function_name, llvm_void_t);
84+
FunctionCallee log_call_function = module.getOrInsertFunction(log_call_function_name, llvm_void_t, llvm_str_t);
8185

8286
FunctionType *log_deinit_function_t = FunctionType::get(llvm_void_t, false);
8387
Function *log_deinit_function =
@@ -116,9 +120,11 @@ namespace {
116120
};
117121
} // end of anonymous namespace
118122

119-
extern "C" PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() {
123+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() {
120124
return {
121-
LLVM_PLUGIN_API_VERSION, "Log-Pass", "v1.0", [](PassBuilder &PB) {
125+
LLVM_PLUGIN_API_VERSION, "LogPass", "v0.3",
126+
/* lambda to insert our pass into the pass pipeline. */
127+
[](PassBuilder &PB) {
122128
PB.registerOptimizerLastEPCallback([](ModulePassManager &MPM, OptimizationLevel OL) { MPM.addPass(LogPass()); });
123129
}};
124130
}

0 commit comments

Comments
 (0)