Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * llvm C frontend for perf. Support dynamically compile C file
4 *
5 * Inspired by clang example code:
6 * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp
7 *
8 * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
9 * Copyright (C) 2016 Huawei Inc.
10 */
11
12#include "clang/Basic/Version.h"
13#include "clang/CodeGen/CodeGenAction.h"
14#include "clang/Frontend/CompilerInvocation.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/TextDiagnosticPrinter.h"
17#include "clang/Tooling/Tooling.h"
18#include "llvm/IR/LegacyPassManager.h"
19#include "llvm/IR/Module.h"
20#include "llvm/Option/Option.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/TargetRegistry.h"
24#include "llvm/Support/TargetSelect.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetOptions.h"
27#include <memory>
28
29#include "clang.h"
30#include "clang-c.h"
31
32namespace perf {
33
34static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
35
36using namespace clang;
37
38static CompilerInvocation *
39createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
40 DiagnosticsEngine& Diags)
41{
42 llvm::opt::ArgStringList CCArgs {
43 "-cc1",
44 "-triple", "bpf-pc-linux",
45 "-fsyntax-only",
46 "-O2",
47 "-nostdsysteminc",
48 "-nobuiltininc",
49 "-vectorize-loops",
50 "-vectorize-slp",
51 "-Wno-unused-value",
52 "-Wno-pointer-sign",
53 "-x", "c"};
54
55 CCArgs.append(CFlags.begin(), CFlags.end());
56 CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs
57#if CLANG_VERSION_MAJOR >= 11
58 ,/*BinaryName=*/nullptr
59#endif
60 );
61
62 FrontendOptions& Opts = CI->getFrontendOpts();
63 Opts.Inputs.clear();
64 Opts.Inputs.emplace_back(Path,
65 FrontendOptions::getInputKindForExtension("c"));
66 return CI;
67}
68
69static std::unique_ptr<llvm::Module>
70getModuleFromSource(llvm::opt::ArgStringList CFlags,
71 StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
72{
73 CompilerInstance Clang;
74 Clang.createDiagnostics();
75
76#if CLANG_VERSION_MAJOR < 9
77 Clang.setVirtualFileSystem(&*VFS);
78#else
79 Clang.createFileManager(&*VFS);
80#endif
81
82#if CLANG_VERSION_MAJOR < 4
83 IntrusiveRefCntPtr<CompilerInvocation> CI =
84 createCompilerInvocation(std::move(CFlags), Path,
85 Clang.getDiagnostics());
86 Clang.setInvocation(&*CI);
87#else
88 std::shared_ptr<CompilerInvocation> CI(
89 createCompilerInvocation(std::move(CFlags), Path,
90 Clang.getDiagnostics()));
91 Clang.setInvocation(CI);
92#endif
93
94 std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
95 if (!Clang.ExecuteAction(*Act))
96 return std::unique_ptr<llvm::Module>(nullptr);
97
98 return Act->takeModule();
99}
100
101std::unique_ptr<llvm::Module>
102getModuleFromSource(llvm::opt::ArgStringList CFlags,
103 StringRef Name, StringRef Content)
104{
105 using namespace vfs;
106
107 llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
108 new OverlayFileSystem(getRealFileSystem()));
109 llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
110 new InMemoryFileSystem(true));
111
112 /*
113 * pushOverlay helps setting working dir for MemFS. Must call
114 * before addFile.
115 */
116 OverlayFS->pushOverlay(MemFS);
117 MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
118
119 return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
120}
121
122std::unique_ptr<llvm::Module>
123getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
124{
125 IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
126 return getModuleFromSource(std::move(CFlags), Path, VFS);
127}
128
129std::unique_ptr<llvm::SmallVectorImpl<char>>
130getBPFObjectFromModule(llvm::Module *Module)
131{
132 using namespace llvm;
133
134 std::string TargetTriple("bpf-pc-linux");
135 std::string Error;
136 const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
137 if (!Target) {
138 llvm::errs() << Error;
139 return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);
140 }
141
142 llvm::TargetOptions Opt;
143 TargetMachine *TargetMachine =
144 Target->createTargetMachine(TargetTriple,
145 "generic", "",
146 Opt, Reloc::Static);
147
148 Module->setDataLayout(TargetMachine->createDataLayout());
149 Module->setTargetTriple(TargetTriple);
150
151 std::unique_ptr<SmallVectorImpl<char>> Buffer(new SmallVector<char, 0>());
152 raw_svector_ostream ostream(*Buffer);
153
154 legacy::PassManager PM;
155 bool NotAdded;
156 NotAdded = TargetMachine->addPassesToEmitFile(PM, ostream
157#if CLANG_VERSION_MAJOR >= 7
158 , /*DwoOut=*/nullptr
159#endif
160#if CLANG_VERSION_MAJOR < 10
161 , TargetMachine::CGFT_ObjectFile
162#else
163 , llvm::CGFT_ObjectFile
164#endif
165 );
166 if (NotAdded) {
167 llvm::errs() << "TargetMachine can't emit a file of this type\n";
168 return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);
169 }
170 PM.run(*Module);
171
172 return Buffer;
173}
174
175}
176
177extern "C" {
178void perf_clang__init(void)
179{
180 perf::LLVMCtx.reset(new llvm::LLVMContext());
181 LLVMInitializeBPFTargetInfo();
182 LLVMInitializeBPFTarget();
183 LLVMInitializeBPFTargetMC();
184 LLVMInitializeBPFAsmPrinter();
185}
186
187void perf_clang__cleanup(void)
188{
189 perf::LLVMCtx.reset(nullptr);
190 llvm::llvm_shutdown();
191}
192
193int perf_clang__compile_bpf(const char *filename,
194 void **p_obj_buf,
195 size_t *p_obj_buf_sz)
196{
197 using namespace perf;
198
199 if (!p_obj_buf || !p_obj_buf_sz)
200 return -EINVAL;
201
202 llvm::opt::ArgStringList CFlags;
203 auto M = getModuleFromSource(std::move(CFlags), filename);
204 if (!M)
205 return -EINVAL;
206 auto O = getBPFObjectFromModule(&*M);
207 if (!O)
208 return -EINVAL;
209
210 size_t size = O->size_in_bytes();
211 void *buffer;
212
213 buffer = malloc(size);
214 if (!buffer)
215 return -ENOMEM;
216 memcpy(buffer, O->data(), size);
217 *p_obj_buf = buffer;
218 *p_obj_buf_sz = size;
219 return 0;
220}
221}