YJIT: Replace Array#each only when YJIT is enabled (#11955)

* YJIT: Replace Array#each only when YJIT is enabled

* Add comments about BUILTIN_ATTR_C_TRACE

* Make Ruby Array#each available with --yjit as well

* Fix all paths that expect a C location

* Use method_basic_definition_p to detect patches

* Copy a comment about C_TRACE flag to compilers

* Rephrase a comment about add_yjit_hook

* Give METHOD_ENTRY_BASIC flag to Array#each

* Add --yjit-c-builtin option

* Allow inconsistent source_location in test-spec

* Refactor a check of BUILTIN_ATTR_C_TRACE

* Set METHOD_ENTRY_BASIC without touching vm->running
This commit is contained in:
Takashi Kokubun 2024-11-04 08:14:28 -08:00 committed by GitHub
parent 51ac93011a
commit 478e0fc710
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
Notes: git 2024-11-04 16:14:48 +00:00
Merged-By: maximecb <maximecb@ruby-lang.org>
21 changed files with 261 additions and 61 deletions

View file

@ -1,49 +1,4 @@
class Array
# call-seq:
# each {|element| ... } -> self
# each -> new_enumerator
#
# With a block given, iterates over the elements of +self+,
# passing each element to the block;
# returns +self+:
#
# a = [:foo, 'bar', 2]
# a.each {|element| puts "#{element.class} #{element}" }
#
# Output:
#
# Symbol foo
# String bar
# Integer 2
#
# Allows the array to be modified during iteration:
#
# a = [:foo, 'bar', 2]
# a.each {|element| puts element; a.clear if element.to_s.start_with?('b') }
#
# Output:
#
# foo
# bar
#
# With no block given, returns a new Enumerator.
#
# Related: see {Methods for Iterating}[rdoc-ref:Array@Methods+for+Iterating].
def each
Primitive.attr! :inline_block
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
yield value
end
self
end
# call-seq:
# shuffle!(random: Random) -> self
#
@ -258,4 +213,24 @@ class Array
indexes.map! { |i| fetch(i, &block) }
indexes
end
with_yjit do
if Primitive.rb_builtin_basic_definition_p(:each)
undef :each
def each # :nodoc:
Primitive.attr! :inline_block, :c_trace
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
yield value
end
self
end
end
end
end