Commit 8cfbe0fc authored by Danylo Boiko's avatar Danylo Boiko Committed by V8 LUCI CQ

[turbofan] Turbofan's node bytecode origins

Bug: v8:7327
Change-Id: Ic805dc9bb1f653930d0bb34163d21aa34efc6a51
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3820069Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Danylo Boiko <danielboyko02@gmail.com>
Cr-Commit-Position: refs/heads/main@{#82498}
parent 1fe4edb2
......@@ -16,6 +16,7 @@
#include "src/compiler/linkage.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-observer.h"
#include "src/compiler/node-origin-table.h"
#include "src/compiler/operator-properties.h"
#include "src/compiler/simplified-operator.h"
#include "src/compiler/state-values-utils.h"
......@@ -39,7 +40,8 @@ class BytecodeGraphBuilder {
FeedbackCellRef const& feedback_cell,
BytecodeOffset osr_offset, JSGraph* jsgraph,
CallFrequency const& invocation_frequency,
SourcePositionTable* source_positions, int inlining_id,
SourcePositionTable* source_positions,
NodeOriginTable* node_origins, int inlining_id,
CodeKind code_kind, BytecodeGraphBuilderFlags flags,
TickCounter* tick_counter,
ObserveNodeInfo const& observe_node_info);
......@@ -53,6 +55,7 @@ class BytecodeGraphBuilder {
private:
class Environment;
class OsrIteratorState;
class BytecodePositionDecorator;
struct SubEnvironment;
void RemoveMergeEnvironmentsBeforeOffset(int limit_offset);
......@@ -65,6 +68,9 @@ class BytecodeGraphBuilder {
void VisitSingleBytecode();
void VisitBytecodes();
void AddBytecodePositionDecorator();
void RemoveBytecodePositionDecorator();
// Get or create the node that represents the outer function closure.
Node* GetFunctionClosure();
......@@ -353,9 +359,9 @@ class BytecodeGraphBuilder {
// Simulates entry and exit of exception handlers.
void ExitThenEnterExceptionHandlers(int current_offset);
// Update the current position of the {SourcePositionTable} to that of the
// bytecode at {offset}, if any.
void UpdateSourcePosition(int offset);
// Update the current position of {SourcePositionTable} and
// {NodeOriginTable} to that bytecode at {offset}, if any.
void UpdateSourceAndBytecodePosition(int offset);
// Growth increment for the temporary buffer used to construct input lists to
// new nodes.
......@@ -458,6 +464,7 @@ class BytecodeGraphBuilder {
interpreter::BytecodeArrayIterator bytecode_iterator_;
BytecodeAnalysis const bytecode_analysis_;
Environment* environment_;
BytecodePositionDecorator* decorator_;
bool const osr_;
int currently_peeled_loop_offset_;
......@@ -504,6 +511,9 @@ class BytecodeGraphBuilder {
StateValuesCache state_values_cache_;
// The node origins table, to store bytecode origins.
NodeOriginTable* const node_origins_;
// The source position table, to be populated.
SourcePositionTable* const source_positions_;
......@@ -1014,15 +1024,30 @@ Node* BytecodeGraphBuilder::Environment::Checkpoint(
return result;
}
class BytecodeGraphBuilder::BytecodePositionDecorator final :
public GraphDecorator {
public:
explicit BytecodePositionDecorator(NodeOriginTable* node_origins)
: node_origins_(node_origins) {}
void Decorate(Node* node) final {
node_origins_->SetNodeOrigin(node->id(), NodeOrigin::kJSBytecode,
node_origins_->GetCurrentBytecodePosition());
}
private:
NodeOriginTable* node_origins_;
};
BytecodeGraphBuilder::BytecodeGraphBuilder(
JSHeapBroker* broker, Zone* local_zone,
NativeContextRef const& native_context,
SharedFunctionInfoRef const& shared_info,
FeedbackCellRef const& feedback_cell, BytecodeOffset osr_offset,
JSGraph* jsgraph, CallFrequency const& invocation_frequency,
SourcePositionTable* source_positions, int inlining_id, CodeKind code_kind,
BytecodeGraphBuilderFlags flags, TickCounter* tick_counter,
ObserveNodeInfo const& observe_node_info)
SourcePositionTable* source_positions, NodeOriginTable* node_origins,
int inlining_id, CodeKind code_kind, BytecodeGraphBuilderFlags flags,
TickCounter* tick_counter, ObserveNodeInfo const& observe_node_info)
: broker_(broker),
local_isolate_(broker_->local_isolate()
? broker_->local_isolate()
......@@ -1051,6 +1076,7 @@ BytecodeGraphBuilder::BytecodeGraphBuilder(
bytecode_array().object(), local_zone, osr_offset,
flags & BytecodeGraphBuilderFlag::kAnalyzeEnvironmentLiveness),
environment_(nullptr),
decorator_(nullptr),
osr_(!osr_offset.IsNone()),
currently_peeled_loop_offset_(-1),
skip_first_stack_and_tierup_check_(
......@@ -1068,6 +1094,7 @@ BytecodeGraphBuilder::BytecodeGraphBuilder(
needs_eager_checkpoint_(true),
exit_controls_(local_zone),
state_values_cache_(jsgraph),
node_origins_(node_origins),
source_positions_(source_positions),
start_position_(shared_info.StartPosition(), inlining_id),
tick_counter_(tick_counter),
......@@ -1132,7 +1159,9 @@ FeedbackSource BytecodeGraphBuilder::CreateFeedbackSource(FeedbackSlot slot) {
void BytecodeGraphBuilder::CreateGraph() {
SourcePositionTable::Scope pos_scope(source_positions_, start_position_);
if (node_origins_) {
AddBytecodePositionDecorator();
}
// Set up the basic structure of the graph. Outputs for {Start} are the formal
// parameters (including the receiver) plus new target, number of arguments,
// context and closure.
......@@ -1157,6 +1186,9 @@ void BytecodeGraphBuilder::CreateGraph() {
Node** const inputs = &exit_controls_.front();
Node* end = graph()->NewNode(common()->End(input_count), input_count, inputs);
graph()->SetEnd(end);
if (node_origins_) {
RemoveBytecodePositionDecorator();
}
}
void BytecodeGraphBuilder::PrepareEagerCheckpoint() {
......@@ -1213,7 +1245,8 @@ void BytecodeGraphBuilder::PrepareFrameState(
void BytecodeGraphBuilder::AdvanceIteratorsTo(int bytecode_offset) {
for (; bytecode_iterator().current_offset() != bytecode_offset;
bytecode_iterator().Advance()) {
UpdateSourcePosition(bytecode_iterator().current_offset());
int current_offset = bytecode_iterator().current_offset();
UpdateSourceAndBytecodePosition(current_offset);
}
}
......@@ -1416,7 +1449,7 @@ void BytecodeGraphBuilder::AdvanceToOsrEntryAndPeelLoops() {
void BytecodeGraphBuilder::VisitSingleBytecode() {
tick_counter_->TickAndMaybeEnterSafepoint();
int current_offset = bytecode_iterator().current_offset();
UpdateSourcePosition(current_offset);
UpdateSourceAndBytecodePosition(current_offset);
ExitThenEnterExceptionHandlers(current_offset);
DCHECK_GE(exception_handlers_.empty() ? current_offset
: exception_handlers_.top().end_offset_,
......@@ -1460,6 +1493,18 @@ void BytecodeGraphBuilder::VisitBytecodes() {
DCHECK(exception_handlers_.empty());
}
void BytecodeGraphBuilder::AddBytecodePositionDecorator() {
DCHECK_NULL(decorator_);
decorator_ = graph_zone()->New<BytecodePositionDecorator>(node_origins_);
graph()->AddDecorator(decorator_);
}
void BytecodeGraphBuilder::RemoveBytecodePositionDecorator() {
DCHECK_NOT_NULL(decorator_);
graph()->RemoveDecorator(decorator_);
decorator_ = nullptr;
}
void BytecodeGraphBuilder::VisitLdaZero() {
Node* node = jsgraph()->ZeroConstant();
environment()->BindAccumulator(node);
......@@ -4391,7 +4436,10 @@ Node* BytecodeGraphBuilder::MergeValue(Node* value, Node* other,
return value;
}
void BytecodeGraphBuilder::UpdateSourcePosition(int offset) {
void BytecodeGraphBuilder::UpdateSourceAndBytecodePosition(int offset) {
if (node_origins_) {
node_origins_->SetCurrentBytecodePosition(offset);
}
if (source_position_iterator().done()) return;
if (source_position_iterator().code_offset() == offset) {
source_positions_->SetCurrentPosition(SourcePosition(
......@@ -4409,15 +4457,15 @@ void BuildGraphFromBytecode(JSHeapBroker* broker, Zone* local_zone,
BytecodeOffset osr_offset, JSGraph* jsgraph,
CallFrequency const& invocation_frequency,
SourcePositionTable* source_positions,
int inlining_id, CodeKind code_kind,
BytecodeGraphBuilderFlags flags,
NodeOriginTable* node_origins, int inlining_id,
CodeKind code_kind, BytecodeGraphBuilderFlags flags,
TickCounter* tick_counter,
ObserveNodeInfo const& observe_node_info) {
BytecodeGraphBuilder builder(
broker, local_zone, broker->target_native_context(), shared_info,
feedback_cell, osr_offset, jsgraph, invocation_frequency,
source_positions, inlining_id, code_kind, flags, tick_counter,
observe_node_info);
source_positions, node_origins, inlining_id, code_kind, flags,
tick_counter, observe_node_info);
builder.CreateGraph();
}
......
......@@ -26,6 +26,7 @@ namespace compiler {
class JSGraph;
class NodeObserver;
class SourcePositionTable;
class NodeOriginTable;
enum class BytecodeGraphBuilderFlag : uint8_t {
kSkipFirstStackAndTierupCheck = 1 << 0,
......@@ -45,8 +46,8 @@ void BuildGraphFromBytecode(JSHeapBroker* broker, Zone* local_zone,
BytecodeOffset osr_offset, JSGraph* jsgraph,
CallFrequency const& invocation_frequency,
SourcePositionTable* source_positions,
int inlining_id, CodeKind code_kind,
BytecodeGraphBuilderFlags flags,
NodeOriginTable* node_origins, int inlining_id,
CodeKind code_kind, BytecodeGraphBuilderFlags flags,
TickCounter* tick_counter,
ObserveNodeInfo const& observe_node_info = {});
......
......@@ -18,9 +18,11 @@ class JSInliningHeuristic final : public AdvancedReducer {
JSInliningHeuristic(Editor* editor, Zone* local_zone,
OptimizedCompilationInfo* info, JSGraph* jsgraph,
JSHeapBroker* broker,
SourcePositionTable* source_positions, Mode mode)
SourcePositionTable* source_positions,
NodeOriginTable* node_origins, Mode mode)
: AdvancedReducer(editor),
inliner_(editor, local_zone, info, jsgraph, broker, source_positions),
inliner_(editor, local_zone, info, jsgraph, broker, source_positions,
node_origins),
candidates_(local_zone),
seen_(local_zone),
source_positions_(source_positions),
......
......@@ -579,8 +579,8 @@ Reduction JSInliner::ReduceJSCall(Node* node) {
CallFrequency frequency = call.frequency();
BuildGraphFromBytecode(broker(), zone(), *shared_info, feedback_cell,
BytecodeOffset::None(), jsgraph(), frequency,
source_positions_, inlining_id, info_->code_kind(),
flags, &info_->tick_counter());
source_positions_, node_origins_, inlining_id,
info_->code_kind(), flags, &info_->tick_counter());
}
// Extract the inlinee start/end nodes.
......
......@@ -7,6 +7,7 @@
#include "src/compiler/graph-reducer.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node-origin-table.h"
namespace v8 {
namespace internal {
......@@ -25,13 +26,15 @@ class JSInliner final : public AdvancedReducer {
public:
JSInliner(Editor* editor, Zone* local_zone, OptimizedCompilationInfo* info,
JSGraph* jsgraph, JSHeapBroker* broker,
SourcePositionTable* source_positions)
SourcePositionTable* source_positions,
NodeOriginTable* node_origins)
: AdvancedReducer(editor),
local_zone_(local_zone),
info_(info),
jsgraph_(jsgraph),
broker_(broker),
source_positions_(source_positions) {}
source_positions_(source_positions),
node_origins_(node_origins){}
const char* reducer_name() const override { return "JSInliner"; }
......@@ -61,6 +64,7 @@ class JSInliner final : public AdvancedReducer {
JSGraph* const jsgraph_;
JSHeapBroker* const broker_;
SourcePositionTable* const source_positions_;
NodeOriginTable* const node_origins_;
base::Optional<SharedFunctionInfoRef> DetermineCallTarget(Node* node);
FeedbackCellRef DetermineCallContext(Node* node, Node** context_out);
......
......@@ -17,6 +17,7 @@ void NodeOrigin::PrintJson(std::ostream& out) const {
out << "\"nodeId\" : ";
break;
case kWasmBytecode:
case kJSBytecode:
out << "\"bytecodePosition\" : ";
break;
}
......@@ -42,6 +43,7 @@ NodeOriginTable::NodeOriginTable(Graph* graph)
: graph_(graph),
decorator_(nullptr),
current_origin_(NodeOrigin::Unknown()),
current_bytecode_position_(0),
current_phase_name_("unknown"),
table_(graph->zone()) {}
......@@ -70,6 +72,10 @@ void NodeOriginTable::SetNodeOrigin(Node* node, const NodeOrigin& no) {
void NodeOriginTable::SetNodeOrigin(NodeId id, NodeId origin) {
table_.Set(id, NodeOrigin(current_phase_name_, "", origin));
}
void NodeOriginTable::SetNodeOrigin(NodeId id, NodeOrigin::OriginKind kind,
NodeId origin) {
table_.Set(id, NodeOrigin(current_phase_name_, "", kind, origin));
}
void NodeOriginTable::PrintJson(std::ostream& os) const {
os << "{";
......
......@@ -16,7 +16,7 @@ namespace compiler {
class NodeOrigin {
public:
enum OriginKind { kWasmBytecode, kGraphNode };
enum OriginKind { kWasmBytecode, kGraphNode, kJSBytecode };
NodeOrigin(const char* phase_name, const char* reducer_name,
NodeId created_from)
: phase_name_(phase_name),
......@@ -123,9 +123,16 @@ class V8_EXPORT_PRIVATE NodeOriginTable final
NodeOrigin GetNodeOrigin(NodeId id) const;
void SetNodeOrigin(Node* node, const NodeOrigin& no);
void SetNodeOrigin(NodeId id, NodeId origin);
void SetNodeOrigin(NodeId id, NodeOrigin::OriginKind kind, NodeId origin);
void SetCurrentPosition(const NodeOrigin& no) { current_origin_ = no; }
void SetCurrentBytecodePosition(int offset) {
current_bytecode_position_ = offset;
}
int GetCurrentBytecodePosition() { return current_bytecode_position_; }
void PrintJson(std::ostream& os) const;
private:
......@@ -134,6 +141,7 @@ class V8_EXPORT_PRIVATE NodeOriginTable final
Graph* const graph_;
Decorator* decorator_;
NodeOrigin current_origin_;
int current_bytecode_position_;
const char* current_phase_name_;
static NodeOrigin UnknownNodeOrigin(Zone* zone) {
......
......@@ -1353,8 +1353,9 @@ struct GraphBuilderPhase {
data->broker(), temp_zone, closure.shared(),
closure.raw_feedback_cell(data->dependencies()),
data->info()->osr_offset(), data->jsgraph(), frequency,
data->source_positions(), SourcePosition::kNotInlined,
data->info()->code_kind(), flags, &data->info()->tick_counter(),
data->source_positions(), data->node_origins(),
SourcePosition::kNotInlined, data->info()->code_kind(),
flags, &data->info()->tick_counter(),
ObserveNodeInfo{data->observe_node_manager(),
data->info()->node_observer()});
}
......@@ -1402,7 +1403,8 @@ struct InliningPhase {
data->dependencies(), temp_zone, info->zone());
JSInliningHeuristic inlining(
&graph_reducer, temp_zone, data->info(), data->jsgraph(),
data->broker(), data->source_positions(), JSInliningHeuristic::kJSOnly);
data->broker(), data->source_positions(), data->node_origins(),
JSInliningHeuristic::kJSOnly);
JSIntrinsicLowering intrinsic_lowering(&graph_reducer, data->jsgraph(),
data->broker());
......@@ -1443,6 +1445,7 @@ struct JSWasmInliningPhase {
JSInliningHeuristic inlining(&graph_reducer, temp_zone, data->info(),
data->jsgraph(), data->broker(),
data->source_positions(),
data->node_origins(),
JSInliningHeuristic::kWasmOnly);
AddReducer(data, &graph_reducer, &dead_code_elimination);
AddReducer(data, &graph_reducer, &common_reducer);
......@@ -3676,7 +3679,7 @@ bool PipelineImpl::SelectInstructions(Linkage* linkage) {
} else {
source_position_output << "{}";
}
source_position_output << ",\n\"NodeOrigins\" : ";
source_position_output << ",\n\"nodeOrigins\" : ";
data_->node_origins()->PrintJson(source_position_output);
data_->set_source_position_output(source_position_output.str());
}
......
......@@ -34,7 +34,7 @@ void JSONTurboshaftGraphWriter::PrintNodes() {
OpIndex index = turboshaft_graph_.Index(op);
if (!first) os_ << ",\n";
first = false;
os_ << "{\"id\":" << turboshaft_graph_.Index(op).id() << ",";
os_ << "{\"id\":" << index.id() << ",";
os_ << "\"title\":\"" << OpcodeName(op.opcode) << "\",";
os_ << "\"block_id\":" << block.index().id() << ",";
os_ << "\"op_properties_type\":\"" << op.properties() << "\"";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment