diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ruby_vm/rjit/compiler.rb | 70 | ||||
-rw-r--r-- | lib/ruby_vm/rjit/insn_compiler.rb | 18 | ||||
-rw-r--r-- | lib/ruby_vm/rjit/jit_state.rb | 7 |
3 files changed, 86 insertions, 9 deletions
diff --git a/lib/ruby_vm/rjit/compiler.rb b/lib/ruby_vm/rjit/compiler.rb index 203c2658df..1c024c4c40 100644 --- a/lib/ruby_vm/rjit/compiler.rb +++ b/lib/ruby_vm/rjit/compiler.rb @@ -279,6 +279,8 @@ module RubyVM::RJIT end # @param asm [RubyVM::RJIT::Assembler] + # @param jit [RubyVM::RJIT::JITState] + # @param ctx [RubyVM::RJIT::Context] def compile_block(asm, jit:, pc:, ctx: Context.new) # Mark the block start address and prepare an exit code storage ctx = limit_block_versions(jit.iseq, pc, ctx) @@ -292,6 +294,7 @@ module RubyVM::RJIT # Compile each insn index = (pc - iseq.body.iseq_encoded.to_i) / C.VALUE.size while index < iseq.body.iseq_size + # Set the current instruction insn = self.class.decode_insn(iseq.body.iseq_encoded[index]) jit.pc = (iseq.body.iseq_encoded + index).to_i jit.stack_size_for_pc = ctx.stack_size @@ -308,6 +311,11 @@ module RubyVM::RJIT jit.record_boundary_patch_point = false end + # In debug mode, verify our existing assumption + if C.rjit_opts.verify_ctx && jit.at_current_insn? + verify_ctx(jit, ctx) + end + case status = @insn_compiler.compile(jit, ctx, asm, insn) when KeepCompiling # For now, reset the chain depth after each instruction as only the @@ -435,5 +443,67 @@ module RubyVM::RJIT rescue RangeError # bignum too big to convert into `unsigned long long' (RangeError) -1 end + + # Verify the ctx's types and mappings against the compile-time stack, self, and locals. + # @param jit [RubyVM::RJIT::JITState] + # @param ctx [RubyVM::RJIT::Context] + def verify_ctx(jit, ctx) + # Only able to check types when at current insn + assert(jit.at_current_insn?) + + self_val = jit.peek_at_self + self_val_type = Type.from(self_val) + + # Verify self operand type + assert_compatible(self_val_type, ctx.get_opnd_type(SelfOpnd)) + + # Verify stack operand types + [ctx.stack_size, MAX_TEMP_TYPES].min.times do |i| + learned_mapping, learned_type = ctx.get_opnd_mapping(StackOpnd[i]) + stack_val = jit.peek_at_stack(i) + val_type = Type.from(stack_val) + + case learned_mapping + in MapToSelf + if C.to_value(self_val) != C.to_value(stack_val) + raise "verify_ctx: stack value was mapped to self, but values did not match:\n"\ + "stack: #{stack_val.inspect}, self: #{self_val.inspect}" + end + in MapToLocal[local_idx] + local_val = jit.peek_at_local(local_idx) + if C.to_value(local_val) != C.to_value(stack_val) + raise "verify_ctx: stack value was mapped to local, but values did not match:\n"\ + "stack: #{stack_val.inspect}, local: #{local_val.inspect}" + end + in MapToStack + # noop + end + + # If the actual type differs from the learned type + assert_compatible(val_type, learned_type) + end + + # Verify local variable types + local_table_size = jit.iseq.body.local_table_size + [local_table_size, MAX_TEMP_TYPES].min.times do |i| + learned_type = ctx.get_local_type(i) + local_val = jit.peek_at_local(i) + local_type = Type.from(local_val) + + assert_compatible(local_type, learned_type) + end + end + + def assert_compatible(actual_type, ctx_type) + if actual_type.diff(ctx_type) == TypeDiff::Incompatible + raise "verify_ctx: ctx type (#{ctx_type.type.inspect}) is incompatible with actual type (#{actual_type.type.inspect})" + end + end + + def assert(cond) + unless cond + raise "'#{cond.inspect}' was not true" + end + end end end diff --git a/lib/ruby_vm/rjit/insn_compiler.rb b/lib/ruby_vm/rjit/insn_compiler.rb index e3d3c36836..d9aca5b52c 100644 --- a/lib/ruby_vm/rjit/insn_compiler.rb +++ b/lib/ruby_vm/rjit/insn_compiler.rb @@ -21,16 +21,16 @@ module RubyVM::RJIT # @param insn `RubyVM::RJIT::Instruction` def compile(jit, ctx, asm, insn) asm.incr_counter(:rjit_insns_count) - insn_idx = format('%04d', (jit.pc.to_i - jit.iseq.body.iseq_encoded.to_i) / C.VALUE.size) - asm.comment("Insn: #{insn_idx} #{insn.name}") - # stack = ctx.stack_size.times.map do |stack_idx| - # ctx.get_opnd_type(StackOpnd[ctx.stack_size - stack_idx - 1]).type - # end - # locals = jit.iseq.body.local_table_size.times.map do |local_idx| - # (ctx.local_types[local_idx] || Type::Unknown).type - # end - # asm.comment("Insn: #{insn_idx} #{insn.name} (stack: [#{stack.join(', ')}], locals: [#{locals.join(', ')}])") + stack = ctx.stack_size.times.map do |stack_idx| + ctx.get_opnd_type(StackOpnd[ctx.stack_size - stack_idx - 1]).type + end + locals = jit.iseq.body.local_table_size.times.map do |local_idx| + (ctx.local_types[local_idx] || Type::Unknown).type + end + + insn_idx = format('%04d', (jit.pc.to_i - jit.iseq.body.iseq_encoded.to_i) / C.VALUE.size) + asm.comment("Insn: #{insn_idx} #{insn.name} (stack: [#{stack.join(', ')}], locals: [#{locals.join(', ')}])") # 83/102 case insn.name diff --git a/lib/ruby_vm/rjit/jit_state.rb b/lib/ruby_vm/rjit/jit_state.rb index f31223e478..02a713474e 100644 --- a/lib/ruby_vm/rjit/jit_state.rb +++ b/lib/ruby_vm/rjit/jit_state.rb @@ -27,6 +27,13 @@ module RubyVM::RJIT pc == cfp.pc.to_i end + def peek_at_local(n) + local_table_size = iseq.body.local_table_size + offset = -C::VM_ENV_DATA_SIZE - local_table_size + n + 1 + value = (cfp.ep + offset).* + C.to_ruby(value) + end + def peek_at_stack(depth_from_top) raise 'not at current insn' unless at_current_insn? offset = -(1 + depth_from_top) |