Skip to content

Commit 48c8df9

Browse files
jeremyevansk0kubun
andauthored
Allow tracing of optimized methods
This updates the trace instructions to directly dispatch to opt_send_without_block. So this should cause no slowdown in non-trace mode. To enable the tracing of the optimized methods, RUBY_EVENT_C_CALL and RUBY_EVENT_C_RETURN are added as events to the specialized instructions. Fixes [Bug #14870] Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
1 parent f51a6ac commit 48c8df9

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,6 +3441,7 @@ insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj, int insn_id)
34413441
{
34423442
iobj->insn_id = insn_id;
34433443
iobj->operand_size = insn_len(insn_id) - 1;
3444+
iobj->insn_info.events |= RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN;
34443445

34453446
if (insn_id == BIN(opt_neq)) {
34463447
VALUE original_ci = iobj->operands[0];

iseq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ ISEQ_ORIGINAL_ISEQ_ALLOC(const rb_iseq_t *iseq, long size)
7474
RUBY_EVENT_END | \
7575
RUBY_EVENT_CALL | \
7676
RUBY_EVENT_RETURN| \
77+
RUBY_EVENT_C_CALL| \
78+
RUBY_EVENT_C_RETURN| \
7779
RUBY_EVENT_B_CALL| \
7880
RUBY_EVENT_B_RETURN| \
7981
RUBY_EVENT_COVERAGE_LINE| \

spec/ruby/core/tracepoint/inspect_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ def trace_point_spec_test_return
6565
it 'returns a String showing the event, method, path and line for a :c_call event' do
6666
inspect = nil
6767
line = nil
68-
TracePoint.new(:c_call) { |tp|
68+
tp = TracePoint.new(:c_call) { |tp|
6969
next unless TracePointSpec.target_thread?
7070
inspect ||= tp.inspect
71-
}.enable do
72-
line = __LINE__ + 1
71+
}
72+
line = __LINE__ + 2
73+
tp.enable do
7374
[0, 1].max
7475
end
7576

test/ruby/test_optimization.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,51 @@ def test_string_uminus
150150
assert_redefine_method('String', '-@', 'assert_nil(-"foo")')
151151
end
152152

153+
def test_trace_optimized_methods
154+
bug14870 = "[ruby-core:87638]"
155+
expected = [:-@, :max, :min, :+, :-, :*, :/, :%, :==, :<, :<=, :>, :>=, :<<,
156+
:&, :|, :[], :[]=, :length, :empty?, :nil?, :succ, :!, :=~]
157+
[:c_call, :c_return].each do |type|
158+
methods = []
159+
tp = TracePoint.new(type) { |tp| methods << tp.method_id }
160+
tp.enable do
161+
x = "a"; -x
162+
[1].max
163+
[1].min
164+
x = 42 + 2
165+
x = 42 - 2
166+
x = 42 * 2
167+
x = 42 / 2
168+
x = 42 % 2
169+
x == 42
170+
x < 42
171+
x <= 42
172+
x > 42
173+
x >= 42
174+
x = x << 1
175+
x = x & 1
176+
x = x | 1
177+
x = []; x[1]
178+
x[1] = 2
179+
x.length
180+
x.empty?
181+
x.nil?
182+
x = 1; x.succ
183+
!x
184+
x = 'a'; x =~ /a/
185+
end
186+
assert_equal(expected, methods, bug14870)
187+
end
188+
189+
methods = []
190+
tp = TracePoint.new(:c_call, :c_return) { |tp| methods << tp.method_id }
191+
tp.enable do
192+
x = 1
193+
x != 42
194+
end
195+
assert_equal([:!=, :==, :==, :!=], methods, bug14870)
196+
end
197+
153198
def test_string_freeze_saves_memory
154199
n = 16384
155200
data = '.'.freeze

tool/ruby_vm/views/_trace_instruction.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
INSN_ENTRY(<%= insn.name %>)
1212
{
1313
vm_trace(ec, GET_CFP());
14-
DISPATCH_ORIGINAL_INSN(<%= insn.jump_destination %>);
14+
% if insn.name =~
15+
% /\Atrace_opt_(plus|minus|mult|div|mod|eq|neq|lt|le|gt|ge|ltlt|and|or|aref|aset|length|size|empty_p|nil_p|succ|not|regexpmatch2)\z/
16+
% jump_dest = "opt_send_without_block"
17+
% end
18+
<%= 'ADD_PC(1);' if insn.name == 'trace_opt_neq' %>
19+
DISPATCH_ORIGINAL_INSN(<%= jump_dest || insn.jump_destination %>);
1520
END_INSN(<%= insn.name %>);
1621
}

0 commit comments

Comments
 (0)