summaryrefslogtreecommitdiff
path: root/yjit_iface.c
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2021-06-10 13:16:58 -0700
committerAlan Wu <XrXr@users.noreply.github.com>2021-10-20 18:19:36 -0400
commit46d5e10279b6f1cea46682b5a5da3a09c5ce0c07 (patch)
tree36d54adb88de3fb5a6c81bb64c16c49b6a97cf7e /yjit_iface.c
parentf54e6e131099b5502f7d9be57f29bab11c70d841 (diff)
Add graphviz output
This adds a method to blocks to get outgoing ids, then uses the outgoing ids to generate a graphviz graph. Two methods were added to the Block object. One method returns an id for the block, which is just the address of the underlying block. The other method returns a list of outgoing block ids. We can use Block#id in conjunction with Block#outgoing_ids to construct a graph of blocks
Diffstat (limited to 'yjit_iface.c')
-rw-r--r--yjit_iface.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/yjit_iface.c b/yjit_iface.c
index b255a44221..754a2d6529 100644
--- a/yjit_iface.c
+++ b/yjit_iface.c
@@ -1025,6 +1025,55 @@ rb_yjit_call_threshold(void)
return rb_yjit_opts.call_threshold;
}
+# 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) {
+ branch_t* out_branch = rb_darray_get(block->outgoing, branch_idx);
+
+ for (size_t succ_idx = 0; succ_idx < 2; succ_idx++) {
+ block_t* succ = out_branch->blocks[succ_idx];
+
+ if (succ == NULL)
+ continue;
+
+ rb_ary_push(ids, PTR2NUM(succ));
+ }
+
+ }
+
+ return ids;
+}
+
// Can raise RuntimeError
void
rb_yjit_init(struct rb_yjit_options *options)
@@ -1069,9 +1118,11 @@ rb_yjit_init(struct rb_yjit_options *options)
// YJIT::Block (block version, code block)
cYjitBlock = rb_define_class_under(mYjit, "Block", rb_cObject);
rb_define_method(cYjitBlock, "address", block_address, 0);
+ rb_define_method(cYjitBlock, "id", block_id, 0);
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);
+ rb_define_method(cYjitBlock, "outgoing_ids", outgoing_ids, 0);
// YJIT disassembler interface
#if HAVE_LIBCAPSTONE