2021-10-02 01:38:39 +03:00
|
|
|
// This file is a fragment of the yjit.o compilation unit. See yjit.c.
|
2020-12-09 00:54:41 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "vm_sync.h"
|
|
|
|
#include "vm_callinfo.h"
|
|
|
|
#include "builtin.h"
|
2021-03-18 02:07:20 +03:00
|
|
|
#include "gc.h"
|
2021-10-02 01:38:39 +03:00
|
|
|
#include "iseq.h"
|
2020-12-09 00:54:41 +03:00
|
|
|
#include "internal/compile.h"
|
|
|
|
#include "internal/class.h"
|
2021-03-07 02:46:56 +03:00
|
|
|
#include "yjit.h"
|
|
|
|
#include "yjit_iface.h"
|
|
|
|
#include "yjit_codegen.h"
|
|
|
|
#include "yjit_core.h"
|
2021-02-13 01:12:18 +03:00
|
|
|
#include "darray.h"
|
2021-01-22 22:26:20 +03:00
|
|
|
|
2021-10-01 14:17:09 +03:00
|
|
|
#ifdef HAVE_LIBCAPSTONE
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
#include <capstone/capstone.h>
|
2021-04-01 01:27:34 +03:00
|
|
|
static VALUE cYjitDisasm;
|
|
|
|
static VALUE cYjitDisasmInsn;
|
2021-01-22 22:26:20 +03:00
|
|
|
#endif
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
static VALUE mYjit;
|
|
|
|
static VALUE cYjitBlock;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-09-14 22:13:15 +03:00
|
|
|
#if YJIT_STATS
|
2021-04-07 22:27:05 +03:00
|
|
|
static VALUE cYjitCodeComment;
|
2021-08-24 13:32:07 +03:00
|
|
|
#endif
|
2021-06-29 17:37:12 +03:00
|
|
|
|
2021-08-24 13:32:07 +03:00
|
|
|
#if YJIT_STATS
|
2021-06-29 17:37:12 +03:00
|
|
|
extern const int rb_vm_max_insn_name_size;
|
2021-08-24 13:32:07 +03:00
|
|
|
static int64_t exit_op_count[VM_INSTRUCTION_SIZE] = { 0 };
|
2021-02-25 23:10:38 +03:00
|
|
|
#endif
|
2021-01-26 23:21:47 +03:00
|
|
|
|
|
|
|
// Hash table of encoded instructions
|
|
|
|
extern st_table *rb_encoded_insn_data;
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
struct rb_yjit_options rb_yjit_opts;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-11-04 23:05:41 +03:00
|
|
|
// Size of code pages to allocate
|
|
|
|
#define CODE_PAGE_SIZE 16 * 1024
|
|
|
|
|
|
|
|
// How many code pages to allocate at once
|
|
|
|
#define PAGES_PER_ALLOC 512
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
static const rb_data_type_t yjit_block_type = {
|
|
|
|
"YJIT/Block",
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
{0, 0, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
2021-03-02 20:03:11 +03:00
|
|
|
// Get the PC for a given index in an iseq
|
2021-10-02 01:38:39 +03:00
|
|
|
static VALUE *
|
2021-03-25 01:07:26 +03:00
|
|
|
yjit_iseq_pc_at_idx(const rb_iseq_t *iseq, uint32_t insn_idx)
|
2021-03-02 20:03:11 +03:00
|
|
|
{
|
|
|
|
RUBY_ASSERT(iseq != NULL);
|
|
|
|
RUBY_ASSERT(insn_idx < iseq->body->iseq_size);
|
|
|
|
VALUE *encoded = iseq->body->iseq_encoded;
|
|
|
|
VALUE *pc = &encoded[insn_idx];
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:00:45 +03:00
|
|
|
// For debugging. Print the disassembly of an iseq.
|
2021-11-04 23:05:41 +03:00
|
|
|
RBIMPL_ATTR_MAYBE_UNUSED()
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-08-26 00:00:45 +03:00
|
|
|
yjit_print_iseq(const rb_iseq_t *iseq)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
long len;
|
|
|
|
VALUE disassembly = rb_iseq_disasm(iseq);
|
|
|
|
RSTRING_GETMEM(disassembly, ptr, len);
|
|
|
|
fprintf(stderr, "%.*s\n", (int)len, ptr);
|
|
|
|
}
|
|
|
|
|
2021-10-02 01:38:39 +03:00
|
|
|
static int
|
2021-03-25 01:07:26 +03:00
|
|
|
yjit_opcode_at_pc(const rb_iseq_t *iseq, const VALUE *pc)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
|
|
|
const VALUE at_pc = *pc;
|
|
|
|
if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
|
|
|
|
return rb_vm_insn_addr2opcode((const void *)at_pc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (int)at_pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:10:52 +03:00
|
|
|
// Verify that calling with cd on receiver goes to callee
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-03-23 04:04:00 +03:00
|
|
|
check_cfunc_dispatch(VALUE receiver, struct rb_callinfo *ci, void *callee, rb_callable_method_entry_t *compile_time_cme)
|
2021-01-16 01:10:52 +03:00
|
|
|
{
|
|
|
|
if (METHOD_ENTRY_INVALIDATED(compile_time_cme)) {
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_bug("yjit: output code uses invalidated cme %p", (void *)compile_time_cme);
|
2021-01-16 01:10:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool callee_correct = false;
|
2021-03-23 04:04:00 +03:00
|
|
|
const rb_callable_method_entry_t *cme = rb_callable_method_entry(CLASS_OF(receiver), vm_ci_mid(ci));
|
2021-01-16 01:10:52 +03:00
|
|
|
if (cme->def->type == VM_METHOD_TYPE_CFUNC) {
|
|
|
|
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
|
|
|
|
if ((void *)cfunc->func == callee) {
|
|
|
|
callee_correct = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!callee_correct) {
|
2021-03-23 04:04:00 +03:00
|
|
|
rb_bug("yjit: output code calls wrong method");
|
2021-01-16 01:10:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MJIT_FUNC_EXPORTED VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
// GC root for interacting with the GC
|
2021-03-07 02:46:56 +03:00
|
|
|
struct yjit_root_struct {
|
2021-02-17 00:50:09 +03:00
|
|
|
int unused; // empty structs are not legal in C99
|
|
|
|
};
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// Hash table of BOP blocks
|
|
|
|
static st_table *blocks_assuming_bops;
|
|
|
|
|
2021-10-02 01:38:39 +03:00
|
|
|
static bool
|
2021-11-04 19:30:30 +03:00
|
|
|
assume_bop_not_redefined(jitstate_t *jit, int redefined_flag, enum ruby_basic_operators bop)
|
2021-03-18 02:07:20 +03:00
|
|
|
{
|
|
|
|
if (BASIC_OP_UNREDEFINED_P(bop, redefined_flag)) {
|
2021-11-04 19:30:30 +03:00
|
|
|
RUBY_ASSERT(blocks_assuming_bops);
|
|
|
|
|
|
|
|
jit_ensure_block_entry_exit(jit);
|
|
|
|
st_insert(blocks_assuming_bops, (st_data_t)jit->block, 0);
|
2021-03-18 02:07:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
2021-02-25 23:10:38 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// Map klass => id_table[mid, set of blocks]
|
|
|
|
// While a block `b` is in the table, b->callee_cme == rb_callable_method_entry(klass, mid).
|
|
|
|
// See assume_method_lookup_stable()
|
2021-02-25 23:10:38 +03:00
|
|
|
static st_table *method_lookup_dependency;
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// For adding to method_lookup_dependency data with st_update
|
|
|
|
struct lookup_dependency_insertion {
|
|
|
|
block_t *block;
|
|
|
|
ID mid;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Map cme => set of blocks
|
|
|
|
// See assume_method_lookup_stable()
|
|
|
|
static st_table *cme_validity_dependency;
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
static int
|
2021-03-18 02:07:20 +03:00
|
|
|
add_cme_validity_dependency_i(st_data_t *key, st_data_t *value, st_data_t new_block, int existing)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-03-18 02:07:20 +03:00
|
|
|
st_table *block_set;
|
2020-12-09 00:54:41 +03:00
|
|
|
if (existing) {
|
2021-03-18 02:07:20 +03:00
|
|
|
block_set = (st_table *)*value;
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
2021-03-18 02:07:20 +03:00
|
|
|
else {
|
|
|
|
// Make the set and put it into cme_validity_dependency
|
|
|
|
block_set = st_init_numtable();
|
|
|
|
*value = (st_data_t)block_set;
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// Put block into set
|
|
|
|
st_insert(block_set, new_block, 1);
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
static int
|
|
|
|
add_lookup_dependency_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
|
2021-03-09 22:01:16 +03:00
|
|
|
{
|
2021-03-18 02:07:20 +03:00
|
|
|
struct lookup_dependency_insertion *info = (void *)data;
|
|
|
|
|
|
|
|
// Find or make an id table
|
|
|
|
struct rb_id_table *id2blocks;
|
|
|
|
if (existing) {
|
|
|
|
id2blocks = (void *)*value;
|
2021-03-09 22:01:16 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-03-18 02:07:20 +03:00
|
|
|
// Make an id table and put it into the st_table
|
|
|
|
id2blocks = rb_id_table_create(1);
|
|
|
|
*value = (st_data_t)id2blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find or make a block set
|
|
|
|
st_table *block_set;
|
|
|
|
{
|
|
|
|
VALUE blocks;
|
|
|
|
if (rb_id_table_lookup(id2blocks, info->mid, &blocks)) {
|
|
|
|
// Take existing set
|
|
|
|
block_set = (st_table *)blocks;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Make new block set and put it into the id table
|
|
|
|
block_set = st_init_numtable();
|
|
|
|
rb_id_table_insert(id2blocks, info->mid, (VALUE)block_set);
|
|
|
|
}
|
2021-03-09 22:01:16 +03:00
|
|
|
}
|
2021-03-18 02:07:20 +03:00
|
|
|
|
|
|
|
st_insert(block_set, (st_data_t)info->block, 1);
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
2021-03-09 22:01:16 +03:00
|
|
|
}
|
|
|
|
|
2021-06-24 03:28:42 +03:00
|
|
|
// Remember that a block assumes that
|
|
|
|
// `rb_callable_method_entry(receiver_klass, cme->called_id) == cme` and that
|
|
|
|
// `cme` is valid.
|
2021-03-18 02:07:20 +03:00
|
|
|
// When either of these assumptions becomes invalid, rb_yjit_method_lookup_change() or
|
|
|
|
// rb_yjit_cme_invalidate() invalidates the block.
|
2021-03-23 03:12:34 +03:00
|
|
|
//
|
|
|
|
// @raise NoMemoryError
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-11-04 19:30:30 +03:00
|
|
|
assume_method_lookup_stable(VALUE receiver_klass, const rb_callable_method_entry_t *cme, jitstate_t *jit)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-03-18 02:07:20 +03:00
|
|
|
RUBY_ASSERT(cme_validity_dependency);
|
|
|
|
RUBY_ASSERT(method_lookup_dependency);
|
2021-06-24 03:28:42 +03:00
|
|
|
RUBY_ASSERT(rb_callable_method_entry(receiver_klass, cme->called_id) == cme);
|
2021-08-31 06:58:53 +03:00
|
|
|
RUBY_ASSERT_ALWAYS(RB_TYPE_P(receiver_klass, T_CLASS) || RB_TYPE_P(receiver_klass, T_ICLASS));
|
2021-03-18 02:07:20 +03:00
|
|
|
RUBY_ASSERT_ALWAYS(!rb_objspace_garbage_object_p(receiver_klass));
|
|
|
|
|
2021-11-04 19:30:30 +03:00
|
|
|
jit_ensure_block_entry_exit(jit);
|
|
|
|
|
|
|
|
block_t *block = jit->block;
|
|
|
|
|
2021-08-08 08:49:31 +03:00
|
|
|
cme_dependency_t cme_dep = { receiver_klass, (VALUE)cme };
|
|
|
|
rb_darray_append(&block->cme_dependencies, cme_dep);
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_update(cme_validity_dependency, (st_data_t)cme, add_cme_validity_dependency_i, (st_data_t)block);
|
|
|
|
|
|
|
|
struct lookup_dependency_insertion info = { block, cme->called_id };
|
|
|
|
st_update(method_lookup_dependency, (st_data_t)receiver_klass, add_lookup_dependency_i, (st_data_t)&info);
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:10:38 +03:00
|
|
|
static st_table *blocks_assuming_single_ractor_mode;
|
|
|
|
|
|
|
|
// Can raise NoMemoryError.
|
|
|
|
RBIMPL_ATTR_NODISCARD()
|
2021-10-02 01:38:39 +03:00
|
|
|
static bool
|
2021-11-04 19:30:30 +03:00
|
|
|
assume_single_ractor_mode(jitstate_t *jit)
|
|
|
|
{
|
2021-02-25 23:10:38 +03:00
|
|
|
if (rb_multi_ractor_p()) return false;
|
|
|
|
|
2021-11-04 19:30:30 +03:00
|
|
|
jit_ensure_block_entry_exit(jit);
|
|
|
|
|
|
|
|
st_insert(blocks_assuming_single_ractor_mode, (st_data_t)jit->block, 1);
|
2021-02-25 23:10:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static st_table *blocks_assuming_stable_global_constant_state;
|
|
|
|
|
|
|
|
// Assume that the global constant state has not changed since call to this function.
|
|
|
|
// Can raise NoMemoryError.
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-11-04 19:30:30 +03:00
|
|
|
assume_stable_global_constant_state(jitstate_t *jit)
|
2021-10-02 01:38:39 +03:00
|
|
|
{
|
2021-11-04 19:30:30 +03:00
|
|
|
jit_ensure_block_entry_exit(jit);
|
|
|
|
st_insert(blocks_assuming_stable_global_constant_state, (st_data_t)jit->block, 1);
|
2021-02-25 23:10:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
static int
|
2021-04-01 17:44:45 +03:00
|
|
|
mark_and_pin_keys_i(st_data_t k, st_data_t v, st_data_t ignore)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-04-01 17:44:45 +03:00
|
|
|
rb_gc_mark((VALUE)k);
|
2021-02-13 01:12:18 +03:00
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
// GC callback during mark phase
|
|
|
|
static void
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_root_mark(void *ptr)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
|
|
|
if (method_lookup_dependency) {
|
2021-03-18 02:07:20 +03:00
|
|
|
// TODO: This is a leak. Unused blocks linger in the table forever, preventing the
|
|
|
|
// callee class they speculate on from being collected.
|
|
|
|
// We could do a bespoke weak reference scheme on classes similar to
|
|
|
|
// the interpreter's call cache. See finalizer for T_CLASS and cc_table_free().
|
2021-04-01 17:44:45 +03:00
|
|
|
st_foreach(method_lookup_dependency, mark_and_pin_keys_i, 0);
|
2021-03-18 02:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cme_validity_dependency) {
|
2021-04-01 17:44:45 +03:00
|
|
|
// Why not let the GC move the cme keys in this table?
|
|
|
|
// Because this is basically a compare_by_identity Hash.
|
|
|
|
// If a key moves, we would need to reinsert it into the table so it is rehashed.
|
|
|
|
// That is tricky to do, espcially as it could trigger allocation which could
|
|
|
|
// trigger GC. Not sure if it is okay to trigger GC while the GC is updating
|
|
|
|
// references.
|
|
|
|
st_foreach(cme_validity_dependency, mark_and_pin_keys_i, 0);
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_root_free(void *ptr)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
|
|
|
// Do nothing. The root lives as long as the process.
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_root_memsize(const void *ptr)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
|
|
|
// Count off-gc-heap allocation size of the dependency table
|
|
|
|
return st_memsize(method_lookup_dependency); // TODO: more accurate accounting
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:07:27 +03:00
|
|
|
// GC callback during compaction
|
|
|
|
static void
|
|
|
|
yjit_root_update_references(void *ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
// Custom type for interacting with the GC
|
|
|
|
// TODO: make this write barrier protected
|
2021-03-07 02:46:56 +03:00
|
|
|
static const rb_data_type_t yjit_root_type = {
|
|
|
|
"yjit_root",
|
|
|
|
{yjit_root_mark, yjit_root_free, yjit_root_memsize, yjit_root_update_references},
|
2020-12-09 00:54:41 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
2021-09-22 23:56:42 +03:00
|
|
|
// st_table iterator for invalidating blocks that are keys to the table.
|
2021-03-18 02:07:20 +03:00
|
|
|
static int
|
|
|
|
block_set_invalidate_i(st_data_t key, st_data_t v, st_data_t ignore)
|
|
|
|
{
|
|
|
|
block_t *version = (block_t *)key;
|
|
|
|
|
2021-09-22 23:56:42 +03:00
|
|
|
// Thankfully, st_table supports deleting while iterating.
|
2021-03-18 02:07:20 +03:00
|
|
|
invalidate_block_version(version);
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for when rb_callable_method_entry(klass, mid) is going to change.
|
|
|
|
// Invalidate blocks that assume stable method lookup of `mid` in `klass` when this happens.
|
2020-12-09 00:54:41 +03:00
|
|
|
void
|
2021-03-18 02:07:20 +03:00
|
|
|
rb_yjit_method_lookup_change(VALUE klass, ID mid)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-03-18 02:07:20 +03:00
|
|
|
if (!method_lookup_dependency) return;
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-01-29 20:07:18 +03:00
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_data_t image;
|
|
|
|
st_data_t key = (st_data_t)klass;
|
|
|
|
if (st_lookup(method_lookup_dependency, key, &image)) {
|
|
|
|
struct rb_id_table *id2blocks = (void *)image;
|
|
|
|
VALUE blocks;
|
|
|
|
|
|
|
|
// Invalidate all blocks in method_lookup_dependency[klass][mid]
|
|
|
|
if (rb_id_table_lookup(id2blocks, mid, &blocks)) {
|
|
|
|
rb_id_table_delete(id2blocks, mid);
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_table *block_set = (st_table *)blocks;
|
2021-09-22 23:56:42 +03:00
|
|
|
|
|
|
|
#if YJIT_STATS
|
|
|
|
yjit_runtime_counters.invalidate_method_lookup += block_set->num_entries;
|
|
|
|
#endif
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_foreach(block_set, block_set_invalidate_i, 0);
|
2021-01-29 20:07:18 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_free_table(block_set);
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
2021-03-18 02:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for when a cme becomes invalid.
|
|
|
|
// Invalidate all blocks that depend on cme being valid.
|
|
|
|
void
|
|
|
|
rb_yjit_cme_invalidate(VALUE cme)
|
|
|
|
{
|
|
|
|
if (!cme_validity_dependency) return;
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(cme, imemo_ment));
|
|
|
|
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
|
|
|
|
// Delete the block set from the table
|
|
|
|
st_data_t cme_as_st_data = (st_data_t)cme;
|
|
|
|
st_data_t blocks;
|
|
|
|
if (st_delete(cme_validity_dependency, &cme_as_st_data, &blocks)) {
|
|
|
|
st_table *block_set = (st_table *)blocks;
|
|
|
|
|
2021-09-22 23:56:42 +03:00
|
|
|
#if YJIT_STATS
|
|
|
|
yjit_runtime_counters.invalidate_method_lookup += block_set->num_entries;
|
|
|
|
#endif
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// Invalidate each block
|
|
|
|
st_foreach(block_set, block_set_invalidate_i, 0);
|
|
|
|
|
|
|
|
st_free_table(block_set);
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
2021-01-29 20:07:18 +03:00
|
|
|
|
|
|
|
RB_VM_LOCK_LEAVE();
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// For dealing with refinements
|
|
|
|
void
|
|
|
|
rb_yjit_invalidate_all_method_lookup_assumptions(void)
|
|
|
|
{
|
2021-10-02 01:38:39 +03:00
|
|
|
// It looks like Module#using actually doesn't need to invalidate all the
|
|
|
|
// method caches, so we do nothing here for now.
|
2021-03-18 02:07:20 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
// Remove a block from the method lookup dependency table
|
|
|
|
static void
|
2021-08-08 08:49:31 +03:00
|
|
|
remove_method_lookup_dependency(block_t *block, VALUE receiver_klass, const rb_callable_method_entry_t *callee_cme)
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
2021-08-08 08:49:31 +03:00
|
|
|
RUBY_ASSERT(receiver_klass);
|
|
|
|
RUBY_ASSERT(callee_cme); // callee_cme should be set when receiver_klass is set
|
2021-03-18 02:07:20 +03:00
|
|
|
|
|
|
|
st_data_t image;
|
2021-08-08 08:49:31 +03:00
|
|
|
st_data_t key = (st_data_t)receiver_klass;
|
2021-02-13 01:12:18 +03:00
|
|
|
if (st_lookup(method_lookup_dependency, key, &image)) {
|
2021-03-18 02:07:20 +03:00
|
|
|
struct rb_id_table *id2blocks = (void *)image;
|
2021-08-08 08:49:31 +03:00
|
|
|
ID mid = callee_cme->called_id;
|
2021-03-18 02:07:20 +03:00
|
|
|
|
|
|
|
// Find block set
|
|
|
|
VALUE blocks;
|
|
|
|
if (rb_id_table_lookup(id2blocks, mid, &blocks)) {
|
|
|
|
st_table *block_set = (st_table *)blocks;
|
|
|
|
|
|
|
|
// Remove block from block set
|
|
|
|
st_data_t block_as_st_data = (st_data_t)block;
|
|
|
|
(void)st_delete(block_set, &block_as_st_data, NULL);
|
|
|
|
|
|
|
|
if (block_set->num_entries == 0) {
|
|
|
|
// Block set now empty. Remove from id table.
|
|
|
|
rb_id_table_delete(id2blocks, mid);
|
|
|
|
st_free_table(block_set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a block from cme_validity_dependency
|
|
|
|
static void
|
2021-08-08 08:49:31 +03:00
|
|
|
remove_cme_validity_dependency(block_t *block, const rb_callable_method_entry_t *callee_cme)
|
2021-03-18 02:07:20 +03:00
|
|
|
{
|
2021-08-08 08:49:31 +03:00
|
|
|
RUBY_ASSERT(callee_cme);
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_data_t blocks;
|
2021-08-08 08:49:31 +03:00
|
|
|
if (st_lookup(cme_validity_dependency, (st_data_t)callee_cme, &blocks)) {
|
2021-03-18 02:07:20 +03:00
|
|
|
st_table *block_set = (st_table *)blocks;
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
st_data_t block_as_st_data = (st_data_t)block;
|
|
|
|
(void)st_delete(block_set, &block_as_st_data, NULL);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_unlink_method_lookup_dependency(block_t *block)
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
2021-08-08 08:49:31 +03:00
|
|
|
cme_dependency_t *cme_dep;
|
|
|
|
rb_darray_foreach(block->cme_dependencies, cme_dependency_idx, cme_dep) {
|
|
|
|
remove_method_lookup_dependency(block, cme_dep->receiver_klass, (const rb_callable_method_entry_t *)cme_dep->callee_cme);
|
|
|
|
remove_cme_validity_dependency(block, (const rb_callable_method_entry_t *)cme_dep->callee_cme);
|
|
|
|
}
|
|
|
|
rb_darray_free(block->cme_dependencies);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2021-10-02 01:38:39 +03:00
|
|
|
static void
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_block_assumptions_free(block_t *block)
|
2021-02-25 23:10:38 +03:00
|
|
|
{
|
|
|
|
st_data_t as_st_data = (st_data_t)block;
|
|
|
|
if (blocks_assuming_stable_global_constant_state) {
|
|
|
|
st_delete(blocks_assuming_stable_global_constant_state, &as_st_data, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blocks_assuming_single_ractor_mode) {
|
|
|
|
st_delete(blocks_assuming_single_ractor_mode, &as_st_data, NULL);
|
|
|
|
}
|
2021-03-09 22:01:16 +03:00
|
|
|
|
|
|
|
if (blocks_assuming_bops) {
|
|
|
|
st_delete(blocks_assuming_bops, &as_st_data, NULL);
|
|
|
|
}
|
2021-02-25 23:10:38 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 22:40:34 +03:00
|
|
|
typedef VALUE (*yjit_func_t)(rb_execution_context_t *, rb_control_frame_t *);
|
|
|
|
|
2021-07-07 21:15:40 +03:00
|
|
|
bool
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-07-16 01:43:39 +03:00
|
|
|
#if (OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE) && JIT_ENABLED
|
2021-07-07 21:15:40 +03:00
|
|
|
bool success = true;
|
2020-12-09 00:54:41 +03:00
|
|
|
RB_VM_LOCK_ENTER();
|
2021-10-27 02:57:30 +03:00
|
|
|
rb_vm_barrier();
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2020-12-10 08:06:10 +03:00
|
|
|
// Compile a block version starting at the first instruction
|
2021-09-29 21:58:01 +03:00
|
|
|
uint8_t *code_ptr = gen_entry_point(iseq, 0, ec);
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-11-20 07:44:13 +03:00
|
|
|
if (code_ptr) {
|
2021-07-15 22:40:34 +03:00
|
|
|
iseq->body->jit_func = (yjit_func_t)code_ptr;
|
2021-07-07 21:15:40 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
iseq->body->jit_func = 0;
|
|
|
|
success = false;
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
2020-12-10 08:06:10 +03:00
|
|
|
|
2020-12-09 00:54:41 +03:00
|
|
|
RB_VM_LOCK_LEAVE();
|
2021-07-07 21:15:40 +03:00
|
|
|
return success;
|
2021-07-16 01:43:39 +03:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
struct yjit_block_itr {
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
const rb_iseq_t *iseq;
|
|
|
|
VALUE list;
|
|
|
|
};
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
/* Get a list of the YJIT blocks associated with `rb_iseq` */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
static VALUE
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_blocks_for(VALUE mod, VALUE rb_iseq)
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
{
|
2021-02-04 21:00:29 +03:00
|
|
|
if (CLASS_OF(rb_iseq) != rb_cISeq) {
|
2021-02-04 20:29:36 +03:00
|
|
|
return rb_ary_new();
|
|
|
|
}
|
2021-02-13 01:12:18 +03:00
|
|
|
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
const rb_iseq_t *iseq = rb_iseqw_to_iseq(rb_iseq);
|
|
|
|
|
2021-03-04 20:05:18 +03:00
|
|
|
VALUE all_versions = rb_ary_new();
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_darray_for(iseq->body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t versions = rb_darray_get(iseq->body->yjit_blocks, version_array_idx);
|
2021-03-04 23:31:37 +03:00
|
|
|
|
|
|
|
rb_darray_for(versions, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(versions, block_idx);
|
|
|
|
|
2021-04-07 22:27:05 +03:00
|
|
|
// FIXME: The object craeted here can outlive the block itself
|
2021-03-07 02:46:56 +03:00
|
|
|
VALUE rb_block = TypedData_Wrap_Struct(cYjitBlock, &yjit_block_type, block);
|
2021-02-13 01:12:18 +03:00
|
|
|
rb_ary_push(all_versions, rb_block);
|
|
|
|
}
|
|
|
|
}
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
return all_versions;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
}
|
|
|
|
|
2021-11-02 12:18:26 +03:00
|
|
|
/* Get the address of the code associated with a YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
static VALUE
|
|
|
|
block_address(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-07 02:46:56 +03:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
2021-11-04 23:05:41 +03:00
|
|
|
return LONG2NUM((intptr_t)block->start_addr);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
/* Get the machine code for YJIT::Block as a binary string */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
static VALUE
|
|
|
|
block_code(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-07 02:46:56 +03:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-01-22 20:22:34 +03:00
|
|
|
return (VALUE)rb_str_new(
|
2021-11-04 23:05:41 +03:00
|
|
|
(const char*)block->start_addr,
|
|
|
|
block->end_addr - block->start_addr
|
2021-01-22 20:22:34 +03:00
|
|
|
);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the start index in the Instruction Sequence that corresponds to this
|
2021-03-07 02:46:56 +03:00
|
|
|
* YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
static VALUE
|
|
|
|
iseq_start_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-07 02:46:56 +03:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
|
|
|
return INT2NUM(block->blockid.idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the end index in the Instruction Sequence that corresponds to this
|
2021-03-07 02:46:56 +03:00
|
|
|
* YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
static VALUE
|
|
|
|
iseq_end_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-07 02:46:56 +03:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
|
|
|
return INT2NUM(block->end_idx);
|
|
|
|
}
|
|
|
|
|
2021-03-09 22:01:16 +03:00
|
|
|
/* Called when a basic operation is redefined */
|
|
|
|
void
|
|
|
|
rb_yjit_bop_redefined(VALUE klass, const rb_method_entry_t *me, enum ruby_basic_operators bop)
|
|
|
|
{
|
2021-03-11 19:19:02 +03:00
|
|
|
if (blocks_assuming_bops) {
|
2021-09-22 23:56:42 +03:00
|
|
|
#if YJIT_STATS
|
|
|
|
yjit_runtime_counters.invalidate_bop_redefined += blocks_assuming_bops->num_entries;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
st_foreach(blocks_assuming_bops, block_set_invalidate_i, 0);
|
2021-03-11 19:19:02 +03:00
|
|
|
}
|
2021-03-09 22:01:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 02:09:34 +03:00
|
|
|
/* Called when the constant state changes */
|
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_constant_state_changed(void)
|
2021-02-04 02:09:34 +03:00
|
|
|
{
|
2021-02-25 23:10:38 +03:00
|
|
|
if (blocks_assuming_stable_global_constant_state) {
|
2021-09-15 20:59:50 +03:00
|
|
|
#if YJIT_STATS
|
|
|
|
yjit_runtime_counters.constant_state_bumps++;
|
2021-09-22 23:56:42 +03:00
|
|
|
yjit_runtime_counters.invalidate_constant_state_bump += blocks_assuming_stable_global_constant_state->num_entries;
|
2021-09-15 20:59:50 +03:00
|
|
|
#endif
|
2021-09-22 23:56:42 +03:00
|
|
|
|
|
|
|
st_foreach(blocks_assuming_stable_global_constant_state, block_set_invalidate_i, 0);
|
2021-02-25 23:10:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:55:10 +03:00
|
|
|
// Callback from the opt_setinlinecache instruction in the interpreter.
|
|
|
|
// Invalidate the block for the matching opt_getinlinecache so it could regenerate code
|
|
|
|
// using the new value in the constant cache.
|
2021-03-25 01:07:26 +03:00
|
|
|
void
|
2021-12-07 01:09:52 +03:00
|
|
|
rb_yjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic)
|
2021-03-25 01:07:26 +03:00
|
|
|
{
|
2021-10-22 20:41:34 +03:00
|
|
|
if (!rb_yjit_enabled_p()) return;
|
|
|
|
|
2021-09-20 21:55:10 +03:00
|
|
|
// We can't generate code in these situations, so no need to invalidate.
|
|
|
|
// See gen_opt_getinlinecache.
|
|
|
|
if (ic->entry->ic_cref || rb_multi_ractor_p()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-25 01:07:26 +03:00
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
rb_vm_barrier(); // Stop other ractors since we are going to patch machine code.
|
|
|
|
{
|
|
|
|
const struct rb_iseq_constant_body *const body = iseq->body;
|
|
|
|
VALUE *code = body->iseq_encoded;
|
2021-12-07 01:09:52 +03:00
|
|
|
const unsigned get_insn_idx = ic->get_insn_idx;
|
2021-03-25 01:07:26 +03:00
|
|
|
|
|
|
|
// This should come from a running iseq, so direct threading translation
|
|
|
|
// should have been done
|
|
|
|
RUBY_ASSERT(FL_TEST((VALUE)iseq, ISEQ_TRANSLATED));
|
2021-12-07 01:09:52 +03:00
|
|
|
RUBY_ASSERT(get_insn_idx < body->iseq_size);
|
|
|
|
RUBY_ASSERT(rb_vm_insn_addr2insn((const void *)code[get_insn_idx]) == BIN(opt_getinlinecache));
|
2021-03-25 01:07:26 +03:00
|
|
|
|
|
|
|
// Find the matching opt_getinlinecache and invalidate all the blocks there
|
|
|
|
RUBY_ASSERT(insn_op_type(BIN(opt_getinlinecache), 1) == TS_IC);
|
2021-12-07 01:09:52 +03:00
|
|
|
if (ic == (IC)code[get_insn_idx + 1 + 1]) {
|
|
|
|
rb_yjit_block_array_t getinlinecache_blocks = yjit_get_version_array(iseq, get_insn_idx);
|
|
|
|
|
|
|
|
// Put a bound for loop below to be defensive
|
|
|
|
const int32_t initial_version_count = rb_darray_size(getinlinecache_blocks);
|
|
|
|
for (int32_t iteration=0; iteration<initial_version_count; ++iteration) {
|
|
|
|
getinlinecache_blocks = yjit_get_version_array(iseq, get_insn_idx);
|
|
|
|
|
|
|
|
if (rb_darray_size(getinlinecache_blocks) > 0) {
|
|
|
|
block_t *block = rb_darray_get(getinlinecache_blocks, 0);
|
|
|
|
invalidate_block_version(block);
|
2021-09-22 23:56:42 +03:00
|
|
|
#if YJIT_STATS
|
2021-12-07 01:09:52 +03:00
|
|
|
yjit_runtime_counters.invalidate_constant_ic_fill++;
|
2021-09-22 23:56:42 +03:00
|
|
|
#endif
|
2021-12-07 01:09:52 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-25 01:07:26 +03:00
|
|
|
}
|
2021-12-07 01:09:52 +03:00
|
|
|
|
|
|
|
// All versions at get_insn_idx should now be gone
|
|
|
|
RUBY_ASSERT(0 == rb_darray_size(yjit_get_version_array(iseq, get_insn_idx)));
|
2021-03-25 01:07:26 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
RUBY_ASSERT(false && "ic->get_insn_diex not set properly");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
}
|
|
|
|
|
2021-02-25 23:10:38 +03:00
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_before_ractor_spawn(void)
|
2021-02-25 23:10:38 +03:00
|
|
|
{
|
|
|
|
if (blocks_assuming_single_ractor_mode) {
|
2021-09-22 23:56:42 +03:00
|
|
|
#if YJIT_STATS
|
|
|
|
yjit_runtime_counters.invalidate_ractor_spawn += blocks_assuming_single_ractor_mode->num_entries;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
st_foreach(blocks_assuming_single_ractor_mode, block_set_invalidate_i, 0);
|
2021-02-25 23:10:38 +03:00
|
|
|
}
|
2021-02-04 02:09:34 +03:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:17:09 +03:00
|
|
|
#ifdef HAVE_LIBCAPSTONE
|
2021-03-07 02:46:56 +03:00
|
|
|
static const rb_data_type_t yjit_disasm_type = {
|
|
|
|
"YJIT/Disasm",
|
2021-01-22 22:26:20 +03:00
|
|
|
{0, (void(*)(void *))cs_close, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
static VALUE
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_disasm_init(VALUE klass)
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
{
|
|
|
|
csh * handle;
|
2021-03-07 02:46:56 +03:00
|
|
|
VALUE disasm = TypedData_Make_Struct(klass, csh, &yjit_disasm_type, handle);
|
2021-03-12 20:22:19 +03:00
|
|
|
if (cs_open(CS_ARCH_X86, CS_MODE_64, handle) != CS_ERR_OK) {
|
|
|
|
rb_raise(rb_eRuntimeError, "failed to make Capstone handle");
|
|
|
|
}
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
return disasm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_disasm(VALUE self, VALUE code, VALUE from)
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
csh * handle;
|
|
|
|
cs_insn *insns;
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
TypedData_Get_Struct(self, csh, &yjit_disasm_type, handle);
|
2021-03-12 20:22:19 +03:00
|
|
|
count = cs_disasm(*handle, (uint8_t*)StringValuePtr(code), RSTRING_LEN(code), NUM2ULL(from), 0, &insns);
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
VALUE insn_list = rb_ary_new_capa(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
VALUE vals = rb_ary_new_from_args(3, LONG2NUM(insns[i].address),
|
|
|
|
rb_str_new2(insns[i].mnemonic),
|
|
|
|
rb_str_new2(insns[i].op_str));
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_ary_push(insn_list, rb_struct_alloc(cYjitDisasmInsn, vals));
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
}
|
|
|
|
cs_free(insns, count);
|
|
|
|
return insn_list;
|
|
|
|
}
|
2021-01-22 22:26:20 +03:00
|
|
|
#endif
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
|
2021-04-07 22:27:05 +03:00
|
|
|
// Primitive called in yjit.rb. Export all machine code comments as a Ruby array.
|
|
|
|
static VALUE
|
|
|
|
comments_for(rb_execution_context_t *ec, VALUE self, VALUE start_address, VALUE end_address)
|
|
|
|
{
|
|
|
|
VALUE comment_array = rb_ary_new();
|
|
|
|
#if RUBY_DEBUG
|
|
|
|
uint8_t *start = (void *)NUM2ULL(start_address);
|
|
|
|
uint8_t *end = (void *)NUM2ULL(end_address);
|
|
|
|
|
|
|
|
rb_darray_for(yjit_code_comments, i) {
|
|
|
|
struct yjit_comment comment = rb_darray_get(yjit_code_comments, i);
|
|
|
|
uint8_t *comment_pos = cb_get_ptr(cb, comment.offset);
|
|
|
|
|
|
|
|
if (comment_pos >= end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (comment_pos >= start) {
|
|
|
|
VALUE vals = rb_ary_new_from_args(
|
|
|
|
2,
|
|
|
|
LL2NUM((long long) comment_pos),
|
|
|
|
rb_str_new_cstr(comment.comment)
|
|
|
|
);
|
|
|
|
rb_ary_push(comment_array, rb_struct_alloc(cYjitCodeComment, vals));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // if RUBY_DEBUG
|
|
|
|
|
|
|
|
return comment_array;
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:15:46 +03:00
|
|
|
static VALUE
|
|
|
|
yjit_stats_enabled_p(rb_execution_context_t *ec, VALUE self)
|
|
|
|
{
|
|
|
|
return RBOOL(YJIT_STATS && rb_yjit_opts.gen_stats);
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:37:12 +03:00
|
|
|
// Primitive called in yjit.rb. Export all YJIT statistics as a Ruby hash.
|
2021-03-03 02:27:50 +03:00
|
|
|
static VALUE
|
2021-06-29 17:37:12 +03:00
|
|
|
get_yjit_stats(rb_execution_context_t *ec, VALUE self)
|
2021-03-03 02:27:50 +03:00
|
|
|
{
|
2021-07-27 18:54:45 +03:00
|
|
|
// Return Qnil if YJIT isn't enabled
|
|
|
|
if (cb == NULL) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2021-03-03 02:27:50 +03:00
|
|
|
VALUE hash = rb_hash_new();
|
2021-07-19 14:19:25 +03:00
|
|
|
|
2021-03-03 02:27:50 +03:00
|
|
|
RB_VM_LOCK_ENTER();
|
2021-06-29 17:37:12 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
VALUE key = ID2SYM(rb_intern("inline_code_size"));
|
|
|
|
VALUE value = LL2NUM((long long)cb->write_pos);
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
|
|
|
|
key = ID2SYM(rb_intern("outlined_code_size"));
|
|
|
|
value = LL2NUM((long long)ocb->write_pos);
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:32:07 +03:00
|
|
|
#if YJIT_STATS
|
2021-07-19 14:19:25 +03:00
|
|
|
if (rb_yjit_opts.gen_stats) {
|
2021-07-20 19:34:04 +03:00
|
|
|
// Indicate that the complete set of stats is available
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("all_stats")), Qtrue);
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
int64_t *counter_reader = (int64_t *)&yjit_runtime_counters;
|
|
|
|
int64_t *counter_reader_end = &yjit_runtime_counters.last_member;
|
2021-03-03 02:27:50 +03:00
|
|
|
|
2021-07-19 14:19:25 +03:00
|
|
|
// For each counter in yjit_counter_names, add that counter as
|
|
|
|
// a key/value pair.
|
|
|
|
|
2021-03-03 02:27:50 +03:00
|
|
|
// Iterate through comma separated counter name list
|
2021-03-07 02:46:56 +03:00
|
|
|
char *name_reader = yjit_counter_names;
|
|
|
|
char *counter_name_end = yjit_counter_names + sizeof(yjit_counter_names);
|
2021-03-03 02:27:50 +03:00
|
|
|
while (name_reader < counter_name_end && counter_reader < counter_reader_end) {
|
|
|
|
if (*name_reader == ',' || *name_reader == ' ') {
|
|
|
|
name_reader++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:19:25 +03:00
|
|
|
// Compute length of counter name
|
2021-03-03 02:27:50 +03:00
|
|
|
int name_len;
|
|
|
|
char *name_end;
|
|
|
|
{
|
|
|
|
name_end = strchr(name_reader, ',');
|
|
|
|
if (name_end == NULL) break;
|
|
|
|
name_len = (int)(name_end - name_reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put counter into hash
|
|
|
|
VALUE key = ID2SYM(rb_intern2(name_reader, name_len));
|
|
|
|
VALUE value = LL2NUM((long long)*counter_reader);
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
|
|
|
|
counter_reader++;
|
|
|
|
name_reader = name_end;
|
|
|
|
}
|
2021-06-29 17:37:12 +03:00
|
|
|
|
2021-07-19 14:19:25 +03:00
|
|
|
// For each entry in exit_op_count, add a stats entry with key "exit_INSTRUCTION_NAME"
|
|
|
|
// and the value is the count of side exits for that instruction.
|
2021-06-29 17:37:12 +03:00
|
|
|
|
2021-06-29 20:25:33 +03:00
|
|
|
char key_string[rb_vm_max_insn_name_size + 6]; // Leave room for "exit_" and a final NUL
|
2021-06-29 17:37:12 +03:00
|
|
|
for (int i = 0; i < VM_INSTRUCTION_SIZE; i++) {
|
2021-06-29 20:25:33 +03:00
|
|
|
const char *i_name = insn_name(i); // Look up Ruby's NUL-terminated insn name string
|
2021-06-30 12:00:07 +03:00
|
|
|
snprintf(key_string, rb_vm_max_insn_name_size + 6, "%s%s", "exit_", i_name);
|
2021-06-29 17:37:12 +03:00
|
|
|
|
|
|
|
VALUE key = ID2SYM(rb_intern(key_string));
|
|
|
|
VALUE value = LL2NUM((long long)exit_op_count[i]);
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 14:19:25 +03:00
|
|
|
#endif
|
2021-06-29 17:37:12 +03:00
|
|
|
|
2021-03-03 02:27:50 +03:00
|
|
|
RB_VM_LOCK_LEAVE();
|
2021-07-19 14:19:25 +03:00
|
|
|
|
2021-03-03 02:27:50 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
// Primitive called in yjit.rb. Zero out all the counters.
|
2021-03-04 00:56:24 +03:00
|
|
|
static VALUE
|
|
|
|
reset_stats_bang(rb_execution_context_t *ec, VALUE self)
|
|
|
|
{
|
2021-08-24 13:32:07 +03:00
|
|
|
#if YJIT_STATS
|
2021-03-04 00:56:24 +03:00
|
|
|
memset(&exit_op_count, 0, sizeof(exit_op_count));
|
2021-03-07 02:46:56 +03:00
|
|
|
memset(&yjit_runtime_counters, 0, sizeof(yjit_runtime_counters));
|
2021-08-24 13:32:07 +03:00
|
|
|
#endif // if YJIT_STATS
|
2021-03-04 00:56:24 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2021-11-04 19:30:30 +03:00
|
|
|
// Primitive for yjit.rb. For testing running out of executable memory
|
|
|
|
static VALUE
|
|
|
|
simulate_oom_bang(rb_execution_context_t *ec, VALUE self)
|
|
|
|
{
|
|
|
|
if (RUBY_DEBUG && cb && ocb) {
|
|
|
|
// Only simulate in debug builds for paranoia.
|
|
|
|
cb_set_pos(cb, cb->mem_size-1);
|
|
|
|
cb_set_pos(ocb, ocb->mem_size-1);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
#include "yjit.rbinc"
|
2021-03-03 02:27:50 +03:00
|
|
|
|
2021-08-24 13:32:07 +03:00
|
|
|
#if YJIT_STATS
|
2021-01-28 00:13:27 +03:00
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_collect_vm_usage_insn(int insn)
|
2021-01-28 00:13:27 +03:00
|
|
|
{
|
2021-06-28 20:06:03 +03:00
|
|
|
yjit_runtime_counters.vm_insns_count++;
|
2021-01-28 00:13:27 +03:00
|
|
|
}
|
2021-04-07 22:51:50 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_yjit_collect_binding_alloc(void)
|
|
|
|
{
|
|
|
|
yjit_runtime_counters.binding_allocations++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_yjit_collect_binding_set(void)
|
|
|
|
{
|
|
|
|
yjit_runtime_counters.binding_set++;
|
|
|
|
}
|
2021-01-28 00:13:27 +03:00
|
|
|
|
2021-10-02 01:38:39 +03:00
|
|
|
static const VALUE *
|
|
|
|
yjit_count_side_exit_op(const VALUE *exit_pc)
|
2021-01-28 00:13:27 +03:00
|
|
|
{
|
|
|
|
int insn = rb_vm_insn_addr2opcode((const void *)*exit_pc);
|
|
|
|
exit_op_count[insn]++;
|
|
|
|
return exit_pc; // This function must return exit_pc!
|
|
|
|
}
|
2021-06-29 18:52:09 +03:00
|
|
|
#endif
|
2021-01-28 00:13:27 +03:00
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_iseq_mark(const struct rb_iseq_constant_body *body)
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 20:05:18 +03:00
|
|
|
|
2021-03-04 23:31:37 +03:00
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
2021-03-04 20:05:18 +03:00
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
rb_gc_mark_movable((VALUE)block->blockid.iseq);
|
2021-08-08 08:49:31 +03:00
|
|
|
|
|
|
|
cme_dependency_t *cme_dep;
|
|
|
|
rb_darray_foreach(block->cme_dependencies, cme_dependency_idx, cme_dep) {
|
|
|
|
rb_gc_mark_movable(cme_dep->receiver_klass);
|
|
|
|
rb_gc_mark_movable(cme_dep->callee_cme);
|
|
|
|
}
|
2021-02-19 23:03:12 +03:00
|
|
|
|
2021-04-20 00:07:27 +03:00
|
|
|
// Mark outgoing branch entries
|
|
|
|
rb_darray_for(block->outgoing, branch_idx) {
|
2021-09-29 21:58:01 +03:00
|
|
|
branch_t *branch = rb_darray_get(block->outgoing, branch_idx);
|
2021-04-20 00:07:27 +03:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
rb_gc_mark_movable((VALUE)branch->targets[i].iseq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:03:12 +03:00
|
|
|
// Walk over references to objects in generated code.
|
|
|
|
uint32_t *offset_element;
|
|
|
|
rb_darray_foreach(block->gc_object_offsets, offset_idx, offset_element) {
|
|
|
|
uint32_t offset_to_value = *offset_element;
|
|
|
|
uint8_t *value_address = cb_get_ptr(cb, offset_to_value);
|
|
|
|
|
|
|
|
VALUE object;
|
|
|
|
memcpy(&object, value_address, SIZEOF_VALUE);
|
|
|
|
rb_gc_mark_movable(object);
|
|
|
|
}
|
2021-07-13 21:56:02 +03:00
|
|
|
|
|
|
|
// Mark the machine code page this block lives on
|
2021-11-04 23:05:41 +03:00
|
|
|
//rb_gc_mark_movable(block->code_page);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_iseq_update_references(const struct rb_iseq_constant_body *body)
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
2021-10-27 02:57:30 +03:00
|
|
|
rb_vm_barrier();
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 23:31:37 +03:00
|
|
|
|
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
block->blockid.iseq = (const rb_iseq_t *)rb_gc_location((VALUE)block->blockid.iseq);
|
|
|
|
|
2021-08-08 08:49:31 +03:00
|
|
|
cme_dependency_t *cme_dep;
|
|
|
|
rb_darray_foreach(block->cme_dependencies, cme_dependency_idx, cme_dep) {
|
|
|
|
cme_dep->receiver_klass = rb_gc_location(cme_dep->receiver_klass);
|
|
|
|
cme_dep->callee_cme = rb_gc_location(cme_dep->callee_cme);
|
|
|
|
}
|
2021-02-19 23:03:12 +03:00
|
|
|
|
2021-04-20 00:07:27 +03:00
|
|
|
// Update outgoing branch entries
|
|
|
|
rb_darray_for(block->outgoing, branch_idx) {
|
2021-09-29 21:58:01 +03:00
|
|
|
branch_t *branch = rb_darray_get(block->outgoing, branch_idx);
|
2021-04-20 00:07:27 +03:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
branch->targets[i].iseq = (const void *)rb_gc_location((VALUE)branch->targets[i].iseq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:03:12 +03:00
|
|
|
// Walk over references to objects in generated code.
|
|
|
|
uint32_t *offset_element;
|
|
|
|
rb_darray_foreach(block->gc_object_offsets, offset_idx, offset_element) {
|
|
|
|
uint32_t offset_to_value = *offset_element;
|
|
|
|
uint8_t *value_address = cb_get_ptr(cb, offset_to_value);
|
|
|
|
|
|
|
|
VALUE object;
|
|
|
|
memcpy(&object, value_address, SIZEOF_VALUE);
|
|
|
|
VALUE possibly_moved = rb_gc_location(object);
|
|
|
|
// Only write when the VALUE moves, to be CoW friendly.
|
|
|
|
if (possibly_moved != object) {
|
2021-10-27 02:57:30 +03:00
|
|
|
// Possibly unlock the page we need to update
|
|
|
|
cb_mark_position_writeable(cb, offset_to_value);
|
|
|
|
|
|
|
|
// Object could cross a page boundary, so unlock there as well
|
|
|
|
cb_mark_position_writeable(cb, offset_to_value + SIZEOF_VALUE - 1);
|
2021-02-19 23:03:12 +03:00
|
|
|
memcpy(value_address, &possibly_moved, SIZEOF_VALUE);
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 21:56:02 +03:00
|
|
|
|
|
|
|
// Update the machine code page this block lives on
|
2021-11-04 23:05:41 +03:00
|
|
|
//block->code_page = rb_gc_location(block->code_page);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 23:32:47 +03:00
|
|
|
|
|
|
|
/* If YJIT isn't initialized, then cb or ocb could be NULL. */
|
|
|
|
if (cb) {
|
|
|
|
cb_mark_all_executable(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ocb) {
|
|
|
|
cb_mark_all_executable(ocb);
|
|
|
|
}
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
// Free the yjit resources associated with an iseq
|
2021-02-13 01:12:18 +03:00
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_iseq_free(const struct rb_iseq_constant_body *body)
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 23:31:37 +03:00
|
|
|
|
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_free_block(block);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
2021-03-04 23:31:37 +03:00
|
|
|
|
|
|
|
rb_darray_free(version_array);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_darray_free(body->yjit_blocks);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:05:41 +03:00
|
|
|
// Struct representing a code page
|
|
|
|
typedef struct code_page_struct
|
|
|
|
{
|
|
|
|
// Chunk of executable memory
|
|
|
|
uint8_t* mem_block;
|
|
|
|
|
|
|
|
// Size of the executable memory chunk
|
|
|
|
uint32_t page_size;
|
|
|
|
|
|
|
|
// Inline code block
|
|
|
|
codeblock_t cb;
|
|
|
|
|
|
|
|
// Outlined code block
|
|
|
|
codeblock_t ocb;
|
|
|
|
|
|
|
|
// Next node in the free list (private)
|
|
|
|
struct code_page_struct* _next;
|
|
|
|
|
|
|
|
} code_page_t;
|
|
|
|
|
|
|
|
// Current code page we are writing machine code into
|
|
|
|
static VALUE yjit_cur_code_page = Qfalse;
|
|
|
|
|
|
|
|
// Head of the list of free code pages
|
|
|
|
static code_page_t *code_page_freelist = NULL;
|
|
|
|
|
|
|
|
// Free a code page, add it to the free list
|
2021-07-13 21:56:02 +03:00
|
|
|
static void
|
2021-11-04 23:05:41 +03:00
|
|
|
yjit_code_page_free(void *voidp)
|
2021-07-13 21:56:02 +03:00
|
|
|
{
|
2021-11-04 23:05:41 +03:00
|
|
|
code_page_t* code_page = (code_page_t*)voidp;
|
|
|
|
code_page->_next = code_page_freelist;
|
|
|
|
code_page_freelist = code_page;
|
2021-07-13 21:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom type for interacting with the GC
|
|
|
|
static const rb_data_type_t yjit_code_page_type = {
|
|
|
|
"yjit_code_page",
|
|
|
|
{NULL, yjit_code_page_free, NULL, NULL},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allocate a code page and wrap it into a Ruby object owned by the GC
|
2021-11-04 23:05:41 +03:00
|
|
|
static VALUE
|
|
|
|
rb_yjit_code_page_alloc(void)
|
2021-07-13 21:56:02 +03:00
|
|
|
{
|
2021-11-04 23:05:41 +03:00
|
|
|
// If the free list is empty
|
|
|
|
if (!code_page_freelist) {
|
|
|
|
// Allocate many pages at once
|
|
|
|
uint8_t* code_chunk = alloc_exec_mem(PAGES_PER_ALLOC * CODE_PAGE_SIZE);
|
|
|
|
|
|
|
|
// Do this in reverse order so we allocate our pages in order
|
|
|
|
for (int i = PAGES_PER_ALLOC - 1; i >= 0; --i) {
|
|
|
|
code_page_t* code_page = malloc(sizeof(code_page_t));
|
|
|
|
code_page->mem_block = code_chunk + i * CODE_PAGE_SIZE;
|
|
|
|
assert ((intptr_t)code_page->mem_block % CODE_PAGE_SIZE == 0);
|
|
|
|
code_page->page_size = CODE_PAGE_SIZE;
|
|
|
|
code_page->_next = code_page_freelist;
|
|
|
|
code_page_freelist = code_page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code_page_t* code_page = code_page_freelist;
|
|
|
|
code_page_freelist = code_page_freelist->_next;
|
|
|
|
|
|
|
|
// Create a Ruby wrapper struct for the code page object
|
|
|
|
VALUE wrapper = TypedData_Wrap_Struct(0, &yjit_code_page_type, code_page);
|
|
|
|
|
|
|
|
// Write a pointer to the wrapper object on the page
|
|
|
|
*((VALUE*)code_page->mem_block) = wrapper;
|
2021-09-21 17:59:55 +03:00
|
|
|
|
2021-11-04 23:05:41 +03:00
|
|
|
// Initialize the code blocks
|
|
|
|
uint8_t* page_start = code_page->mem_block + sizeof(VALUE);
|
|
|
|
uint8_t* page_end = code_page->mem_block + CODE_PAGE_SIZE;
|
|
|
|
uint32_t halfsize = (uint32_t)(page_end - page_start) / 2;
|
|
|
|
cb_init(&code_page->cb, page_start, halfsize);
|
|
|
|
cb_init(&code_page->cb, page_start + halfsize, halfsize);
|
2021-09-21 17:59:55 +03:00
|
|
|
|
2021-11-04 23:05:41 +03:00
|
|
|
return wrapper;
|
2021-07-13 21:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap the Ruby object representing a code page
|
2021-11-04 23:05:41 +03:00
|
|
|
static code_page_t *
|
|
|
|
rb_yjit_code_page_unwrap(VALUE cp_obj)
|
2021-07-13 21:56:02 +03:00
|
|
|
{
|
|
|
|
code_page_t * code_page;
|
|
|
|
TypedData_Get_Struct(cp_obj, code_page_t, &yjit_code_page_type, code_page);
|
|
|
|
return code_page;
|
|
|
|
}
|
|
|
|
|
2021-09-21 17:59:55 +03:00
|
|
|
// Get the code page wrapper object for a code pointer
|
2021-11-04 23:05:41 +03:00
|
|
|
static VALUE
|
|
|
|
rb_yjit_code_page_from_ptr(uint8_t* code_ptr)
|
2021-09-21 17:59:55 +03:00
|
|
|
{
|
2021-11-04 23:05:41 +03:00
|
|
|
VALUE* page_start = (VALUE*)((intptr_t)code_ptr & ~(CODE_PAGE_SIZE - 1));
|
2021-09-21 17:59:55 +03:00
|
|
|
VALUE wrapper = *page_start;
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the inline code block corresponding to a code pointer
|
2021-11-04 23:05:41 +03:00
|
|
|
static void
|
|
|
|
yjit_get_cb(codeblock_t* cb, uint8_t* code_ptr)
|
2021-09-21 17:59:55 +03:00
|
|
|
{
|
|
|
|
VALUE page_wrapper = rb_yjit_code_page_from_ptr(code_ptr);
|
|
|
|
code_page_t *code_page = rb_yjit_code_page_unwrap(page_wrapper);
|
|
|
|
|
|
|
|
// A pointer to the page wrapper object is written at the start of the code page
|
2021-11-04 23:05:41 +03:00
|
|
|
uint8_t* mem_block = code_page->mem_block + sizeof(VALUE);
|
2021-09-21 17:59:55 +03:00
|
|
|
uint32_t mem_size = (code_page->page_size/2) - sizeof(VALUE);
|
|
|
|
RUBY_ASSERT(mem_block);
|
|
|
|
|
|
|
|
// Map the code block to this memory region
|
|
|
|
cb_init(cb, mem_block, mem_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the outlined code block corresponding to a code pointer
|
2021-11-04 23:05:41 +03:00
|
|
|
static void
|
|
|
|
yjit_get_ocb(codeblock_t* cb, uint8_t* code_ptr)
|
2021-09-21 17:59:55 +03:00
|
|
|
{
|
|
|
|
VALUE page_wrapper = rb_yjit_code_page_from_ptr(code_ptr);
|
|
|
|
code_page_t *code_page = rb_yjit_code_page_unwrap(page_wrapper);
|
|
|
|
|
|
|
|
// A pointer to the page wrapper object is written at the start of the code page
|
2021-11-04 23:05:41 +03:00
|
|
|
uint8_t* mem_block = code_page->mem_block + (code_page->page_size/2);
|
2021-09-21 17:59:55 +03:00
|
|
|
uint32_t mem_size = code_page->page_size/2;
|
|
|
|
RUBY_ASSERT(mem_block);
|
|
|
|
|
|
|
|
// Map the code block to this memory region
|
|
|
|
cb_init(cb, mem_block, mem_size);
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:05:41 +03:00
|
|
|
// Get the current code page or allocate a new one
|
|
|
|
static VALUE
|
|
|
|
yjit_get_code_page(uint32_t cb_bytes_needed, uint32_t ocb_bytes_needed)
|
|
|
|
{
|
|
|
|
// If this is the first code page
|
|
|
|
if (yjit_cur_code_page == Qfalse) {
|
|
|
|
yjit_cur_code_page = rb_yjit_code_page_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current code page
|
|
|
|
code_page_t *code_page = rb_yjit_code_page_unwrap(yjit_cur_code_page);
|
|
|
|
|
|
|
|
// Compute how many bytes are left in the code blocks
|
|
|
|
uint32_t cb_bytes_left = code_page->cb.mem_size - code_page->cb.write_pos;
|
|
|
|
uint32_t ocb_bytes_left = code_page->ocb.mem_size - code_page->ocb.write_pos;
|
|
|
|
RUBY_ASSERT_ALWAYS(cb_bytes_needed <= code_page->cb.mem_size);
|
|
|
|
RUBY_ASSERT_ALWAYS(ocb_bytes_needed <= code_page->ocb.mem_size);
|
|
|
|
|
|
|
|
// If there's enough space left in the current code page
|
|
|
|
if (cb_bytes_needed <= cb_bytes_left && ocb_bytes_needed <= ocb_bytes_left) {
|
|
|
|
return yjit_cur_code_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new code page
|
|
|
|
yjit_cur_code_page = rb_yjit_code_page_alloc();
|
|
|
|
code_page_t *new_code_page = rb_yjit_code_page_unwrap(yjit_cur_code_page);
|
|
|
|
|
|
|
|
// Jump to the new code page
|
2021-12-01 22:15:23 +03:00
|
|
|
jmp_ptr(&code_page->cb, cb_get_ptr(&new_code_page->cb, 0));
|
2021-11-04 23:05:41 +03:00
|
|
|
|
|
|
|
return yjit_cur_code_page;
|
|
|
|
}
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
bool
|
|
|
|
rb_yjit_enabled_p(void)
|
2021-02-23 23:22:20 +03:00
|
|
|
{
|
2021-03-07 02:46:56 +03:00
|
|
|
return rb_yjit_opts.yjit_enabled;
|
2021-02-23 23:22:20 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
unsigned
|
|
|
|
rb_yjit_call_threshold(void)
|
2021-02-23 23:22:20 +03:00
|
|
|
{
|
2021-03-07 02:46:56 +03:00
|
|
|
return rb_yjit_opts.call_threshold;
|
2021-02-23 23:22:20 +03:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:16:58 +03:00
|
|
|
# define PTR2NUM(x) (LONG2NUM((long)(x)))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq: block.id -> unique_id
|
|
|
|
*
|
|
|
|
* Returns a unique integer ID for the block. For example:
|
|
|
|
*
|
|
|
|
* blocks = blocks_for(iseq)
|
|
|
|
* blocks.group_by(&:id)
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
block_id(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
|
|
|
return PTR2NUM(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* call-seq: block.outgoing_ids -> list
|
|
|
|
*
|
|
|
|
* Returns a list of outgoing ids for the current block. This list can be used
|
|
|
|
* in conjunction with Block#id to construct a graph of block objects.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
outgoing_ids(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
|
|
|
|
|
|
|
VALUE ids = rb_ary_new();
|
|
|
|
|
|
|
|
rb_darray_for(block->outgoing, branch_idx) {
|
2021-09-29 21:58:01 +03:00
|
|
|
branch_t *out_branch = rb_darray_get(block->outgoing, branch_idx);
|
2021-06-10 23:16:58 +03:00
|
|
|
|
|
|
|
for (size_t succ_idx = 0; succ_idx < 2; succ_idx++) {
|
2021-09-29 21:58:01 +03:00
|
|
|
block_t *succ = out_branch->blocks[succ_idx];
|
2021-06-10 23:16:58 +03:00
|
|
|
|
|
|
|
if (succ == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rb_ary_push(ids, PTR2NUM(succ));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2021-06-14 14:07:27 +03:00
|
|
|
// Can raise RuntimeError
|
2020-12-09 00:54:41 +03:00
|
|
|
void
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_init(struct rb_yjit_options *options)
|
2020-12-09 00:54:41 +03:00
|
|
|
{
|
2021-12-14 03:08:01 +03:00
|
|
|
if (!YJIT_SUPPORTED_P || !JIT_ENABLED) {
|
2020-12-09 00:54:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_yjit_opts = *options;
|
|
|
|
rb_yjit_opts.yjit_enabled = true;
|
2021-01-26 23:21:47 +03:00
|
|
|
|
2021-10-01 15:45:02 +03:00
|
|
|
rb_yjit_opts.gen_stats = rb_yjit_opts.gen_stats || getenv("RUBY_YJIT_STATS");
|
2021-06-11 18:40:14 +03:00
|
|
|
|
2021-08-24 13:32:07 +03:00
|
|
|
#if !YJIT_STATS
|
2021-06-14 14:07:27 +03:00
|
|
|
if(rb_yjit_opts.gen_stats) {
|
2021-08-24 13:32:07 +03:00
|
|
|
rb_warning("--yjit-stats requires that Ruby is compiled with CPPFLAGS='-DYJIT_STATS=1' or CPPFLAGS='-DRUBY_DEBUG=1'");
|
2021-06-14 14:07:27 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-26 22:25:50 +03:00
|
|
|
// Normalize command-line options to default values
|
|
|
|
if (rb_yjit_opts.exec_mem_size < 1) {
|
|
|
|
rb_yjit_opts.exec_mem_size = 256;
|
|
|
|
}
|
2021-03-07 02:46:56 +03:00
|
|
|
if (rb_yjit_opts.call_threshold < 1) {
|
2021-06-24 04:19:10 +03:00
|
|
|
rb_yjit_opts.call_threshold = YJIT_DEFAULT_CALL_THRESHOLD;
|
2021-02-23 23:22:20 +03:00
|
|
|
}
|
2021-06-23 20:55:34 +03:00
|
|
|
if (rb_yjit_opts.max_versions < 1) {
|
|
|
|
rb_yjit_opts.max_versions = 4;
|
2021-04-24 07:16:48 +03:00
|
|
|
}
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-11-18 18:44:31 +03:00
|
|
|
// If type propagation is disabled, max 1 version per block
|
YJIT: Add ability to exit to interpreter from stubs
Previously, YJIT assumed that it's always possible to generate a new
basic block when servicing a stub in branch_stub_hit(). When YJIT is out
of executable memory, for example, this assumption doesn't hold up.
Add handling to branch_stub_hit() for servicing stubs without consuming
more executable memory by adding a code path that exits to the
interpreter at the location the branch stub represents. The new code
path reconstructs interpreter state in branch_stub_hit() and then exits
with a new snippet called `code_for_exit_from_stub` that returns
`Qundef` from the YJIT native stack frame.
As this change adds another place where we regenerate code from
`branch_t`, extract the logic for it into a new function and call it
regenerate_branch(). While we are at it, make the branch shrinking code
path in branch_stub_hit() more explicit.
This new functionality is hard to test without full support for out of
memory conditions. To verify this change, I ran
`RUBY_YJIT_ENABLE=1 make check -j12` with the following patch to stress
test the new code path:
```diff
diff --git a/yjit_core.c b/yjit_core.c
index 4ab63d9806..5788b8c5ed 100644
--- a/yjit_core.c
+++ b/yjit_core.c
@@ -878,8 +878,12 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex
cb_set_write_ptr(cb, branch->end_addr);
}
+if (rand() < RAND_MAX/2) {
// Compile the new block version
p_block = gen_block_version(target, target_ctx, ec);
+}else{
+ p_block = NULL;
+}
if (!p_block && branch_modified) {
// We couldn't generate a new block for the branch, but we modified the branch.
```
We can enable the new test along with other OOM tests once full support
lands.
Other small changes:
* yjit_utils.c (print_str): Update to work with new native frame shape.
Follow up for 8fa0ee4d404.
* yjit_iface.c (rb_yjit_init): Run yjit_init_core() after
yjit_init_codegen() so `cb` and `ocb` are available.
2021-11-27 02:00:42 +03:00
|
|
|
if (rb_yjit_opts.no_type_prop) {
|
2021-11-18 18:44:31 +03:00
|
|
|
rb_yjit_opts.max_versions = 1;
|
|
|
|
}
|
|
|
|
|
2021-02-25 23:10:38 +03:00
|
|
|
blocks_assuming_stable_global_constant_state = st_init_numtable();
|
|
|
|
blocks_assuming_single_ractor_mode = st_init_numtable();
|
2021-03-09 22:01:16 +03:00
|
|
|
blocks_assuming_bops = st_init_numtable();
|
2021-02-25 23:10:38 +03:00
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
yjit_init_codegen();
|
YJIT: Add ability to exit to interpreter from stubs
Previously, YJIT assumed that it's always possible to generate a new
basic block when servicing a stub in branch_stub_hit(). When YJIT is out
of executable memory, for example, this assumption doesn't hold up.
Add handling to branch_stub_hit() for servicing stubs without consuming
more executable memory by adding a code path that exits to the
interpreter at the location the branch stub represents. The new code
path reconstructs interpreter state in branch_stub_hit() and then exits
with a new snippet called `code_for_exit_from_stub` that returns
`Qundef` from the YJIT native stack frame.
As this change adds another place where we regenerate code from
`branch_t`, extract the logic for it into a new function and call it
regenerate_branch(). While we are at it, make the branch shrinking code
path in branch_stub_hit() more explicit.
This new functionality is hard to test without full support for out of
memory conditions. To verify this change, I ran
`RUBY_YJIT_ENABLE=1 make check -j12` with the following patch to stress
test the new code path:
```diff
diff --git a/yjit_core.c b/yjit_core.c
index 4ab63d9806..5788b8c5ed 100644
--- a/yjit_core.c
+++ b/yjit_core.c
@@ -878,8 +878,12 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex
cb_set_write_ptr(cb, branch->end_addr);
}
+if (rand() < RAND_MAX/2) {
// Compile the new block version
p_block = gen_block_version(target, target_ctx, ec);
+}else{
+ p_block = NULL;
+}
if (!p_block && branch_modified) {
// We couldn't generate a new block for the branch, but we modified the branch.
```
We can enable the new test along with other OOM tests once full support
lands.
Other small changes:
* yjit_utils.c (print_str): Update to work with new native frame shape.
Follow up for 8fa0ee4d404.
* yjit_iface.c (rb_yjit_init): Run yjit_init_core() after
yjit_init_codegen() so `cb` and `ocb` are available.
2021-11-27 02:00:42 +03:00
|
|
|
yjit_init_core();
|
2020-12-09 00:54:41 +03:00
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
// YJIT Ruby module
|
2021-10-27 23:10:25 +03:00
|
|
|
mYjit = rb_define_module_under(rb_cRubyVM, "YJIT");
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_define_module_function(mYjit, "blocks_for", yjit_blocks_for, 1);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
// YJIT::Block (block version, code block)
|
|
|
|
cYjitBlock = rb_define_class_under(mYjit, "Block", rb_cObject);
|
|
|
|
rb_define_method(cYjitBlock, "address", block_address, 0);
|
2021-06-10 23:16:58 +03:00
|
|
|
rb_define_method(cYjitBlock, "id", block_id, 0);
|
2021-03-07 02:46:56 +03:00
|
|
|
rb_define_method(cYjitBlock, "code", block_code, 0);
|
|
|
|
rb_define_method(cYjitBlock, "iseq_start_index", iseq_start_index, 0);
|
|
|
|
rb_define_method(cYjitBlock, "iseq_end_index", iseq_end_index, 0);
|
2021-06-10 23:16:58 +03:00
|
|
|
rb_define_method(cYjitBlock, "outgoing_ids", outgoing_ids, 0);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 21:50:13 +03:00
|
|
|
|
2021-03-07 02:46:56 +03:00
|
|
|
// YJIT disassembler interface
|
2021-10-01 14:17:09 +03:00
|
|
|
#ifdef HAVE_LIBCAPSTONE
|
2021-03-07 02:46:56 +03:00
|
|
|
cYjitDisasm = rb_define_class_under(mYjit, "Disasm", rb_cObject);
|
|
|
|
rb_define_alloc_func(cYjitDisasm, yjit_disasm_init);
|
|
|
|
rb_define_method(cYjitDisasm, "disasm", yjit_disasm, 2);
|
|
|
|
cYjitDisasmInsn = rb_struct_define_under(cYjitDisasm, "Insn", "address", "mnemonic", "op_str", NULL);
|
2021-04-20 23:47:38 +03:00
|
|
|
#if RUBY_DEBUG
|
2021-07-13 01:38:56 +03:00
|
|
|
cYjitCodeComment = rb_struct_define_under(cYjitDisasm, "Comment", "address", "comment", NULL);
|
2021-04-20 23:47:38 +03:00
|
|
|
#endif
|
2021-01-22 22:26:20 +03:00
|
|
|
#endif
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 21:43:26 +03:00
|
|
|
|
2021-03-18 02:07:20 +03:00
|
|
|
// Make dependency tables
|
2020-12-09 00:54:41 +03:00
|
|
|
method_lookup_dependency = st_init_numtable();
|
2021-03-18 02:07:20 +03:00
|
|
|
cme_validity_dependency = st_init_numtable();
|
|
|
|
|
|
|
|
// Initialize the GC hooks
|
2021-03-07 02:46:56 +03:00
|
|
|
struct yjit_root_struct *root;
|
|
|
|
VALUE yjit_root = TypedData_Make_Struct(0, struct yjit_root_struct, &yjit_root_type, root);
|
|
|
|
rb_gc_register_mark_object(yjit_root);
|
2021-11-05 04:32:55 +03:00
|
|
|
|
|
|
|
(void)yjit_get_cb;
|
|
|
|
(void)yjit_get_ocb;
|
|
|
|
(void)yjit_get_code_page;
|
2020-12-09 00:54:41 +03:00
|
|
|
}
|