Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/examples/npm_passes.py
1154 views
1
"""
2
This example demonstrates how to use the new module pass manager to optimize a
3
module using the loop unrolling and CFG simplification passes.
4
"""
5
6
import faulthandler
7
import llvmlite.binding as llvm
8
9
# Dump Python traceback in the event of a segfault
10
faulthandler.enable()
11
12
# All are required to initialize LLVM
13
llvm.initialize_native_target()
14
llvm.initialize_native_asmprinter()
15
16
# Module to optimize
17
strmod = """
18
define i32 @foo3(i32* noalias nocapture readonly %src) {
19
entry:
20
br label %loop.header
21
22
loop.header:
23
%iv = phi i64 [ 0, %entry ], [ %inc, %loop.latch ]
24
%r1 = phi i32 [ 0, %entry ], [ %r3, %loop.latch ]
25
%arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
26
%src_element = load i32, i32* %arrayidx, align 4
27
%cmp = icmp eq i32 0, %src_element
28
br i1 %cmp, label %loop.if, label %loop.latch
29
30
loop.if:
31
%r2 = add i32 %r1, 1
32
br label %loop.latch
33
loop.latch:
34
%r3 = phi i32 [%r1, %loop.header], [%r2, %loop.if]
35
%inc = add nuw nsw i64 %iv, 1
36
%exitcond = icmp eq i64 %inc, 9
37
br i1 %exitcond, label %loop.end, label %loop.header
38
loop.end:
39
%r.lcssa = phi i32 [ %r3, %loop.latch ]
40
ret i32 %r.lcssa
41
}
42
"""
43
44
45
module = llvm.parse_assembly(strmod)
46
47
print("Module before optimization:\n")
48
print(module)
49
50
# Set up the module pass manager used to run our optimization pipeline.
51
# We create it unpopulated, and then add the loop unroll and simplify CFG
52
# passes.
53
pm = llvm.create_new_module_pass_manager()
54
pm.add_loop_unroll_pass()
55
pm.add_simplify_cfg_pass()
56
57
58
# To run the pass manager, we need a pass builder object - we create pipeline
59
# tuning options with no optimization, then use that to create a pass builder.
60
target_machine = llvm.Target.from_default_triple().create_target_machine()
61
pto = llvm.create_pipeline_tuning_options(speed_level=0)
62
pb = llvm.create_pass_builder(target_machine, pto)
63
64
# Now we can run the pass manager on our module
65
pm.run(module, pb)
66
67
68
# We should observer a fully unrolled loop, and the function now consists of a
69
# single basic block executing all the iterations of the loop in a straight
70
# line.
71
print("\nModule after optimization:\n")
72
print(module)
73
74