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