1 //===-------------- PassBuilder bindings for LLVM-C -----------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines the C bindings to the new pass manager
12 //===----------------------------------------------------------------------===//
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"
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
{
28 explicit LLVMPassBuilderOptions(
29 bool DebugLogging
= false, bool VerifyEach
= false,
30 PipelineTuningOptions PTO
= PipelineTuningOptions())
31 : DebugLogging(DebugLogging
), VerifyEach(VerifyEach
), PTO(PTO
) {}
35 PipelineTuningOptions PTO
;
39 static TargetMachine
*unwrap(LLVMTargetMachineRef P
) {
40 return reinterpret_cast<TargetMachine
*>(P
);
43 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions
,
44 LLVMPassBuilderOptionsRef
)
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
;
54 Module
*Mod
= unwrap(M
);
55 PassInstrumentationCallbacks PIC
;
56 PassBuilder
PB(Machine
, PassOpts
->PTO
, std::nullopt
, &PIC
);
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
);
68 StandardInstrumentations
SI(Mod
->getContext(), Debug
, VerifyEach
);
69 SI
.registerCallbacks(PIC
, &MAM
);
70 ModulePassManager MPM
;
72 MPM
.addPass(VerifierPass());
74 if (auto Err
= PB
.parsePassPipeline(MPM
, Passes
)) {
75 return wrap(std::move(Err
));
79 return LLVMErrorSuccess
;
82 LLVMPassBuilderOptionsRef
LLVMCreatePassBuilderOptions() {
83 return wrap(new LLVMPassBuilderOptions());
86 void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options
,
87 LLVMBool VerifyEach
) {
88 unwrap(Options
)->VerifyEach
= VerifyEach
;
91 void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options
,
92 LLVMBool DebugLogging
) {
93 unwrap(Options
)->DebugLogging
= DebugLogging
;
96 void LLVMPassBuilderOptionsSetLoopInterleaving(
97 LLVMPassBuilderOptionsRef Options
, LLVMBool LoopInterleaving
) {
98 unwrap(Options
)->PTO
.LoopInterleaving
= LoopInterleaving
;
101 void LLVMPassBuilderOptionsSetLoopVectorization(
102 LLVMPassBuilderOptionsRef Options
, LLVMBool LoopVectorization
) {
103 unwrap(Options
)->PTO
.LoopVectorization
= LoopVectorization
;
106 void LLVMPassBuilderOptionsSetSLPVectorization(
107 LLVMPassBuilderOptionsRef Options
, LLVMBool SLPVectorization
) {
108 unwrap(Options
)->PTO
.SLPVectorization
= SLPVectorization
;
111 void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options
,
112 LLVMBool LoopUnrolling
) {
113 unwrap(Options
)->PTO
.LoopUnrolling
= LoopUnrolling
;
116 void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(
117 LLVMPassBuilderOptionsRef Options
, LLVMBool ForgetAllSCEVInLoopUnroll
) {
118 unwrap(Options
)->PTO
.ForgetAllSCEVInLoopUnroll
= ForgetAllSCEVInLoopUnroll
;
121 void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options
,
122 unsigned LicmMssaOptCap
) {
123 unwrap(Options
)->PTO
.LicmMssaOptCap
= LicmMssaOptCap
;
126 void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(
127 LLVMPassBuilderOptionsRef Options
, unsigned LicmMssaNoAccForPromotionCap
) {
128 unwrap(Options
)->PTO
.LicmMssaNoAccForPromotionCap
=
129 LicmMssaNoAccForPromotionCap
;
132 void LLVMPassBuilderOptionsSetCallGraphProfile(
133 LLVMPassBuilderOptionsRef Options
, LLVMBool CallGraphProfile
) {
134 unwrap(Options
)->PTO
.CallGraphProfile
= CallGraphProfile
;
137 void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options
,
138 LLVMBool MergeFunctions
) {
139 unwrap(Options
)->PTO
.MergeFunctions
= MergeFunctions
;
142 void LLVMPassBuilderOptionsSetInlinerThreshold(
143 LLVMPassBuilderOptionsRef Options
, int Threshold
) {
144 unwrap(Options
)->PTO
.InlinerThreshold
= Threshold
;
147 void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options
) {
148 delete unwrap(Options
);