|
| 1 | +# Julia throw lowering |
| 2 | + |
| 3 | +"""Return `Some(x)` when `x` can be reconstructed without runtime values.""" |
| 4 | +function throw_constant(sci::StructuredIRCode, def_index, @nospecialize(x)) |
| 5 | + if x isa QuoteNode |
| 6 | + return Some(x.value) |
| 7 | + elseif x isa GlobalRef |
| 8 | + return IRStructurizer.const_value(sci, x) |
| 9 | + elseif x isa SSAValue |
| 10 | + inst = def(def_index, x) |
| 11 | + inst === nothing && return nothing |
| 12 | + call = resolve_call(inst.block, inst[:stmt]) |
| 13 | + call === nothing && return nothing |
| 14 | + func, args = call |
| 15 | + if func === Core.tuple |
| 16 | + vals = Any[] |
| 17 | + for arg in args |
| 18 | + val = throw_constant(sci, def_index, arg) |
| 19 | + val === nothing && return nothing |
| 20 | + push!(vals, something(val)) |
| 21 | + end |
| 22 | + return Some(Tuple(vals)) |
| 23 | + elseif func === Intrinsics.format_string |
| 24 | + vals = Any[] |
| 25 | + for arg in args |
| 26 | + val = throw_constant(sci, def_index, arg) |
| 27 | + val === nothing && return nothing |
| 28 | + push!(vals, something(val)) |
| 29 | + end |
| 30 | + return Some(string(vals...)) |
| 31 | + end |
| 32 | + return nothing |
| 33 | + elseif x isa Union{Argument, SlotNumber, BlockArgument, Expr} |
| 34 | + return nothing |
| 35 | + else |
| 36 | + return Some(x) |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +function thrown_type(block::Block, @nospecialize(exception)) |
| 41 | + if exception isa QuoteNode |
| 42 | + return typeof(exception.value) |
| 43 | + elseif !(exception isa Union{SSAValue, Argument, SlotNumber, BlockArgument, Expr, GlobalRef}) |
| 44 | + return typeof(exception) |
| 45 | + end |
| 46 | + T = value_type(block, exception) |
| 47 | + T === nothing && return Exception |
| 48 | + return CC.widenconst(T) |
| 49 | +end |
| 50 | + |
| 51 | +""" |
| 52 | +Best-effort reconstruction of an exception from its optimized constructor. |
| 53 | +Only constructors Julia marked removable are evaluated at compile time. |
| 54 | +""" |
| 55 | +function thrown_exception(sci::StructuredIRCode, def_index, block::Block, |
| 56 | + @nospecialize(exception)) |
| 57 | + direct = throw_constant(sci, def_index, exception) |
| 58 | + direct !== nothing && something(direct) isa Exception && return something(direct) |
| 59 | + exception isa SSAValue || return nothing |
| 60 | + |
| 61 | + inst = def(def_index, exception) |
| 62 | + inst === nothing && return nothing |
| 63 | + flag = inst[:flag] |
| 64 | + (flag & CC.IR_FLAGS_REMOVABLE) == CC.IR_FLAGS_REMOVABLE || return nothing |
| 65 | + |
| 66 | + stmt = inst[:stmt] |
| 67 | + T = thrown_type(block, exception) |
| 68 | + T isa Type && T <: Exception || return nothing |
| 69 | + |
| 70 | + args = if stmt isa Expr && stmt.head === :new |
| 71 | + stmt.args[2:end] |
| 72 | + else |
| 73 | + call = resolve_call(inst.block, stmt) |
| 74 | + call === nothing && return nothing |
| 75 | + func, call_args = call |
| 76 | + func === T || return nothing |
| 77 | + call_args |
| 78 | + end |
| 79 | + |
| 80 | + vals = Any[] |
| 81 | + for arg in args |
| 82 | + val = throw_constant(sci, def_index, arg) |
| 83 | + val === nothing && return nothing |
| 84 | + push!(vals, something(val)) |
| 85 | + end |
| 86 | + try |
| 87 | + return T(vals...) |
| 88 | + catch |
| 89 | + return nothing |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +function exception_message(sci::StructuredIRCode, def_index, block::Block, |
| 94 | + @nospecialize(exception)) |
| 95 | + if thrown_type(block, exception) === MethodError && exception isa SSAValue |
| 96 | + inst = def(def_index, exception) |
| 97 | + if inst !== nothing |
| 98 | + stmt = inst[:stmt] |
| 99 | + args = if stmt isa Expr && stmt.head === :new |
| 100 | + stmt.args[2:end] |
| 101 | + else |
| 102 | + call = resolve_call(inst.block, stmt) |
| 103 | + call === nothing ? Any[] : last(call) |
| 104 | + end |
| 105 | + if length(args) >= 2 && args[2] isa SSAValue |
| 106 | + tuple_inst = def(def_index, args[2]) |
| 107 | + tuple_call = tuple_inst === nothing ? nothing : |
| 108 | + resolve_call(tuple_inst.block, tuple_inst[:stmt]) |
| 109 | + if tuple_call !== nothing && first(tuple_call) === Core.tuple |
| 110 | + return method_error_message(sci, def_index, block, |
| 111 | + Any[args[1]; last(tuple_call)]) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + ex = thrown_exception(sci, def_index, block, exception) |
| 118 | + if ex !== nothing |
| 119 | + if hasfield(typeof(ex), :msg) && isdefined(ex, :msg) |
| 120 | + msg = getfield(ex, :msg) |
| 121 | + msg isa AbstractString && return String(msg) |
| 122 | + end |
| 123 | + return sprint(showerror, ex) |
| 124 | + end |
| 125 | + |
| 126 | + T = thrown_type(block, exception) |
| 127 | + return T isa Type && T <: Exception ? "$(T) was thrown" : "exception was thrown" |
| 128 | +end |
| 129 | + |
| 130 | +function method_error_message(sci::StructuredIRCode, def_index, block::Block, args) |
| 131 | + isempty(args) && return "Unsupported function call during Tile IR compilation" |
| 132 | + func = throw_constant(sci, def_index, args[1]) |
| 133 | + funcstr = func === nothing ? string(args[1]) : string(something(func)) |
| 134 | + argtypes = Any[value_type(block, arg) for arg in args[2:end]] |
| 135 | + typestr = isempty(argtypes) ? "" : " with argument types ($(join(argtypes, ", ")))" |
| 136 | + return "Unsupported function call during Tile IR compilation: $funcstr$typestr has no Tile IR equivalent" |
| 137 | +end |
| 138 | + |
| 139 | +""" |
| 140 | + lower_throws!(sci) |
| 141 | +
|
| 142 | +Canonicalize Julia throws before normal optimization and codegen. Throws nested |
| 143 | +in structured control flow remain runtime failures. A throw in the entry block |
| 144 | +is unavoidable after Julia's CFG simplification and becomes a collected |
| 145 | +compile-time diagnostic. |
| 146 | +""" |
| 147 | +function lower_throws!(sci::StructuredIRCode) |
| 148 | + def_index = defs(sci) |
| 149 | + for block in eachblock(sci) |
| 150 | + runtime = block !== sci.entry |
| 151 | + for inst in instructions(block) |
| 152 | + call = resolve_call(block, inst[:stmt]) |
| 153 | + call === nothing && continue |
| 154 | + func, args = call |
| 155 | + message = if func === throw |
| 156 | + length(args) == 1 || continue |
| 157 | + exception_message(sci, def_index, block, args[1]) |
| 158 | + elseif @static isdefined(Core, :throw_methoderror) && func === Core.throw_methoderror |
| 159 | + method_error_message(sci, def_index, block, args) |
| 160 | + else |
| 161 | + continue |
| 162 | + end |
| 163 | + inst[:stmt] = ThrowNode(message, runtime) |
| 164 | + inst[:flag] = CC.flags_for_effects(CC.EFFECTS_THROWS) |
| 165 | + end |
| 166 | + end |
| 167 | + return sci |
| 168 | +end |
0 commit comments