blob: bf31ceab33bd487d0021ccd9818384dde51fd371 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Wang Nan00b86692016-11-26 07:03:34 +00002/*
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
Sandipan Das7854e492018-04-04 23:34:18 +053012#include "clang/Basic/Version.h"
Wang Nan00b86692016-11-26 07:03:34 +000013#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"
Wang Nan5e08a762016-11-26 07:03:38 +000018#include "llvm/IR/LegacyPassManager.h"
Wang Nan00b86692016-11-26 07:03:34 +000019#include "llvm/IR/Module.h"
20#include "llvm/Option/Option.h"
Wang Nan77dfa842016-11-26 07:03:35 +000021#include "llvm/Support/FileSystem.h"
Wang Nan00b86692016-11-26 07:03:34 +000022#include "llvm/Support/ManagedStatic.h"
Wang Nan5e08a762016-11-26 07:03:38 +000023#include "llvm/Support/TargetRegistry.h"
24#include "llvm/Support/TargetSelect.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetOptions.h"
Wang Nan00b86692016-11-26 07:03:34 +000027#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
Wang Nan00b86692016-11-26 07:03:34 +000038static CompilerInvocation *
Wang Nana9495fe2016-11-26 07:03:36 +000039createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
40 DiagnosticsEngine& Diags)
Wang Nan00b86692016-11-26 07:03:34 +000041{
42 llvm::opt::ArgStringList CCArgs {
43 "-cc1",
44 "-triple", "bpf-pc-linux",
45 "-fsyntax-only",
46 "-ferror-limit", "19",
47 "-fmessage-length", "127",
48 "-O2",
49 "-nostdsysteminc",
50 "-nobuiltininc",
51 "-vectorize-loops",
52 "-vectorize-slp",
53 "-Wno-unused-value",
54 "-Wno-pointer-sign",
55 "-x", "c"};
Wang Nana9495fe2016-11-26 07:03:36 +000056
57 CCArgs.append(CFlags.begin(), CFlags.end());
Wang Nan00b86692016-11-26 07:03:34 +000058 CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
59
60 FrontendOptions& Opts = CI->getFrontendOpts();
61 Opts.Inputs.clear();
Sandipan Das7854e492018-04-04 23:34:18 +053062 Opts.Inputs.emplace_back(Path,
63 FrontendOptions::getInputKindForExtension("c"));
Wang Nan00b86692016-11-26 07:03:34 +000064 return CI;
65}
66
Wang Nan77dfa842016-11-26 07:03:35 +000067static std::unique_ptr<llvm::Module>
Wang Nana9495fe2016-11-26 07:03:36 +000068getModuleFromSource(llvm::opt::ArgStringList CFlags,
69 StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
Wang Nan00b86692016-11-26 07:03:34 +000070{
71 CompilerInstance Clang;
72 Clang.createDiagnostics();
73
Wang Nan00b86692016-11-26 07:03:34 +000074 Clang.setVirtualFileSystem(&*VFS);
75
Sandipan Das7854e492018-04-04 23:34:18 +053076#if CLANG_VERSION_MAJOR < 4
Wang Nan00b86692016-11-26 07:03:34 +000077 IntrusiveRefCntPtr<CompilerInvocation> CI =
Wang Nana9495fe2016-11-26 07:03:36 +000078 createCompilerInvocation(std::move(CFlags), Path,
79 Clang.getDiagnostics());
Wang Nan00b86692016-11-26 07:03:34 +000080 Clang.setInvocation(&*CI);
Sandipan Das7854e492018-04-04 23:34:18 +053081#else
82 std::shared_ptr<CompilerInvocation> CI(
83 createCompilerInvocation(std::move(CFlags), Path,
84 Clang.getDiagnostics()));
85 Clang.setInvocation(CI);
86#endif
Wang Nan00b86692016-11-26 07:03:34 +000087
88 std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
89 if (!Clang.ExecuteAction(*Act))
90 return std::unique_ptr<llvm::Module>(nullptr);
91
92 return Act->takeModule();
93}
94
Wang Nan77dfa842016-11-26 07:03:35 +000095std::unique_ptr<llvm::Module>
Wang Nana9495fe2016-11-26 07:03:36 +000096getModuleFromSource(llvm::opt::ArgStringList CFlags,
97 StringRef Name, StringRef Content)
Wang Nan77dfa842016-11-26 07:03:35 +000098{
99 using namespace vfs;
100
101 llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
102 new OverlayFileSystem(getRealFileSystem()));
103 llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
104 new InMemoryFileSystem(true));
105
106 /*
107 * pushOverlay helps setting working dir for MemFS. Must call
108 * before addFile.
109 */
110 OverlayFS->pushOverlay(MemFS);
111 MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
112
Wang Nana9495fe2016-11-26 07:03:36 +0000113 return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
Wang Nan77dfa842016-11-26 07:03:35 +0000114}
115
116std::unique_ptr<llvm::Module>
Wang Nana9495fe2016-11-26 07:03:36 +0000117getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
Wang Nan77dfa842016-11-26 07:03:35 +0000118{
119 IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
Wang Nana9495fe2016-11-26 07:03:36 +0000120 return getModuleFromSource(std::move(CFlags), Path, VFS);
Wang Nan77dfa842016-11-26 07:03:35 +0000121}
122
Wang Nan5e08a762016-11-26 07:03:38 +0000123std::unique_ptr<llvm::SmallVectorImpl<char>>
124getBPFObjectFromModule(llvm::Module *Module)
125{
126 using namespace llvm;
127
128 std::string TargetTriple("bpf-pc-linux");
129 std::string Error;
130 const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
131 if (!Target) {
132 llvm::errs() << Error;
133 return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);
134 }
135
136 llvm::TargetOptions Opt;
137 TargetMachine *TargetMachine =
138 Target->createTargetMachine(TargetTriple,
139 "generic", "",
140 Opt, Reloc::Static);
141
142 Module->setDataLayout(TargetMachine->createDataLayout());
143 Module->setTargetTriple(TargetTriple);
144
145 std::unique_ptr<SmallVectorImpl<char>> Buffer(new SmallVector<char, 0>());
146 raw_svector_ostream ostream(*Buffer);
147
148 legacy::PassManager PM;
149 if (TargetMachine->addPassesToEmitFile(PM, ostream,
150 TargetMachine::CGFT_ObjectFile)) {
151 llvm::errs() << "TargetMachine can't emit a file of this type\n";
152 return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);;
153 }
154 PM.run(*Module);
155
156 return std::move(Buffer);
157}
158
Wang Nan00b86692016-11-26 07:03:34 +0000159}
160
161extern "C" {
162void perf_clang__init(void)
163{
164 perf::LLVMCtx.reset(new llvm::LLVMContext());
Wang Nan5e08a762016-11-26 07:03:38 +0000165 LLVMInitializeBPFTargetInfo();
166 LLVMInitializeBPFTarget();
167 LLVMInitializeBPFTargetMC();
168 LLVMInitializeBPFAsmPrinter();
Wang Nan00b86692016-11-26 07:03:34 +0000169}
170
171void perf_clang__cleanup(void)
172{
173 perf::LLVMCtx.reset(nullptr);
174 llvm::llvm_shutdown();
175}
Wang Nanedd695b2016-11-26 07:03:39 +0000176
177int perf_clang__compile_bpf(const char *filename,
178 void **p_obj_buf,
179 size_t *p_obj_buf_sz)
180{
181 using namespace perf;
182
183 if (!p_obj_buf || !p_obj_buf_sz)
184 return -EINVAL;
185
186 llvm::opt::ArgStringList CFlags;
187 auto M = getModuleFromSource(std::move(CFlags), filename);
188 if (!M)
189 return -EINVAL;
190 auto O = getBPFObjectFromModule(&*M);
191 if (!O)
192 return -EINVAL;
193
194 size_t size = O->size_in_bytes();
195 void *buffer;
196
197 buffer = malloc(size);
198 if (!buffer)
199 return -ENOMEM;
200 memcpy(buffer, O->data(), size);
201 *p_obj_buf = buffer;
202 *p_obj_buf_sz = size;
203 return 0;
204}
Wang Nan00b86692016-11-26 07:03:34 +0000205}