From 507db4c2de99edb1712d71cb5f10c22ab3c5da73 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Tue, 9 Jul 2024 11:57:49 +0530 Subject: [PATCH 1/8] Add instrumentation callbacks for pass managers The new pass manager relies on PIC callbacks for deciding if a function needs to be skipped for optimization when compiled with 'optnone'. --- ffi/newpassmanagers.cpp | 64 ++++++++++++++----- llvmlite/binding/newpassmanagers.py | 15 +++-- llvmlite/tests/test_binding.py | 97 ++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 24 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index 259e65002..f6ba40fbf 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -1,9 +1,11 @@ #include "core.h" #include "llvm-c/TargetMachine.h" #include "llvm/Analysis/AliasAnalysisEvaluator.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/Scalar/JumpThreading.h" #include "llvm/Transforms/Scalar/LoopRotation.h" @@ -53,21 +55,36 @@ LLVMPY_CreateNewModulePassManager() { API_EXPORT(void) LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, - LLVMPassBuilderRef PBRef, LLVMModuleRef mod) { + LLVMModuleRef mod, + LLVMPipelineTuningOptionsRef PTORef, + LLVMTargetMachineRef TMRef) { ModulePassManager *MPM = llvm::unwrap(MPMRef); - PassBuilder *PB = llvm::unwrap(PBRef); Module *M = llvm::unwrap(mod); + PipelineTuningOptions *PTO = llvm::unwrap(PTORef); + TargetMachine *TM = llvm::unwrap(TMRef); + + // TODO: Make these set(able) by user + bool DebugLogging = false; + bool VerifyEach = false; + + PrintPassOptions PrintPassOpts; + StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); + PassInstrumentationCallbacks PIC; LoopAnalysisManager LAM; FunctionAnalysisManager FAM; CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; - PB->registerLoopAnalyses(LAM); - PB->registerFunctionAnalyses(FAM); - PB->registerCGSCCAnalyses(CGAM); - PB->registerModuleAnalyses(MAM); - PB->crossRegisterProxies(LAM, FAM, CGAM, MAM); + + SI.registerCallbacks(PIC, &FAM); + PassBuilder PB(TM, *PTO, None, &PIC); + + PB.registerLoopAnalyses(LAM); + PB.registerFunctionAnalyses(FAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerModuleAnalyses(MAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); MPM->run(*M, MAM); } @@ -126,11 +143,26 @@ LLVMPY_CreateNewFunctionPassManager() { API_EXPORT(void) LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, - LLVMPassBuilderRef PBRef, LLVMValueRef FRef) { + LLVMValueRef FRef, + LLVMPipelineTuningOptionsRef PTORef, + LLVMTargetMachineRef TMRef) { FunctionPassManager *FPM = llvm::unwrap(FPMRef); - PassBuilder *PB = llvm::unwrap(PBRef); Function *F = reinterpret_cast(FRef); + PipelineTuningOptions *PTO = llvm::unwrap(PTORef); + TargetMachine *TM = llvm::unwrap(TMRef); + + // Don't try to optimize function declarations + if (F->isDeclaration()) + return; + + // TODO: Make these set(able) by user + bool DebugLogging = false; + bool VerifyEach = false; + + PrintPassOptions PrintPassOpts; + StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); + PassInstrumentationCallbacks PIC; // Don't try to optimize function declarations if (F->isDeclaration()) @@ -140,11 +172,15 @@ LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, FunctionAnalysisManager FAM; CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; - PB->registerLoopAnalyses(LAM); - PB->registerFunctionAnalyses(FAM); - PB->registerCGSCCAnalyses(CGAM); - PB->registerModuleAnalyses(MAM); - PB->crossRegisterProxies(LAM, FAM, CGAM, MAM); + + SI.registerCallbacks(PIC, &FAM); + PassBuilder PB(TM, *PTO, None, &PIC); + + PB.registerLoopAnalyses(LAM); + PB.registerFunctionAnalyses(FAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerModuleAnalyses(MAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); FPM->run(*F, FAM); } diff --git a/llvmlite/binding/newpassmanagers.py b/llvmlite/binding/newpassmanagers.py index f1dd20cc7..e4baa54f7 100644 --- a/llvmlite/binding/newpassmanagers.py +++ b/llvmlite/binding/newpassmanagers.py @@ -26,7 +26,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, module, pb): - ffi.lib.LLVMPY_RunNewModulePassManager(self, pb, module) + ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb._pto, pb._tm) def add_verifier(self): ffi.lib.LLVMPY_AddVerifierPass(self) @@ -61,7 +61,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, fun, pb): - ffi.lib.LLVMPY_RunNewFunctionPassManager(self, pb, fun) + ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb._pto, pb._tm) def add_aa_eval_pass(self): ffi.lib.LLVMPY_AddAAEvalPass_function(self) @@ -193,9 +193,9 @@ def _dispose(self): ffi.lib.LLVMPY_CreateNewModulePassManager.restype = ffi.LLVMModulePassManagerRef -ffi.lib.LLVMPY_RunNewModulePassManager.argtypes = [ffi.LLVMModulePassManagerRef, - ffi.LLVMPassBuilderRef, - ffi.LLVMModuleRef,] +ffi.lib.LLVMPY_RunNewModulePassManager.argtypes = [ + ffi.LLVMModulePassManagerRef, ffi.LLVMModuleRef, + ffi.LLVMPipelineTuningOptionsRef, ffi.LLVMTargetMachineRef, ] ffi.lib.LLVMPY_AddVerifierPass.argtypes = [ffi.LLVMModulePassManagerRef,] ffi.lib.LLVMPY_AddAAEvalPass_module.argtypes = [ffi.LLVMModulePassManagerRef,] @@ -223,9 +223,8 @@ def _dispose(self): ffi.LLVMFunctionPassManagerRef ffi.lib.LLVMPY_RunNewFunctionPassManager.argtypes = [ - ffi.LLVMFunctionPassManagerRef, - ffi.LLVMPassBuilderRef, - ffi.LLVMValueRef,] + ffi.LLVMFunctionPassManagerRef, ffi.LLVMValueRef, + ffi.LLVMPipelineTuningOptionsRef, ffi.LLVMTargetMachineRef, ] ffi.lib.LLVMPY_AddAAEvalPass_function.argtypes = [ ffi.LLVMFunctionPassManagerRef,] diff --git a/llvmlite/tests/test_binding.py b/llvmlite/tests/test_binding.py index e2378d36e..1922eaa70 100644 --- a/llvmlite/tests/test_binding.py +++ b/llvmlite/tests/test_binding.py @@ -452,6 +452,40 @@ def no_de_locale(): declare i8* @a_arg0_return_func(i8* returned, i32*) """ +asm_alloca_optnone = r""" +define double @foo(i32 %i, double %j) optnone noinline { + %I = alloca i32 ; [#uses=4] + %J = alloca double ; [#uses=2] + store i32 %i, i32* %I + store double %j, double* %J + %t1 = load i32, i32* %I ; [#uses=1] + %t2 = add i32 %t1, 1 ; [#uses=1] + store i32 %t2, i32* %I + %t3 = load i32, i32* %I ; [#uses=1] + %t4 = sitofp i32 %t3 to double ; [#uses=1] + %t5 = load double, double* %J ; [#uses=1] + %t6 = fmul double %t4, %t5 ; [#uses=1] + ret double %t6 +} +""" + +asm_alloca_no_optnone = r""" +define double @foo(i32 %i, double %j) noinline { + %I = alloca i32 ; [#uses=4] + %J = alloca double ; [#uses=2] + store i32 %i, i32* %I + store double %j, double* %J + %t1 = load i32, i32* %I ; [#uses=1] + %t2 = add i32 %t1, 1 ; [#uses=1] + store i32 %t2, i32* %I + %t3 = load i32, i32* %I ; [#uses=1] + %t4 = sitofp i32 %t3 to double ; [#uses=1] + %t5 = load double, double* %J ; [#uses=1] + %t6 = fmul double %t4, %t5 ; [#uses=1] + ret double %t6 +} +""" + asm_declaration = r""" declare void @test_declare(i32* ) """ @@ -3027,7 +3061,7 @@ def test_close(self): mpm = self.pm() mpm.close() - def test_run(self): + def test_run_O3(self): pb = self.pb(speed_level=3, size_level=0) mod = self.module() orig_asm = str(mod) @@ -3037,6 +3071,16 @@ def test_run(self): self.assertIn("%.4", orig_asm) self.assertNotIn("%.4", optimized_asm) + def test_run_O0(self): + pb = self.pb(speed_level=0, size_level=0) + mod = self.module() + orig_asm = str(mod) + mpm = pb.getModulePassManager() + mpm.run(mod, pb) + optimized_asm = str(mod) + self.assertIn("%.4", orig_asm) + self.assertIn("%.4", optimized_asm) + def test_instcombine(self): pb = self.pb() mpm = self.pm() @@ -3048,6 +3092,25 @@ def test_instcombine(self): self.assertIn("%.3", orig_asm) self.assertNotIn("%.3", optimized_asm) + def test_optnone(self): + # Module shouldn't be optimized if the function as `optnone` attached + pb = self.pb(speed_level=3, size_level=0) + orig_asm_no_optnone = str(asm_alloca_no_optnone) + mod = llvm.parse_assembly(orig_asm_no_optnone) + mpm = pb.getModulePassManager() + mpm.run(mod, pb) + optimized_asm_no_optnone = str(mod) + self.assertIn("alloca", orig_asm_no_optnone) + self.assertNotIn("alloca", optimized_asm_no_optnone) + + orig_asm_optnone = str(asm_alloca_optnone) + mpm = pb.getModulePassManager() + mod = llvm.parse_assembly(orig_asm_optnone) + mpm.run(mod, pb) + optimized_asm_optnone = str(mod) + self.assertIn("alloca", orig_asm_optnone) + self.assertIn("alloca", optimized_asm_optnone) + def test_add_passes(self): mpm = self.pm() mpm.add_verifier() @@ -3067,7 +3130,7 @@ def test_close(self): fpm = self.pm() fpm.close() - def test_run(self): + def test_run_O3(self): pb = self.pb(3) mod = self.module() fun = mod.get_function("sum") @@ -3078,6 +3141,36 @@ def test_run(self): self.assertIn("%.4", orig_asm) self.assertNotIn("%.4", optimized_asm) + def test_run_O0(self): + pb = self.pb(0) + mod = self.module() + fun = mod.get_function("sum") + orig_asm = str(fun) + fpm = pb.getFunctionPassManager() + fpm.run(fun, pb) + optimized_asm = str(fun) + self.assertIn("%.4", orig_asm) + self.assertIn("%.4", optimized_asm) + + def test_optnone(self): + # Function shouldn't be optimized if the function as `optnone` attached + pb = self.pb(speed_level=3, size_level=0) + orig_asm_no_optnone = str(asm_alloca_no_optnone) + fun = llvm.parse_assembly(orig_asm_no_optnone).get_function("foo") + fpm = pb.getFunctionPassManager() + fpm.run(fun, pb) + optimized_asm_no_optnone = str(fun) + self.assertIn("alloca", orig_asm_no_optnone) + self.assertNotIn("alloca", optimized_asm_no_optnone) + + orig_asm_optnone = str(asm_alloca_optnone) + fun = llvm.parse_assembly(orig_asm_optnone).get_function("foo") + fpm = pb.getFunctionPassManager() + fpm.run(fun, pb) + optimized_asm_optnone = str(fun) + self.assertIn("alloca", orig_asm_optnone) + self.assertIn("alloca", optimized_asm_optnone) + def test_instcombine(self): pb = self.pb() fpm = self.pm() From c34d53909ed22ef1e8d1bd42194100bed3b4b74d Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Thu, 18 Jul 2024 14:52:32 +0530 Subject: [PATCH 2/8] Add llvm16 supported syntax --- ffi/newpassmanagers.cpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index f6ba40fbf..13acdef6d 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -68,17 +68,23 @@ LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, bool DebugLogging = false; bool VerifyEach = false; - PrintPassOptions PrintPassOpts; - StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); - PassInstrumentationCallbacks PIC; - LoopAnalysisManager LAM; FunctionAnalysisManager FAM; CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; + PassInstrumentationCallbacks PIC; + PrintPassOptions PrintPassOpts; + +#if LLVM_VERSION_MAJOR < 16 + StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, None, &PIC); +# else + StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach, PrintPassOpts); + SI.registerCallbacks(PIC, &FAM); + PassBuilder PB(TM, *PTO, std::nullopt, &PIC); +# endif PB.registerLoopAnalyses(LAM); PB.registerFunctionAnalyses(FAM); @@ -160,21 +166,23 @@ LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, bool DebugLogging = false; bool VerifyEach = false; - PrintPassOptions PrintPassOpts; - StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); - PassInstrumentationCallbacks PIC; - - // Don't try to optimize function declarations - if (F->isDeclaration()) - return; - LoopAnalysisManager LAM; FunctionAnalysisManager FAM; CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; + PrintPassOptions PrintPassOpts; + PassInstrumentationCallbacks PIC; + +#if LLVM_VERSION_MAJOR < 16 + StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, None, &PIC); +# else + StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach, PrintPassOpts); + SI.registerCallbacks(PIC, &FAM); + PassBuilder PB(TM, *PTO, std::nullopt, &PIC); +#endif PB.registerLoopAnalyses(LAM); PB.registerFunctionAnalyses(FAM); From 5aaa1561a5de5a8d028a54da7ceae4a857a87ff9 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Thu, 18 Jul 2024 15:03:22 +0530 Subject: [PATCH 3/8] clang-format --- ffi/newpassmanagers.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index 13acdef6d..8199ce647 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -80,11 +80,12 @@ LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, None, &PIC); -# else - StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach, PrintPassOpts); +#else + StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach, + PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, std::nullopt, &PIC); -# endif +#endif PB.registerLoopAnalyses(LAM); PB.registerFunctionAnalyses(FAM); @@ -178,8 +179,9 @@ LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, None, &PIC); -# else - StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach, PrintPassOpts); +#else + StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach, + PrintPassOpts); SI.registerCallbacks(PIC, &FAM); PassBuilder PB(TM, *PTO, std::nullopt, &PIC); #endif From 58569a0dddea2cd6b4b046f74c54db96a727c96c Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Fri, 19 Jul 2024 14:37:29 +0530 Subject: [PATCH 4/8] Expose PIC in ffi layer --- ffi/newpassmanagers.cpp | 75 +++++++++++++++++------------ llvmlite/binding/ffi.py | 2 + llvmlite/binding/newpassmanagers.py | 27 +++++++++-- 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index 8199ce647..01b0aaf8b 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -38,6 +38,11 @@ typedef OpaquePipelineTuningOptions *LLVMPipelineTuningOptionsRef; DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PipelineTuningOptions, LLVMPipelineTuningOptionsRef) +struct OpaquePassInstrumentationCallbacks; +typedef OpaquePassInstrumentationCallbacks *LLVMPassInstrumentationCallbacksRef; +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PassInstrumentationCallbacks, + LLVMPassInstrumentationCallbacksRef) + static TargetMachine *unwrap(LLVMTargetMachineRef P) { return reinterpret_cast(P); } @@ -56,13 +61,13 @@ LLVMPY_CreateNewModulePassManager() { API_EXPORT(void) LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, LLVMModuleRef mod, - LLVMPipelineTuningOptionsRef PTORef, - LLVMTargetMachineRef TMRef) { + LLVMPassBuilderRef PBRef, + LLVMPassInstrumentationCallbacksRef PICRef) { ModulePassManager *MPM = llvm::unwrap(MPMRef); Module *M = llvm::unwrap(mod); - PipelineTuningOptions *PTO = llvm::unwrap(PTORef); - TargetMachine *TM = llvm::unwrap(TMRef); + PassBuilder *PB = llvm::unwrap(PBRef); + PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); // TODO: Make these set(able) by user bool DebugLogging = false; @@ -73,25 +78,21 @@ LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; - PassInstrumentationCallbacks PIC; PrintPassOptions PrintPassOpts; #if LLVM_VERSION_MAJOR < 16 StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); - SI.registerCallbacks(PIC, &FAM); - PassBuilder PB(TM, *PTO, None, &PIC); #else StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach, PrintPassOpts); - SI.registerCallbacks(PIC, &FAM); - PassBuilder PB(TM, *PTO, std::nullopt, &PIC); #endif + SI.registerCallbacks(*PIC, &FAM); - PB.registerLoopAnalyses(LAM); - PB.registerFunctionAnalyses(FAM); - PB.registerCGSCCAnalyses(CGAM); - PB.registerModuleAnalyses(MAM); - PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + PB->registerLoopAnalyses(LAM); + PB->registerFunctionAnalyses(FAM); + PB->registerCGSCCAnalyses(CGAM); + PB->registerModuleAnalyses(MAM); + PB->crossRegisterProxies(LAM, FAM, CGAM, MAM); MPM->run(*M, MAM); } @@ -151,13 +152,13 @@ LLVMPY_CreateNewFunctionPassManager() { API_EXPORT(void) LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, LLVMValueRef FRef, - LLVMPipelineTuningOptionsRef PTORef, - LLVMTargetMachineRef TMRef) { + LLVMPassBuilderRef PBRef, + LLVMPassInstrumentationCallbacksRef PICRef) { FunctionPassManager *FPM = llvm::unwrap(FPMRef); Function *F = reinterpret_cast(FRef); - PipelineTuningOptions *PTO = llvm::unwrap(PTORef); - TargetMachine *TM = llvm::unwrap(TMRef); + PassBuilder *PB = llvm::unwrap(PBRef); + PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); // Don't try to optimize function declarations if (F->isDeclaration()) @@ -172,25 +173,22 @@ LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, CGSCCAnalysisManager CGAM; ModuleAnalysisManager MAM; + // TODO: Can expose this in ffi layer PrintPassOptions PrintPassOpts; - PassInstrumentationCallbacks PIC; #if LLVM_VERSION_MAJOR < 16 StandardInstrumentations SI(DebugLogging, VerifyEach, PrintPassOpts); - SI.registerCallbacks(PIC, &FAM); - PassBuilder PB(TM, *PTO, None, &PIC); #else StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach, PrintPassOpts); - SI.registerCallbacks(PIC, &FAM); - PassBuilder PB(TM, *PTO, std::nullopt, &PIC); #endif + SI.registerCallbacks(*PIC, &FAM); - PB.registerLoopAnalyses(LAM); - PB.registerFunctionAnalyses(FAM); - PB.registerCGSCCAnalyses(CGAM); - PB.registerModuleAnalyses(MAM); - PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + PB->registerLoopAnalyses(LAM); + PB->registerFunctionAnalyses(FAM); + PB->registerCGSCCAnalyses(CGAM); + PB->registerModuleAnalyses(MAM); + PB->crossRegisterProxies(LAM, FAM, CGAM, MAM); FPM->run(*F, FAM); } @@ -293,14 +291,31 @@ LLVMPY_DisposePipelineTuningOptions(LLVMPipelineTuningOptionsRef PTO) { delete llvm::unwrap(PTO); } +// PIC +API_EXPORT(LLVMPassInstrumentationCallbacksRef) +LLVMPY_CreatePassInstrumentationCallbacks() { + return llvm::wrap(new PassInstrumentationCallbacks()); +} + +API_EXPORT(void) +LLVMPY_DisposePassInstrumentationCallbacks(LLVMPassInstrumentationCallbacksRef PIC) { + delete llvm::unwrap(PIC); +} + // PB API_EXPORT(LLVMPassBuilderRef) LLVMPY_CreatePassBuilder(LLVMTargetMachineRef TM, - LLVMPipelineTuningOptionsRef PTO) { + LLVMPipelineTuningOptionsRef PTO, + LLVMPassInstrumentationCallbacksRef PICRef) { TargetMachine *target = llvm::unwrap(TM); PipelineTuningOptions *pt = llvm::unwrap(PTO); - return llvm::wrap(new PassBuilder(target, *pt)); + PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); +#if LLVM_VERSION_MAJOR < 16 + return llvm::wrap(new PassBuilder(target, *pt, None, PIC)); +#else + return llvm::wrap(new PassBuilder(target, *pt, std::nullopt, PIC)); +#endif } API_EXPORT(void) diff --git a/llvmlite/binding/ffi.py b/llvmlite/binding/ffi.py index da7bb9a3c..d7bbb9c8c 100644 --- a/llvmlite/binding/ffi.py +++ b/llvmlite/binding/ffi.py @@ -42,6 +42,8 @@ def _make_opaque_ref(name): LLVMOrcDylibTrackerRef = _make_opaque_ref("LLVMOrcDylibTrackerRef") LLVMPipelineTuningOptionsRef = _make_opaque_ref("LLVMPipeLineTuningOptions") +LLVMPassInstrumentationCallbacksRef = \ + _make_opaque_ref("LLVMPassInstrumentationCallbacks") LLVMModulePassManagerRef = _make_opaque_ref("LLVMModulePassManager") LLVMFunctionPassManagerRef = _make_opaque_ref("LLVMFunctionPassManager") LLVMPassBuilderRef = _make_opaque_ref("LLVMPassBuilder") diff --git a/llvmlite/binding/newpassmanagers.py b/llvmlite/binding/newpassmanagers.py index e4baa54f7..ec479132b 100644 --- a/llvmlite/binding/newpassmanagers.py +++ b/llvmlite/binding/newpassmanagers.py @@ -26,7 +26,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, module, pb): - ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb._pto, pb._tm) + ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb, pb._pic) def add_verifier(self): ffi.lib.LLVMPY_AddVerifierPass(self) @@ -61,7 +61,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, fun, pb): - ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb._pto, pb._tm) + ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb, pb._pic) def add_aa_eval_pass(self): ffi.lib.LLVMPY_AddAAEvalPass_function(self) @@ -163,12 +163,23 @@ def _dispose(self): ffi.lib.LLVMPY_DisposePipelineTuningOptions(self) +class PassInstrumentationCallbacks(ffi.ObjectRef): + + def __init__(self): + super().__init__(ffi.lib.LLVMPY_CreatePassInstrumentationCallbacks()) + + def _dispose(self): + return ffi.lib.LLVMPY_DisposePassInstrumentationCallbacks(self) + + class PassBuilder(ffi.ObjectRef): def __init__(self, tm, pto): - super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto)) + pic = PassInstrumentationCallbacks() + super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto, pic)) self._pto = pto self._tm = tm + self._pic = pic def getModulePassManager(self): return ModulePassManager( @@ -195,7 +206,7 @@ def _dispose(self): ffi.lib.LLVMPY_RunNewModulePassManager.argtypes = [ ffi.LLVMModulePassManagerRef, ffi.LLVMModuleRef, - ffi.LLVMPipelineTuningOptionsRef, ffi.LLVMTargetMachineRef, ] + ffi.LLVMPassBuilderRef, ffi.LLVMPassInstrumentationCallbacksRef,] ffi.lib.LLVMPY_AddVerifierPass.argtypes = [ffi.LLVMModulePassManagerRef,] ffi.lib.LLVMPY_AddAAEvalPass_module.argtypes = [ffi.LLVMModulePassManagerRef,] @@ -224,7 +235,7 @@ def _dispose(self): ffi.lib.LLVMPY_RunNewFunctionPassManager.argtypes = [ ffi.LLVMFunctionPassManagerRef, ffi.LLVMValueRef, - ffi.LLVMPipelineTuningOptionsRef, ffi.LLVMTargetMachineRef, ] + ffi.LLVMPassBuilderRef, ffi.LLVMPassInstrumentationCallbacksRef, ] ffi.lib.LLVMPY_AddAAEvalPass_function.argtypes = [ ffi.LLVMFunctionPassManagerRef,] @@ -283,6 +294,12 @@ def _dispose(self): ffi.lib.LLVMPY_DisposePipelineTuningOptions.argtypes = \ [ffi.LLVMPipelineTuningOptionsRef,] +# PassInstrumentationCallbacks +ffi.lib.LLVMPY_CreatePassInstrumentationCallbacks.restype = \ + ffi.LLVMPassInstrumentationCallbacksRef +ffi.lib.LLVMPY_DisposePassInstrumentationCallbacks.argtypes = \ + [ffi.LLVMPassInstrumentationCallbacksRef,] + # PassBuilder ffi.lib.LLVMPY_CreatePassBuilder.restype = ffi.LLVMPassBuilderRef From 75131d5e774191947dcddbe60d64e059df2202f3 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Fri, 19 Jul 2024 15:33:32 +0530 Subject: [PATCH 5/8] Address review comments for tests --- ffi/newpassmanagers.cpp | 1 - llvmlite/tests/test_binding.py | 70 ++++++++++++---------------------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index 01b0aaf8b..ba169aeb3 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -1,7 +1,6 @@ #include "core.h" #include "llvm-c/TargetMachine.h" #include "llvm/Analysis/AliasAnalysisEvaluator.h" -#include "llvm/IR/LLVMContext.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/Passes/PassBuilder.h" diff --git a/llvmlite/tests/test_binding.py b/llvmlite/tests/test_binding.py index 1922eaa70..2f91c6042 100644 --- a/llvmlite/tests/test_binding.py +++ b/llvmlite/tests/test_binding.py @@ -469,23 +469,6 @@ def no_de_locale(): } """ -asm_alloca_no_optnone = r""" -define double @foo(i32 %i, double %j) noinline { - %I = alloca i32 ; [#uses=4] - %J = alloca double ; [#uses=2] - store i32 %i, i32* %I - store double %j, double* %J - %t1 = load i32, i32* %I ; [#uses=1] - %t2 = add i32 %t1, 1 ; [#uses=1] - store i32 %t2, i32* %I - %t3 = load i32, i32* %I ; [#uses=1] - %t4 = sitofp i32 %t3 to double ; [#uses=1] - %t5 = load double, double* %J ; [#uses=1] - %t6 = fmul double %t4, %t5 ; [#uses=1] - ret double %t6 -} -""" - asm_declaration = r""" declare void @test_declare(i32* ) """ @@ -3057,27 +3040,26 @@ class TestNewModulePassManager(BaseTest, NewPassManagerMixin): def pm(self): return llvm.create_new_module_pass_manager() - def test_close(self): - mpm = self.pm() - mpm.close() - - def test_run_O3(self): - pb = self.pb(speed_level=3, size_level=0) + def run_o_n(self, level): mod = self.module() - orig_asm = str(mod) + orig_asm = str(self.module()) + pb = self.pb(speed_level=level, size_level=0) mpm = pb.getModulePassManager() mpm.run(mod, pb) optimized_asm = str(mod) + return orig_asm, optimized_asm + + def test_close(self): + mpm = self.pm() + mpm.close() + + def test_run_o3(self): + orig_asm, optimized_asm = self.run_o_n(3) self.assertIn("%.4", orig_asm) self.assertNotIn("%.4", optimized_asm) - def test_run_O0(self): - pb = self.pb(speed_level=0, size_level=0) - mod = self.module() - orig_asm = str(mod) - mpm = pb.getModulePassManager() - mpm.run(mod, pb) - optimized_asm = str(mod) + def test_run_o0(self): + orig_asm, optimized_asm = self.run_o_n(0) self.assertIn("%.4", orig_asm) self.assertIn("%.4", optimized_asm) @@ -3093,9 +3075,9 @@ def test_instcombine(self): self.assertNotIn("%.3", optimized_asm) def test_optnone(self): - # Module shouldn't be optimized if the function as `optnone` attached + # Module shouldn't be optimized if the function has `optnone` attached pb = self.pb(speed_level=3, size_level=0) - orig_asm_no_optnone = str(asm_alloca_no_optnone) + orig_asm_no_optnone = str(asm_alloca_optnone.replace("optnone ", "")) mod = llvm.parse_assembly(orig_asm_no_optnone) mpm = pb.getModulePassManager() mpm.run(mod, pb) @@ -3130,32 +3112,30 @@ def test_close(self): fpm = self.pm() fpm.close() - def test_run_O3(self): - pb = self.pb(3) + def run_o_n(self, level): mod = self.module() fun = mod.get_function("sum") orig_asm = str(fun) + pb = self.pb(speed_level=level, size_level=0) fpm = pb.getFunctionPassManager() fpm.run(fun, pb) optimized_asm = str(fun) + return orig_asm, optimized_asm + + def test_run_o3(self): + orig_asm, optimized_asm = self.run_o_n(3) self.assertIn("%.4", orig_asm) self.assertNotIn("%.4", optimized_asm) - def test_run_O0(self): - pb = self.pb(0) - mod = self.module() - fun = mod.get_function("sum") - orig_asm = str(fun) - fpm = pb.getFunctionPassManager() - fpm.run(fun, pb) - optimized_asm = str(fun) + def test_run_o0(self): + orig_asm, optimized_asm = self.run_o_n(0) self.assertIn("%.4", orig_asm) self.assertIn("%.4", optimized_asm) def test_optnone(self): - # Function shouldn't be optimized if the function as `optnone` attached + # Function shouldn't be optimized if the function has `optnone` attached pb = self.pb(speed_level=3, size_level=0) - orig_asm_no_optnone = str(asm_alloca_no_optnone) + orig_asm_no_optnone = str(asm_alloca_optnone.replace("optnone ", "")) fun = llvm.parse_assembly(orig_asm_no_optnone).get_function("foo") fpm = pb.getFunctionPassManager() fpm.run(fun, pb) From fdaeabbda7fe06e2fc287c8ba9303fbd5714d212 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Fri, 19 Jul 2024 20:41:50 +0530 Subject: [PATCH 6/8] more formatting --- ffi/newpassmanagers.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index ba169aeb3..108bebef3 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -59,8 +59,7 @@ LLVMPY_CreateNewModulePassManager() { API_EXPORT(void) LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, - LLVMModuleRef mod, - LLVMPassBuilderRef PBRef, + LLVMModuleRef mod, LLVMPassBuilderRef PBRef, LLVMPassInstrumentationCallbacksRef PICRef) { ModulePassManager *MPM = llvm::unwrap(MPMRef); @@ -150,8 +149,7 @@ LLVMPY_CreateNewFunctionPassManager() { API_EXPORT(void) LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, - LLVMValueRef FRef, - LLVMPassBuilderRef PBRef, + LLVMValueRef FRef, LLVMPassBuilderRef PBRef, LLVMPassInstrumentationCallbacksRef PICRef) { FunctionPassManager *FPM = llvm::unwrap(FPMRef); @@ -297,7 +295,8 @@ LLVMPY_CreatePassInstrumentationCallbacks() { } API_EXPORT(void) -LLVMPY_DisposePassInstrumentationCallbacks(LLVMPassInstrumentationCallbacksRef PIC) { +LLVMPY_DisposePassInstrumentationCallbacks( + LLVMPassInstrumentationCallbacksRef PIC) { delete llvm::unwrap(PIC); } From adad42b68d4ed2ac7c1a16e02efcaca7457073ec Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Tue, 23 Jul 2024 12:07:10 +0530 Subject: [PATCH 7/8] Query PIC directly from PB object, instead of managing it seperately --- ffi/newpassmanagers.cpp | 34 +++++------------------------ llvmlite/binding/ffi.py | 2 -- llvmlite/binding/newpassmanagers.py | 27 +++++------------------ 3 files changed, 11 insertions(+), 52 deletions(-) diff --git a/ffi/newpassmanagers.cpp b/ffi/newpassmanagers.cpp index 108bebef3..3643bac46 100644 --- a/ffi/newpassmanagers.cpp +++ b/ffi/newpassmanagers.cpp @@ -37,11 +37,6 @@ typedef OpaquePipelineTuningOptions *LLVMPipelineTuningOptionsRef; DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PipelineTuningOptions, LLVMPipelineTuningOptionsRef) -struct OpaquePassInstrumentationCallbacks; -typedef OpaquePassInstrumentationCallbacks *LLVMPassInstrumentationCallbacksRef; -DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PassInstrumentationCallbacks, - LLVMPassInstrumentationCallbacksRef) - static TargetMachine *unwrap(LLVMTargetMachineRef P) { return reinterpret_cast(P); } @@ -59,13 +54,11 @@ LLVMPY_CreateNewModulePassManager() { API_EXPORT(void) LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, - LLVMModuleRef mod, LLVMPassBuilderRef PBRef, - LLVMPassInstrumentationCallbacksRef PICRef) { + LLVMModuleRef mod, LLVMPassBuilderRef PBRef) { ModulePassManager *MPM = llvm::unwrap(MPMRef); Module *M = llvm::unwrap(mod); PassBuilder *PB = llvm::unwrap(PBRef); - PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); // TODO: Make these set(able) by user bool DebugLogging = false; @@ -84,7 +77,7 @@ LLVMPY_RunNewModulePassManager(LLVMModulePassManagerRef MPMRef, StandardInstrumentations SI(M->getContext(), DebugLogging, VerifyEach, PrintPassOpts); #endif - SI.registerCallbacks(*PIC, &FAM); + SI.registerCallbacks(*PB->getPassInstrumentationCallbacks(), &FAM); PB->registerLoopAnalyses(LAM); PB->registerFunctionAnalyses(FAM); @@ -149,13 +142,11 @@ LLVMPY_CreateNewFunctionPassManager() { API_EXPORT(void) LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, - LLVMValueRef FRef, LLVMPassBuilderRef PBRef, - LLVMPassInstrumentationCallbacksRef PICRef) { + LLVMValueRef FRef, LLVMPassBuilderRef PBRef) { FunctionPassManager *FPM = llvm::unwrap(FPMRef); Function *F = reinterpret_cast(FRef); PassBuilder *PB = llvm::unwrap(PBRef); - PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); // Don't try to optimize function declarations if (F->isDeclaration()) @@ -179,7 +170,7 @@ LLVMPY_RunNewFunctionPassManager(LLVMFunctionPassManagerRef FPMRef, StandardInstrumentations SI(F->getContext(), DebugLogging, VerifyEach, PrintPassOpts); #endif - SI.registerCallbacks(*PIC, &FAM); + SI.registerCallbacks(*PB->getPassInstrumentationCallbacks(), &FAM); PB->registerLoopAnalyses(LAM); PB->registerFunctionAnalyses(FAM); @@ -288,27 +279,14 @@ LLVMPY_DisposePipelineTuningOptions(LLVMPipelineTuningOptionsRef PTO) { delete llvm::unwrap(PTO); } -// PIC -API_EXPORT(LLVMPassInstrumentationCallbacksRef) -LLVMPY_CreatePassInstrumentationCallbacks() { - return llvm::wrap(new PassInstrumentationCallbacks()); -} - -API_EXPORT(void) -LLVMPY_DisposePassInstrumentationCallbacks( - LLVMPassInstrumentationCallbacksRef PIC) { - delete llvm::unwrap(PIC); -} - // PB API_EXPORT(LLVMPassBuilderRef) LLVMPY_CreatePassBuilder(LLVMTargetMachineRef TM, - LLVMPipelineTuningOptionsRef PTO, - LLVMPassInstrumentationCallbacksRef PICRef) { + LLVMPipelineTuningOptionsRef PTO) { TargetMachine *target = llvm::unwrap(TM); PipelineTuningOptions *pt = llvm::unwrap(PTO); - PassInstrumentationCallbacks *PIC = llvm::unwrap(PICRef); + PassInstrumentationCallbacks *PIC = new PassInstrumentationCallbacks(); #if LLVM_VERSION_MAJOR < 16 return llvm::wrap(new PassBuilder(target, *pt, None, PIC)); #else diff --git a/llvmlite/binding/ffi.py b/llvmlite/binding/ffi.py index d7bbb9c8c..da7bb9a3c 100644 --- a/llvmlite/binding/ffi.py +++ b/llvmlite/binding/ffi.py @@ -42,8 +42,6 @@ def _make_opaque_ref(name): LLVMOrcDylibTrackerRef = _make_opaque_ref("LLVMOrcDylibTrackerRef") LLVMPipelineTuningOptionsRef = _make_opaque_ref("LLVMPipeLineTuningOptions") -LLVMPassInstrumentationCallbacksRef = \ - _make_opaque_ref("LLVMPassInstrumentationCallbacks") LLVMModulePassManagerRef = _make_opaque_ref("LLVMModulePassManager") LLVMFunctionPassManagerRef = _make_opaque_ref("LLVMFunctionPassManager") LLVMPassBuilderRef = _make_opaque_ref("LLVMPassBuilder") diff --git a/llvmlite/binding/newpassmanagers.py b/llvmlite/binding/newpassmanagers.py index ec479132b..f09267f46 100644 --- a/llvmlite/binding/newpassmanagers.py +++ b/llvmlite/binding/newpassmanagers.py @@ -26,7 +26,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, module, pb): - ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb, pb._pic) + ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb) def add_verifier(self): ffi.lib.LLVMPY_AddVerifierPass(self) @@ -61,7 +61,7 @@ def __init__(self, ptr=None): super().__init__(ptr) def run(self, fun, pb): - ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb, pb._pic) + ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb) def add_aa_eval_pass(self): ffi.lib.LLVMPY_AddAAEvalPass_function(self) @@ -163,23 +163,12 @@ def _dispose(self): ffi.lib.LLVMPY_DisposePipelineTuningOptions(self) -class PassInstrumentationCallbacks(ffi.ObjectRef): - - def __init__(self): - super().__init__(ffi.lib.LLVMPY_CreatePassInstrumentationCallbacks()) - - def _dispose(self): - return ffi.lib.LLVMPY_DisposePassInstrumentationCallbacks(self) - - class PassBuilder(ffi.ObjectRef): def __init__(self, tm, pto): - pic = PassInstrumentationCallbacks() - super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto, pic)) + super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto)) self._pto = pto self._tm = tm - self._pic = pic def getModulePassManager(self): return ModulePassManager( @@ -206,7 +195,7 @@ def _dispose(self): ffi.lib.LLVMPY_RunNewModulePassManager.argtypes = [ ffi.LLVMModulePassManagerRef, ffi.LLVMModuleRef, - ffi.LLVMPassBuilderRef, ffi.LLVMPassInstrumentationCallbacksRef,] + ffi.LLVMPassBuilderRef,] ffi.lib.LLVMPY_AddVerifierPass.argtypes = [ffi.LLVMModulePassManagerRef,] ffi.lib.LLVMPY_AddAAEvalPass_module.argtypes = [ffi.LLVMModulePassManagerRef,] @@ -235,7 +224,7 @@ def _dispose(self): ffi.lib.LLVMPY_RunNewFunctionPassManager.argtypes = [ ffi.LLVMFunctionPassManagerRef, ffi.LLVMValueRef, - ffi.LLVMPassBuilderRef, ffi.LLVMPassInstrumentationCallbacksRef, ] + ffi.LLVMPassBuilderRef,] ffi.lib.LLVMPY_AddAAEvalPass_function.argtypes = [ ffi.LLVMFunctionPassManagerRef,] @@ -294,12 +283,6 @@ def _dispose(self): ffi.lib.LLVMPY_DisposePipelineTuningOptions.argtypes = \ [ffi.LLVMPipelineTuningOptionsRef,] -# PassInstrumentationCallbacks -ffi.lib.LLVMPY_CreatePassInstrumentationCallbacks.restype = \ - ffi.LLVMPassInstrumentationCallbacksRef -ffi.lib.LLVMPY_DisposePassInstrumentationCallbacks.argtypes = \ - [ffi.LLVMPassInstrumentationCallbacksRef,] - # PassBuilder ffi.lib.LLVMPY_CreatePassBuilder.restype = ffi.LLVMPassBuilderRef From 664d2a280391b7a4f6aa56e55cc452ec2cb15269 Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Tue, 23 Jul 2024 12:07:36 +0530 Subject: [PATCH 8/8] review comments in tests --- llvmlite/tests/test_binding.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/llvmlite/tests/test_binding.py b/llvmlite/tests/test_binding.py index 2f91c6042..59e653c9e 100644 --- a/llvmlite/tests/test_binding.py +++ b/llvmlite/tests/test_binding.py @@ -3042,7 +3042,7 @@ def pm(self): def run_o_n(self, level): mod = self.module() - orig_asm = str(self.module()) + orig_asm = str(mod) pb = self.pb(speed_level=level, size_level=0) mpm = pb.getModulePassManager() mpm.run(mod, pb) @@ -3075,16 +3075,16 @@ def test_instcombine(self): self.assertNotIn("%.3", optimized_asm) def test_optnone(self): - # Module shouldn't be optimized if the function has `optnone` attached pb = self.pb(speed_level=3, size_level=0) - orig_asm_no_optnone = str(asm_alloca_optnone.replace("optnone ", "")) - mod = llvm.parse_assembly(orig_asm_no_optnone) + orig_asm = str(asm_alloca_optnone.replace("optnone ", "")) + mod = llvm.parse_assembly(orig_asm) mpm = pb.getModulePassManager() mpm.run(mod, pb) - optimized_asm_no_optnone = str(mod) - self.assertIn("alloca", orig_asm_no_optnone) - self.assertNotIn("alloca", optimized_asm_no_optnone) + optimized_asm = str(mod) + self.assertIn("alloca", orig_asm) + self.assertNotIn("alloca", optimized_asm) + # Module shouldn't be optimized if the function has `optnone` attached orig_asm_optnone = str(asm_alloca_optnone) mpm = pb.getModulePassManager() mod = llvm.parse_assembly(orig_asm_optnone) @@ -3133,16 +3133,16 @@ def test_run_o0(self): self.assertIn("%.4", optimized_asm) def test_optnone(self): - # Function shouldn't be optimized if the function has `optnone` attached pb = self.pb(speed_level=3, size_level=0) - orig_asm_no_optnone = str(asm_alloca_optnone.replace("optnone ", "")) - fun = llvm.parse_assembly(orig_asm_no_optnone).get_function("foo") + orig_asm = str(asm_alloca_optnone.replace("optnone ", "")) + fun = llvm.parse_assembly(orig_asm).get_function("foo") fpm = pb.getFunctionPassManager() fpm.run(fun, pb) - optimized_asm_no_optnone = str(fun) - self.assertIn("alloca", orig_asm_no_optnone) - self.assertNotIn("alloca", optimized_asm_no_optnone) + optimized_asm = str(fun) + self.assertIn("alloca", orig_asm) + self.assertNotIn("alloca", optimized_asm) + # Function shouldn't be optimized if the function has `optnone` attached orig_asm_optnone = str(asm_alloca_optnone) fun = llvm.parse_assembly(orig_asm_optnone).get_function("foo") fpm = pb.getFunctionPassManager()