Commit f01243db

f01243db55b3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 18 files · +16 added · ~2 modified

Description

Merge pull request #1361 from ryanseys/rs-iexec-core

analyze,codegen: inline-splice lowering for direct instance_exec/eval

Details

Commit
f01243db55b3
Parent
2f3d03c6f06d
Merge parent
0aea90f3dccc

spinel_analyze.rbmodifiedView file

@@ -3475,11 +3475,17 @@
34753475 return "obj_" + @cls_names[@ieval_class_idxs[n]]
34763476 end
34773477 end
3478- # iexec direct-path lift: the synthetic `__sp_iexec_<N>` function is
3479- # void-returning in the baseline, so its call resolves to "void".
3480- # The expression-position follow-up surfaces the real last-expression
3481- # type here via infer_iexec_body_return_types.
3478+ # iexec direct-path lift: a heap receiver is inlined at the call site
3479+ # and the call's value is the block's last expression, so type the call
3480+ # by that expression (simple ctypes only -- array / hash / poly bodies
3481+ # stay "void" and take the receiver-yielding comma-expr fallback, like
3482+ # the ieval arm above). Computed on the fly to avoid a per-lift ivar.
34823483 if is_iexec_call_name(mname) == 1
3484+ suffix = mname[11, mname.length - 11]
3485+ rt = iexec_call_value_type(suffix.to_i)
3486+ if ieval_return_type_is_simple(rt) == 1
3487+ return rt
3488+ end
34833489 return "void"
34843490 end
34853491
@@ -9321,6 +9327,14 @@
93219327 if ci < 0
93229328 return
93239329 end
9330+ # Override-aware intrinsic: if the receiver's class (or its parent
9331+ # chain) defines its own `instance_eval`, the user has shadowed the
9332+ # intrinsic. Skip the lift so ordinary dispatch resolves to the
9333+ # user's method, matching CRuby. A gate on the method-name string
9334+ # alone would silently bypass the override.
9335+ if cls_find_method(ci, "instance_eval") >= 0
9336+ return
9337+ end
93249338 body_id = @nd_body[blk]
93259339 # v1: bail if the block uses yield/block_given?. Lifting it as a
93269340 # plain function would lose the enclosing method's block plumbing.
@@ -9329,6 +9343,15 @@
93299343 if body_id >= 0 && body_has_yield(body_id) == 1
93309344 return
93319345 end
9346+ # Reject non-local control flow and runtime method definition inside
9347+ # the block -- return / break / next / def / define_method have no
9348+ # statically-translatable semantics once the body is lifted.
9349+ if body_id >= 0
9350+ kind = body_iexec_unsupported_kind(body_id)
9351+ if kind != ""
9352+ iexec_reject_kind("instance_eval", kind)
9353+ end
9354+ end
93329355 n = @ieval_counter
93339356 @ieval_counter = @ieval_counter + 1
93349357 @ieval_class_idxs.push(ci)
@@ -9349,6 +9372,105 @@
93499372 @nd_block[nid] = -1
93509373 end
93519374
9375+ # Emit a hard `Spinel: <intrinsic>: <reason>; <suggestion>` diagnostic
9376+ # and exit. `intrinsic` is the user-facing method name so the message
9377+ # points at the right call.
9378+ def iexec_reject(intrinsic, reason, suggestion)
9379+ msg = "Spinel: " + intrinsic + ": " + reason
9380+ if suggestion != ""
9381+ msg = msg + "; " + suggestion
9382+ end
9383+ $stderr.puts msg
9384+ exit(1)
9385+ end
9386+
9387+ # Rejection helper for the body_iexec_unsupported_kind enum. Maps the
9388+ # construct name to a reason + suggestion, then rejects.
9389+ def iexec_reject_kind(intrinsic, kind)
9390+ reason = ""
9391+ suggestion = ""
9392+ if kind == "return"
9393+ reason = "block uses 'return', which would return from the enclosing method"
9394+ suggestion = "remove the 'return' (the block's last expression is its value), or move the non-local exit outside the " + intrinsic + " block"
9395+ elsif kind == "break"
9396+ reason = "block uses 'break', which has no enclosing-iterator semantics under " + intrinsic
9397+ suggestion = "restructure to avoid non-local control flow"
9398+ elsif kind == "next"
9399+ reason = "block uses 'next', which has no enclosing-iterator semantics under " + intrinsic
9400+ suggestion = "restructure to avoid non-local control flow"
9401+ elsif kind == "def"
9402+ reason = "block contains a 'def', defining a method at runtime"
9403+ suggestion = "Spinel does not support runtime method definition; define the method statically on the receiver's class"
9404+ elsif kind == "define_method"
9405+ reason = "block calls 'define_method', defining a method at runtime"
9406+ suggestion = "Spinel does not support runtime method definition; define the method statically on the receiver's class"
9407+ else
9408+ reason = "block uses unsupported construct '" + kind + "'"
9409+ suggestion = "remove the construct or restructure the block body"
9410+ end
9411+ iexec_reject(intrinsic, reason, suggestion)
9412+ end
9413+
9414+ # Walks an instance_eval / instance_exec block body and returns the
9415+ # name of the first unsupported construct found (return / break /
9416+ # next / def / define_method), or "" when the body uses only
9417+ # constructs the lift can model. Nested DefNodes return "def" at the
9418+ # boundary rather than descending -- their bodies belong to a
9419+ # different scope and are checked when those methods compile.
9420+ def body_iexec_unsupported_kind(nid)
9421+ if nid < 0
9422+ return ""
9423+ end
9424+ t = @nd_type[nid]
9425+ if t == "ReturnNode"
9426+ return "return"
9427+ end
9428+ if t == "BreakNode"
9429+ return "break"
9430+ end
9431+ if t == "NextNode"
9432+ return "next"
9433+ end
9434+ if t == "DefNode"
9435+ return "def"
9436+ end
9437+ if t == "CallNode" && @nd_name[nid] == "define_method"
9438+ return "define_method"
9439+ end
9440+ cs = []
9441+ push_child_ids(nid, cs)
9442+ k = 0
9443+ while k < cs.length
9444+ r = body_iexec_unsupported_kind(cs[k])
9445+ if r != ""
9446+ return r
9447+ end
9448+ k = k + 1
9449+ end
9450+ ""
9451+ end
9452+
9453+ # A `recv.<intrinsic>(args)` with no literal block and no `&proc`
9454+ # argument is a no-block call. CRuby raises LocalJumpError at
9455+ # runtime; Spinel surfaces the same as a compile-time error. Returns
9456+ # 1 when the args carry a BlockArgumentNode (`&proc` form), so the
9457+ # caller can leave that case for the proc-arg path instead.
9458+ def iexec_args_have_block_arg(nid)
9459+ args_id = @nd_arguments[nid]
9460+ if args_id < 0
9461+ return 0
9462+ end
9463+ aids = get_args(args_id)
9464+ k = 0
9465+ while k < aids.length
9466+ if @nd_type[aids[k]] == "BlockArgumentNode"
9467+ return 1
9468+ end
9469+ k = k + 1
9470+ end
9471+ 0
9472+ end
9473+
93529474 # Direct-path lift for `recv.instance_exec(args) { |params| body }`.
93539475 # Sibling of ieval_rewrite_call -- detects an explicit-receiver
93549476 # instance_exec call with a block literal, validates the
@@ -9379,14 +9501,41 @@
93799501 end
93809502 recv = @nd_receiver[nid]
93819503 blk = @nd_block[nid]
9504+ # Receiverless `instance_exec(...)` is the trampoline-body shape
9505+ # (`def m(x, &b); instance_exec(x, &b); end`), handled by codegen's
9506+ # call-site inlining. Leave it for that path.
93829507 if recv < 0
93839508 return
93849509 end
93859510 if blk < 0
9511+ # An explicit-receiver call with no literal block: either a no-block
9512+ # call (CRuby raises LocalJumpError -- surface it at compile time)
9513+ # or a `&proc` form (left for the proc-arg path).
9514+ if iexec_args_have_block_arg(nid) == 0
9515+ iexec_reject(
9516+ "instance_exec",
9517+ "no block given",
9518+ "pass a literal block (`recv.instance_exec { ... }`)"
9519+ )
9520+ end
93869521 return
93879522 end
93889523 ci = recv_class_idx_for_rebind(recv, local_class)
93899524 if ci < 0
9525+ # AOT codegen needs the receiver's class to emit a typed
9526+ # `sp_iexec_<N>(sp_<C> *self, ...)` signature; a polymorphic /
9527+ # un-narrowed receiver can't be lifted without runtime dispatch.
9528+ iexec_reject(
9529+ "instance_exec",
9530+ "receiver type cannot be statically resolved",
9531+ "assign the receiver to a typed local first (e.g. `r = Foo.new`), or narrow via a class predicate (`raise unless r.is_a?(Foo)`) before the call"
9532+ )
9533+ end
9534+ # Override-aware intrinsic: bypass the lift when the receiver's
9535+ # class defines its own `instance_exec`; ordinary dispatch then
9536+ # resolves to the user's method, preserving CRuby semantics. Same
9537+ # shape as the override check in ieval_rewrite_call.
9538+ if cls_find_method(ci, "instance_exec") >= 0
93909539 return
93919540 end
93929541 body_id = @nd_body[blk]
@@ -9394,7 +9543,21 @@
93949543 # plumbing once we lift the body to a static function. Same
93959544 # gate as ieval_rewrite_call.
93969545 if body_id >= 0 && body_has_yield(body_id) == 1
9397- return
9546+ iexec_reject(
9547+ "instance_exec",
9548+ "block uses 'yield' or 'block_given?', which require an enclosing block-receiving method",
9549+ "restructure the inner logic to take a Proc parameter instead of yielding"
9550+ )
9551+ end
9552+ # Reject non-local control flow and runtime method definition --
9553+ # return / break / next have no enclosing-method semantics once the
9554+ # body is lifted to a static function, and def / define_method are
9555+ # runtime definitions Spinel does not model.
9556+ if body_id >= 0
9557+ kind = body_iexec_unsupported_kind(body_id)
9558+ if kind != ""
9559+ iexec_reject_kind("instance_exec", kind)
9560+ end
93989561 end
93999562
94009563 # Block param extraction (mirrors compile_yield_method_call_stmt
@@ -9622,6 +9785,45 @@
96229785 end
96239786 end
96249787
9788+ # On-the-fly last-expression type of a lifted iexec block, used to
9789+ # type a local assigned from `recv.instance_exec(...) { ... }`. Computed
9790+ # here rather than cached in a per-lift ivar so no new serialized
9791+ # compiler state is added. Mirrors infer_ieval_body_return_types' setup:
9792+ # rebind the class and declare the block params, then infer the body's
9793+ # last-expression type.
9794+ def iexec_call_value_type(n)
9795+ if n < 0 || n >= @iexec_body_ids.length
9796+ return "void"
9797+ end
9798+ bid = @iexec_body_ids[n]
9799+ if bid < 0
9800+ return "void"
9801+ end
9802+ ci = @iexec_class_idxs[n]
9803+ saved_ci = @current_class_idx
9804+ @current_class_idx = ci
9805+ push_scope
9806+ pnames_s = @iexec_block_pnames[n]
9807+ ptypes_s = @iexec_block_ptypes[n]
9808+ if pnames_s != ""
9809+ pnames = pnames_s.split("|")
9810+ ptypes = ptypes_s.split("|")
9811+ k = 0
9812+ while k < pnames.length
9813+ pt = "int"
9814+ if k < ptypes.length
9815+ pt = ptypes[k]
9816+ end
9817+ declare_var(pnames[k], pt)
9818+ k = k + 1
9819+ end
9820+ end
9821+ rt = infer_body_return(bid)
9822+ pop_scope
9823+ @current_class_idx = saved_ci
9824+ rt
9825+ end
9826+
96259827 def is_iexec_call_name(mname)
96269828 if mname.length <= 11
96279829 return 0