PassBuilderBindings.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===-------------- PassBuilder bindings for LLVM-C -----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file defines the C bindings to the new pass manager
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-c/Transforms/PassBuilder.h"
  14. #include "llvm/IR/Verifier.h"
  15. #include "llvm/Passes/PassBuilder.h"
  16. #include "llvm/Passes/StandardInstrumentations.h"
  17. #include "llvm/Support/CBindingWrapping.h"
  18. using namespace llvm;
  19. namespace llvm {
  20. /// Helper struct for holding a set of builder options for LLVMRunPasses. This
  21. /// structure is used to keep LLVMRunPasses backwards compatible with future
  22. /// versions in case we modify the options the new Pass Manager utilizes.
  23. class LLVMPassBuilderOptions {
  24. public:
  25. explicit LLVMPassBuilderOptions(
  26. bool DebugLogging = false, bool VerifyEach = false,
  27. PipelineTuningOptions PTO = PipelineTuningOptions())
  28. : DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {}
  29. bool DebugLogging;
  30. bool VerifyEach;
  31. PipelineTuningOptions PTO;
  32. };
  33. } // namespace llvm
  34. static TargetMachine *unwrap(LLVMTargetMachineRef P) {
  35. return reinterpret_cast<TargetMachine *>(P);
  36. }
  37. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions,
  38. LLVMPassBuilderOptionsRef)
  39. LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
  40. LLVMTargetMachineRef TM,
  41. LLVMPassBuilderOptionsRef Options) {
  42. TargetMachine *Machine = unwrap(TM);
  43. LLVMPassBuilderOptions *PassOpts = unwrap(Options);
  44. bool Debug = PassOpts->DebugLogging;
  45. bool VerifyEach = PassOpts->VerifyEach;
  46. Module *Mod = unwrap(M);
  47. PassInstrumentationCallbacks PIC;
  48. PassBuilder PB(Machine, PassOpts->PTO, None, &PIC);
  49. LoopAnalysisManager LAM;
  50. FunctionAnalysisManager FAM;
  51. CGSCCAnalysisManager CGAM;
  52. ModuleAnalysisManager MAM;
  53. PB.registerLoopAnalyses(LAM);
  54. PB.registerFunctionAnalyses(FAM);
  55. PB.registerCGSCCAnalyses(CGAM);
  56. PB.registerModuleAnalyses(MAM);
  57. PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
  58. StandardInstrumentations SI(Debug, VerifyEach);
  59. SI.registerCallbacks(PIC, &FAM);
  60. ModulePassManager MPM;
  61. if (VerifyEach) {
  62. MPM.addPass(VerifierPass());
  63. }
  64. if (auto Err = PB.parsePassPipeline(MPM, Passes)) {
  65. return wrap(std::move(Err));
  66. }
  67. MPM.run(*Mod, MAM);
  68. return LLVMErrorSuccess;
  69. }
  70. LLVMPassBuilderOptionsRef LLVMCreatePassBuilderOptions() {
  71. return wrap(new LLVMPassBuilderOptions());
  72. }
  73. void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,
  74. LLVMBool VerifyEach) {
  75. unwrap(Options)->VerifyEach = VerifyEach;
  76. }
  77. void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
  78. LLVMBool DebugLogging) {
  79. unwrap(Options)->DebugLogging = DebugLogging;
  80. }
  81. void LLVMPassBuilderOptionsSetLoopInterleaving(
  82. LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {
  83. unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving;
  84. }
  85. void LLVMPassBuilderOptionsSetLoopVectorization(
  86. LLVMPassBuilderOptionsRef Options, LLVMBool LoopVectorization) {
  87. unwrap(Options)->PTO.LoopVectorization = LoopVectorization;
  88. }
  89. void LLVMPassBuilderOptionsSetSLPVectorization(
  90. LLVMPassBuilderOptionsRef Options, LLVMBool SLPVectorization) {
  91. unwrap(Options)->PTO.SLPVectorization = SLPVectorization;
  92. }
  93. void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options,
  94. LLVMBool LoopUnrolling) {
  95. unwrap(Options)->PTO.LoopUnrolling = LoopUnrolling;
  96. }
  97. void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(
  98. LLVMPassBuilderOptionsRef Options, LLVMBool ForgetAllSCEVInLoopUnroll) {
  99. unwrap(Options)->PTO.ForgetAllSCEVInLoopUnroll = ForgetAllSCEVInLoopUnroll;
  100. }
  101. void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options,
  102. unsigned LicmMssaOptCap) {
  103. unwrap(Options)->PTO.LicmMssaOptCap = LicmMssaOptCap;
  104. }
  105. void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(
  106. LLVMPassBuilderOptionsRef Options, unsigned LicmMssaNoAccForPromotionCap) {
  107. unwrap(Options)->PTO.LicmMssaNoAccForPromotionCap =
  108. LicmMssaNoAccForPromotionCap;
  109. }
  110. void LLVMPassBuilderOptionsSetCallGraphProfile(
  111. LLVMPassBuilderOptionsRef Options, LLVMBool CallGraphProfile) {
  112. unwrap(Options)->PTO.CallGraphProfile = CallGraphProfile;
  113. }
  114. void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options,
  115. LLVMBool MergeFunctions) {
  116. unwrap(Options)->PTO.MergeFunctions = MergeFunctions;
  117. }
  118. void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) {
  119. delete unwrap(Options);
  120. }