Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/examples/llvmir_iter.py
1154 views
1
import llvmlite.binding as llvm
2
ir = r'''
3
; ModuleID = '<string>'
4
target triple = "unknown-unknown-unknown"
5
%struct.glob_type = type { i64, [2 x i64] }
6
7
@glob = global i32 0
8
@glob_b = global i8 0
9
@glob_f = global float 1.5
10
@glob_struct = global %struct.glob_type {i64 0, [2 x i64] [i64 0, i64 0]}
11
12
define i32 @sum(i32 %.1, i32 %.2) {
13
%.3 = add i32 %.1, %.2
14
%.4 = add i32 0, %.3
15
ret i32 %.4
16
}
17
18
define void @foo() {
19
call void asm sideeffect "nop", ""()
20
ret void
21
}
22
23
declare void @a_readonly_func(i8 *) readonly
24
'''
25
26
m = llvm.parse_assembly(ir)
27
28
for f in m.functions:
29
print(f'Function: {f.name}/`{f.type}`')
30
assert f.module is m
31
assert f.function is None
32
assert f.block is None
33
assert f.is_function and not (f.is_block or f.is_instruction)
34
print(f'Function attributes: {list(f.attributes)}')
35
for a in f.arguments:
36
print(f'Argument: {a.name}/`{a.type}`')
37
print(f'Argument attributes: {list(a.attributes)}')
38
for b in f.blocks:
39
print(f'Block: {b.name}/`{b.type}`\n{b}\nEnd of Block')
40
assert b.module is m
41
assert b.function is f
42
assert b.block is None
43
assert b.is_block and not (b.is_function or b.is_instruction)
44
for i in b.instructions:
45
print(f'Instruction: {i.name}/`{i.opcode}`/`{i.type}`: `{i}`')
46
print(f'Attributes: {list(i.attributes)}')
47
assert i.module is m
48
assert i.function is f
49
assert i.block is b
50
assert i.is_instruction and not (i.is_function or i.is_block)
51
for o in i.operands:
52
print(f'Operand: {o.name}/{o.type}')
53
54
for g in m.global_variables:
55
print(f'Global: {g.name}/`{g.type}`')
56
assert g.is_global
57
print(f'Attributes: {list(g.attributes)}')
58
print(g)
59
60