Skip to content

Commit e210b89

Browse files
k0kubunXrXr
andauthored
Move the PC regardless of the leaf flag (#8232)
Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>
1 parent 5bb9462 commit e210b89

File tree

7 files changed

+11
-74
lines changed

7 files changed

+11
-74
lines changed

compile.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10217,12 +10217,6 @@ dump_disasm_list_with_cursor(const LINK_ELEMENT *link, const LINK_ELEMENT *curr,
1021710217
fflush(stdout);
1021810218
}
1021910219

10220-
bool
10221-
rb_insns_leaf_p(int i)
10222-
{
10223-
return insn_leaf_p(i);
10224-
}
10225-
1022610220
int
1022710221
rb_insn_len(VALUE insn)
1022810222
{

gc.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,42 +2423,7 @@ static void
24232423
gc_event_hook_body(rb_execution_context_t *ec, rb_objspace_t *objspace, const rb_event_flag_t event, VALUE data)
24242424
{
24252425
if (UNLIKELY(!ec->cfp)) return;
2426-
const VALUE *pc = ec->cfp->pc;
2427-
if (pc && VM_FRAME_RUBYFRAME_P(ec->cfp)) {
2428-
int prev_opcode = rb_vm_insn_addr2opcode((void *)*ec->cfp->iseq->body->iseq_encoded);
2429-
for (const VALUE *insn = ec->cfp->iseq->body->iseq_encoded; insn < pc; insn += rb_insn_len(prev_opcode)) {
2430-
prev_opcode = rb_vm_insn_addr2opcode((void *)*insn);
2431-
}
2432-
2433-
/* If the previous instruction is a leaf instruction, then the PC is
2434-
* the currently executing instruction. We should increment the PC
2435-
* because the source line is calculated with PC-1 in calc_pos.
2436-
*
2437-
* If the previous instruction is not a leaf instruction and the
2438-
* current instruction is not a leaf instruction, then the PC was
2439-
* incremented before the instruction was ran (meaning the currently
2440-
* executing instruction is actually the previous instruction), so we
2441-
* should not increment the PC otherwise we will calculate the source
2442-
* line for the next instruction.
2443-
*
2444-
* However, this implementation still has a bug. Consider the
2445-
* following situation:
2446-
*
2447-
* non-leaf
2448-
* leaf <-
2449-
*
2450-
* Where the PC currently points to a leaf instruction. We don't know
2451-
* which instruction we really are at since we could be at the non-leaf
2452-
* instruction (since it incremented the PC before executing the
2453-
* instruction). We could also be at the leaf instruction since the PC
2454-
* doesn't get incremented until the instruction finishes.
2455-
*/
2456-
if (rb_insns_leaf_p(prev_opcode)) {
2457-
ec->cfp->pc++;
2458-
}
2459-
}
24602426
EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, data);
2461-
ec->cfp->pc = pc;
24622427
}
24632428

24642429
#define gc_event_hook_available_p(objspace) ((objspace)->flags.has_hook)

internal/compile.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct rb_iseq_struct; /* in vm_core.h */
1717
/* compile.c */
1818
int rb_dvar_defined(ID, const struct rb_iseq_struct *);
1919
int rb_local_defined(ID, const struct rb_iseq_struct *);
20-
bool rb_insns_leaf_p(int i);
2120
int rb_insn_len(VALUE insn);
2221
const char *rb_insns_name(int i);
2322
VALUE rb_insns_name_array(void);

test/objspace/test_objspace.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ def test_trace_object_allocations
225225
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(o4))
226226
assert_equal(line4, ObjectSpace.allocation_sourceline(o4))
227227

228+
# The line number should be based on newarray instead of getinstancevariable.
229+
line5 = __LINE__; o5 = [ # newarray (leaf)
230+
@ivar, # getinstancevariable (not leaf)
231+
]
232+
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(o5))
233+
assert_equal(line5, ObjectSpace.allocation_sourceline(o5))
234+
228235
# [Bug #19482]
229236
EnvUtil.under_gc_stress do
230237
100.times do

tool/ruby_vm/views/_insn_entry.erb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ INSN_ENTRY(<%= insn.name %>)
2424
<%= ope[:decl] %> = (<%= ope[:type] %>)GET_OPERAND(<%= i + 1 %>);
2525
% end
2626
# define INSN_ATTR(x) <%= insn.call_attribute(' ## x ## ') %>
27-
const bool leaf = INSN_ATTR(leaf);
27+
const bool MAYBE_UNUSED(leaf) = INSN_ATTR(leaf);
2828
% insn.pops.reverse_each.with_index.reverse_each do |pop, i|
2929
<%= pop[:decl] %> = <%= insn.cast_from_VALUE pop, "TOPN(#{i})"%>;
3030
% end
@@ -35,7 +35,7 @@ INSN_ENTRY(<%= insn.name %>)
3535
% end
3636

3737
/* ### Instruction preambles. ### */
38-
if (! leaf) ADD_PC(INSN_ATTR(width));
38+
ADD_PC(INSN_ATTR(width));
3939
% if insn.handles_sp?
4040
POPN(INSN_ATTR(popn));
4141
% end
@@ -68,7 +68,6 @@ INSN_ENTRY(<%= insn.name %>)
6868
VM_ASSERT(!RB_TYPE_P(TOPN(<%= i %>), T_MOVED));
6969
% end
7070
% end
71-
if (leaf) ADD_PC(INSN_ATTR(width));
7271
# undef INSN_ATTR
7372

7473
/* ### Leave the instruction. ### */

tool/ruby_vm/views/_leaf_helpers.erb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,6 @@
1010

1111
#include "iseq.h"
1212

13-
extern const bool rb_vm_insn_leaf_p[];
14-
15-
#ifdef RUBY_VM_INSNS_INFO
16-
const bool rb_vm_insn_leaf_p[] = {
17-
% RubyVM::Instructions.each_slice(20) do |insns|
18-
<%= insns.map do |insn|
19-
if insn.is_a?(RubyVM::BareInstructions)
20-
insn.always_leaf? ? '1' : '0'
21-
else
22-
'0'
23-
end
24-
end.join(', ')
25-
%>,
26-
% end
27-
};
28-
#endif
29-
30-
CONSTFUNC(MAYBE_UNUSED(static bool insn_leaf_p(VALUE insn)));
31-
32-
bool
33-
insn_leaf_p(VALUE insn)
34-
{
35-
return rb_vm_insn_leaf_p[insn];
36-
}
37-
3813
// This is used to tell RJIT that this insn would be leaf if CHECK_INTS didn't exist.
3914
// It should be used only when RUBY_VM_CHECK_INTS is directly written in insns.def.
4015
static bool leafness_of_check_ints = false;

vm_insnhelper.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ CC_SET_FASTPATH(const struct rb_callcache *cc, vm_call_handler func, bool enable
181181
/**********************************************************/
182182

183183
#define CALL_SIMPLE_METHOD() do { \
184-
rb_snum_t x = leaf ? INSN_ATTR(width) : 0; \
185-
rb_snum_t y = attr_width_opt_send_without_block(0); \
186-
rb_snum_t z = x - y; \
187-
ADD_PC(z); \
184+
rb_snum_t insn_width = attr_width_opt_send_without_block(0); \
185+
ADD_PC(-insn_width); \
188186
DISPATCH_ORIGINAL_INSN(opt_send_without_block); \
189187
} while (0)
190188

0 commit comments

Comments
 (0)