Skip to content

Commit f782755

Browse files
committed
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 ```
1 parent adc7198 commit f782755

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ COMMONOBJS = array.$(OBJEXT) \
155155
ujit_core.$(OBJEXT) \
156156
ujit_codegen.$(OBJEXT) \
157157
ujit_iface.$(OBJEXT) \
158+
ujit_ruby.$(OBJEXT) \
158159
$(COROUTINE_OBJ) \
159160
$(DTRACE_OBJ) \
160161
$(BUILTIN_ENCOBJS) \

ujit_iface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ rb_ujit_compile_iseq(const rb_iseq_t *iseq)
288288
#endif
289289
}
290290

291+
void Init_ujit_ruby();
292+
291293
void
292294
rb_ujit_init(void)
293295
{
@@ -300,6 +302,7 @@ rb_ujit_init(void)
300302

301303
ujit_init_core();
302304
ujit_init_codegen();
305+
Init_ujit_ruby();
303306

304307
// Initialize the GC hooks
305308
method_lookup_dependency = st_init_numtable();

ujit_ruby.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <ruby.h>
2+
#include "vm_core.h"
3+
#include "ujit.h"
4+
#include "ujit_core.h"
5+
#include "iseq.h"
6+
7+
extern st_table * version_tbl;
8+
extern codeblock_t *ocb;
9+
extern codeblock_t *cb;
10+
11+
VALUE cUjitBlock;
12+
13+
static const rb_data_type_t ujit_block_type = {
14+
"UJIT/Block",
15+
{0, 0, 0, },
16+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
17+
};
18+
19+
struct ujit_block_itr {
20+
const rb_iseq_t *iseq;
21+
VALUE list;
22+
};
23+
24+
static int
25+
iseqw_ujit_collect_blocks(st_data_t key, st_data_t value, st_data_t argp)
26+
{
27+
block_t * block = (block_t *)value;
28+
struct ujit_block_itr * itr = (struct ujit_block_itr *)argp;
29+
30+
if (block->blockid.iseq == itr->iseq) {
31+
VALUE rb_block = TypedData_Wrap_Struct(cUjitBlock, &ujit_block_type, block);
32+
rb_ary_push(itr->list, rb_block);
33+
}
34+
return ST_CONTINUE;
35+
}
36+
37+
/* Get a list of the UJIT blocks associated with `rb_iseq` */
38+
static VALUE
39+
ujit_blocks_for(VALUE mod, VALUE rb_iseq)
40+
{
41+
const rb_iseq_t *iseq = rb_iseqw_to_iseq(rb_iseq);
42+
st_table * vt = (st_table *)version_tbl;
43+
struct ujit_block_itr itr;
44+
itr.iseq = iseq;
45+
itr.list = rb_ary_new();
46+
47+
rb_st_foreach(vt, iseqw_ujit_collect_blocks, (st_data_t)&itr);
48+
49+
return itr.list;
50+
}
51+
52+
static VALUE
53+
ujit_insert(VALUE mod, VALUE iseq)
54+
{
55+
rb_ujit_compile_iseq(rb_iseqw_to_iseq(iseq));
56+
return iseq;
57+
}
58+
59+
/* Get the address of the UJIT::Block */
60+
static VALUE
61+
block_address(VALUE self)
62+
{
63+
block_t * block;
64+
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
65+
return LONG2NUM((intptr_t)block);
66+
}
67+
68+
/* Get the machine code for UJIT::Block as a binary string */
69+
static VALUE
70+
block_code(VALUE self)
71+
{
72+
block_t * block;
73+
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
74+
75+
return rb_str_new(cb->mem_block + block->start_pos, block->end_pos - block->start_pos);
76+
}
77+
78+
/* Get the start index in the Instruction Sequence that corresponds to this
79+
* UJIT::Block */
80+
static VALUE
81+
iseq_start_index(VALUE self)
82+
{
83+
block_t * block;
84+
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
85+
86+
return INT2NUM(block->blockid.idx);
87+
}
88+
89+
/* Get the end index in the Instruction Sequence that corresponds to this
90+
* UJIT::Block */
91+
static VALUE
92+
iseq_end_index(VALUE self)
93+
{
94+
block_t * block;
95+
TypedData_Get_Struct(self, block_t, &ujit_block_type, block);
96+
97+
return INT2NUM(block->end_idx);
98+
}
99+
100+
void Init_ujit_ruby(void)
101+
{
102+
VALUE mUjit = rb_define_module("UJIT");
103+
rb_define_module_function(mUjit, "insert", ujit_insert, 1);
104+
rb_define_module_function(mUjit, "blocks_for", ujit_blocks_for, 1);
105+
106+
cUjitBlock = rb_define_class_under(mUjit, "Block", rb_cObject);
107+
rb_define_method(cUjitBlock, "address", block_address, 0);
108+
rb_define_method(cUjitBlock, "code", block_code, 0);
109+
rb_define_method(cUjitBlock, "iseq_start_index", iseq_start_index, 0);
110+
rb_define_method(cUjitBlock, "iseq_end_index", iseq_end_index, 0);
111+
}

0 commit comments

Comments
 (0)