Skip to content

Commit 53ba326

Browse files
committed
update irutils.jl and fix the irpasses.jl test
1 parent 64f8634 commit 53ba326

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

test/compiler/irutils.jl

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Core: CodeInfo, ReturnNode, MethodInstance
1+
using Core.IR
22
using Core.Compiler: IRCode, IncrementalCompact, singleton_type, VarState
33
using Base.Meta: isexpr
44
using InteractiveUtils: gen_call_with_extracted_types_and_kwargs
@@ -16,7 +16,8 @@ end
1616
# check if `x` is a statement with a given `head`
1717
isnew(@nospecialize x) = isexpr(x, :new)
1818
issplatnew(@nospecialize x) = isexpr(x, :splatnew)
19-
isreturn(@nospecialize x) = isa(x, ReturnNode)
19+
isreturn(@nospecialize x) = isa(x, ReturnNode) && isdefined(x, :val)
20+
isisdefined(@nospecialize x) = isexpr(x, :isdefined)
2021

2122
# check if `x` is a dynamic call of a given function
2223
iscall(y) = @nospecialize(x) -> iscall(y, x)
@@ -42,19 +43,49 @@ fully_eliminated(@nospecialize args...; retval=(@__FILE__), kwargs...) =
4243
fully_eliminated(src::CodeInfo; retval=(@__FILE__)) = fully_eliminated(src.code; retval)
4344
fully_eliminated(ir::IRCode; retval=(@__FILE__)) = fully_eliminated(ir.stmts.inst; retval)
4445
function fully_eliminated(code::Vector{Any}; retval=(@__FILE__), kwargs...)
45-
if retval !== (@__FILE__)
46-
length(code) == 1 || return false
47-
code1 = code[1]
48-
isreturn(code1) || return false
49-
val = code1.val
50-
if val isa QuoteNode
51-
val = val.value
52-
end
53-
return val == retval
54-
else
55-
return length(code) == 1 && isreturn(code[1])
46+
length(code) == 1 || return false
47+
retstmt = only(code)
48+
isreturn(retstmt) || return false
49+
retval === (@__FILE__) && return true
50+
retval′ = retstmt.val
51+
if retval′ isa QuoteNode
52+
retval′ = retval′.value
5653
end
54+
return retval′ == retval
5755
end
5856
macro fully_eliminated(ex0...)
5957
return gen_call_with_extracted_types_and_kwargs(__module__, :fully_eliminated, ex0)
6058
end
59+
60+
let m = Meta.@lower 1 + 1
61+
@assert Meta.isexpr(m, :thunk)
62+
orig_src = m.args[1]::CodeInfo
63+
global function make_codeinfo(code::Vector{Any};
64+
ssavaluetypes::Union{Nothing,Vector{Any}}=nothing,
65+
slottypes::Union{Nothing,Vector{Any}}=nothing)
66+
src = copy(orig_src)
67+
src.code = code
68+
nstmts = length(src.code)
69+
if ssavaluetypes === nothing
70+
src.ssavaluetypes = nstmts
71+
else
72+
src.ssavaluetypes = ssavaluetypes
73+
end
74+
src.codelocs = fill(one(Int32), nstmts)
75+
src.ssaflags = fill(zero(UInt32), nstmts)
76+
if slottypes !== nothing
77+
src.slottypes = slottypes
78+
src.slotflags = fill(zero(UInt8), length(slottypes))
79+
end
80+
return src
81+
end
82+
global function make_ircode(code::Vector{Any};
83+
ssavaluetypes::Union{Nothing,Vector{Any}}=nothing,
84+
slottypes::Union{Nothing,Vector{Any}}=nothing,
85+
verify::Bool=true)
86+
src = make_codeinfo(code; ssavaluetypes, slottypes)
87+
ir = Core.Compiler.inflate_ir(src)
88+
verify && Core.Compiler.verify_ir(ir)
89+
return ir
90+
end
91+
end

0 commit comments

Comments
 (0)