Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/strscan/strscan.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Some examples here assume that certain helper methods are defined:
- `match_values_cleared?(scanner)`:
Returns whether the scanner's [match values][9] are cleared.

See examples at [helper methods](doc/strscan/helper_methods.md).
See examples at [helper methods](helper_methods.md).

## The `StringScanner` \Object

Expand Down
1 change: 1 addition & 0 deletions enc/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BUILTIN_ENCS = enc/ascii.c enc/us_ascii.c\
enc/unicode.c enc/utf_8.c

BUILTIN_TRANSES = enc/trans/newline.trans
BUILTIN_TRANS_CSRCS = $(BUILTIN_TRANSES:.trans=.c)

RUBY_SO_NAME = @RUBY_SO_NAME@
LIBRUBY = @LIBRUBY@
Expand Down
6 changes: 3 additions & 3 deletions enc/depend
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ clean:
% end
% unless inplace
$(Q)$(RM) enc/unicode/*/casefold.h enc/unicode/*/name2ctype.h
$(Q)$(RM) enc/jis/props.h
-$(Q)$(RMDIR) enc/unicode<%=ignore_error%>
$(Q)$(RM) enc/trans/newline.c enc/jis/props.h
-$(Q)$(RMDIR) enc/trnas enc/unicode<%=ignore_error%>
% end
% workdirs.reverse_each do|d|
-$(Q)$(RMDIR) <%=pathrep[d]%><%=ignore_error%>
% end

clean-srcs:
$(Q)$(RM) <%=pathrep['$(TRANSCSRCS)']%>
$(Q)$(RM) <%=pathrep['$(TRANSCSRCS)']%> <%=pathrep['$(BUILTIN_TRANS_CSRCS)']%>
-$(Q)$(RMDIR) <%=pathrep['enc/trans']%><%=ignore_error%>
$(Q)$(RM) enc/unicode/*/casefold.h enc/unicode/*/name2ctype.h
$(Q)$(RM) enc/jis/props.h
Expand Down
4 changes: 3 additions & 1 deletion gc/default/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -6197,7 +6197,7 @@ rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj)
struct rb_gc_object_metadata_names {
// Must be ID only
ID ID_wb_protected, ID_age, ID_old, ID_uncollectible, ID_marking,
ID_marked, ID_pinned, ID_object_id, ID_shareable;
ID_marked, ID_pinned, ID_remembered, ID_object_id, ID_shareable;
};

#define RB_GC_OBJECT_METADATA_ENTRY_COUNT (sizeof(struct rb_gc_object_metadata_names) / sizeof(ID))
Expand All @@ -6219,6 +6219,7 @@ rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj)
I(marking);
I(marked);
I(pinned);
I(remembered);
I(object_id);
I(shareable);
#undef I
Expand All @@ -6238,6 +6239,7 @@ rb_gc_impl_object_metadata(void *objspace_ptr, VALUE obj)
if (RVALUE_MARKING(objspace, obj)) SET_ENTRY(marking, Qtrue);
if (RVALUE_MARKED(objspace, obj)) SET_ENTRY(marked, Qtrue);
if (RVALUE_PINNED(objspace, obj)) SET_ENTRY(pinned, Qtrue);
if (RVALUE_REMEMBERED(objspace, obj)) SET_ENTRY(remembered, Qtrue);
if (rb_obj_id_p(obj)) SET_ENTRY(object_id, rb_obj_id(obj));
if (FL_TEST(obj, FL_SHAREABLE)) SET_ENTRY(shareable, Qtrue);

Expand Down
1 change: 1 addition & 0 deletions internal/basic_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum ruby_basic_operators {
BOP_SUCC,
BOP_GT,
BOP_GE,
BOP_GTGT,
BOP_NOT,
BOP_NEQ,
BOP_MATCH,
Expand Down
2 changes: 2 additions & 0 deletions jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,5 @@ rb_yarv_str_eql_internal(VALUE str1, VALUE str2)
// We wrap this since it's static inline
return rb_str_eql_internal(str1, str2);
}

void rb_jit_str_concat_codepoint(VALUE str, VALUE codepoint);
4 changes: 2 additions & 2 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -12580,9 +12580,9 @@ rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc)
return rb_enc_interned_str(ptr, strlen(ptr), enc);
}

#if USE_YJIT
#if USE_YJIT || USE_ZJIT
void
rb_yjit_str_concat_codepoint(VALUE str, VALUE codepoint)
rb_jit_str_concat_codepoint(VALUE str, VALUE codepoint)
{
if (RB_LIKELY(ENCODING_GET_INLINED(str) == rb_ascii8bit_encindex())) {
ssize_t code = RB_NUM2SSIZE(codepoint);
Expand Down
30 changes: 30 additions & 0 deletions test/ruby/test_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,34 @@ def test_method_table_assignment_just_after_class_init
class C; end
end;
end

def test_singleton_cc_invalidation
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}")
begin;
class T
def hi
"hi"
end
end

t = T.new
t.singleton_class

def hello(t)
t.hi
end

5.times do
hello(t) # populate inline cache on `t.singleton_class`.
end

class T
remove_method :hi # invalidate `t.singleton_class` ccs for `hi`
end

assert_raise NoMethodError do
hello(t)
end
end;
end
end
30 changes: 29 additions & 1 deletion test/ruby/test_zjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,35 @@ def test
test

Ractor.new { test }.value
}
}, call_threshold: 2
end

def test_ivar_get_with_already_multi_ractor_mode
assert_compiles '42', %q{
class Foo
def self.set_bar
@bar = [] # needs to be a ractor unshareable object
end

def self.bar
@bar
rescue Ractor::IsolationError
42
end
end

Foo.set_bar
r = Ractor.new {
Ractor.receive
Foo.bar
}

Foo.bar
Foo.bar

r << :go
r.value
}, call_threshold: 2
end

def test_ivar_set_with_multi_ractor_mode
Expand Down
1 change: 1 addition & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,7 @@ vm_init_redefined_flag(void)
OP(GT, GT), (C(Integer), C(Float));
OP(GE, GE), (C(Integer), C(Float));
OP(LTLT, LTLT), (C(String), C(Array));
OP(GTGT, GTGT), (C(Integer));
OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
OP(ASET, ASET), (C(Array), C(Hash));
OP(Length, LENGTH), (C(Array), C(String), C(Hash));
Expand Down
2 changes: 1 addition & 1 deletion yjit/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn main() {
.allowlist_function("rb_yjit_sendish_sp_pops")
.allowlist_function("rb_yjit_invokeblock_sp_pops")
.allowlist_function("rb_yjit_set_exception_return")
.allowlist_function("rb_yjit_str_concat_codepoint")
.allowlist_function("rb_jit_str_concat_codepoint")
.allowlist_type("rstring_offsets")
.allowlist_function("rb_assert_holding_vm_lock")
.allowlist_function("rb_jit_shape_too_complex_p")
Expand Down
2 changes: 1 addition & 1 deletion yjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6244,7 +6244,7 @@ fn jit_rb_str_concat_codepoint(

guard_object_is_fixnum(jit, asm, codepoint, StackOpnd(0));

asm.ccall(rb_yjit_str_concat_codepoint as *const u8, vec![recv, codepoint]);
asm.ccall(rb_jit_str_concat_codepoint as *const u8, vec![recv, codepoint]);

// The receiver is the return value, so we only need to pop the codepoint argument off the stack.
// We can reuse the receiver slot in the stack as the return value.
Expand Down
1 change: 0 additions & 1 deletion yjit/src/cruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ extern "C" {
pub fn rb_float_new(d: f64) -> VALUE;

pub fn rb_hash_empty_p(hash: VALUE) -> VALUE;
pub fn rb_yjit_str_concat_codepoint(str: VALUE, codepoint: VALUE);
pub fn rb_str_setbyte(str: VALUE, index: VALUE, value: VALUE) -> VALUE;
pub fn rb_vm_splat_array(flag: VALUE, ary: VALUE) -> VALUE;
pub fn rb_vm_concat_array(ary1: VALUE, ary2st: VALUE) -> VALUE;
Expand Down
34 changes: 18 additions & 16 deletions yjit/src/cruby_bindings.inc.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions zjit/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn main() {
.allowlist_function("rb_jit_get_page_size")
.allowlist_function("rb_jit_array_len")
.allowlist_function("rb_jit_iseq_builtin_attrs")
.allowlist_function("rb_jit_str_concat_codepoint")
.allowlist_function("rb_zjit_iseq_inspect")
.allowlist_function("rb_zjit_iseq_insn_set")
.allowlist_function("rb_zjit_local_id")
Expand Down Expand Up @@ -325,6 +326,7 @@ fn main() {
.allowlist_function("rb_attr_get")
.allowlist_function("rb_ivar_defined")
.allowlist_function("rb_ivar_get")
.allowlist_function("rb_ivar_get_at_no_ractor_check")
.allowlist_function("rb_ivar_set")
.allowlist_function("rb_mod_name")
.allowlist_var("rb_vm_insn_count")
Expand Down
19 changes: 18 additions & 1 deletion zjit/src/backend/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ impl Assembler {
asm.store(mem_out, SCRATCH0_OPND);
}
}
Insn::Lea { out, .. } => {
Insn::Lea { opnd, out } => {
*opnd = split_stack_membase(asm, *opnd, SCRATCH0_OPND, &stack_state);
let mem_out = split_memory_write(out, SCRATCH0_OPND);
asm.push_insn(insn);
if let Some(mem_out) = mem_out {
Expand Down Expand Up @@ -1804,4 +1805,20 @@ mod tests {
");
assert_snapshot!(cb.hexdump(), @"4c8b55f84c8b5df04d8b5b024d0f441a4c895df8");
}

#[test]
fn test_lea_split_memory_read() {
let (mut asm, mut cb) = setup_asm();

let opnd = Opnd::Mem(Mem { base: MemBase::Stack { stack_idx: 0, num_bits: 64 }, disp: 0, num_bits: 64 });
let _ = asm.lea(opnd);
asm.compile_with_num_regs(&mut cb, 0);

assert_disasm_snapshot!(cb.disasm(), @"
0x0: mov r11, qword ptr [rbp - 8]
0x4: lea r11, [r11]
0x7: mov qword ptr [rbp - 8], r11
");
assert_snapshot!(cb.hexdump(), @"4c8b5df84d8d1b4c895df8");
}
}
59 changes: 55 additions & 4 deletions zjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
&Insn::Const { val: Const::Value(val) } => gen_const_value(val),
&Insn::Const { val: Const::CPtr(val) } => gen_const_cptr(val),
&Insn::Const { val: Const::CInt64(val) } => gen_const_long(val),
&Insn::Const { val: Const::CUInt16(val) } => gen_const_uint16(val),
Insn::Const { .. } => panic!("Unexpected Const in gen_insn: {insn}"),
Insn::NewArray { elements, state } => gen_new_array(asm, opnds!(elements), &function.frame_state(*state)),
Insn::NewHash { elements, state } => gen_new_hash(jit, asm, opnds!(elements), &function.frame_state(*state)),
Expand All @@ -365,9 +366,10 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
// If it happens we abort the compilation for now
Insn::StringConcat { strings, state, .. } if strings.is_empty() => return Err(*state),
Insn::StringConcat { strings, state } => gen_string_concat(jit, asm, opnds!(strings), &function.frame_state(*state)),
&Insn::StringGetbyteFixnum { string, index } => gen_string_getbyte_fixnum(asm, opnd!(string), opnd!(index)),
&Insn::StringGetbyte { string, index } => gen_string_getbyte(asm, opnd!(string), opnd!(index)),
Insn::StringSetbyteFixnum { string, index, value } => gen_string_setbyte_fixnum(asm, opnd!(string), opnd!(index), opnd!(value)),
Insn::StringAppend { recv, other, state } => gen_string_append(jit, asm, opnd!(recv), opnd!(other), &function.frame_state(*state)),
Insn::StringAppendCodepoint { recv, other, state } => gen_string_append_codepoint(jit, asm, opnd!(recv), opnd!(other), &function.frame_state(*state)),
Insn::StringIntern { val, state } => gen_intern(asm, opnd!(val), &function.frame_state(*state)),
Insn::ToRegexp { opt, values, state } => gen_toregexp(jit, asm, *opt, opnds!(values), &function.frame_state(*state)),
Insn::Param => unreachable!("block.insns should not have Insn::Param"),
Expand Down Expand Up @@ -408,6 +410,12 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
let shift_amount = function.type_of(right).fixnum_value().unwrap() as u64;
gen_fixnum_lshift(jit, asm, opnd!(left), shift_amount, &function.frame_state(state))
}
&Insn::FixnumRShift { left, right } => {
// We only create FixnumRShift when we know the shift amount statically and it's in [0,
// 63].
let shift_amount = function.type_of(right).fixnum_value().unwrap() as u64;
gen_fixnum_rshift(asm, opnd!(left), shift_amount)
}
&Insn::FixnumMod { left, right, state } => gen_fixnum_mod(jit, asm, opnd!(left), opnd!(right), &function.frame_state(state)),
Insn::IsNil { val } => gen_isnil(asm, opnd!(val)),
&Insn::IsMethodCfunc { val, cd, cfunc, state: _ } => gen_is_method_cfunc(jit, asm, opnd!(val), cd, cfunc),
Expand Down Expand Up @@ -1147,6 +1155,10 @@ fn gen_const_long(val: i64) -> lir::Opnd {
Opnd::Imm(val)
}

fn gen_const_uint16(val: u16) -> lir::Opnd {
Opnd::UImm(val as u64)
}

/// Compile a basic block argument
fn gen_param(asm: &mut Assembler, idx: usize) -> lir::Opnd {
// Allocate a register or a stack slot
Expand Down Expand Up @@ -1753,6 +1765,15 @@ fn gen_fixnum_lshift(jit: &mut JITState, asm: &mut Assembler, left: lir::Opnd, s
out_val
}

/// Compile Fixnum >> Fixnum
fn gen_fixnum_rshift(asm: &mut Assembler, left: lir::Opnd, shift_amount: u64) -> lir::Opnd {
// Shift amount is known statically to be in the range [0, 63]
assert!(shift_amount < 64);
let result = asm.rshift(left, shift_amount.into());
// Re-tag the output value
asm.or(result, 1.into())
}

fn gen_fixnum_mod(jit: &mut JITState, asm: &mut Assembler, left: lir::Opnd, right: lir::Opnd, state: &FrameState) -> lir::Opnd {
// Check for left % 0, which raises ZeroDivisionError
asm.cmp(right, Opnd::from(VALUE::fixnum_from_usize(0)));
Expand Down Expand Up @@ -2480,9 +2501,34 @@ fn gen_string_concat(jit: &mut JITState, asm: &mut Assembler, strings: Vec<Opnd>
result
}

fn gen_string_getbyte_fixnum(asm: &mut Assembler, string: Opnd, index: Opnd) -> Opnd {
// TODO(max): Open-code rb_str_getbyte to avoid a call
asm_ccall!(asm, rb_str_getbyte, string, index)
// Generate RSTRING_PTR
fn get_string_ptr(asm: &mut Assembler, string: Opnd) -> Opnd {
asm_comment!(asm, "get string pointer for embedded or heap");
let string = asm.load(string);
let flags = Opnd::mem(VALUE_BITS, string, RUBY_OFFSET_RBASIC_FLAGS);
asm.test(flags, (RSTRING_NOEMBED as u64).into());
let heap_ptr = asm.load(Opnd::mem(
usize::BITS as u8,
string,
RUBY_OFFSET_RSTRING_AS_HEAP_PTR,
));
// Load the address of the embedded array
// (struct RString *)(obj)->as.ary
let ary = asm.lea(Opnd::mem(VALUE_BITS, string, RUBY_OFFSET_RSTRING_AS_ARY));
asm.csel_nz(heap_ptr, ary)
}

fn gen_string_getbyte(asm: &mut Assembler, string: Opnd, index: Opnd) -> Opnd {
let string_ptr = get_string_ptr(asm, string);
// TODO(max): Use SIB indexing here once the backend supports it
let string_ptr = asm.add(string_ptr, index);
let byte = asm.load(Opnd::mem(8, string_ptr, 0));
// Zero-extend the byte to 64 bits
let byte = byte.with_num_bits(64);
let byte = asm.and(byte, 0xFF.into());
// Tag the byte
let byte = asm.lshift(byte, Opnd::UImm(1));
asm.or(byte, Opnd::UImm(1))
}

fn gen_string_setbyte_fixnum(asm: &mut Assembler, string: Opnd, index: Opnd, value: Opnd) -> Opnd {
Expand All @@ -2495,6 +2541,11 @@ fn gen_string_append(jit: &mut JITState, asm: &mut Assembler, string: Opnd, val:
asm_ccall!(asm, rb_str_buf_append, string, val)
}

fn gen_string_append_codepoint(jit: &mut JITState, asm: &mut Assembler, string: Opnd, val: Opnd, state: &FrameState) -> Opnd {
gen_prepare_non_leaf_call(jit, asm, state);
asm_ccall!(asm, rb_jit_str_concat_codepoint, string, val)
}

/// Generate a JIT entry that just increments exit_compilation_failure and exits
fn gen_compile_error_counter(cb: &mut CodeBlock, compile_error: &CompileError) -> Result<CodePtr, CompileError> {
let mut asm = Assembler::new();
Expand Down
Loading