xref: /freebsd/contrib/llvm-project/llvm/lib/Passes/PassBuilderBindings.cpp (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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 
14 #include "llvm-c/Transforms/PassBuilder.h"
15 #include "llvm/IR/Verifier.h"
16 #include "llvm/Passes/PassBuilder.h"
17 #include "llvm/Passes/StandardInstrumentations.h"
18 #include "llvm/Support/CBindingWrapping.h"
19 
20 using namespace llvm;
21 
22 namespace llvm {
23 /// Helper struct for holding a set of builder options for LLVMRunPasses. This
24 /// structure is used to keep LLVMRunPasses backwards compatible with future
25 /// versions in case we modify the options the new Pass Manager utilizes.
26 class LLVMPassBuilderOptions {
27 public:
28   explicit LLVMPassBuilderOptions(
29       bool DebugLogging = false, bool VerifyEach = false,
30       PipelineTuningOptions PTO = PipelineTuningOptions())
31       : DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {}
32 
33   bool DebugLogging;
34   bool VerifyEach;
35   PipelineTuningOptions PTO;
36 };
37 } // namespace llvm
38 
39 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
40   return reinterpret_cast<TargetMachine *>(P);
41 }
42 
43 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions,
44                                    LLVMPassBuilderOptionsRef)
45 
46 LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
47                            LLVMTargetMachineRef TM,
48                            LLVMPassBuilderOptionsRef Options) {
49   TargetMachine *Machine = unwrap(TM);
50   LLVMPassBuilderOptions *PassOpts = unwrap(Options);
51   bool Debug = PassOpts->DebugLogging;
52   bool VerifyEach = PassOpts->VerifyEach;
53 
54   Module *Mod = unwrap(M);
55   PassInstrumentationCallbacks PIC;
56   PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC);
57 
58   LoopAnalysisManager LAM;
59   FunctionAnalysisManager FAM;
60   CGSCCAnalysisManager CGAM;
61   ModuleAnalysisManager MAM;
62   PB.registerLoopAnalyses(LAM);
63   PB.registerFunctionAnalyses(FAM);
64   PB.registerCGSCCAnalyses(CGAM);
65   PB.registerModuleAnalyses(MAM);
66   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
67 
68   StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach);
69   SI.registerCallbacks(PIC, &FAM);
70   ModulePassManager MPM;
71   if (VerifyEach) {
72     MPM.addPass(VerifierPass());
73   }
74   if (auto Err = PB.parsePassPipeline(MPM, Passes)) {
75     return wrap(std::move(Err));
76   }
77 
78   MPM.run(*Mod, MAM);
79   return LLVMErrorSuccess;
80 }
81 
82 LLVMPassBuilderOptionsRef LLVMCreatePassBuilderOptions() {
83   return wrap(new LLVMPassBuilderOptions());
84 }
85 
86 void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,
87                                          LLVMBool VerifyEach) {
88   unwrap(Options)->VerifyEach = VerifyEach;
89 }
90 
91 void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
92                                            LLVMBool DebugLogging) {
93   unwrap(Options)->DebugLogging = DebugLogging;
94 }
95 
96 void LLVMPassBuilderOptionsSetLoopInterleaving(
97     LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {
98   unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving;
99 }
100 
101 void LLVMPassBuilderOptionsSetLoopVectorization(
102     LLVMPassBuilderOptionsRef Options, LLVMBool LoopVectorization) {
103   unwrap(Options)->PTO.LoopVectorization = LoopVectorization;
104 }
105 
106 void LLVMPassBuilderOptionsSetSLPVectorization(
107     LLVMPassBuilderOptionsRef Options, LLVMBool SLPVectorization) {
108   unwrap(Options)->PTO.SLPVectorization = SLPVectorization;
109 }
110 
111 void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options,
112                                             LLVMBool LoopUnrolling) {
113   unwrap(Options)->PTO.LoopUnrolling = LoopUnrolling;
114 }
115 
116 void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(
117     LLVMPassBuilderOptionsRef Options, LLVMBool ForgetAllSCEVInLoopUnroll) {
118   unwrap(Options)->PTO.ForgetAllSCEVInLoopUnroll = ForgetAllSCEVInLoopUnroll;
119 }
120 
121 void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options,
122                                              unsigned LicmMssaOptCap) {
123   unwrap(Options)->PTO.LicmMssaOptCap = LicmMssaOptCap;
124 }
125 
126 void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(
127     LLVMPassBuilderOptionsRef Options, unsigned LicmMssaNoAccForPromotionCap) {
128   unwrap(Options)->PTO.LicmMssaNoAccForPromotionCap =
129       LicmMssaNoAccForPromotionCap;
130 }
131 
132 void LLVMPassBuilderOptionsSetCallGraphProfile(
133     LLVMPassBuilderOptionsRef Options, LLVMBool CallGraphProfile) {
134   unwrap(Options)->PTO.CallGraphProfile = CallGraphProfile;
135 }
136 
137 void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options,
138                                              LLVMBool MergeFunctions) {
139   unwrap(Options)->PTO.MergeFunctions = MergeFunctions;
140 }
141 
142 void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) {
143   delete unwrap(Options);
144 }
145