diff --git a/lib/ruby_vm/mjit/block.rb b/lib/ruby_vm/mjit/block.rb index efa8bff1b3..abe0c221b2 100644 --- a/lib/ruby_vm/mjit/block.rb +++ b/lib/ruby_vm/mjit/block.rb @@ -1,9 +1,10 @@ class RubyVM::MJIT::Block < Struct.new( - :pc, # @param [Integer] Starting PC - :ctx, # @param [RubyVM::MJIT::Context] **Starting** Context (TODO: freeze?) - :start_addr, # @param [Integer] Starting address of this block's JIT code - :entry_exit, # @param [Integer] Address of entry exit (optional) - :incoming, # @param [Array] Incoming branches + :pc, # @param [Integer] Starting PC + :ctx, # @param [RubyVM::MJIT::Context] **Starting** Context (TODO: freeze?) + :start_addr, # @param [Integer] Starting address of this block's JIT code + :entry_exit, # @param [Integer] Address of entry exit (optional) + :incoming, # @param [Array] Incoming branches + :invalidated, # @param [TrueClass,FalseClass] true if already invalidated ) - def initialize(incoming: [], **) = super + def initialize(incoming: [], invalidated: false, **) = super end diff --git a/lib/ruby_vm/mjit/compiler.rb b/lib/ruby_vm/mjit/compiler.rb index 967adec2aa..f19e1bd6fb 100644 --- a/lib/ruby_vm/mjit/compiler.rb +++ b/lib/ruby_vm/mjit/compiler.rb @@ -203,6 +203,12 @@ module RubyVM::MJIT break when CantCompile @exit_compiler.compile_side_exit(jit.pc, ctx, asm) + + # If this is the first instruction, this block never needs to be invalidated. + if block.pc == iseq.body.iseq_encoded.to_i + index * C.VALUE.size + block.invalidated = true + end + break else raise "compiling #{insn.name} returned unexpected status: #{status.inspect}" @@ -224,11 +230,14 @@ module RubyVM::MJIT remove_block(iseq, block) # Invalidate the block with entry exit - @cb.with_write_addr(block.start_addr) do - asm = Assembler.new - asm.comment('invalidate_block') - asm.jmp(block.entry_exit) - @cb.write(asm) + unless block.invalidated + @cb.with_write_addr(block.start_addr) do + asm = Assembler.new + asm.comment('invalidate_block') + asm.jmp(block.entry_exit) + @cb.write(asm) + end + block.invalidated = true end # Re-stub incoming branches