1- using Core: CodeInfo, ReturnNode, MethodInstance
1+ using Core. IR
22using Core. Compiler: IRCode, IncrementalCompact, singleton_type, VarState
33using Base. Meta: isexpr
44using InteractiveUtils: gen_call_with_extracted_types_and_kwargs
1616# check if `x` is a statement with a given `head`
1717isnew (@nospecialize x) = isexpr (x, :new )
1818issplatnew (@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
2223iscall (y) = @nospecialize (x) -> iscall (y, x)
@@ -42,19 +43,49 @@ fully_eliminated(@nospecialize args...; retval=(@__FILE__), kwargs...) =
4243fully_eliminated (src:: CodeInfo ; retval= (@__FILE__ )) = fully_eliminated (src. code; retval)
4344fully_eliminated (ir:: IRCode ; retval= (@__FILE__ )) = fully_eliminated (ir. stmts. inst; retval)
4445function 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
5755end
5856macro fully_eliminated (ex0... )
5957 return gen_call_with_extracted_types_and_kwargs (__module__, :fully_eliminated , ex0)
6058end
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