RJIT: Fix block finding logic

like YJIT does
This commit is contained in:
Takashi Kokubun 2023-03-12 21:14:35 -07:00
parent e28f83703a
commit a23f64221e
2 changed files with 34 additions and 13 deletions

View File

@ -257,7 +257,7 @@ module RubyVM::RJIT
end end
incr_counter(:compiled_block_count) incr_counter(:compiled_block_count)
set_block(iseq, block) add_block(iseq, block)
end end
def leave_exit def leave_exit
@ -274,35 +274,52 @@ module RubyVM::RJIT
end end
def list_blocks(iseq, pc) def list_blocks(iseq, pc)
rjit_blocks(iseq)[pc].values rjit_blocks(iseq)[pc]
end end
# @param [Integer] pc # @param [Integer] pc
# @param [RubyVM::RJIT::Context] ctx # @param [RubyVM::RJIT::Context] ctx
# @return [RubyVM::RJIT::Block,NilClass] # @return [RubyVM::RJIT::Block,NilClass]
def find_block(iseq, pc, ctx) def find_block(iseq, pc, ctx)
rjit_blocks(iseq)[pc][ctx] src = ctx
rjit_blocks(iseq)[pc].find do |block|
dst = block.ctx
# Can only lookup the first version in the chain
if dst.chain_depth != 0
next false
end
# Blocks with depth > 0 always produce new versions
# Sidechains cannot overlap
if src.chain_depth != 0
next false
end
src.stack_size == dst.stack_size &&
src.sp_offset == dst.sp_offset
end
end end
# @param [RubyVM::RJIT::Block] block # @param [RubyVM::RJIT::Block] block
def set_block(iseq, block) def add_block(iseq, block)
rjit_blocks(iseq)[block.pc][block.ctx] = block rjit_blocks(iseq)[block.pc] << block
end end
# @param [RubyVM::RJIT::Block] block # @param [RubyVM::RJIT::Block] block
def remove_block(iseq, block) def remove_block(iseq, block)
rjit_blocks(iseq)[block.pc].delete(block.ctx) rjit_blocks(iseq)[block.pc].delete(block)
end end
def rjit_blocks(iseq) def rjit_blocks(iseq)
# Guard against ISEQ GC at random moments # Guard against ISEQ GC at random moments
unless C.imemo_type_p(iseq, C.imemo_iseq) unless C.imemo_type_p(iseq, C.imemo_iseq)
return Hash.new { |h, k| h[k] = {} } return Hash.new { |h, k| h[k] = [] }
end end
unless iseq.body.rjit_blocks unless iseq.body.rjit_blocks
iseq.body.rjit_blocks = Hash.new { |h, k| h[k] = {} } iseq.body.rjit_blocks = Hash.new { |blocks, pc| blocks[pc] = [] }
# For some reason, rb_rjit_iseq_mark didn't protect this Hash # For some reason, rb_rjit_iseq_mark didn't protect this Hash
# from being freed. So we rely on GC_REFS to keep the Hash. # from being freed. So we rely on GC_REFS to keep the Hash.
GC_REFS << iseq.body.rjit_blocks GC_REFS << iseq.body.rjit_blocks

View File

@ -1249,7 +1249,7 @@ module RubyVM::RJIT
end end
pc = jit.pc + C.VALUE.size * (jit.insn.len + jump_offset) pc = jit.pc + C.VALUE.size * (jit.insn.len + jump_offset)
stub_next_block(jit.iseq, pc, ctx, asm) jit_direct_jump(jit.iseq, pc, ctx, asm)
EndBlock EndBlock
end end
@ -2782,7 +2782,7 @@ module RubyVM::RJIT
jit.record_boundary_patch_point = false jit.record_boundary_patch_point = false
end end
stub_next_block(jit.iseq, next_pc, reset_depth, asm, comment: 'jump_to_next_insn') jit_direct_jump(jit.iseq, next_pc, reset_depth, asm, comment: 'jump_to_next_insn')
end end
# rb_vm_check_ints # rb_vm_check_ints
@ -3246,7 +3246,7 @@ module RubyVM::RJIT
# Jump to a stub for the callee ISEQ # Jump to a stub for the callee ISEQ
callee_ctx = Context.new callee_ctx = Context.new
pc = (iseq.body.iseq_encoded + opt_pc).to_i pc = (iseq.body.iseq_encoded + opt_pc).to_i
stub_next_block(iseq, pc, callee_ctx, asm) jit_direct_jump(iseq, pc, callee_ctx, asm)
EndBlock EndBlock
end end
@ -3898,10 +3898,14 @@ module RubyVM::RJIT
# @param asm [RubyVM::RJIT::Assembler] # @param asm [RubyVM::RJIT::Assembler]
def defer_compilation(jit, ctx, asm) def defer_compilation(jit, ctx, asm)
# Make a stub to compile the current insn # Make a stub to compile the current insn
stub_next_block(jit.iseq, jit.pc, ctx, asm, comment: 'defer_compilation') if ctx.chain_depth != 0
raise "double defer!"
end
ctx.chain_depth += 1
jit_direct_jump(jit.iseq, jit.pc, ctx, asm, comment: 'defer_compilation')
end end
def stub_next_block(iseq, pc, ctx, asm, comment: 'stub_next_block') def jit_direct_jump(iseq, pc, ctx, asm, comment: 'jit_direct_jump')
branch_stub = BranchStub.new( branch_stub = BranchStub.new(
iseq:, iseq:,
shape: Default, shape: Default,