Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * llvm C frontend for perf. Support dynamically compile C file |
| 3 | * |
| 4 | * Inspired by clang example code: |
| 5 | * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp |
| 6 | * |
| 7 | * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com> |
| 8 | * Copyright (C) 2016 Huawei Inc. |
| 9 | */ |
| 10 | |
| 11 | #include "clang/CodeGen/CodeGenAction.h" |
| 12 | #include "clang/Frontend/CompilerInvocation.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 15 | #include "clang/Tooling/Tooling.h" |
| 16 | #include "llvm/IR/Module.h" |
| 17 | #include "llvm/Option/Option.h" |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
| 20 | #include <memory> |
| 21 | |
| 22 | #include "clang.h" |
| 23 | #include "clang-c.h" |
| 24 | |
| 25 | namespace perf { |
| 26 | |
| 27 | static std::unique_ptr<llvm::LLVMContext> LLVMCtx; |
| 28 | |
| 29 | using namespace clang; |
| 30 | |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 31 | static CompilerInvocation * |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 32 | createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, |
| 33 | DiagnosticsEngine& Diags) |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 34 | { |
| 35 | llvm::opt::ArgStringList CCArgs { |
| 36 | "-cc1", |
| 37 | "-triple", "bpf-pc-linux", |
| 38 | "-fsyntax-only", |
| 39 | "-ferror-limit", "19", |
| 40 | "-fmessage-length", "127", |
| 41 | "-O2", |
| 42 | "-nostdsysteminc", |
| 43 | "-nobuiltininc", |
| 44 | "-vectorize-loops", |
| 45 | "-vectorize-slp", |
| 46 | "-Wno-unused-value", |
| 47 | "-Wno-pointer-sign", |
| 48 | "-x", "c"}; |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 49 | |
| 50 | CCArgs.append(CFlags.begin(), CFlags.end()); |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 51 | CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); |
| 52 | |
| 53 | FrontendOptions& Opts = CI->getFrontendOpts(); |
| 54 | Opts.Inputs.clear(); |
| 55 | Opts.Inputs.emplace_back(Path, IK_C); |
| 56 | return CI; |
| 57 | } |
| 58 | |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 59 | static std::unique_ptr<llvm::Module> |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 60 | getModuleFromSource(llvm::opt::ArgStringList CFlags, |
| 61 | StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS) |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 62 | { |
| 63 | CompilerInstance Clang; |
| 64 | Clang.createDiagnostics(); |
| 65 | |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 66 | Clang.setVirtualFileSystem(&*VFS); |
| 67 | |
| 68 | IntrusiveRefCntPtr<CompilerInvocation> CI = |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 69 | createCompilerInvocation(std::move(CFlags), Path, |
| 70 | Clang.getDiagnostics()); |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 71 | Clang.setInvocation(&*CI); |
| 72 | |
| 73 | std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); |
| 74 | if (!Clang.ExecuteAction(*Act)) |
| 75 | return std::unique_ptr<llvm::Module>(nullptr); |
| 76 | |
| 77 | return Act->takeModule(); |
| 78 | } |
| 79 | |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 80 | std::unique_ptr<llvm::Module> |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 81 | getModuleFromSource(llvm::opt::ArgStringList CFlags, |
| 82 | StringRef Name, StringRef Content) |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 83 | { |
| 84 | using namespace vfs; |
| 85 | |
| 86 | llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS( |
| 87 | new OverlayFileSystem(getRealFileSystem())); |
| 88 | llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS( |
| 89 | new InMemoryFileSystem(true)); |
| 90 | |
| 91 | /* |
| 92 | * pushOverlay helps setting working dir for MemFS. Must call |
| 93 | * before addFile. |
| 94 | */ |
| 95 | OverlayFS->pushOverlay(MemFS); |
| 96 | MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); |
| 97 | |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 98 | return getModuleFromSource(std::move(CFlags), Name, OverlayFS); |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | std::unique_ptr<llvm::Module> |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 102 | getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 103 | { |
| 104 | IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); |
Wang Nan | a9495fe | 2016-11-26 07:03:36 +0000 | [diff] [blame^] | 105 | return getModuleFromSource(std::move(CFlags), Path, VFS); |
Wang Nan | 77dfa84 | 2016-11-26 07:03:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Wang Nan | 00b8669 | 2016-11-26 07:03:34 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | extern "C" { |
| 111 | void perf_clang__init(void) |
| 112 | { |
| 113 | perf::LLVMCtx.reset(new llvm::LLVMContext()); |
| 114 | } |
| 115 | |
| 116 | void perf_clang__cleanup(void) |
| 117 | { |
| 118 | perf::LLVMCtx.reset(nullptr); |
| 119 | llvm::llvm_shutdown(); |
| 120 | } |
| 121 | } |