forked from JuliaGPU/cuTile.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirutils.jl
More file actions
104 lines (90 loc) · 2.97 KB
/
irutils.jl
File metadata and controls
104 lines (90 loc) · 2.97 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# StructuredIRCode / SSAMap mutation utilities
#
# Helpers for passes that modify the structured IR in place.
# Inspired by Julia's IncrementalCompact (Compiler/src/ssair/ir.jl).
"""
new_ssa_idx!(sci::StructuredIRCode) -> Int
Allocate a fresh SSA index from the StructuredIRCode.
"""
function new_ssa_idx!(sci::StructuredIRCode)
sci.max_ssa_idx += 1
return sci.max_ssa_idx
end
"""
new_block_arg!(block::Block, sci::StructuredIRCode, @nospecialize(typ)) -> BlockArg
Add a new BlockArg to a block, allocating a fresh ID.
"""
function new_block_arg!(block::Block, sci::StructuredIRCode, @nospecialize(typ))
id = new_ssa_idx!(sci)
arg = BlockArg(id, typ)
push!(block.args, arg)
return arg
end
"""
Base.pushfirst!(m::SSAMap, (idx, stmt, typ)::Tuple{Int,Any,Any})
Prepend a statement at the beginning of an SSAMap.
"""
function Base.pushfirst!(m::SSAMap, (idx, stmt, typ)::Tuple{Int,Any,Any})
pushfirst!(m.ssa_idxes, idx)
pushfirst!(m.stmts, stmt)
pushfirst!(m.types, typ)
return nothing
end
"""
insert_before!(m::SSAMap, before_idx::Int, new_idx::Int, stmt, typ)
Insert a new entry before the entry with SSA index `before_idx`.
"""
function insert_before!(m::SSAMap, before_idx::Int, new_idx::Int, stmt, typ)
pos = findfirst(==(before_idx), m.ssa_idxes)
pos === nothing && throw(KeyError(before_idx))
insert!(m.ssa_idxes, pos, new_idx)
insert!(m.stmts, pos, stmt)
insert!(m.types, pos, typ)
return nothing
end
"""
insert_after!(m::SSAMap, after_idx::Int, new_idx::Int, stmt, typ)
Insert a new entry after the entry with SSA index `after_idx`.
"""
function insert_after!(m::SSAMap, after_idx::Int, new_idx::Int, stmt, typ)
pos = findfirst(==(after_idx), m.ssa_idxes)
pos === nothing && throw(KeyError(after_idx))
insert!(m.ssa_idxes, pos + 1, new_idx)
insert!(m.stmts, pos + 1, stmt)
insert!(m.types, pos + 1, typ)
return nothing
end
"""
update_type!(m::SSAMap, ssa_idx::Int, @nospecialize(new_type))
Update the type annotation for an existing SSAMap entry.
"""
function update_type!(m::SSAMap, ssa_idx::Int, @nospecialize(new_type))
pos = findfirst(==(ssa_idx), m.ssa_idxes)
pos === nothing && throw(KeyError(ssa_idx))
m.types[pos] = new_type
return nothing
end
"""
resolve_call(stmt) -> (resolved_func, operands) or nothing
Extract the resolved function and operands from a `:call` or `:invoke` Expr.
Shared by alias analysis and token ordering passes.
"""
function resolve_call(stmt)
stmt isa Expr || return nothing
if stmt.head === :call
func_ref = stmt.args[1]
operands = @view stmt.args[2:end]
elseif stmt.head === :invoke
func_ref = stmt.args[2]
operands = @view stmt.args[3:end]
else
return nothing
end
resolved = if func_ref isa GlobalRef
try; getfield(func_ref.mod, func_ref.name); catch; nothing; end
else
func_ref
end
resolved === nothing && return nothing
return (resolved, operands)
end