8000 ZJIT: Add `insns` keyword to test that a certain opcode is present by XrXr · Pull Request #13470 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

ZJIT: Add insns keyword to test that a certain opcode is present #13470

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 3 commits into from
Jun 5, 2025
Merged
Changes from 1 commit
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
Next Next commit
ZJIT: Add insns keyword to test that a certain opcode is present
  • Loading branch information
XrXr committed May 29, 2025
commit 89e8ab12a50ed9a471ee796c9b8766a877c2784f
47 changes: 34 additions & 13 deletions test/ruby/test_zjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_opt_eq
def test(a, b) = a == b
test(0, 2) # profile opt_eq
[test(1, 1), test(0, 1)]
}, call_threshold: 2
}, insns: [:opt_eq], call_threshold: 2
end

def test_opt_neq_dynamic
Expand All @@ -144,7 +144,7 @@ def test_opt_neq_dynamic
def test(a, b) = a != b
test(0, 2) # profile opt_neq
[test(1, 1), test(0, 1)]
}, call_threshold: 1
}, insns: [:opt_neq], call_threshold: 1
end

def test_opt_neq_fixnum
Expand All @@ -160,46 +160,46 @@ def test_opt_lt
def test(a, b) = a < b
test(2, 3) # profile opt_lt
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
}, insns: [:opt_lt], call_threshold: 2
end

def test_opt_lt_with_literal_lhs
assert_compiles '[false, false, true]', %q{
def test(n) = 2 < n
test(2) # profile opt_lt
[test(1), test(2), test(3)]
}, call_threshold: 2
}, insns: [:opt_lt], call_threshold: 2
end

def test_opt_le
assert_compiles '[true, true, false]', %q{
def test(a, b) = a <= b
test(2, 3) # profile opt_le
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
}, insns: [:opt_le], call_threshold: 2
end

def test_opt_gt
assert_compiles '[false, false, true]', %q{
def test(a, b) = a > b
test(2, 3) # profile opt_gt
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
}, insns: [:opt_gt], call_threshold: 2
end

def test_opt_ge
assert_compiles '[false, true, true]', %q{
def test(a, b) = a >= b
test(2, 3) # profile opt_ge
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
}, insns: [:opt_ge], call_threshold: 2
end

def test_new_array_empty
assert_compiles '[]', %q{
def test = []
test
}
}, insns: [:newarray]
end

def test_new_array_nonempty
Expand Down Expand Up @@ -504,26 +504,47 @@ def test_instruction_order

# Assert that every method call in `test_script` can be compiled by ZJIT
# at a given call_threshold
def assert_compiles(expected, test_script, **opts)
def assert_compiles(expected, test_script, insns: [], **opts)
pipe_fd = 3

script = <<~RUBY
_test_proc = -> {
RubyVM::ZJIT.assert_compiles
#{test_script}
}
result = _test_proc.call
IO.open(#{pipe_fd}).write(result.inspect)
ret_val = _test_proc.call
result = {
ret_val:,
#{ unless insns.empty?
'insns: RubyVM::InstructionSequence.of(_test_proc).enum_for(:each_child).map(&:to_a)'
end}
}
IO.open(#{pipe_fd}).write(Marshal.dump(result))
RUBY

status, out, err, actual = eval_with_jit(script, pipe_fd:, **opts)
status, out, err, result = eval_with_jit(script, pipe_fd:, **opts)

message = "exited with status #{status.to_i}"
message << "\nstdout:\n```\n#{out}```\n" unless out.empty?
message << "\nstderr:\n```\n#{err}```\n" unless err.empty?
assert status.success?, message

assert_equal expected, actual
result = Marshal.load(result)
assert_equal expected, result.fetch(:ret_val).inspect

unless insns.empty?
iseqs = result.fetch(:insns)
iseqs.filter! { it[9] == :method } # ISeq type
assert_equal 1, iseqs.size, "Opcode assertions tests must define exactly one method"
iseq_insns = iseqs.first.last

expected_insns = Set.new(insns)
iseq_insns.each do
next unless it.is_a?(Array)
expected_insns.delete(it.first)
end
assert(expected_insns.empty?, -> { "Not present in ISeq: #{expected_insns.to_a}" })
end
end

# Run a Ruby process with ZJIT options and a pipe for writing test results
Expand Down
Loading
0