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