clang.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "clang/CodeGen/CodeGenAction.h"
  11. #include "clang/Frontend/CompilerInvocation.h"
  12. #include "clang/Frontend/CompilerInstance.h"
  13. #include "clang/Frontend/TextDiagnosticPrinter.h"
  14. #include "clang/Tooling/Tooling.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/Option/Option.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/ManagedStatic.h"
  19. #include <memory>
  20. #include "clang.h"
  21. #include "clang-c.h"
  22. namespace perf {
  23. static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
  24. using namespace clang;
  25. static CompilerInvocation *
  26. createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
  27. DiagnosticsEngine& Diags)
  28. {
  29. llvm::opt::ArgStringList CCArgs {
  30. "-cc1",
  31. "-triple", "bpf-pc-linux",
  32. "-fsyntax-only",
  33. "-ferror-limit", "19",
  34. "-fmessage-length", "127",
  35. "-O2",
  36. "-nostdsysteminc",
  37. "-nobuiltininc",
  38. "-vectorize-loops",
  39. "-vectorize-slp",
  40. "-Wno-unused-value",
  41. "-Wno-pointer-sign",
  42. "-x", "c"};
  43. CCArgs.append(CFlags.begin(), CFlags.end());
  44. CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
  45. FrontendOptions& Opts = CI->getFrontendOpts();
  46. Opts.Inputs.clear();
  47. Opts.Inputs.emplace_back(Path, IK_C);
  48. return CI;
  49. }
  50. static std::unique_ptr<llvm::Module>
  51. getModuleFromSource(llvm::opt::ArgStringList CFlags,
  52. StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
  53. {
  54. CompilerInstance Clang;
  55. Clang.createDiagnostics();
  56. Clang.setVirtualFileSystem(&*VFS);
  57. IntrusiveRefCntPtr<CompilerInvocation> CI =
  58. createCompilerInvocation(std::move(CFlags), Path,
  59. Clang.getDiagnostics());
  60. Clang.setInvocation(&*CI);
  61. std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
  62. if (!Clang.ExecuteAction(*Act))
  63. return std::unique_ptr<llvm::Module>(nullptr);
  64. return Act->takeModule();
  65. }
  66. std::unique_ptr<llvm::Module>
  67. getModuleFromSource(llvm::opt::ArgStringList CFlags,
  68. StringRef Name, StringRef Content)
  69. {
  70. using namespace vfs;
  71. llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
  72. new OverlayFileSystem(getRealFileSystem()));
  73. llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
  74. new InMemoryFileSystem(true));
  75. /*
  76. * pushOverlay helps setting working dir for MemFS. Must call
  77. * before addFile.
  78. */
  79. OverlayFS->pushOverlay(MemFS);
  80. MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
  81. return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
  82. }
  83. std::unique_ptr<llvm::Module>
  84. getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
  85. {
  86. IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
  87. return getModuleFromSource(std::move(CFlags), Path, VFS);
  88. }
  89. }
  90. extern "C" {
  91. void perf_clang__init(void)
  92. {
  93. perf::LLVMCtx.reset(new llvm::LLVMContext());
  94. }
  95. void perf_clang__cleanup(void)
  96. {
  97. perf::LLVMCtx.reset(nullptr);
  98. llvm::llvm_shutdown();
  99. }
  100. }