2023-03-06 23:17:25 -08:00
|
|
|
require 'ruby_vm/rjit/assembler'
|
|
|
|
require 'ruby_vm/rjit/block'
|
|
|
|
require 'ruby_vm/rjit/branch_stub'
|
|
|
|
require 'ruby_vm/rjit/code_block'
|
|
|
|
require 'ruby_vm/rjit/context'
|
2023-04-02 15:26:46 -07:00
|
|
|
require 'ruby_vm/rjit/entry_stub'
|
2023-03-06 23:17:25 -08:00
|
|
|
require 'ruby_vm/rjit/exit_compiler'
|
|
|
|
require 'ruby_vm/rjit/insn_compiler'
|
|
|
|
require 'ruby_vm/rjit/instruction'
|
|
|
|
require 'ruby_vm/rjit/invariants'
|
|
|
|
require 'ruby_vm/rjit/jit_state'
|
2023-04-02 16:59:07 -07:00
|
|
|
require 'ruby_vm/rjit/type'
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
module RubyVM::RJIT
|
2022-12-17 13:39:35 -08:00
|
|
|
# Compilation status
|
2022-12-23 10:43:18 -08:00
|
|
|
KeepCompiling = :KeepCompiling
|
|
|
|
CantCompile = :CantCompile
|
|
|
|
EndBlock = :EndBlock
|
|
|
|
|
|
|
|
# Ruby constants
|
2023-01-02 22:53:14 -08:00
|
|
|
Qtrue = Fiddle::Qtrue
|
|
|
|
Qfalse = Fiddle::Qfalse
|
2022-12-23 10:43:18 -08:00
|
|
|
Qnil = Fiddle::Qnil
|
|
|
|
Qundef = Fiddle::Qundef
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2022-12-31 14:14:53 -08:00
|
|
|
# Callee-saved registers
|
|
|
|
# TODO: support using r12/r13 here
|
|
|
|
EC = :r14
|
|
|
|
CFP = :r15
|
2022-12-23 16:23:21 -08:00
|
|
|
SP = :rbx
|
|
|
|
|
2023-04-01 21:52:35 -07:00
|
|
|
# Scratch registers: rax, rcx, rdx
|
2023-01-02 22:53:14 -08:00
|
|
|
|
2023-02-24 13:52:43 -08:00
|
|
|
# Mark objects in this Array during GC
|
|
|
|
GC_REFS = []
|
|
|
|
|
2023-04-02 23:34:57 -07:00
|
|
|
# Maximum number of versions per block
|
|
|
|
# 1 means always create generic versions
|
|
|
|
MAX_VERSIONS = 4
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
class Compiler
|
|
|
|
attr_accessor :write_pos
|
2022-09-04 21:53:46 -07:00
|
|
|
|
2022-12-26 22:46:40 -08:00
|
|
|
def self.decode_insn(encoded)
|
|
|
|
INSNS.fetch(C.rb_vm_insn_decode(encoded))
|
|
|
|
end
|
|
|
|
|
2023-03-10 11:55:48 -08:00
|
|
|
def initialize
|
|
|
|
mem_size = C.rjit_opts.exec_mem_size * 1024 * 1024
|
|
|
|
mem_block = C.mmap(mem_size)
|
2022-12-30 22:16:07 -08:00
|
|
|
@cb = CodeBlock.new(mem_block: mem_block, mem_size: mem_size / 2)
|
2022-12-30 23:42:00 -08:00
|
|
|
@ocb = CodeBlock.new(mem_block: mem_block + mem_size / 2, mem_size: mem_size / 2, outlined: true)
|
2022-12-30 21:27:12 -08:00
|
|
|
@exit_compiler = ExitCompiler.new
|
2023-02-03 22:42:13 -08:00
|
|
|
@insn_compiler = InsnCompiler.new(@cb, @ocb, @exit_compiler)
|
2023-02-18 14:45:17 -08:00
|
|
|
Invariants.initialize(@cb, @ocb, self, @exit_compiler)
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-11-28 21:33:55 -08:00
|
|
|
|
2022-12-31 13:41:32 -08:00
|
|
|
# Compile an ISEQ from its entry point.
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param iseq `RubyVM::RJIT::CPointer::Struct_rb_iseq_t`
|
|
|
|
# @param cfp `RubyVM::RJIT::CPointer::Struct_rb_control_frame_t`
|
2022-12-31 13:41:32 -08:00
|
|
|
def compile(iseq, cfp)
|
2023-12-13 09:36:06 -08:00
|
|
|
return unless supported_platform?
|
2023-04-02 14:44:40 -07:00
|
|
|
pc = cfp.pc.to_i
|
2023-02-03 22:42:13 -08:00
|
|
|
jit = JITState.new(iseq:, cfp:)
|
2022-12-30 22:16:07 -08:00
|
|
|
asm = Assembler.new
|
2023-04-02 14:44:40 -07:00
|
|
|
compile_prologue(asm, iseq, pc)
|
|
|
|
compile_block(asm, jit:, pc:)
|
2023-08-08 16:06:22 -07:00
|
|
|
iseq.body.jit_entry = @cb.write(asm)
|
2022-12-17 13:39:35 -08:00
|
|
|
rescue Exception => e
|
2023-12-22 21:47:35 -08:00
|
|
|
STDERR.puts "#{e.class}: #{e.message}"
|
|
|
|
STDERR.puts e.backtrace
|
2023-02-10 15:22:05 -08:00
|
|
|
exit 1
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2022-12-11 21:42:25 -08:00
|
|
|
|
2023-04-02 15:26:46 -07:00
|
|
|
# Compile an entry.
|
|
|
|
# @param entry [RubyVM::RJIT::EntryStub]
|
|
|
|
def entry_stub_hit(entry_stub, cfp)
|
|
|
|
# Compile a new entry guard as a next entry
|
|
|
|
pc = cfp.pc.to_i
|
|
|
|
next_entry = Assembler.new.then do |asm|
|
|
|
|
compile_entry_chain_guard(asm, cfp.iseq, pc)
|
|
|
|
@cb.write(asm)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Try to find an existing compiled version of this block
|
|
|
|
ctx = Context.new
|
|
|
|
block = find_block(cfp.iseq, pc, ctx)
|
|
|
|
if block
|
|
|
|
# If an existing block is found, generate a jump to the block.
|
|
|
|
asm = Assembler.new
|
|
|
|
asm.jmp(block.start_addr)
|
|
|
|
@cb.write(asm)
|
|
|
|
else
|
|
|
|
# If this block hasn't yet been compiled, generate blocks after the entry guard.
|
|
|
|
asm = Assembler.new
|
|
|
|
jit = JITState.new(iseq: cfp.iseq, cfp:)
|
|
|
|
compile_block(asm, jit:, pc:, ctx:)
|
|
|
|
@cb.write(asm)
|
|
|
|
|
|
|
|
block = jit.block
|
|
|
|
end
|
|
|
|
|
|
|
|
# Regenerate the previous entry
|
|
|
|
@cb.with_write_addr(entry_stub.start_addr) do
|
|
|
|
# The last instruction of compile_entry_chain_guard is jne
|
|
|
|
asm = Assembler.new
|
|
|
|
asm.jne(next_entry)
|
|
|
|
@cb.write(asm)
|
|
|
|
end
|
|
|
|
|
|
|
|
return block.start_addr
|
|
|
|
rescue Exception => e
|
2023-12-22 21:47:35 -08:00
|
|
|
STDERR.puts e.full_message
|
2023-04-02 15:26:46 -07:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2023-01-07 13:21:14 -08:00
|
|
|
# Compile a branch stub.
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param branch_stub [RubyVM::RJIT::BranchStub]
|
|
|
|
# @param cfp `RubyVM::RJIT::CPointer::Struct_rb_control_frame_t`
|
2023-02-07 00:17:13 -08:00
|
|
|
# @param target0_p [TrueClass,FalseClass]
|
2023-01-07 13:21:14 -08:00
|
|
|
# @return [Integer] The starting address of the compiled branch stub
|
2023-02-07 00:17:13 -08:00
|
|
|
def branch_stub_hit(branch_stub, cfp, target0_p)
|
2023-01-07 13:21:14 -08:00
|
|
|
# Update cfp->pc for `jit.at_current_insn?`
|
2023-02-07 00:17:13 -08:00
|
|
|
target = target0_p ? branch_stub.target0 : branch_stub.target1
|
|
|
|
cfp.pc = target.pc
|
2023-01-07 13:21:14 -08:00
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
# Reuse an existing block if it already exists
|
|
|
|
block = find_block(branch_stub.iseq, target.pc, target.ctx)
|
2023-01-07 13:21:14 -08:00
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
# If the branch stub's jump is the last code, allow overwriting part of
|
|
|
|
# the old branch code with the new block code.
|
|
|
|
fallthrough = block.nil? && @cb.write_addr == branch_stub.end_addr
|
|
|
|
if fallthrough
|
2023-02-07 00:17:13 -08:00
|
|
|
# If the branch stub's jump is the last code, allow overwriting part of
|
|
|
|
# the old branch code with the new block code.
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.set_write_addr(branch_stub.start_addr)
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.shape = target0_p ? Next0 : Next1
|
2023-01-07 13:21:14 -08:00
|
|
|
Assembler.new.tap do |branch_asm|
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_stub.compile.call(branch_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.write(branch_asm)
|
|
|
|
end
|
2023-02-07 13:03:47 -08:00
|
|
|
end
|
2023-01-07 13:21:14 -08:00
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
# Reuse or generate a block
|
|
|
|
if block
|
|
|
|
target.address = block.start_addr
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
2023-02-07 13:03:47 -08:00
|
|
|
jit = JITState.new(iseq: branch_stub.iseq, cfp:)
|
|
|
|
target.address = Assembler.new.then do |asm|
|
|
|
|
compile_block(asm, jit:, pc: target.pc, ctx: target.ctx.dup)
|
|
|
|
@cb.write(asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
2023-02-18 14:45:17 -08:00
|
|
|
block = jit.block
|
2023-02-07 13:03:47 -08:00
|
|
|
end
|
2023-02-18 14:45:17 -08:00
|
|
|
block.incoming << branch_stub # prepare for invalidate_block
|
2023-01-07 13:21:14 -08:00
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
# Re-generate the branch code for non-fallthrough cases
|
|
|
|
unless fallthrough
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.with_write_addr(branch_stub.start_addr) do
|
2023-02-07 00:17:13 -08:00
|
|
|
branch_asm = Assembler.new
|
|
|
|
branch_stub.compile.call(branch_asm)
|
2023-01-07 13:21:14 -08:00
|
|
|
@cb.write(branch_asm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
return target.address
|
2023-02-10 15:22:05 -08:00
|
|
|
rescue Exception => e
|
2023-12-22 21:47:35 -08:00
|
|
|
STDERR.puts e.full_message
|
2023-02-10 15:22:05 -08:00
|
|
|
exit 1
|
2023-01-07 13:21:14 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param iseq `RubyVM::RJIT::CPointer::Struct_rb_iseq_t`
|
2023-02-18 14:45:17 -08:00
|
|
|
# @param pc [Integer]
|
|
|
|
def invalidate_blocks(iseq, pc)
|
|
|
|
list_blocks(iseq, pc).each do |block|
|
2023-03-01 21:32:36 -08:00
|
|
|
invalidate_block(block)
|
2023-02-18 14:45:17 -08:00
|
|
|
end
|
2023-02-24 14:38:22 -08:00
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
# If they were the ISEQ's first blocks, re-compile RJIT entry as well
|
2023-02-24 14:38:22 -08:00
|
|
|
if iseq.body.iseq_encoded.to_i == pc
|
2023-08-08 16:06:22 -07:00
|
|
|
iseq.body.jit_entry = 0
|
|
|
|
iseq.body.jit_entry_calls = 0
|
2023-02-24 14:38:22 -08:00
|
|
|
end
|
2023-02-18 14:45:17 -08:00
|
|
|
end
|
|
|
|
|
2023-03-01 21:32:36 -08:00
|
|
|
def invalidate_block(block)
|
|
|
|
iseq = block.iseq
|
2023-03-01 22:06:57 -08:00
|
|
|
# Avoid touching GCed ISEQs. We assume it won't be re-entered.
|
2023-03-09 22:14:43 -08:00
|
|
|
return unless C.imemo_type_p(iseq, C.imemo_iseq)
|
2023-03-01 22:06:57 -08:00
|
|
|
|
2023-03-01 21:32:36 -08:00
|
|
|
# Remove this block from the version array
|
|
|
|
remove_block(iseq, block)
|
|
|
|
|
|
|
|
# Invalidate the block with entry exit
|
|
|
|
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
|
|
|
|
block.incoming.each do |branch_stub|
|
|
|
|
target = [branch_stub.target0, branch_stub.target1].compact.find do |target|
|
|
|
|
target.pc == block.pc && target.ctx == block.ctx
|
|
|
|
end
|
|
|
|
next if target.nil?
|
|
|
|
# TODO: Could target.address be a stub address? Is invalidation not needed in that case?
|
|
|
|
|
|
|
|
# If the target being re-generated is currently a fallthrough block,
|
|
|
|
# the fallthrough code must be rewritten with a jump to the stub.
|
|
|
|
if target.address == branch_stub.end_addr
|
|
|
|
branch_stub.shape = Default
|
|
|
|
end
|
|
|
|
|
|
|
|
target.address = Assembler.new.then do |ocb_asm|
|
|
|
|
@exit_compiler.compile_branch_stub(block.ctx, ocb_asm, branch_stub, target == branch_stub.target0)
|
|
|
|
@ocb.write(ocb_asm)
|
|
|
|
end
|
|
|
|
@cb.with_write_addr(branch_stub.start_addr) do
|
|
|
|
branch_asm = Assembler.new
|
|
|
|
branch_stub.compile.call(branch_asm)
|
|
|
|
@cb.write(branch_asm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
private
|
2022-12-15 22:20:43 -08:00
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Callee-saved: rbx, rsp, rbp, r12, r13, r14, r15
|
|
|
|
# Caller-saved: rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11
|
|
|
|
#
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param asm [RubyVM::RJIT::Assembler]
|
2023-04-02 14:44:40 -07:00
|
|
|
def compile_prologue(asm, iseq, pc)
|
2023-03-06 23:15:30 -08:00
|
|
|
asm.comment('RJIT entry point')
|
2022-12-26 22:06:43 -08:00
|
|
|
|
2022-12-23 14:46:39 -08:00
|
|
|
# Save callee-saved registers used by JITed code
|
2022-12-31 14:14:53 -08:00
|
|
|
asm.push(CFP)
|
|
|
|
asm.push(EC)
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.push(SP)
|
2022-12-23 14:46:39 -08:00
|
|
|
|
2022-12-31 14:14:53 -08:00
|
|
|
# Move arguments EC and CFP to dedicated registers
|
|
|
|
asm.mov(EC, :rdi)
|
|
|
|
asm.mov(CFP, :rsi)
|
|
|
|
|
|
|
|
# Load sp to a dedicated register
|
2022-12-23 16:23:21 -08:00
|
|
|
asm.mov(SP, [CFP, C.rb_control_frame_t.offsetof(:sp)]) # rbx = cfp->sp
|
2023-01-07 21:24:30 -08:00
|
|
|
|
|
|
|
# Setup cfp->jit_return
|
2023-03-09 21:55:14 -08:00
|
|
|
asm.mov(:rax, leave_exit)
|
2023-01-07 21:24:30 -08:00
|
|
|
asm.mov([CFP, C.rb_control_frame_t.offsetof(:jit_return)], :rax)
|
2023-04-02 14:44:40 -07:00
|
|
|
|
|
|
|
# We're compiling iseqs that we *expect* to start at `insn_idx`. But in
|
|
|
|
# the case of optional parameters, the interpreter can set the pc to a
|
|
|
|
# different location depending on the optional parameters. If an iseq
|
|
|
|
# has optional parameters, we'll add a runtime check that the PC we've
|
|
|
|
# compiled for is the same PC that the interpreter wants us to run with.
|
|
|
|
# If they don't match, then we'll take a side exit.
|
|
|
|
if iseq.body.param.flags.has_opt
|
2023-04-02 15:26:46 -07:00
|
|
|
compile_entry_chain_guard(asm, iseq, pc)
|
2023-04-02 14:44:40 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-02 15:26:46 -07:00
|
|
|
def compile_entry_chain_guard(asm, iseq, pc)
|
|
|
|
entry_stub = EntryStub.new
|
|
|
|
stub_addr = Assembler.new.then do |ocb_asm|
|
|
|
|
@exit_compiler.compile_entry_stub(ocb_asm, entry_stub)
|
|
|
|
@ocb.write(ocb_asm)
|
|
|
|
end
|
|
|
|
|
2023-04-02 14:44:40 -07:00
|
|
|
asm.comment('guard expected PC')
|
|
|
|
asm.mov(:rax, pc)
|
|
|
|
asm.cmp([CFP, C.rb_control_frame_t.offsetof(:pc)], :rax)
|
|
|
|
|
2023-04-02 15:26:46 -07:00
|
|
|
asm.stub(entry_stub) do
|
|
|
|
asm.jne(stub_addr)
|
|
|
|
end
|
2022-12-18 23:45:17 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param asm [RubyVM::RJIT::Assembler]
|
2023-04-03 22:52:38 -07:00
|
|
|
# @param jit [RubyVM::RJIT::JITState]
|
|
|
|
# @param ctx [RubyVM::RJIT::Context]
|
2023-04-02 14:44:40 -07:00
|
|
|
def compile_block(asm, jit:, pc:, ctx: Context.new)
|
2023-01-02 14:11:06 -08:00
|
|
|
# Mark the block start address and prepare an exit code storage
|
2023-04-02 23:34:57 -07:00
|
|
|
ctx = limit_block_versions(jit.iseq, pc, ctx)
|
2023-03-01 21:32:36 -08:00
|
|
|
block = Block.new(iseq: jit.iseq, pc:, ctx: ctx.dup)
|
2023-02-18 14:45:17 -08:00
|
|
|
jit.block = block
|
|
|
|
asm.block(block)
|
2023-01-02 14:11:06 -08:00
|
|
|
|
2022-12-31 13:41:32 -08:00
|
|
|
iseq = jit.iseq
|
2023-04-01 22:39:15 -07:00
|
|
|
asm.comment("Block: #{iseq.body.location.label}@#{C.rb_iseq_path(iseq)}:#{iseq_lineno(iseq, pc)}")
|
|
|
|
|
|
|
|
# Compile each insn
|
2023-01-02 14:11:06 -08:00
|
|
|
index = (pc - iseq.body.iseq_encoded.to_i) / C.VALUE.size
|
2022-12-17 13:39:35 -08:00
|
|
|
while index < iseq.body.iseq_size
|
2023-04-03 22:52:38 -07:00
|
|
|
# Set the current instruction
|
2022-12-26 22:46:40 -08:00
|
|
|
insn = self.class.decode_insn(iseq.body.iseq_encoded[index])
|
2022-12-26 14:09:45 -08:00
|
|
|
jit.pc = (iseq.body.iseq_encoded + index).to_i
|
2023-04-01 21:23:36 -07:00
|
|
|
jit.stack_size_for_pc = ctx.stack_size
|
|
|
|
jit.side_exit_for_pc.clear
|
2022-12-26 14:09:45 -08:00
|
|
|
|
2023-02-16 22:29:58 -08:00
|
|
|
# If previous instruction requested to record the boundary
|
|
|
|
if jit.record_boundary_patch_point
|
|
|
|
# Generate an exit to this instruction and record it
|
|
|
|
exit_pos = Assembler.new.then do |ocb_asm|
|
|
|
|
@exit_compiler.compile_side_exit(jit.pc, ctx, ocb_asm)
|
|
|
|
@ocb.write(ocb_asm)
|
|
|
|
end
|
|
|
|
Invariants.record_global_inval_patch(asm, exit_pos)
|
|
|
|
jit.record_boundary_patch_point = false
|
|
|
|
end
|
|
|
|
|
2023-04-03 22:52:38 -07:00
|
|
|
# In debug mode, verify our existing assumption
|
|
|
|
if C.rjit_opts.verify_ctx && jit.at_current_insn?
|
|
|
|
verify_ctx(jit, ctx)
|
|
|
|
end
|
|
|
|
|
2023-01-07 13:21:14 -08:00
|
|
|
case status = @insn_compiler.compile(jit, ctx, asm, insn)
|
|
|
|
when KeepCompiling
|
2023-03-04 22:13:20 -08:00
|
|
|
# For now, reset the chain depth after each instruction as only the
|
|
|
|
# first instruction in the block can concern itself with the depth.
|
|
|
|
ctx.chain_depth = 0
|
|
|
|
|
2023-01-07 13:21:14 -08:00
|
|
|
index += insn.len
|
2022-12-23 10:43:18 -08:00
|
|
|
when EndBlock
|
2023-02-18 14:45:17 -08:00
|
|
|
# TODO: pad nops if entry exit exists (not needed for x86_64?)
|
2022-12-23 10:43:18 -08:00
|
|
|
break
|
|
|
|
when CantCompile
|
2023-04-01 21:23:36 -07:00
|
|
|
# Rewind stack_size using ctx.with_stack_size to allow stack_size changes
|
|
|
|
# before you return CantCompile.
|
|
|
|
@exit_compiler.compile_side_exit(jit.pc, ctx.with_stack_size(jit.stack_size_for_pc), asm)
|
2023-02-19 23:05:29 -08:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-12-17 13:39:35 -08:00
|
|
|
break
|
2023-01-07 13:21:14 -08:00
|
|
|
else
|
|
|
|
raise "compiling #{insn.name} returned unexpected status: #{status.inspect}"
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
|
|
|
end
|
2023-02-07 00:00:09 -08:00
|
|
|
|
|
|
|
incr_counter(:compiled_block_count)
|
2023-03-12 21:14:35 -07:00
|
|
|
add_block(iseq, block)
|
2023-02-07 00:00:09 -08:00
|
|
|
end
|
|
|
|
|
2023-03-09 21:55:14 -08:00
|
|
|
def leave_exit
|
|
|
|
@leave_exit ||= Assembler.new.then do |asm|
|
|
|
|
@exit_compiler.compile_leave_exit(asm)
|
|
|
|
@ocb.write(asm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 00:00:09 -08:00
|
|
|
def incr_counter(name)
|
2023-03-06 23:17:25 -08:00
|
|
|
if C.rjit_opts.stats
|
|
|
|
C.rb_rjit_counters[name][0] += 1
|
2023-02-07 00:00:09 -08:00
|
|
|
end
|
2022-12-17 13:39:35 -08:00
|
|
|
end
|
2023-02-07 13:03:47 -08:00
|
|
|
|
2023-04-02 23:34:57 -07:00
|
|
|
# Produce a generic context when the block version limit is hit for the block
|
|
|
|
def limit_block_versions(iseq, pc, ctx)
|
|
|
|
# Guard chains implement limits separately, do nothing
|
|
|
|
if ctx.chain_depth > 0
|
|
|
|
return ctx.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
# If this block version we're about to add will hit the version limit
|
|
|
|
if list_blocks(iseq, pc).size + 1 >= MAX_VERSIONS
|
|
|
|
# Produce a generic context that stores no type information,
|
|
|
|
# but still respects the stack_size and sp_offset constraints.
|
|
|
|
# This new context will then match all future requests.
|
|
|
|
generic_ctx = Context.new
|
|
|
|
generic_ctx.stack_size = ctx.stack_size
|
|
|
|
generic_ctx.sp_offset = ctx.sp_offset
|
|
|
|
|
|
|
|
if ctx.diff(generic_ctx) == TypeDiff::Incompatible
|
|
|
|
raise 'should substitute a compatible context'
|
|
|
|
end
|
|
|
|
|
|
|
|
return generic_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
return ctx.dup
|
|
|
|
end
|
|
|
|
|
2023-02-18 14:45:17 -08:00
|
|
|
def list_blocks(iseq, pc)
|
2023-03-12 21:14:35 -07:00
|
|
|
rjit_blocks(iseq)[pc]
|
2023-02-18 14:45:17 -08:00
|
|
|
end
|
|
|
|
|
2023-02-07 13:03:47 -08:00
|
|
|
# @param [Integer] pc
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param [RubyVM::RJIT::Context] ctx
|
|
|
|
# @return [RubyVM::RJIT::Block,NilClass]
|
2023-02-07 13:03:47 -08:00
|
|
|
def find_block(iseq, pc, ctx)
|
2023-04-02 23:22:53 -07:00
|
|
|
versions = rjit_blocks(iseq)[pc]
|
|
|
|
|
|
|
|
best_version = nil
|
|
|
|
best_diff = Float::INFINITY
|
|
|
|
|
|
|
|
versions.each do |block|
|
|
|
|
# Note that we always prefer the first matching
|
|
|
|
# version found because of inline-cache chains
|
|
|
|
case ctx.diff(block.ctx)
|
|
|
|
in TypeDiff::Compatible[diff] if diff < best_diff
|
|
|
|
best_version = block
|
|
|
|
best_diff = diff
|
|
|
|
else
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return best_version
|
2023-02-18 14:45:17 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param [RubyVM::RJIT::Block] block
|
2023-03-12 21:14:35 -07:00
|
|
|
def add_block(iseq, block)
|
|
|
|
rjit_blocks(iseq)[block.pc] << block
|
2023-02-07 13:03:47 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:15:30 -08:00
|
|
|
# @param [RubyVM::RJIT::Block] block
|
2023-02-18 14:45:17 -08:00
|
|
|
def remove_block(iseq, block)
|
2023-03-12 21:14:35 -07:00
|
|
|
rjit_blocks(iseq)[block.pc].delete(block)
|
2023-02-24 13:52:43 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:17:25 -08:00
|
|
|
def rjit_blocks(iseq)
|
2023-03-01 23:15:51 -08:00
|
|
|
# Guard against ISEQ GC at random moments
|
2023-03-09 22:14:43 -08:00
|
|
|
|
|
|
|
unless C.imemo_type_p(iseq, C.imemo_iseq)
|
2023-03-12 21:14:35 -07:00
|
|
|
return Hash.new { |h, k| h[k] = [] }
|
2023-03-01 22:06:57 -08:00
|
|
|
end
|
|
|
|
|
2023-03-06 23:17:25 -08:00
|
|
|
unless iseq.body.rjit_blocks
|
2023-03-12 21:14:35 -07:00
|
|
|
iseq.body.rjit_blocks = Hash.new { |blocks, pc| blocks[pc] = [] }
|
2023-03-06 23:17:25 -08:00
|
|
|
# For some reason, rb_rjit_iseq_mark didn't protect this Hash
|
2023-02-24 13:52:43 -08:00
|
|
|
# from being freed. So we rely on GC_REFS to keep the Hash.
|
2023-03-06 23:17:25 -08:00
|
|
|
GC_REFS << iseq.body.rjit_blocks
|
2023-02-24 13:52:43 -08:00
|
|
|
end
|
2023-03-06 23:17:25 -08:00
|
|
|
iseq.body.rjit_blocks
|
2023-02-07 13:03:47 -08:00
|
|
|
end
|
2023-04-01 22:39:15 -07:00
|
|
|
|
|
|
|
def iseq_lineno(iseq, pc)
|
|
|
|
C.rb_iseq_line_no(iseq, (pc - iseq.body.iseq_encoded.to_i) / C.VALUE.size)
|
|
|
|
rescue RangeError # bignum too big to convert into `unsigned long long' (RangeError)
|
|
|
|
-1
|
|
|
|
end
|
2023-04-03 22:52:38 -07:00
|
|
|
|
|
|
|
# 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
|
2023-12-13 09:36:06 -08:00
|
|
|
|
|
|
|
def supported_platform?
|
|
|
|
return @supported_platform if defined?(@supported_platform)
|
|
|
|
@supported_platform = RUBY_PLATFORM.match?(/x86_64/).tap do |supported|
|
|
|
|
warn "warning: RJIT does not support #{RUBY_PLATFORM} yet" unless supported
|
|
|
|
end
|
|
|
|
end
|
2022-12-15 22:20:43 -08:00
|
|
|
end
|
2022-09-04 21:53:46 -07:00
|
|
|
end
|