Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/ffi/newpassmanagers.cpp
1154 views
1
#include "core.h"
2
#include "llvm-c/TargetMachine.h"
3
#include "llvm/Analysis/AliasAnalysisEvaluator.h"
4
#include "llvm/Analysis/AliasSetTracker.h"
5
#include "llvm/Analysis/AssumptionCache.h"
6
#include "llvm/Analysis/BasicAliasAnalysis.h"
7
#include "llvm/Analysis/BlockFrequencyInfo.h"
8
#include "llvm/Analysis/BranchProbabilityInfo.h"
9
#include "llvm/Analysis/CFGPrinter.h"
10
#include "llvm/Analysis/CGSCCPassManager.h"
11
#include "llvm/Analysis/CallGraph.h"
12
#include "llvm/Analysis/CallPrinter.h"
13
#include "llvm/Analysis/CostModel.h"
14
#include "llvm/Analysis/CycleAnalysis.h"
15
#include "llvm/Analysis/DDG.h"
16
#include "llvm/Analysis/DDGPrinter.h"
17
#include "llvm/Analysis/Delinearization.h"
18
#include "llvm/Analysis/DemandedBits.h"
19
#include "llvm/Analysis/DependenceAnalysis.h"
20
#include "llvm/Analysis/DomPrinter.h"
21
#include "llvm/Analysis/DominanceFrontier.h"
22
#include "llvm/Analysis/FunctionPropertiesAnalysis.h"
23
#include "llvm/Analysis/GlobalsModRef.h"
24
#include "llvm/Analysis/IRSimilarityIdentifier.h"
25
#include "llvm/Analysis/IVUsers.h"
26
#include "llvm/Analysis/InlineAdvisor.h"
27
#include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
28
#include "llvm/Analysis/InstCount.h"
29
#include "llvm/Analysis/LazyCallGraph.h"
30
#include "llvm/Analysis/LazyValueInfo.h"
31
#include "llvm/Analysis/Lint.h"
32
#include "llvm/Analysis/LoopAccessAnalysis.h"
33
#include "llvm/Analysis/LoopCacheAnalysis.h"
34
#include "llvm/Analysis/LoopInfo.h"
35
#include "llvm/Analysis/LoopNestAnalysis.h"
36
#include "llvm/Analysis/MemDerefPrinter.h"
37
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
38
#include "llvm/Analysis/MemorySSA.h"
39
#include "llvm/Analysis/ModuleDebugInfoPrinter.h"
40
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
41
#include "llvm/Analysis/MustExecute.h"
42
#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
43
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
44
#include "llvm/Analysis/PhiValues.h"
45
#include "llvm/Analysis/PostDominators.h"
46
#include "llvm/Analysis/ProfileSummaryInfo.h"
47
#include "llvm/Analysis/RegionInfo.h"
48
#include "llvm/Analysis/ScalarEvolution.h"
49
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
50
#include "llvm/Analysis/ScopedNoAliasAA.h"
51
#include "llvm/Analysis/StackLifetime.h"
52
#include "llvm/Analysis/StackSafetyAnalysis.h"
53
#include "llvm/Analysis/TargetLibraryInfo.h"
54
#include "llvm/Analysis/TargetTransformInfo.h"
55
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
56
#include "llvm/IR/Dominators.h"
57
#include "llvm/IR/IRPrintingPasses.h"
58
#include "llvm/IR/PassManager.h"
59
#include "llvm/IR/Verifier.h"
60
#include "llvm/Passes/PassBuilder.h"
61
#include "llvm/Passes/StandardInstrumentations.h"
62
#include "llvm/Support/raw_ostream.h"
63
#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
64
#include "llvm/Transforms/Coroutines/CoroCleanup.h"
65
#include "llvm/Transforms/Coroutines/CoroEarly.h"
66
#include "llvm/Transforms/Coroutines/CoroElide.h"
67
#include "llvm/Transforms/Coroutines/CoroSplit.h"
68
#include "llvm/Transforms/IPO/AlwaysInliner.h"
69
#include "llvm/Transforms/IPO/Annotation2Metadata.h"
70
#include "llvm/Transforms/IPO/ArgumentPromotion.h"
71
#include "llvm/Transforms/IPO/Attributor.h"
72
#include "llvm/Transforms/IPO/BlockExtractor.h"
73
#include "llvm/Transforms/IPO/CalledValuePropagation.h"
74
#include "llvm/Transforms/IPO/ConstantMerge.h"
75
#include "llvm/Transforms/IPO/CrossDSOCFI.h"
76
#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
77
#include "llvm/Transforms/IPO/ElimAvailExtern.h"
78
#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
79
#include "llvm/Transforms/IPO/FunctionAttrs.h"
80
#include "llvm/Transforms/IPO/FunctionImport.h"
81
#include "llvm/Transforms/IPO/GlobalDCE.h"
82
#include "llvm/Transforms/IPO/GlobalOpt.h"
83
#include "llvm/Transforms/IPO/GlobalSplit.h"
84
#include "llvm/Transforms/IPO/HotColdSplitting.h"
85
#include "llvm/Transforms/IPO/IROutliner.h"
86
#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
87
#include "llvm/Transforms/IPO/Inliner.h"
88
#include "llvm/Transforms/IPO/Internalize.h"
89
#include "llvm/Transforms/IPO/LoopExtractor.h"
90
#include "llvm/Transforms/IPO/LowerTypeTests.h"
91
#include "llvm/Transforms/IPO/MergeFunctions.h"
92
#include "llvm/Transforms/IPO/ModuleInliner.h"
93
#include "llvm/Transforms/IPO/OpenMPOpt.h"
94
#include "llvm/Transforms/IPO/PartialInlining.h"
95
#include "llvm/Transforms/IPO/SCCP.h"
96
#include "llvm/Transforms/IPO/SampleProfile.h"
97
#include "llvm/Transforms/IPO/SampleProfileProbe.h"
98
#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
99
#include "llvm/Transforms/IPO/StripSymbols.h"
100
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
101
#include "llvm/Transforms/InstCombine/InstCombine.h"
102
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
103
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
104
#include "llvm/Transforms/Instrumentation/CGProfile.h"
105
#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
106
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
107
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
108
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
109
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
110
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
111
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
112
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
113
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
114
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
115
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
116
#include "llvm/Transforms/ObjCARC.h"
117
#include "llvm/Transforms/Scalar/ADCE.h"
118
#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
119
#include "llvm/Transforms/Scalar/AnnotationRemarks.h"
120
#include "llvm/Transforms/Scalar/BDCE.h"
121
#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
122
#include "llvm/Transforms/Scalar/ConstantHoisting.h"
123
#include "llvm/Transforms/Scalar/ConstraintElimination.h"
124
#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
125
#include "llvm/Transforms/Scalar/DCE.h"
126
#include "llvm/Transforms/Scalar/DFAJumpThreading.h"
127
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
128
#include "llvm/Transforms/Scalar/DivRemPairs.h"
129
#include "llvm/Transforms/Scalar/EarlyCSE.h"
130
#include "llvm/Transforms/Scalar/FlattenCFG.h"
131
#include "llvm/Transforms/Scalar/Float2Int.h"
132
#include "llvm/Transforms/Scalar/GVN.h"
133
#include "llvm/Transforms/Scalar/GuardWidening.h"
134
#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
135
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
136
#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
137
#include "llvm/Transforms/Scalar/InferAddressSpaces.h"
138
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
139
#include "llvm/Transforms/Scalar/JumpThreading.h"
140
#include "llvm/Transforms/Scalar/LICM.h"
141
#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
142
#include "llvm/Transforms/Scalar/LoopBoundSplit.h"
143
#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
144
#include "llvm/Transforms/Scalar/LoopDeletion.h"
145
#include "llvm/Transforms/Scalar/LoopDistribute.h"
146
#include "llvm/Transforms/Scalar/LoopFlatten.h"
147
#include "llvm/Transforms/Scalar/LoopFuse.h"
148
#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
149
#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
150
#include "llvm/Transforms/Scalar/LoopInterchange.h"
151
#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
152
#include "llvm/Transforms/Scalar/LoopPassManager.h"
153
#include "llvm/Transforms/Scalar/LoopPredication.h"
154
#include "llvm/Transforms/Scalar/LoopRotation.h"
155
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
156
#include "llvm/Transforms/Scalar/LoopSink.h"
157
#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
158
#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
159
#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
160
#include "llvm/Transforms/Scalar/LoopVersioningLICM.h"
161
#include "llvm/Transforms/Scalar/LowerAtomicPass.h"
162
#include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
163
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
164
#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
165
#include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h"
166
#include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
167
#include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
168
#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
169
#include "llvm/Transforms/Scalar/MergeICmps.h"
170
#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
171
#include "llvm/Transforms/Scalar/NaryReassociate.h"
172
#include "llvm/Transforms/Scalar/NewGVN.h"
173
#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
174
#include "llvm/Transforms/Scalar/Reassociate.h"
175
#include "llvm/Transforms/Scalar/Reg2Mem.h"
176
#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
177
#include "llvm/Transforms/Scalar/SCCP.h"
178
#include "llvm/Transforms/Scalar/SROA.h"
179
#include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
180
#include "llvm/Transforms/Scalar/Scalarizer.h"
181
#include "llvm/Transforms/Scalar/SeparateConstOffsetFromGEP.h"
182
#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
183
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
184
#include "llvm/Transforms/Scalar/Sink.h"
185
#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
186
#include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h"
187
#include "llvm/Transforms/Scalar/StructurizeCFG.h"
188
#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
189
#include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
190
#include "llvm/Transforms/Utils/AddDiscriminators.h"
191
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
192
#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
193
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
194
#include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"
195
#include "llvm/Transforms/Utils/Debugify.h"
196
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
197
#include "llvm/Transforms/Utils/FixIrreducible.h"
198
#include "llvm/Transforms/Utils/HelloWorld.h"
199
#include "llvm/Transforms/Utils/InjectTLIMappings.h"
200
#include "llvm/Transforms/Utils/InstructionNamer.h"
201
#include "llvm/Transforms/Utils/LCSSA.h"
202
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
203
#include "llvm/Transforms/Utils/LoopSimplify.h"
204
#include "llvm/Transforms/Utils/LoopVersioning.h"
205
#include "llvm/Transforms/Utils/LowerGlobalDtors.h"
206
#include "llvm/Transforms/Utils/LowerInvoke.h"
207
#include "llvm/Transforms/Utils/LowerSwitch.h"
208
#include "llvm/Transforms/Utils/Mem2Reg.h"
209
#include "llvm/Transforms/Utils/MetaRenamer.h"
210
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
211
#include "llvm/Transforms/Utils/PredicateInfo.h"
212
#include "llvm/Transforms/Utils/RelLookupTableConverter.h"
213
#include "llvm/Transforms/Utils/StripGCRelocates.h"
214
#include "llvm/Transforms/Utils/StripNonLineTableDebugInfo.h"
215
#include "llvm/Transforms/Utils/SymbolRewriter.h"
216
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
217
#include "llvm/Transforms/Utils/UnifyLoopExits.h"
218
#include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h"
219
#include "llvm/Transforms/Vectorize/LoopVectorize.h"
220
#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
221
#include "llvm/Transforms/Vectorize/VectorCombine.h"
222
#include <llvm/IR/PassTimingInfo.h>
223
224
using namespace llvm;
225
226
#define STRINGIFY(x) #x
227
#define TOSTRING(x) STRINGIFY(x)
228
229
/*
230
* Exposed API
231
*/
232
233
namespace llvm {
234
235
struct OpaqueModulePassManager;
236
typedef OpaqueModulePassManager *LLVMModulePassManagerRef;
237
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModulePassManager, LLVMModulePassManagerRef)
238
239
struct OpaqueFunctionPassManager;
240
typedef OpaqueFunctionPassManager *LLVMFunctionPassManagerRef;
241
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(FunctionPassManager,
242
LLVMFunctionPassManagerRef)
243
244
struct OpaquePassBuilder;
245
typedef OpaquePassBuilder *LLVMPassBuilderRef;
246
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PassBuilder, LLVMPassBuilderRef)
247
248
struct OpaquePipelineTuningOptions;
249
typedef OpaquePipelineTuningOptions *LLVMPipelineTuningOptionsRef;
250
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PipelineTuningOptions,
251
LLVMPipelineTuningOptionsRef)
252
253
struct OpaqueTimePassesHandler;
254
typedef OpaqueTimePassesHandler *LLVMTimePassesHandlerRef;
255
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TimePassesHandler, LLVMTimePassesHandlerRef)
256
257
static TargetMachine *unwrap(LLVMTargetMachineRef P) {
258
return reinterpret_cast<TargetMachine *>(P);
259
}
260
261
} // namespace llvm
262
263
// C++ linkage
264
static OptimizationLevel mapLevel(int speed_level, int size_level) {
265
switch (size_level) {
266
case 0:
267
switch (speed_level) {
268
case 0:
269
return OptimizationLevel::O0;
270
case 1:
271
return OptimizationLevel::O1;
272
case 2:
273
return OptimizationLevel::O2;
274
case 3:
275
return OptimizationLevel::O3;
276
default:
277
llvm_unreachable("Invalid optimization level");
278
}
279
case 1:
280
if (speed_level == 2)
281
return OptimizationLevel::Os;
282
llvm_unreachable("Invalid optimization level for size level 1");
283
case 2:
284
if (speed_level == 2)
285
return OptimizationLevel::Oz;
286
llvm_unreachable("Invalid optimization level for size level 2");
287
default:
288
llvm_unreachable("Invalid size level");
289
break;
290
}
291
}
292
293
extern "C" {
294
295
API_EXPORT(void)
296
LLVMPY_SetTimePasses(bool enable) { TimePassesIsEnabled = enable; }
297
298
API_EXPORT(void)
299
LLVMPY_ReportAndResetTimings(const char **outmsg) {
300
std::string osbuf;
301
raw_string_ostream os(osbuf);
302
reportAndResetTimings(&os);
303
os.flush();
304
*outmsg = LLVMPY_CreateString(os.str().c_str());
305
}
306
307
// MPM
308
309
API_EXPORT(LLVMModulePassManagerRef)
310
LLVMPY_CreateNewModulePassManager() {
311
return llvm::wrap(new PassManager<Module>());
312
}
313
314
API_EXPORT(void)
315
LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef,
316
LLVMModuleRef mod, LLVMPassBuilderRef PBRef) {
317
318
ModulePassManager *MPM = llvm::unwrap(MPMRef);
319
Module *M = llvm::unwrap(mod);
320
PassBuilder *PB = llvm::unwrap(PBRef);
321
322
// TODO: Make these set(able) by user
323
bool DebugLogging = false;
324
bool VerifyEach = false;
325
326
LoopAnalysisManager LAM;
327
FunctionAnalysisManager FAM;
328
CGSCCAnalysisManager CGAM;
329
ModuleAnalysisManager MAM;
330
331
PrintPassOptions PrintPassOpts;
332
333
// NOTE: The following are used to provide an alternative outstream to
334
// STDOUT and need to be declared ahead of instantiating the
335
// StandardInstrumentations instance as they need to have a lifetime that
336
// is longer than the StandardInstrumentations. See following notes for
337
// details:
338
//
339
// The reason for this is that a StandardInstrumentations instance (SI)
340
// contains a TimePassesHandler instance (TP), when the SI goes out of scope
341
// it triggers the TP member destructor, and this is defined so as to call
342
// `TP->print()` to trigger the side effect of draining the pass timing
343
// buffer and resetting the timers. This `print()` call will by default
344
// drain to stdout and so a buffer is provided in the following and set as
345
// the "out stream" for the TP so that any such printing isn't visible to
346
// the user. Independently of all this, if the user wants timing
347
// information, there is a managed TP instance along with code managing the
348
// state of information capturing available as part of the LLVMPY interface.
349
// This is independently registered and managed outside of the
350
// StandardInstrumentations system.
351
//
352
// Summary: the StandardInstrumentations TimePassesHandler instance isn't
353
// used by anything available to llvmlite users, and so its printing stuff
354
// is just being hidden.
355
std::string osbuf;
356
raw_string_ostream os(osbuf);
357
358
StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach,
359
PrintPassOpts);
360
361
// https://reviews.llvm.org/D146160
362
SI.registerCallbacks(*PB->getPassInstrumentationCallbacks(), &MAM);
363
364
// If the timing information is required, this is handled elsewhere, the
365
// instance of the TimePassesHandler on the StandardInstrumentations object
366
// needs to just redirect its print output to somewhere not visible to
367
// users.
368
if (TimePassesIsEnabled) {
369
TimePassesHandler &TP = SI.getTimePasses();
370
TP.setOutStream(os);
371
}
372
373
PB->registerLoopAnalyses(LAM);
374
PB->registerFunctionAnalyses(FAM);
375
PB->registerCGSCCAnalyses(CGAM);
376
PB->registerModuleAnalyses(MAM);
377
PB->crossRegisterProxies(LAM, FAM, CGAM, MAM);
378
379
MPM->run(*M, MAM);
380
}
381
382
API_EXPORT(void)
383
LLVMPY_AddJumpThreadingPass_module(LLVMModulePassManagerRef MPM, int T) {
384
llvm::unwrap(MPM)->addPass(
385
createModuleToFunctionPassAdaptor(JumpThreadingPass(T)));
386
}
387
388
API_EXPORT(void)
389
LLVMPY_DisposeNewModulePassManger(LLVMModulePassManagerRef MPM) {
390
delete llvm::unwrap(MPM);
391
}
392
393
// FPM
394
395
API_EXPORT(LLVMFunctionPassManagerRef)
396
LLVMPY_CreateNewFunctionPassManager() {
397
return llvm::wrap(new PassManager<Function>());
398
}
399
400
API_EXPORT(void)
401
LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef,
402
LLVMValueRef FRef, LLVMPassBuilderRef PBRef) {
403
404
FunctionPassManager *FPM = llvm::unwrap(FPMRef);
405
Function *F = reinterpret_cast<Function *>(FRef);
406
PassBuilder *PB = llvm::unwrap(PBRef);
407
408
// Don't try to optimize function declarations
409
if (F->isDeclaration())
410
return;
411
412
// TODO: Make these set(able) by user
413
bool DebugLogging = false;
414
bool VerifyEach = false;
415
416
LoopAnalysisManager LAM;
417
FunctionAnalysisManager FAM;
418
CGSCCAnalysisManager CGAM;
419
ModuleAnalysisManager MAM;
420
421
// TODO: Can expose this in ffi layer
422
PrintPassOptions PrintPassOpts;
423
424
// See note in LLVMPY_RunNewModulePassManager for what is going on with
425
// these variables and the call below to TP.setOutStream().
426
std::string osbuf;
427
raw_string_ostream os(osbuf);
428
429
StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach,
430
PrintPassOpts);
431
432
// https://reviews.llvm.org/D146160
433
SI.registerCallbacks(*PB->getPassInstrumentationCallbacks(), &MAM);
434
435
if (TimePassesIsEnabled) {
436
TimePassesHandler &TP = SI.getTimePasses();
437
TP.setOutStream(os);
438
}
439
440
PB->registerLoopAnalyses(LAM);
441
PB->registerFunctionAnalyses(FAM);
442
PB->registerCGSCCAnalyses(CGAM);
443
PB->registerModuleAnalyses(MAM);
444
PB->crossRegisterProxies(LAM, FAM, CGAM, MAM);
445
FPM->run(*F, FAM);
446
}
447
448
API_EXPORT(void)
449
LLVMPY_AddJumpThreadingPass_function(LLVMFunctionPassManagerRef FPM, int T) {
450
llvm::unwrap(FPM)->addPass(JumpThreadingPass(T));
451
}
452
453
API_EXPORT(void)
454
LLVMPY_DisposeNewFunctionPassManger(LLVMFunctionPassManagerRef FPM) {
455
delete llvm::unwrap(FPM);
456
}
457
458
// PTO
459
460
API_EXPORT(LLVMPipelineTuningOptionsRef)
461
LLVMPY_CreatePipelineTuningOptions() {
462
return llvm::wrap(new PipelineTuningOptions());
463
}
464
465
API_EXPORT(bool)
466
LLVMPY_PTOGetLoopInterleaving(LLVMPipelineTuningOptionsRef PTO) {
467
return llvm::unwrap(PTO)->LoopInterleaving;
468
}
469
470
API_EXPORT(void)
471
LLVMPY_PTOSetLoopInterleaving(LLVMPipelineTuningOptionsRef PTO, bool value) {
472
llvm::unwrap(PTO)->LoopInterleaving = value;
473
}
474
475
API_EXPORT(bool)
476
LLVMPY_PTOGetLoopVectorization(LLVMPipelineTuningOptionsRef PTO) {
477
return llvm::unwrap(PTO)->LoopVectorization;
478
}
479
480
API_EXPORT(void)
481
LLVMPY_PTOSetLoopVectorization(LLVMPipelineTuningOptionsRef PTO, bool value) {
482
llvm::unwrap(PTO)->LoopVectorization = value;
483
}
484
485
API_EXPORT(bool)
486
LLVMPY_PTOGetSLPVectorization(LLVMPipelineTuningOptionsRef PTO) {
487
return llvm::unwrap(PTO)->SLPVectorization;
488
}
489
490
API_EXPORT(void)
491
LLVMPY_PTOSetSLPVectorization(LLVMPipelineTuningOptionsRef PTO, bool value) {
492
llvm::unwrap(PTO)->SLPVectorization = value;
493
}
494
495
API_EXPORT(bool)
496
LLVMPY_PTOGetLoopUnrolling(LLVMPipelineTuningOptionsRef PTO) {
497
return llvm::unwrap(PTO)->LoopUnrolling;
498
}
499
500
API_EXPORT(void)
501
LLVMPY_PTOSetLoopUnrolling(LLVMPipelineTuningOptionsRef PTO, bool value) {
502
llvm::unwrap(PTO)->LoopUnrolling = value;
503
}
504
505
API_EXPORT(int)
506
LLVMPY_PTOGetInlinerThreshold(LLVMPipelineTuningOptionsRef PTO) {
507
return llvm::unwrap(PTO)->InlinerThreshold;
508
}
509
510
API_EXPORT(void)
511
LLVMPY_PTOSetInlinerThreshold(LLVMPipelineTuningOptionsRef PTO, bool value) {
512
llvm::unwrap(PTO)->InlinerThreshold = value;
513
}
514
515
API_EXPORT(void)
516
LLVMPY_DisposePipelineTuningOptions(LLVMPipelineTuningOptionsRef PTO) {
517
delete llvm::unwrap(PTO);
518
}
519
520
// PB
521
522
API_EXPORT(LLVMTimePassesHandlerRef)
523
LLVMPY_CreateTimePassesHandler() {
524
bool enabled = true;
525
return llvm::wrap(new TimePassesHandler(enabled));
526
}
527
528
API_EXPORT(void)
529
LLVMPY_DisposeTimePassesHandler(LLVMTimePassesHandlerRef TimePassesRef) {
530
delete llvm::unwrap(TimePassesRef);
531
}
532
533
API_EXPORT(void)
534
LLVMPY_EnableTimePasses(LLVMPassBuilderRef PBRef,
535
LLVMTimePassesHandlerRef TimePassesRef) {
536
TimePassesHandler *TP = llvm::unwrap(TimePassesRef);
537
TimePassesIsEnabled = true;
538
PassBuilder *PB = llvm::unwrap(PBRef);
539
PassInstrumentationCallbacks *PIC = PB->getPassInstrumentationCallbacks();
540
TP->registerCallbacks(*PIC);
541
}
542
543
API_EXPORT(void)
544
LLVMPY_ReportAndDisableTimePasses(LLVMTimePassesHandlerRef TimePassesRef,
545
const char **outmsg) {
546
std::string osbuf;
547
raw_string_ostream os(osbuf);
548
TimePassesHandler *TP = llvm::unwrap(TimePassesRef);
549
TP->setOutStream(os);
550
TP->print();
551
os.flush();
552
*outmsg = LLVMPY_CreateString(os.str().c_str());
553
TimePassesIsEnabled = false;
554
}
555
556
API_EXPORT(LLVMPassBuilderRef)
557
LLVMPY_CreatePassBuilder(LLVMTargetMachineRef TMRef,
558
LLVMPipelineTuningOptionsRef PTORef) {
559
TargetMachine *TM = llvm::unwrap(TMRef);
560
PipelineTuningOptions *PTO = llvm::unwrap(PTORef);
561
PassInstrumentationCallbacks *PIC = new PassInstrumentationCallbacks();
562
return llvm::wrap(new PassBuilder(TM, *PTO, std::nullopt, PIC));
563
}
564
565
API_EXPORT(void)
566
LLVMPY_DisposePassBuilder(LLVMPassBuilderRef PBRef) {
567
delete llvm::unwrap(PBRef);
568
}
569
570
API_EXPORT(LLVMModulePassManagerRef)
571
LLVMPY_buildPerModuleDefaultPipeline(LLVMPassBuilderRef PBref, int speed_level,
572
int size_level) {
573
574
PassBuilder *PB = llvm::unwrap(PBref);
575
OptimizationLevel OL = mapLevel(speed_level, size_level);
576
577
// FIXME: No need to explicitly take care of O0 from LLVM 17
578
if (OL == OptimizationLevel::O0) {
579
return llvm::wrap(
580
new ModulePassManager(PB->buildO0DefaultPipeline(OL)));
581
}
582
583
return llvm::wrap(
584
new ModulePassManager(PB->buildPerModuleDefaultPipeline(OL)));
585
}
586
587
API_EXPORT(LLVMFunctionPassManagerRef)
588
LLVMPY_buildFunctionSimplificationPipeline(LLVMPassBuilderRef PBref,
589
int speed_level, int size_level) {
590
591
PassBuilder *PB = llvm::unwrap(PBref);
592
OptimizationLevel OL = mapLevel(speed_level, size_level);
593
if (OL == OptimizationLevel::O0)
594
return llvm::wrap(new FunctionPassManager());
595
596
FunctionPassManager *FPM = new FunctionPassManager(
597
PB->buildFunctionSimplificationPipeline(OL, ThinOrFullLTOPhase::None));
598
return llvm::wrap(FPM);
599
}
600
601
// TODO: From LLVM 16 SROA takes parameter whether to preserve cfg or not which
602
// can be exposed in the Python API https://reviews.llvm.org/D138238
603
API_EXPORT(void)
604
LLVMPY_module_AddSROAPass(LLVMModulePassManagerRef MPM) {
605
llvm::unwrap(MPM)->addPass(
606
createModuleToFunctionPassAdaptor(SROAPass(SROAOptions::PreserveCFG)));
607
}
608
609
API_EXPORT(void)
610
LLVMPY_function_AddSROAPass(LLVMFunctionPassManagerRef FPM) {
611
llvm::unwrap(FPM)->addPass(SROAPass(SROAOptions::PreserveCFG));
612
}
613
614
API_EXPORT(void)
615
LLVMPY_module_AddModuleDebugInfoPrinterPass(LLVMModulePassManagerRef MPM) {
616
llvm::unwrap(MPM)->addPass(ModuleDebugInfoPrinterPass(llvm::outs()));
617
}
618
619
#define CGSCC_PASS(NAME) \
620
API_EXPORT(void) LLVMPY_module_Add##NAME(LLVMModulePassManagerRef MPM) { \
621
llvm::unwrap(MPM)->addPass( \
622
createModuleToPostOrderCGSCCPassAdaptor(NAME())); \
623
}
624
#include "PASSREGISTRY.def"
625
626
#define MODULE_PASS(NAME) \
627
API_EXPORT(void) LLVMPY_module_Add##NAME(LLVMModulePassManagerRef MPM) { \
628
llvm::unwrap(MPM)->addPass(NAME()); \
629
}
630
#include "PASSREGISTRY.def"
631
632
#define FUNCTION_PASS(NAME) \
633
API_EXPORT(void) LLVMPY_module_Add##NAME(LLVMModulePassManagerRef MPM) { \
634
llvm::unwrap(MPM)->addPass(createModuleToFunctionPassAdaptor(NAME())); \
635
} \
636
API_EXPORT(void) \
637
LLVMPY_function_Add##NAME(LLVMFunctionPassManagerRef FPM) { \
638
llvm::unwrap(FPM)->addPass(NAME()); \
639
}
640
#include "PASSREGISTRY.def"
641
642
#define LOOP_PASS(NAME) \
643
API_EXPORT(void) LLVMPY_module_Add##NAME(LLVMModulePassManagerRef MPM) { \
644
llvm::unwrap(MPM)->addPass(createModuleToFunctionPassAdaptor( \
645
createFunctionToLoopPassAdaptor(NAME()))); \
646
} \
647
API_EXPORT(void) \
648
LLVMPY_function_Add##NAME(LLVMFunctionPassManagerRef FPM) { \
649
llvm::unwrap(FPM)->addPass(createFunctionToLoopPassAdaptor(NAME())); \
650
}
651
#include "PASSREGISTRY.def"
652
653
} // end extern "C"
654
655