|
| 1 | +module MiniCassette |
| 2 | + # A minimal demonstration of the cassette mechanism. Doesn't support all the |
| 3 | + # fancy features, but sufficient to exercise this code path in the compiler. |
| 4 | + |
| 5 | + using Core.Compiler: method_instances, retrieve_code_info, CodeInfo, |
| 6 | + MethodInstance, SSAValue, GotoNode, Slot, SlotNumber, quoted, |
| 7 | + signature_type |
| 8 | + using Base: _methods_by_ftype |
| 9 | + using Base.Meta: isexpr |
| 10 | + using Test |
| 11 | + |
| 12 | + export Ctx, overdub |
| 13 | + |
| 14 | + struct Ctx; end |
| 15 | + |
| 16 | + # A no-op cassette-like transform |
| 17 | + function transform_expr(expr, map_slot_number, map_ssa_value, sparams) |
| 18 | + transform(expr) = transform_expr(expr, map_slot_number, map_ssa_value, sparams) |
| 19 | + if isexpr(expr, :call) |
| 20 | + return Expr(:call, overdub, SlotNumber(2), map(transform, expr.args)...) |
| 21 | + elseif isexpr(expr, :gotoifnot) |
| 22 | + return Expr(:gotoifnot, transform(expr.args[1]), map_ssa_value(SSAValue(expr.args[2])).id) |
| 23 | + elseif isexpr(expr, :static_parameter) |
| 24 | + return quoted(sparams[expr.args[1]]) |
| 25 | + elseif isa(expr, Expr) |
| 26 | + return Expr(expr.head, map(transform, expr.args)...) |
| 27 | + elseif isa(expr, GotoNode) |
| 28 | + return GotoNode(map_ssa_value(SSAValue(expr.label)).id) |
| 29 | + elseif isa(expr, Slot) |
| 30 | + return map_slot_number(expr.id) |
| 31 | + elseif isa(expr, SSAValue) |
| 32 | + return map_ssa_value(expr) |
| 33 | + else |
| 34 | + return expr |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + function transform!(ci, nargs, sparams) |
| 39 | + code = ci.code |
| 40 | + ci.slotnames = Symbol[Symbol("#self#"), :ctx, :f, :args, ci.slotnames[nargs+1:end]...] |
| 41 | + ci.slotflags = UInt8[(0x00 for i = 1:4)..., ci.slotflags[nargs+1:end]...] |
| 42 | + # Insert one SSAValue for every argument statement |
| 43 | + prepend!(code, [Expr(:call, getfield, SlotNumber(4), i) for i = 1:nargs]) |
| 44 | + prepend!(ci.codelocs, [0 for i = 1:nargs]) |
| 45 | + ci.ssavaluetypes += nargs |
| 46 | + function map_slot_number(slot) |
| 47 | + if slot == 1 |
| 48 | + # self in the original function is now `f` |
| 49 | + return SlotNumber(3) |
| 50 | + elseif 2 <= slot <= nargs + 1 |
| 51 | + # Arguments get inserted as ssa values at the top of the function |
| 52 | + return SSAValue(slot - 1) |
| 53 | + else |
| 54 | + # The first non-argument slot will be 5 |
| 55 | + return SlotNumber(slot - (nargs + 1) + 4) |
| 56 | + end |
| 57 | + end |
| 58 | + map_ssa_value(ssa::SSAValue) = SSAValue(ssa.id + nargs) |
| 59 | + for i = (nargs+1:length(code)) |
| 60 | + code[i] = transform_expr(code[i], map_slot_number, map_ssa_value, sparams) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + function overdub_generator(self, c, f, args) |
| 65 | + if f <: Core.Builtin || !isdefined(f, :instance) |
| 66 | + return :(return f(args...)) |
| 67 | + end |
| 68 | + |
| 69 | + tt = Tuple{f, args...} |
| 70 | + mthds = _methods_by_ftype(tt, -1, typemax(UInt)) |
| 71 | + @assert length(mthds) == 1 |
| 72 | + mtypes, msp, m = mthds[1] |
| 73 | + mi = ccall(:jl_specializations_get_linfo, Ref{MethodInstance}, (Any, Any, Any), m, mtypes, msp) |
| 74 | + # Unsupported in this mini-cassette |
| 75 | + @assert !mi.def.isva |
| 76 | + code_info = retrieve_code_info(mi) |
| 77 | + @assert isa(code_info, CodeInfo) |
| 78 | + code_info = copy(code_info) |
| 79 | + if isdefined(code_info, :edges) |
| 80 | + code_info.edges = MethodInstance[mi] |
| 81 | + end |
| 82 | + transform!(code_info, length(args), msp) |
| 83 | + code_info |
| 84 | + end |
| 85 | + |
| 86 | + @eval function overdub(c::Ctx, f, args...) |
| 87 | + $(Expr(:meta, :generated_only)) |
| 88 | + $(Expr(:meta, |
| 89 | + :generated, |
| 90 | + Expr(:new, |
| 91 | + Core.GeneratedFunctionStub, |
| 92 | + :overdub_generator, |
| 93 | + Any[:overdub, :ctx, :f, :args], |
| 94 | + Any[], |
| 95 | + @__LINE__, |
| 96 | + QuoteNode(Symbol(@__FILE__)), |
| 97 | + true))) |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +using .MiniCassette |
| 102 | + |
| 103 | +# Test #265 for Cassette |
| 104 | +f() = 1 |
| 105 | +@test overdub(Ctx(), f) === 1 |
| 106 | +f() = 2 |
| 107 | +@test overdub(Ctx(), f) === 2 |
| 108 | + |
| 109 | +# Test that MiniCassette is at least somewhat capable by overdubbing gcd |
| 110 | +@test overdub(Ctx(), gcd, 10, 20) === gcd(10, 20) |
0 commit comments