当前位置:网站首页>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
边栏推荐
- Alibaba cloud development board haas510 submission device attributes
- Codeforces 1637 A. sorting parts - simple thinking
- Briefly describe the difference between CGI and fastcgi
- Understanding recursion
- 阿里云开发板HaaS510连接物联网平台--HaaS征文
- After reading the question, you will point to offer 16 Integer power of numeric value
- SystemC simulation scheduling mechanism
- 阿里云开发板HaaS510解析串口JSON数据并发送属性
- 肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)
- [WUSTCTF2020]颜值成绩查询-1
猜你喜欢

注重点击,追求更多用户进入网站,可以选择什么出价策略?

Démontage et modification de la machine publicitaire - décompression amateur

Write policy of cache

Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode

【活动早知道】LiveVideoStack近期活动一览

A method of quickly creating test window

Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation

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

Paw advanced user guide

Relevant knowledge points of cocoapods
随机推荐
聊聊MySQL的10大经典错误
1414. minimum number of Fibonacci numbers with sum K
Codeforces 1638 A. reverse - simple thinking
Acwing: topology sequence
Tree reconstruction (pre order + middle order or post order + middle order)
【mysql进阶】mysql索引数据结构的演变(四)
Alibaba cloud development board haas510 submission device attributes
Dismantle and modify the advertising machine - Amateur decompression
Codeforces 1637 C. Andrew and stones - simple thinking
Byte order data read / write
注重点击,追求更多用户进入网站,可以选择什么出价策略?
Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
Top 10 tips for visual studio code on Google
Tlm/systemc: TLM socket binding problem
Talk about the top 10 classic MySQL errors
CSDN博客积分规则
lua 常用内置函数
SystemC time
肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)
Greed issues - Egypt scores