Skip to content

Commit ad0e581

Browse files
sethbrenithCommit Bot
authored andcommitted
[torque] Add option for printing Torque IR
While working on the Torque compiler, I've sometimes found it difficult to understand Torque's intermediate representation and how it corresponds to the output. In this change, I propose adding a build flag that instructs Torque to emit comments describing its IR, interspersed in the generated code. This is particularly useful for seeing the stack management instructions (Peek, Poke, and DeleteRange) which don't emit any corresponding C++ code. Bug: v8:7793 Change-Id: I24bdec47da76c9bd751b928d3cd92aa513dc6593 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2748040 Reviewed-by: Nico Hartmann <nicohartmann@chromium.org> Commit-Queue: Seth Brenith <seth.brenith@microsoft.com> Cr-Commit-Position: refs/heads/master@{#73352}
1 parent df748fc commit ad0e581

10 files changed

Lines changed: 278 additions & 2 deletions

BUILD.gn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ declare_args() {
252252
# file generation
253253
v8_verify_torque_generation_invariance = false
254254

255+
# Generate comments describing the Torque intermediate representation.
256+
v8_annotate_torque_ir = false
257+
255258
# Disable all snapshot compression.
256259
v8_enable_snapshot_compression = true
257260

@@ -1537,6 +1540,9 @@ template("run_torque") {
15371540
"-v8-root",
15381541
rebase_path(".", root_build_dir),
15391542
]
1543+
if (v8_annotate_torque_ir) {
1544+
args += [ "-annotate-ir" ]
1545+
}
15401546
if (defined(invoker.args)) {
15411547
args += invoker.args
15421548
}

src/torque/global-context.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ DEFINE_CONTEXTUAL_VARIABLE(TargetArchitecture)
1414
GlobalContext::GlobalContext(Ast ast)
1515
: collect_language_server_data_(false),
1616
force_assert_statements_(false),
17+
annotate_ir_(false),
1718
ast_(std::move(ast)) {
1819
CurrentScope::Scope current_scope(nullptr);
1920
CurrentSourcePosition::Scope current_source_position(

src/torque/global-context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class GlobalContext : public ContextualClass<GlobalContext> {
5454
static bool force_assert_statements() {
5555
return Get().force_assert_statements_;
5656
}
57+
static void SetAnnotateIR() { Get().annotate_ir_ = true; }
58+
static bool annotate_ir() { return Get().annotate_ir_; }
5759
static Ast* ast() { return &Get().ast_; }
5860
static std::string MakeUniqueName(const std::string& base) {
5961
return base + "_" + std::to_string(Get().fresh_ids_[base]++);
@@ -106,6 +108,7 @@ class GlobalContext : public ContextualClass<GlobalContext> {
106108
private:
107109
bool collect_language_server_data_;
108110
bool force_assert_statements_;
111+
bool annotate_ir_;
109112
Namespace* default_namespace_;
110113
Ast ast_;
111114
std::vector<std::unique_ptr<Declarable>> declarables_;

src/torque/instructions.cc

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ DefinitionLocation NamespaceConstantInstruction::GetValueDefinition(
129129
return DefinitionLocation::Instruction(this, index);
130130
}
131131

132+
std::ostream& operator<<(std::ostream& os,
133+
const NamespaceConstantInstruction& instruction) {
134+
return os << "NamespaceConstant " << instruction.constant->external_name();
135+
}
136+
132137
void InstructionBase::InvalidateTransientTypes(
133138
Stack<const Type*>* stack) const {
134139
auto current = stack->begin();
@@ -183,6 +188,22 @@ DefinitionLocation CallIntrinsicInstruction::GetValueDefinition(
183188
return DefinitionLocation::Instruction(this, index);
184189
}
185190

191+
std::ostream& operator<<(std::ostream& os,
192+
const CallIntrinsicInstruction& instruction) {
193+
os << "CallIntrinsic " << instruction.intrinsic->ReadableName();
194+
if (!instruction.specialization_types.empty()) {
195+
os << "<";
196+
PrintCommaSeparatedList(
197+
os, instruction.specialization_types,
198+
[](const Type* type) -> const Type& { return *type; });
199+
os << ">";
200+
}
201+
os << "(";
202+
PrintCommaSeparatedList(os, instruction.constexpr_arguments);
203+
os << ")";
204+
return os;
205+
}
206+
186207
void CallCsaMacroInstruction::TypeInstruction(Stack<const Type*>* stack,
187208
ControlFlowGraph* cfg) const {
188209
std::vector<const Type*> parameter_types =
@@ -243,6 +264,18 @@ DefinitionLocation CallCsaMacroInstruction::GetValueDefinition(
243264
return DefinitionLocation::Instruction(this, index);
244265
}
245266

267+
std::ostream& operator<<(std::ostream& os,
268+
const CallCsaMacroInstruction& instruction) {
269+
os << "CallCsaMacro " << instruction.macro->ReadableName();
270+
os << "(";
271+
PrintCommaSeparatedList(os, instruction.constexpr_arguments);
272+
os << ")";
273+
if (instruction.catch_block) {
274+
os << ", catch block " << (*instruction.catch_block)->id();
275+
}
276+
return os;
277+
}
278+
246279
void CallCsaMacroAndBranchInstruction::TypeInstruction(
247280
Stack<const Type*>* stack, ControlFlowGraph* cfg) const {
248281
std::vector<const Type*> parameter_types =
@@ -363,6 +396,26 @@ CallCsaMacroAndBranchInstruction::GetExceptionObjectDefinition() const {
363396
return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
364397
}
365398

399+
std::ostream& operator<<(std::ostream& os,
400+
const CallCsaMacroAndBranchInstruction& instruction) {
401+
os << "CallCsaMacroAndBranch " << instruction.macro->ReadableName();
402+
os << "(";
403+
PrintCommaSeparatedList(os, instruction.constexpr_arguments);
404+
os << ")";
405+
if (instruction.return_continuation) {
406+
os << ", return continuation " << (*instruction.return_continuation)->id();
407+
}
408+
if (!instruction.label_blocks.empty()) {
409+
os << ", label blocks ";
410+
PrintCommaSeparatedList(os, instruction.label_blocks,
411+
[](Block* block) { return block->id(); });
412+
}
413+
if (instruction.catch_block) {
414+
os << ", catch block " << (*instruction.catch_block)->id();
415+
}
416+
return os;
417+
}
418+
366419
void CallBuiltinInstruction::TypeInstruction(Stack<const Type*>* stack,
367420
ControlFlowGraph* cfg) const {
368421
std::vector<const Type*> argument_types = stack->PopMany(argc);
@@ -447,6 +500,19 @@ DefinitionLocation CallBuiltinPointerInstruction::GetValueDefinition(
447500
return DefinitionLocation::Instruction(this, index);
448501
}
449502

503+
std::ostream& operator<<(std::ostream& os,
504+
const CallBuiltinInstruction& instruction) {
505+
os << "CallBuiltin " << instruction.builtin->ReadableName()
506+
<< ", argc: " << instruction.argc;
507+
if (instruction.is_tailcall) {
508+
os << ", is_tailcall";
509+
}
510+
if (instruction.catch_block) {
511+
os << ", catch block " << (*instruction.catch_block)->id();
512+
}
513+
return os;
514+
}
515+
450516
void CallRuntimeInstruction::TypeInstruction(Stack<const Type*>* stack,
451517
ControlFlowGraph* cfg) const {
452518
std::vector<const Type*> argument_types = stack->PopMany(argc);
@@ -507,6 +573,19 @@ CallRuntimeInstruction::GetExceptionObjectDefinition() const {
507573
return DefinitionLocation::Instruction(this, GetValueDefinitionCount());
508574
}
509575

576+
std::ostream& operator<<(std::ostream& os,
577+
const CallRuntimeInstruction& instruction) {
578+
os << "CallRuntime " << instruction.runtime_function->ReadableName()
579+
<< ", argc: " << instruction.argc;
580+
if (instruction.is_tailcall) {
581+
os << ", is_tailcall";
582+
}
583+
if (instruction.catch_block) {
584+
os << ", catch block " << (*instruction.catch_block)->id();
585+
}
586+
return os;
587+
}
588+
510589
void BranchInstruction::TypeInstruction(Stack<const Type*>* stack,
511590
ControlFlowGraph* cfg) const {
512591
const Type* condition_type = stack->Pop();
@@ -524,6 +603,12 @@ void BranchInstruction::RecomputeDefinitionLocations(
524603
if_false->MergeInputDefinitions(*locations, worklist);
525604
}
526605

606+
std::ostream& operator<<(std::ostream& os,
607+
const BranchInstruction& instruction) {
608+
return os << "Branch true: " << instruction.if_true->id()
609+
<< ", false: " << instruction.if_false->id();
610+
}
611+
527612
void ConstexprBranchInstruction::TypeInstruction(Stack<const Type*>* stack,
528613
ControlFlowGraph* cfg) const {
529614
if_true->SetInputTypes(*stack);
@@ -536,6 +621,13 @@ void ConstexprBranchInstruction::RecomputeDefinitionLocations(
536621
if_false->MergeInputDefinitions(*locations, worklist);
537622
}
538623

624+
std::ostream& operator<<(std::ostream& os,
625+
const ConstexprBranchInstruction& instruction) {
626+
return os << "ConstexprBranch " << instruction.condition
627+
<< ", true: " << instruction.if_true->id()
628+
<< ", false: " << instruction.if_false->id();
629+
}
630+
539631
void GotoInstruction::TypeInstruction(Stack<const Type*>* stack,
540632
ControlFlowGraph* cfg) const {
541633
destination->SetInputTypes(*stack);
@@ -546,6 +638,10 @@ void GotoInstruction::RecomputeDefinitionLocations(
546638
destination->MergeInputDefinitions(*locations, worklist);
547639
}
548640

641+
std::ostream& operator<<(std::ostream& os, const GotoInstruction& instruction) {
642+
return os << "Goto " << instruction.destination->id();
643+
}
644+
549645
void GotoExternalInstruction::TypeInstruction(Stack<const Type*>* stack,
550646
ControlFlowGraph* cfg) const {
551647
if (variable_names.size() != stack->Size()) {
@@ -693,6 +789,16 @@ DefinitionLocation MakeLazyNodeInstruction::GetValueDefinition() const {
693789
return DefinitionLocation::Instruction(this, 0);
694790
}
695791

792+
std::ostream& operator<<(std::ostream& os,
793+
const MakeLazyNodeInstruction& instruction) {
794+
os << "MakeLazyNode " << instruction.macro->ReadableName() << ", "
795+
<< *instruction.result_type;
796+
for (const std::string& arg : instruction.constexpr_arguments) {
797+
os << ", " << arg;
798+
}
799+
return os;
800+
}
801+
696802
bool CallRuntimeInstruction::IsBlockTerminator() const {
697803
return is_tailcall || runtime_function->signature().return_type ==
698804
TypeOracle::GetNeverType();

0 commit comments

Comments
 (0)