当前位置:网站首页>Llvm 13.1 new pass plug-in form [for win]
Llvm 13.1 new pass plug-in form [for win]
2022-06-12 13:57:00 【HyperCall】
LLVM 13.1 new Pass Plug in form [for win]
4 Key points :
- LLVM 13.1 edition ( The latest edition )
- windows On the platform pass demo
- new pass form ( Not legacy)
- clang Dynamic plug-in form (pass Plug in and llvm Ontology compilation separation )
background :
The current external data are all old versions LLVM + legacy Mode pass, And the current llvm Version disjointed , In the new mode, I did my own research without finding any data , New and old LLVM Version pair pass What is the impact of writing , What is the specific reason , How to make it compatible with the old version? See my Last article ( The principle and thought of analyzing the similarities and differences between the old and the new design )
Operation steps
( In all subsequent steps, if the directory is inconsistent, you need to change the directory structure by yourself , Don't go into )
- establish :D:\Code\llvm-project Folder , Put in a file named clang Source folder for , And called llvm Source folder for , Create a new one in this directory MyCustom The folder is used to store our dynamic plug-in project ( edition LLVM 13.1)
- According to my Last article Steps introduced 1~3 Build up x64release Version of clang&llvm( Old and new pass Yes llvm and clang There is no difference in the construction of ), Compilation is slow and CPU High occupancy, please find free time to complete
- Switch to D:\Code\llvm-project\llvm\build\Release\bin Directory type
.\clang.exe --versionThe command ensures that the output is in the following form
clang version 13.0.1
Target: x86_64-pc-windows-msvc
...
- newly build D:\Code\llvm-project\MyCustom\src Folder , Create a new file MyPass.cpp And write
#include <llvm/IR/PassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/Pass.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
using namespace llvm;
// test for my custom pass
class MyCustomPass : public PassInfoMixin<MyCustomPass> {
public:
PreservedAnalyses run(Module& M, ModuleAnalysisManager& AM)
{
for (auto& f : M) {
errs() << "MyCustomPass: " << f.getName() << "\n";
}
return PreservedAnalyses::all();
}
};
// stay clang Create a custom according to the configuration pass,called by PassManagerBuilder::populateModulePassManager
extern "C" __declspec(dllexport) void __stdcall clangAddCustomPass(ModulePassManager & MPM)
{
MPM.addPass(MyCustomPass());
}
- stay D:\Code\llvm-project\MyCustom New file in directory CMakeLists.txt, And write
cmake_minimum_required(VERSION 3.4)
project(custompass)
# Set compilation mode
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") #/MT
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") #/MTd
# Add source directory
aux_source_directory(./src src_1)
set(srcs ${src_1})
# Generate DLL , So in Windows Next is dll, Will eventually generate custompass.dll
add_library(custompass SHARED ${srcs})
set(LLVM_PROJECT_DIR "" CACHE STRING
"You must input the correct llvm-project dir")
if( LLVM_PROJECT_DIR STREQUAL "" )
MESSAGE(FATAL_ERROR "LLVM_PROJECT_DIR is empty")
else()
MESSAGE("LLVM_PROJECT_DIR=${LLVM_PROJECT_DIR}")
endif()
# Add header file , You need compiled and source header files
include_directories("${LLVM_PROJECT_DIR}/llvm/build/include")
include_directories("${LLVM_PROJECT_DIR}/llvm/include")
# Add compiled lib
set(mylibdir "${LLVM_PROJECT_DIR}/llvm/build/Release/lib")
set(VTKLIBS LLVMCore LLVMSupport LLVMBinaryFormat LLVMRemarks LLVMBitstreamReader)
foreach(libname ${VTKLIBS})
SET(FOUND_LIB "FOUND_LIB-NOTFOUND")
find_library(FOUND_LIB NAMES ${libname} HINTS ${mylibdir} NO_DEFAULT_PATH)
IF (FOUND_LIB)
message("found lib: ${FOUND_LIB}")
LIST(APPEND mylibs ${FOUND_LIB})
ELSE()
MESSAGE("not lib found: ${libname}")
ENDIF ()
endforeach(libname)
#message(${mylibs})
#message(${CPPUNIT_LIBRARY})
target_link_libraries(custompass PUBLIC ${mylibs})
- cmake Choose D:\Code\llvm-project\MyCustom engineering , To configure LLVM_PROJECT_DIR Field is D:\Code\llvm-project, And ensure successful generation sln Engineering documents
- Compile the project file to get custompass.dll The output file , Copy it to D:\Code\llvm-project\llvm\build\Release\bin Under the table of contents
- open llvm engineering , In turn, open
Object Libraries\obj.clangCodeGen\Source Files\BackendUtil.cppfile - stay EmitAssemblyHelper::EmitAssemblyWithNewPassManager In function
if (!CodeGenOpts.DisableLLVMPasses)Add the following code to the end of the judgment body to make it load fixedly custompass.dll Of clangAddCustomPass The export function ( Remember to introduce#include "windows.h")
#if _WINDOWS
using myPassType = void(__stdcall *)(ModulePassManager & MPM);
auto customHandle = ::LoadLibraryA("custompass.dll");
if (!customHandle) {
errs() << "custompass.dll not found\n";
} else {
auto pfn =
(myPassType)::GetProcAddress(customHandle, "clangAddCustomPass");
if (pfn != NULL) {
pfn(MPM);
} else {
errs() << "clangAddCustomPass not found\n";
}
}
#endif // _WINDOWS
- Incremental compilation LLVM engineering ( Little modification and faster compilation )
test
12. D:\Code\llvm-project\llvm\build\Release\bin Next new file test.cpp, And write
#include <stdio.h>
int add(int a,int b)
{
return a+b;
}
int min(int a,int b)
{
return a-b;
}
void show()
{
printf("show\n");
}
int main(int argc, char* argv[]){
add(1,2);
min(5,3);
show();
return 0;
}
- Enter the command
.\clang.exe test.cpp -o test.exeSuccessful output , It only needs to be modified later MyCustom Project and put the compiled plug-in into clang directory , At the same time, the project supports the addition of multiple PASS, First the pass Understand the process PASS Process logic &llvm Syntax component of … Some code is confusing , Data encryption and other functions will be issued later
MyCustomPass: [email protected]@[email protected]
MyCustomPass: [email protected]@[email protected]
MyCustomPass: [email protected]@YAXXZ
MyCustomPass: printf
MyCustomPass: main
MyCustomPass: llvm.va_start
MyCustomPass: _vfprintf_l
MyCustomPass: __acrt_iob_func
MyCustomPass: llvm.va_end
MyCustomPass: __stdio_common_vfprintf
MyCustomPass: __local_stdio_printf_options
边栏推荐
- After reading the question, you will point to offer 16 Integer power of numeric value
- Simple implementation of gpuimage chain texture
- 使用make方法创建slice切片的坑
- Démontage et modification de la machine publicitaire - décompression amateur
- Binary tree traversal
- TestEngine with ID ‘junit-vintage‘ failed to discover tests
- Cmake basic tutorial - 02 b-hello-cmake
- Shell脚本到底是什么高大上的技术吗?
- 十四周作业
- Interview question 17.14 Minimum number of K (almost double hundreds)
猜你喜欢

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

NotePad 常用设置

When the byte jumps, the Chinese 996 is output in the United States

Introduction to database system (Fifth Edition) notes Chapter 1 Introduction

Briefly describe the difference between CGI and fastcgi

Implementing singleton mode of database under QT multithreading

List of common ACM knowledge points (to be continued)

阿里云开发板HaaS510解析串口JSON数据并发送属性

初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS

Encryptor and client authenticate with each other
随机推荐
Greed issues - Egypt scores
Codeforces 1637 E. best pair - Thinking
Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)
Tree reconstruction (pre order + middle order or post order + middle order)
如果要打造品牌知名度,可以选择什么出价策略?
Display logs in the database through loganalyzer
Axi4 increase burst / wrap burst/ fix burst and narrow transfer
阿裏雲開發板HaaS510報送設備屬性
通过loganalyzer展示数据库中的日志
如何使用android studio制作一个阿里云物联网APP
Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
1414. minimum number of Fibonacci numbers with sum K
对于跨境电商,更侧重收入的出价策略 —Google SEM
To SystemC Beginners: the first program
Codeforces 1637 D. yet another minimization problem - Mathematics, DP
阿里云开发板HaaS510连接物联网平台--HaaS征文
Binary tree traversal
SystemC:SC_ Thread and SC_ METHOD
Talk about the top 10 classic MySQL errors
[semidrive source code analysis] [x9 chip startup process] 26 - LK of R5 safetyos_ INIT_ LEVEL_ Target phase code flow analysis (TP drvier, audio server initialization)