Get rid of BlockStub

This commit is contained in:
Takashi Kokubun 2023-02-07 11:55:07 -08:00
parent 47e2ea3a80
commit d415f1e317
6 changed files with 1 additions and 106 deletions

View File

@ -1,9 +0,0 @@
class RubyVM::MJIT::BlockStub < Struct.new(
:iseq, # @param [RubyVM::MJIT::CPointer::Struct_rb_iseq_struct] Stub target ISEQ
:ctx, # @param [RubyVM::MJIT::Context] Stub target context
:pc, # @param [Integer] Stub target pc
:start_addr, # @param [Integer] Stub source start address to be re-generated
:end_addr, # @param [Integer] Stub source end address to be re-generated
:change_block, # @param [Proc] Recompile the source address with a new block address
)
end

View File

@ -1,6 +1,5 @@
require 'ruby_vm/mjit/assembler'
require 'ruby_vm/mjit/block'
require 'ruby_vm/mjit/block_stub'
require 'ruby_vm/mjit/branch_stub'
require 'ruby_vm/mjit/code_block'
require 'ruby_vm/mjit/context'
@ -68,37 +67,6 @@ module RubyVM::MJIT
$stderr.puts e.full_message # TODO: check verbose
end
# Continue compilation from a block stub.
# @param block_stub [RubyVM::MJIT::BlockStub]
# @param cfp `RubyVM::MJIT::CPointer::Struct_rb_control_frame_t`
# @return [Integer] The starting address of the compiled block stub
def block_stub_hit(block_stub, cfp)
# Update cfp->pc for `jit.at_current_insn?`
cfp.pc = block_stub.pc
# Prepare the jump target
jit = JITState.new(iseq: block_stub.iseq, cfp:)
new_asm = Assembler.new.tap do |asm|
compile_block(asm, jit:, pc: block_stub.pc, ctx: block_stub.ctx)
end
# Rewrite the block stub
if @cb.write_addr == block_stub.end_addr
# If the block stub's jump is the last code, overwrite the jump with the new code.
@cb.set_write_addr(block_stub.start_addr)
@cb.write(new_asm)
else
# If the block stub's jump is old code, change the jump target to the new code.
new_addr = @cb.write(new_asm)
@cb.with_write_addr(block_stub.start_addr) do
asm = Assembler.new
block_stub.change_block.call(asm, new_addr)
@cb.write(asm)
end
new_addr
end
end
# Compile a branch stub.
# @param branch_stub [RubyVM::MJIT::BranchStub]
# @param cfp `RubyVM::MJIT::CPointer::Struct_rb_control_frame_t`

View File

@ -57,20 +57,6 @@ module RubyVM::MJIT
asm.ret
end
# @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::Assembler]
# @param block_stub [RubyVM::MJIT::BlockStub]
def compile_block_stub(ctx, asm, block_stub)
# Call rb_mjit_block_stub_hit
asm.comment("block stub hit: #{block_stub.iseq.body.location.label}@#{C.rb_iseq_path(block_stub.iseq)}:#{iseq_lineno(block_stub.iseq, block_stub.pc)}")
asm.mov(:rdi, to_value(block_stub))
asm.mov(:esi, ctx.sp_offset)
asm.call(C.rb_mjit_block_stub_hit)
# Jump to the address returned by rb_mjit_stub_hit
asm.jmp(:rax)
end
# @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::Assembler]
# @param branch_stub [RubyVM::MJIT::BranchStub]

View File

@ -812,24 +812,7 @@ module RubyVM::MJIT
# @param asm [RubyVM::MJIT::Assembler]
def defer_compilation(jit, ctx, asm)
# Make a stub to compile the current insn
compile_block_stub(jit.iseq, jit.pc, ctx, asm, comment: 'defer_compilation: block stub')
end
def compile_block_stub(iseq, pc, ctx, asm, comment: 'block stub')
block_stub = BlockStub.new(iseq:, pc:, ctx: ctx.dup)
stub_hit = Assembler.new.then do |ocb_asm|
@exit_compiler.compile_block_stub(ctx, ocb_asm, block_stub)
@ocb.write(ocb_asm)
end
block_stub.change_block = proc do |jump_asm, new_addr|
jump_asm.comment(comment)
jump_asm.stub(block_stub) do
jump_asm.jmp(new_addr)
end
end
block_stub.change_block.call(asm, stub_hit)
stub_next_block(jit.iseq, jit.pc, ctx, asm, comment: 'defer_compilation')
end
def stub_next_block(iseq, pc, ctx, asm, comment: 'stub_next_block')

26
mjit.c
View File

@ -374,32 +374,6 @@ rb_mjit_compile(const rb_iseq_t *iseq)
RB_VM_LOCK_LEAVE();
}
void *
rb_mjit_block_stub_hit(VALUE block_stub, int sp_offset)
{
VALUE result;
RB_VM_LOCK_ENTER();
rb_vm_barrier();
bool original_call_p = mjit_call_p;
mjit_call_p = false; // Avoid impacting JIT metrics by itself
mjit_stats_p = false; // Avoid impacting JIT stats by itself
rb_control_frame_t *cfp = GET_EC()->cfp;
cfp->sp += sp_offset; // preserve stack values, also using the actual sp_offset to make jit.peek_at_stack work
VALUE cfp_ptr = rb_funcall(rb_cMJITCfpPtr, rb_intern("new"), 1, SIZET2NUM((size_t)cfp));
result = rb_funcall(rb_MJITCompiler, rb_intern("block_stub_hit"), 2, block_stub, cfp_ptr);
cfp->sp -= sp_offset; // reset for consistency with the code without the stub
mjit_stats_p = mjit_opts.stats;
mjit_call_p = original_call_p;
RB_VM_LOCK_LEAVE();
return (void *)NUM2SIZET(result);
}
void *
rb_mjit_branch_stub_hit(VALUE branch_stub, int sp_offset, int target0_p)
{

View File

@ -36,13 +36,6 @@ module RubyVM::MJIT # :nodoc: all
CType::Immediate.parse("size_t").new(addr)
end
def rb_mjit_block_stub_hit
Primitive.cstmt! %{
extern void *rb_mjit_block_stub_hit(VALUE block_stub, int sp_offset);
return SIZET2NUM((size_t)rb_mjit_block_stub_hit);
}
end
def rb_mjit_branch_stub_hit
Primitive.cstmt! %{
extern void *rb_mjit_branch_stub_hit(VALUE branch_stub, int sp_offset, int target0_p);