-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.jl
89 lines (80 loc) · 2 KB
/
Utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Utils
export @code_ircode, @dot
using InteractiveUtils
code_ircode = Base.code_ircode
macro code_ircode(ex0...)
thecall = InteractiveUtils.gen_call_with_extracted_types_and_kwargs(@__MODULE__, :code_ircode, ex0)
quote
local results = $thecall
length(results) == 1 ? results[1] : results
end
end
function create_dotgraph(ir::Core.Compiler.IRCode; clip=false)
blocks = ir.cfg.blocks
edges = []
labels = []
for (i, block) in enumerate(blocks)
r = block.stmts.start:block.stmts.stop
instructions = ir.stmts.inst[r]
# show instructions in label, left-aligned
push!(labels, "$i [label=\"$i\\n$(join(instructions, "\\l"))\\l\"]")
push!(edges, string.(block.preds) .* " -> $i"...)
end
graph = """
digraph G {
node [margin=1 fontname="consolas" fontsize=32 width=1 shape=box style=filled]
$(join(labels, "\n"))
$(join(edges, "\n"))
}
"""
clip && clipboard(graph)
return graph
end
macro dot(ex)
ir, ret = (__module__).eval(:(@code_ircode $(ex)))
command = create_dotgraph(ir, clip=false)
return command
end
module DebugIR
using MLIR.IR
function into(operation::IR.Operation)
out = []
for region in IR.RegionIterator(operation)
push!(out, region)
end
return out
end
function into(region::IR.Region)
out = []
for block in IR.BlockIterator(region)
push!(out, block)
end
return out
end
function into(block::IR.Block)
out = []
for operation in IR.OperationIterator(block)
push!(out, operation)
end
return out
end
struct DOperation
regions::Union{Vector,Nothing}
end
struct DRegion
blocks::Vector
end
struct DBlock
operations::Vector
end
f(element::IR.Operation) = begin
regions = into(element)
if length(regions) == 0
return DOperation(nothing)
end
return DOperation(f.(into(element)))
end
f(element::IR.Region) = DRegion(f.(into(element)))
f(element::IR.Block) = DBlock(f.(into(element)))
end
end