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
21using namespace llvm;
22
23namespace 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.
27class LLVMPassBuilderOptions {
28public:
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
40static TargetMachine *unwrap(LLVMTargetMachineRef P) {
41 return reinterpret_cast<TargetMachine *>(P);
42}
43
44DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassBuilderOptions,
45 LLVMPassBuilderOptionsRef)
46
47LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
48 LLVMTargetMachineRef TM,
49 LLVMPassBuilderOptionsRef Options) {
50 TargetMachine *Machine = unwrap(P: TM);
51 LLVMPassBuilderOptions *PassOpts = unwrap(P: Options);
52 bool Debug = PassOpts->DebugLogging;
53 bool VerifyEach = PassOpts->VerifyEach;
54
55 Module *Mod = unwrap(P: 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: &MAM);
71 ModulePassManager MPM;
72 if (VerifyEach) {
73 MPM.addPass(Pass: VerifierPass());
74 }
75 if (auto Err = PB.parsePassPipeline(MPM, PipelineText: Passes)) {
76 return wrap(Err: std::move(Err));
77 }
78
79 MPM.run(IR&: *Mod, AM&: MAM);
80 return LLVMErrorSuccess;
81}
82
83LLVMPassBuilderOptionsRef LLVMCreatePassBuilderOptions() {
84 return wrap(P: new LLVMPassBuilderOptions());
85}
86
87void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,
88 LLVMBool VerifyEach) {
89 unwrap(P: Options)->VerifyEach = VerifyEach;
90}
91
92void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
93 LLVMBool DebugLogging) {
94 unwrap(P: Options)->DebugLogging = DebugLogging;
95}
96
97void LLVMPassBuilderOptionsSetLoopInterleaving(
98 LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {
99 unwrap(P: Options)->PTO.LoopInterleaving = LoopInterleaving;
100}
101
102void LLVMPassBuilderOptionsSetLoopVectorization(
103 LLVMPassBuilderOptionsRef Options, LLVMBool LoopVectorization) {
104 unwrap(P: Options)->PTO.LoopVectorization = LoopVectorization;
105}
106
107void LLVMPassBuilderOptionsSetSLPVectorization(
108 LLVMPassBuilderOptionsRef Options, LLVMBool SLPVectorization) {
109 unwrap(P: Options)->PTO.SLPVectorization = SLPVectorization;
110}
111
112void LLVMPassBuilderOptionsSetLoopUnrolling(LLVMPassBuilderOptionsRef Options,
113 LLVMBool LoopUnrolling) {
114 unwrap(P: Options)->PTO.LoopUnrolling = LoopUnrolling;
115}
116
117void LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(
118 LLVMPassBuilderOptionsRef Options, LLVMBool ForgetAllSCEVInLoopUnroll) {
119 unwrap(P: Options)->PTO.ForgetAllSCEVInLoopUnroll = ForgetAllSCEVInLoopUnroll;
120}
121
122void LLVMPassBuilderOptionsSetLicmMssaOptCap(LLVMPassBuilderOptionsRef Options,
123 unsigned LicmMssaOptCap) {
124 unwrap(P: Options)->PTO.LicmMssaOptCap = LicmMssaOptCap;
125}
126
127void LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(
128 LLVMPassBuilderOptionsRef Options, unsigned LicmMssaNoAccForPromotionCap) {
129 unwrap(P: Options)->PTO.LicmMssaNoAccForPromotionCap =
130 LicmMssaNoAccForPromotionCap;
131}
132
133void LLVMPassBuilderOptionsSetCallGraphProfile(
134 LLVMPassBuilderOptionsRef Options, LLVMBool CallGraphProfile) {
135 unwrap(P: Options)->PTO.CallGraphProfile = CallGraphProfile;
136}
137
138void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options,
139 LLVMBool MergeFunctions) {
140 unwrap(P: Options)->PTO.MergeFunctions = MergeFunctions;
141}
142
143void LLVMPassBuilderOptionsSetInlinerThreshold(
144 LLVMPassBuilderOptionsRef Options, int Threshold) {
145 unwrap(P: Options)->PTO.InlinerThreshold = Threshold;
146}
147
148void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) {
149 delete unwrap(P: Options);
150}
151