Skip to content

Commit 09f60b6

Browse files
committed
debug-irs
1 parent b99bc00 commit 09f60b6

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

scripts/lldbinit.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ def print_usage() -> None:
4444
Dumps the value of an associated ID, using the C++ Dump() functions.
4545
4646
Usage:
47-
dump <CONTEXT> [<EXPR>|-- <EXPR>|<TYPE><ID>|<TYPE> <ID>]
47+
dump <CONTEXT> [<EXPR>|-- <EXPR>|<TYPE><ID>|<IR>.<TYPE><ID>]
4848
4949
Args:
5050
CONTEXT is the dump context, such a SemIR::Context reference, a SemIR::File,
5151
a Parse::Context, or a Lex::TokenizeBuffer.
5252
EXPR is a C++ expression such as a variable name. Use `--` to prevent it from
5353
being treated as a TYPE and ID.
54+
IR is the CheckIRId(N) in the form `irN`.
5455
TYPE can be `inst`, `constant`, `generic`, `impl`, `entity_name`, etc. See
5556
the `Label` string in `IdBase` classes to find possible TYPE names,
5657
though only Id types that have a matching `Make...Id()` function are
@@ -74,8 +75,13 @@ def print_usage() -> None:
7475

7576
context = args[0]
7677

77-
# The set of "Make" functions in dump.cpp.
78+
# The set of "Make" functions in dump.cpp. These use ADL to find the factory
79+
# function in `context/` or in `sem_ir/`.
7880
id_types = {
81+
"inst": "MakeInstId",
82+
}
83+
# These id types don't have an IdTag embedded in them.
84+
untagged_id_types = {
7985
"class": "SemIR::MakeClassId",
8086
"constant": "SemIR::MakeConstantId",
8187
"symbolic_constant": "SemIR::MakeSymbolicConstantId",
@@ -85,7 +91,6 @@ def print_usage() -> None:
8591
"generic": "SemIR::MakeGenericId",
8692
"impl": "SemIR::MakeImplId",
8793
"inst_block": "SemIR::MakeInstBlockId",
88-
"inst": "SemIR::MakeInstId",
8994
"interface": "SemIR::MakeInterfaceId",
9095
"name": "SemIR::MakeNameId",
9196
"name_scope": "SemIR::MakeNameScopeId",
@@ -110,27 +115,31 @@ def print_dump(context: str, expr: str) -> None:
110115

111116
# Try to find a type + id from the input args. If not, the id will be passed
112117
# through directly to C++, as it can be a variable name.
113-
id_type = None
118+
found_id_type = False
119+
120+
# Look for <irN>.<type><id> as a single argument.
121+
if m := re.fullmatch("ir(\\d+)\\.([a-z_]+)(\\d+)", args[1]):
122+
if m[2] in id_types:
123+
if len(args) != 2:
124+
print_usage()
125+
return
126+
id_type = m[2]
127+
print_dump(
128+
context, f"{id_types[id_type]}({context}, {m[1]}, {m[3]})"
129+
)
130+
found_id_type = True
114131

115132
# Look for <type><id> as a single argument.
116133
if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]):
117-
if m[1] in id_types:
134+
if m[1] in untagged_id_types:
118135
if len(args) != 2:
119136
print_usage()
120137
return
121138
id_type = m[1]
122139
print_dump(context, f"{id_types[id_type]}({m[2]})")
140+
found_id_type = True
123141

124-
# Look for <type> <id> as two arguments.
125-
if not id_type:
126-
if args[1] in id_types:
127-
if len(args) != 3:
128-
print_usage()
129-
return
130-
id_type = args[1]
131-
print_dump(context, f"{id_types[id_type]}({args[2]})")
132-
133-
if not id_type:
142+
if not found_id_type:
134143
# Use `--` to escape a variable name like `inst22`.
135144
if args[1] == "--":
136145
expr = " ".join(args[2:])

toolchain/check/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ cc_library(
180180
"//toolchain/parse:tree",
181181
"//toolchain/sem_ir:dump",
182182
"//toolchain/sem_ir:file",
183+
"//toolchain/sem_ir:typed_insts",
183184
],
184185
# Always link dump methods.
185186
alwayslink = 1,

toolchain/check/context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class Context {
243243
// Directly expose SemIR::File data accessors for brevity in calls.
244244
// --------------------------------------------------------------------------
245245

246+
auto check_ir_id() -> SemIR::CheckIRId { return sem_ir().check_ir_id(); }
246247
auto identifiers() -> SharedValueStores::IdentifierStore& {
247248
return sem_ir().identifiers();
248249
}

toolchain/check/dump.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
// - lldb: `expr Dump(context, id)`
1212
// - gdb: `call Dump(context, id)`
1313

14+
#include "toolchain/sem_ir/ids.h"
1415
#ifndef NDEBUG
1516

16-
#include "toolchain/lex/dump.h"
17-
1817
#include <string>
1918

2019
#include "common/check.h"
2120
#include "common/raw_string_ostream.h"
2221
#include "toolchain/check/context.h"
22+
#include "toolchain/lex/dump.h"
2323
#include "toolchain/lex/tokenized_buffer.h"
2424
#include "toolchain/parse/dump.h"
2525
#include "toolchain/parse/tree.h"
@@ -138,6 +138,17 @@ LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id)
138138
return SemIR::Dump(context.sem_ir(), type_id);
139139
}
140140

141+
LLVM_DUMP_METHOD static auto MakeInstId(Context& context, int check_ir_id,
142+
int id) -> SemIR::InstId {
143+
if (SemIR::CheckIRId(check_ir_id) == context.check_ir_id()) {
144+
return SemIR::MakeInstId(context.sem_ir(), check_ir_id, id);
145+
} else {
146+
const auto& import_ir = context.import_irs().Get(
147+
context.check_ir_map().Get(SemIR::CheckIRId(check_ir_id)));
148+
return SemIR::MakeInstId(*import_ir.sem_ir, check_ir_id, id);
149+
}
150+
}
151+
141152
} // namespace Carbon::Check
142153

143154
#endif // NDEBUG

toolchain/sem_ir/dump.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,13 @@ LLVM_DUMP_METHOD static auto MakeImplId(int id) -> ImplId { return ImplId(id); }
452452
LLVM_DUMP_METHOD static auto MakeInstBlockId(int id) -> InstBlockId {
453453
return InstBlockId(id);
454454
}
455-
LLVM_DUMP_METHOD static auto MakeInstId(int id) -> InstId { return InstId(id); }
455+
LLVM_DUMP_METHOD auto MakeInstId(const SemIR::File& file, int check_ir_id,
456+
int id) -> InstId {
457+
CARBON_CHECK(file.check_ir_id() == SemIR::CheckIRId(check_ir_id),
458+
"SemIR::MakeInstId given a CheckIRId for the wrong SemIR::File. "
459+
"Maybe you want to dump from a Check::Context.");
460+
return InstId(file.insts().GetIdTag().Apply(id));
461+
}
456462
LLVM_DUMP_METHOD static auto MakeInterfaceId(int id) -> InterfaceId {
457463
return InterfaceId(id);
458464
}

toolchain/sem_ir/dump.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ auto Dump(const File& file, StructTypeFieldsId struct_type_fields_id)
4242
-> std::string;
4343
auto Dump(const File& file, TypeId type_id) -> std::string;
4444

45+
auto MakeInstId(const SemIR::File& file, int check_ir_id, int id) -> InstId;
46+
4547
} // namespace Carbon::SemIR
4648

4749
#endif // NDEBUG

0 commit comments

Comments
 (0)