diff options
Diffstat (limited to 'tools/perf/util/c++/clang.cpp')
-rw-r--r-- | tools/perf/util/c++/clang.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index 715ca0a3dee0..2a1a75df204f 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp | |||
@@ -13,10 +13,15 @@ | |||
13 | #include "clang/Frontend/CompilerInstance.h" | 13 | #include "clang/Frontend/CompilerInstance.h" |
14 | #include "clang/Frontend/TextDiagnosticPrinter.h" | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
15 | #include "clang/Tooling/Tooling.h" | 15 | #include "clang/Tooling/Tooling.h" |
16 | #include "llvm/IR/LegacyPassManager.h" | ||
16 | #include "llvm/IR/Module.h" | 17 | #include "llvm/IR/Module.h" |
17 | #include "llvm/Option/Option.h" | 18 | #include "llvm/Option/Option.h" |
18 | #include "llvm/Support/FileSystem.h" | 19 | #include "llvm/Support/FileSystem.h" |
19 | #include "llvm/Support/ManagedStatic.h" | 20 | #include "llvm/Support/ManagedStatic.h" |
21 | #include "llvm/Support/TargetRegistry.h" | ||
22 | #include "llvm/Support/TargetSelect.h" | ||
23 | #include "llvm/Target/TargetMachine.h" | ||
24 | #include "llvm/Target/TargetOptions.h" | ||
20 | #include <memory> | 25 | #include <memory> |
21 | 26 | ||
22 | #include "clang.h" | 27 | #include "clang.h" |
@@ -105,12 +110,52 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) | |||
105 | return getModuleFromSource(std::move(CFlags), Path, VFS); | 110 | return getModuleFromSource(std::move(CFlags), Path, VFS); |
106 | } | 111 | } |
107 | 112 | ||
113 | std::unique_ptr<llvm::SmallVectorImpl<char>> | ||
114 | getBPFObjectFromModule(llvm::Module *Module) | ||
115 | { | ||
116 | using namespace llvm; | ||
117 | |||
118 | std::string TargetTriple("bpf-pc-linux"); | ||
119 | std::string Error; | ||
120 | const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error); | ||
121 | if (!Target) { | ||
122 | llvm::errs() << Error; | ||
123 | return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr); | ||
124 | } | ||
125 | |||
126 | llvm::TargetOptions Opt; | ||
127 | TargetMachine *TargetMachine = | ||
128 | Target->createTargetMachine(TargetTriple, | ||
129 | "generic", "", | ||
130 | Opt, Reloc::Static); | ||
131 | |||
132 | Module->setDataLayout(TargetMachine->createDataLayout()); | ||
133 | Module->setTargetTriple(TargetTriple); | ||
134 | |||
135 | std::unique_ptr<SmallVectorImpl<char>> Buffer(new SmallVector<char, 0>()); | ||
136 | raw_svector_ostream ostream(*Buffer); | ||
137 | |||
138 | legacy::PassManager PM; | ||
139 | if (TargetMachine->addPassesToEmitFile(PM, ostream, | ||
140 | TargetMachine::CGFT_ObjectFile)) { | ||
141 | llvm::errs() << "TargetMachine can't emit a file of this type\n"; | ||
142 | return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);; | ||
143 | } | ||
144 | PM.run(*Module); | ||
145 | |||
146 | return std::move(Buffer); | ||
147 | } | ||
148 | |||
108 | } | 149 | } |
109 | 150 | ||
110 | extern "C" { | 151 | extern "C" { |
111 | void perf_clang__init(void) | 152 | void perf_clang__init(void) |
112 | { | 153 | { |
113 | perf::LLVMCtx.reset(new llvm::LLVMContext()); | 154 | perf::LLVMCtx.reset(new llvm::LLVMContext()); |
155 | LLVMInitializeBPFTargetInfo(); | ||
156 | LLVMInitializeBPFTarget(); | ||
157 | LLVMInitializeBPFTargetMC(); | ||
158 | LLVMInitializeBPFAsmPrinter(); | ||
114 | } | 159 | } |
115 | 160 | ||
116 | void perf_clang__cleanup(void) | 161 | void perf_clang__cleanup(void) |