Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/examples/printer-passes-npm.py
1154 views
1
# Demonstrates the printer and view passes in the new pass manager
2
3
try:
4
import faulthandler; faulthandler.enable()
5
except ImportError:
6
pass
7
8
import llvmlite.ir as ll
9
import llvmlite.binding as llvm
10
11
12
llvm.initialize_native_target()
13
llvm.initialize_native_asmprinter()
14
15
strmod = """
16
define i32 @foo3(i32* noalias nocapture readonly %src) {
17
entry:
18
br label %loop.header
19
20
loop.header:
21
%iv = phi i64 [ 0, %entry ], [ %inc, %loop.latch ]
22
%r1 = phi i32 [ 0, %entry ], [ %r3, %loop.latch ]
23
%arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
24
%src_element = load i32, i32* %arrayidx, align 4
25
%cmp = icmp eq i32 0, %src_element
26
br i1 %cmp, label %loop.if, label %loop.latch
27
28
loop.if:
29
%r2 = add i32 %r1, 1
30
br label %loop.latch
31
loop.latch:
32
%r3 = phi i32 [%r1, %loop.header], [%r2, %loop.if]
33
%inc = add nuw nsw i64 %iv, 1
34
%exitcond = icmp eq i64 %inc, 9
35
br i1 %exitcond, label %loop.end, label %loop.header
36
loop.end:
37
%r.lcssa = phi i32 [ %r3, %loop.latch ]
38
ret i32 %r.lcssa
39
}
40
"""
41
42
dbgmodstr = """
43
define void @f() !dbg !4 {
44
ret void, !dbg !15
45
}
46
47
!llvm.dbg.cu = !{!0, !8}
48
!llvm.module.flags = !{!13, !16}
49
50
!0 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang version 3.4 (192092)", isOptimized: false, emissionKind: FullDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
51
!1 = !DIFile(filename: "test1.c", directory: "/tmp")
52
!2 = !{}
53
!4 = distinct !DISubprogram(name: "f", line: 1, isLocal: false, isDefinition: true, virtualIndex: 6, isOptimized: false, unit: !0, scopeLine: 1, file: !1, scope: !5, type: !6, retainedNodes: !2)
54
!5 = !DIFile(filename: "test1.c", directory: "/tmp")
55
!6 = !DISubroutineType(types: !7)
56
!7 = !{null}
57
!8 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang version 3.4 (192092)", isOptimized: false, emissionKind: FullDebug, file: !9, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
58
!9 = !DIFile(filename: "test2.c", directory: "/tmp")
59
!11 = distinct !DISubprogram(name: "g", line: 1, isLocal: false, isDefinition: true, virtualIndex: 6, isOptimized: false, unit: !8, scopeLine: 1, file: !9, scope: !12, type: !6, retainedNodes: !2)
60
!12 = !DIFile(filename: "test2.c", directory: "/tmp")
61
!13 = !{i32 2, !"Dwarf Version", i32 4}
62
!14 = !DILocation(line: 1, scope: !4)
63
!15 = !DILocation(line: 1, scope: !11, inlinedAt: !14)
64
!16 = !{i32 1, !"Debug Info Version", i32 3}
65
"""
66
67
68
llmod = llvm.parse_assembly(strmod)
69
dbgmod = llvm.parse_assembly(dbgmodstr)
70
71
target_machine = llvm.Target.from_default_triple().create_target_machine()
72
pto = llvm.create_pipeline_tuning_options(speed_level=0)
73
pb = llvm.create_pass_builder(target_machine, pto)
74
pm = llvm.create_new_module_pass_manager()
75
76
pm.add_module_debug_info_pass()
77
pm.add_cfg_printer_pass()
78
pm.add_cfg_only_printer_pass()
79
pm.add_dom_printer_pass()
80
pm.add_dom_only_printer_pass()
81
pm.add_post_dom_printer_pass()
82
pm.add_post_dom_only_printer_pass()
83
pm.add_dom_only_printer_pass()
84
# Uncomment the following to launch viewers as required
85
# pm.add_post_dom_viewer_pass()
86
# pm.add_dom_viewer_pass()
87
# pm.add_post_dom_only_viewer_pass()
88
89
pm.run(llmod, pb)
90
pm.run(dbgmod, pb)
91
92