Skip to content

ZJIT: Disable ZJIT instructions when USE_ZJIT is 0 #13199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2025
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
4 changes: 3 additions & 1 deletion .github/workflows/compilers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with: { sparse-checkout-cone-mode: false, sparse-checkout: /.github }
- { uses: './.github/actions/setup/directories', with: { srcdir: 'src', builddir: 'build', makeup: true, fetch-depth: 10 } }
- { uses: './.github/actions/compilers', name: 'disable-jit', with: { append_configure: '--disable-yjit' } }
- { uses: './.github/actions/compilers', name: 'disable-jit', with: { append_configure: '--disable-yjit --disable-zjit' } }
- { uses: './.github/actions/compilers', name: 'disable-yjit', with: { append_configure: '--disable-yjit' } }
- { uses: './.github/actions/compilers', name: 'disable-zjit', with: { append_configure: '--disable-zjit' } }
- { uses: './.github/actions/compilers', name: 'disable-dln', with: { append_configure: '--disable-dln' } }
- { uses: './.github/actions/compilers', name: 'enable-mkmf-verbose', with: { append_configure: '--enable-mkmf-verbose' } }
- { uses: './.github/actions/compilers', name: 'disable-rubygems', with: { append_configure: '--disable-rubygems' } }
Expand Down
13 changes: 13 additions & 0 deletions test/ruby/test_zjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,19 @@ def fib(n)
}, call_threshold: 5, num_profiles: 3
end

# tool/ruby_vm/views/*.erb relies on the zjit instructions a) being contiguous and
# b) being reliably ordered after all the other instructions.
def test_instruction_order
insn_names = RubyVM::INSTRUCTION_NAMES
zjit, others = insn_names.map.with_index.partition { |name, _| name.start_with?('zjit_') }
zjit_indexes = zjit.map(&:last)
other_indexes = others.map(&:last)
zjit_indexes.product(other_indexes).each do |zjit_index, other_index|
assert zjit_index > other_index, "'#{insn_names[zjit_index]}' at #{zjit_index} "\
"must be defined after '#{insn_names[other_index]}' at #{other_index}"
end
end

private

# Assert that every method call in `test_script` can be compiled by ZJIT
Expand Down
25 changes: 17 additions & 8 deletions tool/ruby_vm/views/_comptime_insn_stack_increase.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%#
%
% stack_increase = proc do |i|
% if i.has_attribute?('sp_inc')
% '-127'
% else
% sprintf("%4d", i.rets.size - i.pops.size)
% end
% end
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
PUREFUNC(MAYBE_UNUSED(static int comptime_insn_stack_increase(int depth, int insn, const VALUE *opes)));
PUREFUNC(static rb_snum_t comptime_insn_stack_increase_dispatch(enum ruby_vminsn_type insn, const VALUE *opes));

rb_snum_t
comptime_insn_stack_increase_dispatch(enum ruby_vminsn_type insn, const VALUE *opes)
{
static const signed char t[] = {
% RubyVM::Instructions.each_slice 8 do |a|
<%= a.map { |i|
if i.has_attribute?('sp_inc')
'-127'
else
sprintf("%4d", i.rets.size - i.pops.size)
end
}.join(', ') -%>,
% insns.each_slice(8) do |row|
<%= row.map(&stack_increase).join(', ') -%>,
% end
#if USE_ZJIT
% zjit_insns.each_slice(8) do |row|
<%= row.map(&stack_increase).join(', ') -%>,
% end
#endif
};
signed char c = t[insn];

Expand Down
12 changes: 10 additions & 2 deletions tool/ruby_vm/views/_insn_len_info.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
%# granted, to either redistribute and/or modify this file, provided that the
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
CONSTFUNC(MAYBE_UNUSED(static int insn_len(VALUE insn)));

RUBY_SYMBOL_EXPORT_BEGIN /* for debuggers */
Expand All @@ -13,9 +16,14 @@ RUBY_SYMBOL_EXPORT_END

#ifdef RUBY_VM_INSNS_INFO
const uint8_t rb_vm_insn_len_info[] = {
% RubyVM::Instructions.each_slice 23 do |a|
<%= a.map(&:width).join(', ') -%>,
% insns.each_slice(23) do |row|
<%= row.map(&:width).join(', ') -%>,
% end
#if USE_ZJIT
% zjit_insns.each_slice(23) do |row|
<%= row.map(&:width).join(', ') -%>,
% end
#endif
};

ASSERT_VM_INSTRUCTION_SIZE(rb_vm_insn_len_info);
Expand Down
32 changes: 23 additions & 9 deletions tool/ruby_vm/views/_insn_name_info.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%
% a = RubyVM::Instructions.map {|i| i.name }
% b = (0...a.size)
% c = a.inject([0]) {|r, i| r << (r[-1] + i.length + 1) }
% c.pop
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
% next_offset = 0
% name_offset = proc do |i|
% offset = sprintf("%4d", next_offset)
% next_offset += i.name.length + 1 # insn.name + \0
% offset
% end
%
CONSTFUNC(MAYBE_UNUSED(static const char *insn_name(VALUE insn)));

Expand All @@ -20,18 +24,28 @@ extern const unsigned short rb_vm_insn_name_offset[VM_INSTRUCTION_SIZE];
RUBY_SYMBOL_EXPORT_END

#ifdef RUBY_VM_INSNS_INFO
const int rb_vm_max_insn_name_size = <%= a.map(&:size).max %>;
const int rb_vm_max_insn_name_size = <%= RubyVM::Instructions.map { |i| i.name.size }.max %>;

const char rb_vm_insn_name_base[] =
% a.each do |i|
<%=cstr i%> "\0"
% insns.each do |i|
<%= cstr i.name %> "\0"
% end
#if USE_ZJIT
% zjit_insns.each do |i|
<%= cstr i.name %> "\0"
% end
#endif
;

const unsigned short rb_vm_insn_name_offset[] = {
% c.each_slice 12 do |d|
<%= d.map {|i| sprintf("%4d", i) }.join(', ') %>,
% insns.each_slice(12) do |row|
<%= row.map(&name_offset).join(', ') %>,
% end
#if USE_ZJIT
% zjit_insns.each_slice(12) do |row|
<%= row.map(&name_offset).join(', ') %>,
% end
#endif
};

ASSERT_VM_INSTRUCTION_SIZE(rb_vm_insn_name_offset);
Expand Down
32 changes: 24 additions & 8 deletions tool/ruby_vm/views/_insn_operand_info.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%
% a = RubyVM::Instructions.map {|i| i.operands_info }
% b = (0...a.size)
% c = a.inject([0]) {|r, i| r << (r[-1] + i.length + 1) }
% c.pop
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
% operands_info = proc { |i| sprintf("%-6s", cstr(i.operands_info)) }
%
% next_offset = 0
% op_offset = proc do |i|
% offset = sprintf("%3d", next_offset)
% next_offset += i.operands_info.length + 1 # insn.operands_info + \0
% offset
% end
%
CONSTFUNC(MAYBE_UNUSED(static const char *insn_op_types(VALUE insn)));
CONSTFUNC(MAYBE_UNUSED(static int insn_op_type(VALUE insn, long pos)));
Expand All @@ -21,15 +27,25 @@ RUBY_SYMBOL_EXPORT_END

#ifdef RUBY_VM_INSNS_INFO
const char rb_vm_insn_op_base[] =
% a.each_slice 5 do |d|
<%= d.map {|i| sprintf("%-6s", cstr(i)) }.join(' "\0" ') %> "\0"
% insns.each_slice(5) do |row|
<%= row.map(&operands_info).join(' "\0" ') %> "\0"
% end
#if USE_ZJIT
% zjit_insns.each_slice(5) do |row|
<%= row.map(&operands_info).join(' "\0" ') %> "\0"
% end
#endif
;

const unsigned short rb_vm_insn_op_offset[] = {
% c.each_slice 12 do |d|
<%= d.map {|i| sprintf("%3d", i) }.join(', ') %>,
% insns.each_slice(12) do |row|
<%= row.map(&op_offset).join(', ') %>,
% end
#if USE_ZJIT
% zjit_insns.each_slice(12) do |row|
<%= row.map(&op_offset).join(', ') %>,
% end
#endif
};

ASSERT_VM_INSTRUCTION_SIZE(rb_vm_insn_op_offset);
Expand Down
4 changes: 4 additions & 0 deletions tool/ruby_vm/views/_zjit_helpers.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#if USE_ZJIT

MAYBE_UNUSED(static int vm_bare_insn_to_zjit_insn(int insn));
static int
vm_bare_insn_to_zjit_insn(int insn)
Expand Down Expand Up @@ -25,3 +27,5 @@ vm_zjit_insn_to_bare_insn(int insn)
return insn;
}
}

#endif
4 changes: 4 additions & 0 deletions tool/ruby_vm/views/_zjit_instruction.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#if USE_ZJIT

/* insn <%= insn.pretty_name %> */
INSN_ENTRY(<%= insn.name %>)
{
Expand All @@ -6,3 +8,5 @@ INSN_ENTRY(<%= insn.name %>)
DISPATCH_ORIGINAL_INSN(<%= insn.jump_destination %>);
END_INSN(<%= insn.name %>);
}

#endif
10 changes: 9 additions & 1 deletion tool/ruby_vm/views/insns.inc.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
%# granted, to either redistribute and/or modify this file, provided that the
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
<%= render 'copyright' %>
<%= render 'notice', locals: {
this_file: 'contains YARV instruction list',
Expand All @@ -19,9 +22,14 @@
#define BIN(n) YARVINSN_##n

enum ruby_vminsn_type {
% RubyVM::Instructions.each do |i|
% insns.each do |i|
<%= i.bin %>,
% end
#if USE_ZJIT
% zjit_insns.each do |i|
<%= i.bin %>,
% end
#endif
VM_INSTRUCTION_SIZE
};

Expand Down
5 changes: 1 addition & 4 deletions tool/ruby_vm/views/optunifs.inc.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
% raise ':FIXME:TBW' if RubyVM::VmOptsH['INSTRUCTIONS_UNIFICATION']
% n = RubyVM::Instructions.size
<%= render 'copyright' %>
<%= render 'notice', locals: {
this_file: 'is for threaded code',
Expand All @@ -16,6 +15,4 @@

/* Let .bss section automatically initialize this variable */
/* cf. Section 6.7.8 of ISO/IEC 9899:1999 */
static const int *const *const unified_insns_data[<%= n %>];

ASSERT_VM_INSTRUCTION_SIZE(unified_insns_data);
static const int *const *const unified_insns_data[VM_INSTRUCTION_SIZE];
10 changes: 9 additions & 1 deletion tool/ruby_vm/views/vmtc.inc.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
%# granted, to either redistribute and/or modify this file, provided that the
%# conditions mentioned in the file COPYING are met. Consult the file for
%# details.
%
% zjit_insns, insns = RubyVM::Instructions.partition { |i| i.name.start_with?('zjit_') }
%
<%= render 'copyright' -%>
<%= render 'notice', locals: {
this_file: 'is for threaded code',
edit: __FILE__,
} -%>

static const void *const insns_address_table[] = {
% RubyVM::Instructions.each do |i|
% insns.each do |i|
LABEL_PTR(<%= i.name %>),
% end
#if USE_ZJIT
% zjit_insns.each do |i|
LABEL_PTR(<%= i.name %>),
% end
#endif
};

ASSERT_VM_INSTRUCTION_SIZE(insns_address_table);
14 changes: 1 addition & 13 deletions yjit/src/cruby_bindings.inc.rs

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

Loading