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