We recently added the ability to optimize a known cfunc with custom code
generation for it.
Previously we performed this lookup with a switch statement on the
address of the c function being called. This commit swaps that out for a
hash lookup on the method definition. For now I've kept this limited
this to cfuncs, but it wouldn't take significant changes to make this
work for other method types.
This implemenation is similar to how the interpreter keeps track of
which BOPs (basic operations) are redefined
This has a few advantages:
- Doesn't the C function's symbol to be exported (they're often static)
- This could support VM_METHOD_TYPE_OPTIMIZED in the future.
- This could support VM_METHOD_TYPE_ISEQ in the future. Kernel#class
would be a good candidate for this since to yjit it will just be a
constant push as we already know the class through BBV.
- Slightly cleaner to declare
- Less tightly coupled to each method's implementation
And a couple minor trade-offs:
- The looser coupling could be seen as a disadvantage (I don't think so,
- If a cfunc is defined multiple times we would need to declare it on
each definition. ex. BasicObject#== and BasicObject#equal?. This is
rare compared to using an alias.
This adds a method to blocks to get outgoing ids, then uses the outgoing
ids to generate a graphviz graph. Two methods were added to the Block
object. One method returns an id for the block, which is just the
address of the underlying block. The other method returns a list of
outgoing block ids. We can use Block#id in conjunction with
Block#outgoing_ids to construct a graph of blocks
As an optimization, multiple objects could share the same singleton
class. The optimization introduced in 6698e43393
wasn't handling this correctly so was generating guards that never pass
for the inputs we defer compilation to wait for. After generating
identical code multiple times and failing, the call site is falsely
recognized as megamorphic and it side exits. See disassembly for the
following before this commit:
def foo(obj)
obj.itself
end
o = Object.new.singleton_class
foo(o)
puts YJIT.disasm(method(:foo))
See also: comment in rb_singleton_class_clone_and_attach().
Singleton classes should only ever be attached to one object. This means
that checking for the object should be the same as checking for the
class. This should be slightly faster by avoiding one memory acccess as
well as allowing us to skip checking if the receiver is a heap object.
This will be most common for calling class methods.
jit_rb_obj_not() wants to access the type information of the receiver,
but we were discarding type info of locals before jit_rb_obj_not() runs
unncessarily.
There are also cases we are unncessarily discarding local type info. For
example, ivar reader and setter methods can never change local
variables.
This commit improves opt_not by making it correct when TrueClass#!
and/or FalseClass#! is defined and genearting better code when the
receiver is a heap object.
guard_known_class() can now handle true, false, and nil, and we
introduce a codegen function reimplementing rb_obj_not(), used when we
know we are calling into rb_obj_not().
Co-authored-by: Maxime Chevalier-Boisvert <maximechevalierb@gmail.com>
Co-authored-by: Noah Gibbs <the.codefolio.guy@gmail.com>
invokebuiltin_delegate can be run in place of
invokebuiltin_delegate_leave because there is always a leave instruction
afterwards (the interpreter takes advantage of this as well when
rewriting iseqs for tracing).
invokebuiltin_delegate is a special version of invokebuiltin used for
sending a contiguous subset of the current method's locals.
In some cases YJIT would already handle this for trivial cases it could
be inlined, implementing this OP allows it to work when the method isn't
inlinable (not marked as 'inline', does more than just call, not called
from yjit, etc).
This commit implements the topn instruction
Co-Authored-By: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>
Co-Authored-By: Noah Gibbs <noah.gibbs@shopify.com>
* Use builtin_inline_p to skip a frame of C methods
* Fix bugs in primitive cfunc call code
* Remove if (push_frame) {}
* Remove if (push_frame) {}
* Push Aaron's fix to avoid hardcoding insn lengths
Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
* Implement gen_newarray
* Implement newhash for n=0
* Add yjit tests for newhash/newarray
* Fix integer size warning on clang
* Save PC and SP in newhash and newarray
Co-authored-by: Maxime Chevalier-Boisvert <maximechevalierb@gmail.com>
Previously this could crash on Nokogiri when JITing the getivar
instruction because we would attempt to treat Nokogiri::XML::Document's
T_DATA as a T_OBJECT in calling rb_iv_index_tbl_lookup.
This commit also checks for T_OBJECT at compile time and emits the
rb_ivar_get fallback in that case.
Co-authored-by: HParker <HParker@github.com>
Co-authored-by: Dinah Shi <dinahshi@github.com>
Co-authored-by: HParker <HParker@github.com>
Co-authored-by: Dinah Shi <dinahshi@github.com>