Skip to content

Commit d259a9e

Browse files
LeszekSwirskiV8 LUCI CQ
authored andcommitted
[maglev] Add source position collection
This change adds support for collecting source positions in Maglev for profiling. It wires a SourcePositionTableBuilder through the code generator and uses the existing source positions from the graph labeller. Fixed: 436575053 Change-Id: I64fa1e8829b036e498fa1d756c996716d16fb2a8 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6897146 Reviewed-by: Victor Gomes <[email protected]> Auto-Submit: Leszek Swirski <[email protected]> Commit-Queue: Leszek Swirski <[email protected]> Cr-Commit-Position: refs/heads/main@{#102119}
1 parent 5f433dd commit d259a9e

5 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎src/maglev/maglev-code-gen-state.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "src/codegen/label.h"
1010
#include "src/codegen/machine-type.h"
1111
#include "src/codegen/maglev-safepoint-table.h"
12+
#include "src/codegen/source-position-table.h"
1213
#include "src/common/globals.h"
1314
#include "src/compiler/backend/instruction.h"
1415
#include "src/compiler/js-heap-broker.h"
@@ -34,9 +35,11 @@ class MaglevCodeGenState {
3435
public:
3536
MaglevCodeGenState(MaglevCompilationInfo* compilation_info,
3637
MaglevSafepointTableBuilder* safepoint_table_builder,
38+
SourcePositionTableBuilder* source_position_table_builder,
3739
uint32_t max_block_id)
3840
: compilation_info_(compilation_info),
3941
safepoint_table_builder_(safepoint_table_builder),
42+
source_position_table_builder_(source_position_table_builder),
4043
real_jump_target_(max_block_id) {}
4144

4245
void set_tagged_slots(int slots) { tagged_slots_ = slots; }
@@ -77,10 +80,13 @@ class MaglevCodeGenState {
7780
return compilation_info_->toplevel_compilation_unit()->parameter_count();
7881
}
7982

83+
MaglevCompilationInfo* compilation_info() const { return compilation_info_; }
8084
MaglevSafepointTableBuilder* safepoint_table_builder() const {
8185
return safepoint_table_builder_;
8286
}
83-
MaglevCompilationInfo* compilation_info() const { return compilation_info_; }
87+
SourcePositionTableBuilder* source_position_table_builder() const {
88+
return source_position_table_builder_;
89+
}
8490

8591
Label* entry_label() { return &entry_label_; }
8692

@@ -121,6 +127,7 @@ class MaglevCodeGenState {
121127
private:
122128
MaglevCompilationInfo* const compilation_info_;
123129
MaglevSafepointTableBuilder* const safepoint_table_builder_;
130+
SourcePositionTableBuilder* const source_position_table_builder_;
124131

125132
std::vector<DeferredCodeInfo*> deferred_code_;
126133
std::vector<EagerDeoptInfo*> eager_deopts_;

‎src/maglev/maglev-code-generator.cc‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,14 @@ class ExceptionHandlerTrampolineBuilder {
713713
class MaglevCodeGeneratingNodeProcessor {
714714
public:
715715
MaglevCodeGeneratingNodeProcessor(MaglevAssembler* masm, Zone* zone)
716-
: masm_(masm), zone_(zone) {}
716+
: masm_(masm),
717+
zone_(zone),
718+
// Cache for faster check.
719+
collect_source_positions_(masm->code_gen_state()
720+
->compilation_info()
721+
->collect_source_positions()) {
722+
DCHECK_IMPLIES(collect_source_positions_, graph_labeller() != nullptr);
723+
}
717724

718725
void PreProcessGraph(Graph* graph) {
719726
// TODO(victorgomes): I wonder if we want to create a struct that shares
@@ -807,6 +814,15 @@ class MaglevCodeGeneratingNodeProcessor {
807814
<< PrintNode(node);
808815
__ RecordComment(ss.str());
809816
}
817+
if (collect_source_positions_) {
818+
// TODO(leszeks): Consider collecting source position in a more memory
819+
// friendly way, if we don't need the whole graph labeller.
820+
const auto& provenance = graph_labeller()->GetNodeProvenance(node);
821+
if (provenance.position.IsKnown()) {
822+
code_gen_state()->source_position_table_builder()->AddPosition(
823+
masm_->pc_offset(), provenance.position, false);
824+
}
825+
}
810826

811827
if (v8_flags.maglev_assert_stack_size) {
812828
__ AssertStackSizeCorrect();
@@ -1113,6 +1129,7 @@ class MaglevCodeGeneratingNodeProcessor {
11131129
}
11141130
MaglevAssembler* const masm_;
11151131
Zone* zone_;
1132+
bool collect_source_positions_;
11161133
};
11171134

11181135
class SafepointingNodeProcessor {
@@ -1713,8 +1730,9 @@ MaglevCodeGenerator::MaglevCodeGenerator(
17131730
safepoint_table_builder_(compilation_info->zone(),
17141731
graph->tagged_stack_slots()),
17151732
frame_translation_builder_(compilation_info->zone()),
1733+
source_position_table_builder_(compilation_info->zone()),
17161734
code_gen_state_(compilation_info, &safepoint_table_builder_,
1717-
graph->max_block_id()),
1735+
&source_position_table_builder_, graph->max_block_id()),
17181736
masm_(isolate->GetMainThreadIsolateUnsafe(), compilation_info->zone(),
17191737
&code_gen_state_),
17201738
graph_(graph),
@@ -1953,6 +1971,10 @@ MaybeHandle<Code> MaglevCodeGenerator::BuildCodeObject(
19531971
LocalIsolate* local_isolate) {
19541972
if (!code_gen_succeeded_) return {};
19551973

1974+
// Allocate the source position table.
1975+
Handle<TrustedByteArray> source_positions =
1976+
source_position_table_builder_.ToSourcePositionTable(local_isolate);
1977+
19561978
Handle<DeoptimizationData> deopt_data =
19571979
(v8_flags.maglev_deopt_data_on_background &&
19581980
!v8_flags.maglev_build_code_on_background)
@@ -1968,7 +1990,7 @@ MaybeHandle<Code> MaglevCodeGenerator::BuildCodeObject(
19681990
.set_stack_slots(stack_slot_count_with_fixed_frame())
19691991
.set_parameter_count(parameter_count())
19701992
.set_deoptimization_data(deopt_data)
1971-
.set_empty_source_position_table()
1993+
.set_source_position_table(source_positions)
19721994
.set_inlined_bytecode_size(
19731995
graph_->total_inlined_bytecode_size() +
19741996
graph_->total_inlined_bytecode_size_small())

‎src/maglev/maglev-code-generator.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define V8_MAGLEV_MAGLEV_CODE_GENERATOR_H_
77

88
#include "src/codegen/maglev-safepoint-table.h"
9+
#include "src/codegen/source-position-table.h"
910
#include "src/common/globals.h"
1011
#include "src/deoptimizer/frame-translation-builder.h"
1112
#include "src/maglev/maglev-assembler.h"
@@ -54,6 +55,7 @@ class MaglevCodeGenerator final {
5455
LocalIsolate* local_isolate_;
5556
MaglevSafepointTableBuilder safepoint_table_builder_;
5657
FrameTranslationBuilder frame_translation_builder_;
58+
SourcePositionTableBuilder source_position_table_builder_;
5759
MaglevCodeGenState code_gen_state_;
5860
MaglevAssembler masm_;
5961
Graph* const graph_;

‎src/maglev/maglev-compiler.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ bool MaglevCompiler::Compile(LocalIsolate* local_isolate,
7474
Graph* graph = Graph::New(compilation_info);
7575

7676
if (V8_UNLIKELY(ALWAYS_MAGLEV_GRAPH_LABELLER_BOOL ||
77-
compilation_info->is_tracing_enabled())) {
77+
compilation_info->is_tracing_enabled() ||
78+
compilation_info->collect_source_positions())) {
7879
compilation_info->set_graph_labeller(new MaglevGraphLabeller());
7980
graph_labeller_scope.emplace(compilation_info->graph_labeller());
8081
}

‎test/unittests/maglev/maglev-assembler-unittest.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MaglevAssemblerTest : public MaglevTest {
1818
public:
1919
MaglevAssemblerTest()
2020
: MaglevTest(),
21-
codegen_state(nullptr, nullptr, 0),
21+
codegen_state(nullptr, nullptr, nullptr, 0),
2222
as(isolate(), zone(), &codegen_state) {}
2323

2424
void FinalizeAndRun(Label* pass, Label* fail) {

0 commit comments

Comments
 (0)