forked from david9991/go-llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backports.cpp
50 lines (47 loc) · 1.66 KB
/
backports.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "backports.h"
#include "llvm/IR/Instructions.h"
#if LLVM_VERSION_MAJOR >= 16
#include "llvm/IR/PassManager.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
#else
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#endif
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Transforms/IPO.h"
#include "llvm-c/DebugInfo.h"
void LLVMGlobalObjectAddMetadata(LLVMValueRef Global, unsigned KindID, LLVMMetadataRef MD) {
llvm::MDNode *N = MD ? llvm::unwrap<llvm::MDNode>(MD) : nullptr;
llvm::GlobalObject *O = llvm::unwrap<llvm::GlobalObject>(Global);
O->addMetadata(KindID, *N);
}
// See https://reviews.llvm.org/D119431
LLVMMemoryBufferRef LLVMGoWriteThinLTOBitcodeToMemoryBuffer(LLVMModuleRef M) {
std::string Data;
llvm::raw_string_ostream OS(Data);
#if LLVM_VERSION_MAJOR >= 16
llvm::LoopAnalysisManager LAM;
llvm::FunctionAnalysisManager FAM;
llvm::CGSCCAnalysisManager CGAM;
llvm::ModuleAnalysisManager MAM;
llvm::PassBuilder PB;
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
llvm::ModulePassManager MPM;
MPM.addPass(llvm::ThinLTOBitcodeWriterPass(OS, nullptr));
MPM.run(*llvm::unwrap(M), MAM);
#else
llvm::legacy::PassManager PM;
PM.add(createWriteThinLTOBitcodePass(OS));
PM.run(*llvm::unwrap(M));
#endif
return llvm::wrap(llvm::MemoryBuffer::getMemBufferCopy(OS.str()).release());
}