Skip to content

Commit e098d8b

Browse files
committed
rename-irs
1 parent 5b5b7b6 commit e098d8b

File tree

10 files changed

+266
-252
lines changed

10 files changed

+266
-252
lines changed

toolchain/base/value_store.h

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,60 @@ class ValueStoreNotPrintable {};
3333
} // namespace Internal
3434

3535
struct IdTag {
36-
IdTag()
37-
: id_tag_(0),
38-
initial_reserved_ids_(std::numeric_limits<int32_t>::max()) {}
36+
IdTag() = default;
37+
3938
explicit IdTag(int32_t id_index, int32_t initial_reserved_ids)
4039
: initial_reserved_ids_(initial_reserved_ids) {
4140
// Shift down by 1 to get out of the high bit to avoid using any negative
42-
// ids, since they have special uses.
43-
// Shift down by another 1 to free up the second highest bit for a marker to
44-
// indicate whether the index is tagged (& needs to be untagged) or not.
45-
// Add one to the index so it's not zero-based, to make it a bit less likely
46-
// this doesn't collide with anything else (though with the
47-
// second-highest-bit-tagging this might not be needed).
41+
// ids, since they have special uses. Shift down by another 1 to free up the
42+
// second highest bit for a marker to indicate whether the index is tagged
43+
// (& needs to be untagged) or not. Add one to the index so it's not
44+
// zero-based, to make it a bit less likely this doesn't collide with
45+
// anything else (though with the second-highest-bit-tagging this might not
46+
// be needed).
4847
id_tag_ = llvm::reverseBits((((id_index + 1) << 1) | 1) << 1);
4948
}
49+
5050
auto GetCheckIRId() const -> int32_t {
5151
return (llvm::reverseBits(id_tag_) >> 2) - 1;
5252
}
53+
5354
auto Apply(int32_t index) const -> int32_t {
5455
if (index < initial_reserved_ids_) {
5556
return index;
5657
}
57-
// assert that id_tag_ doesn't have the second highest bit set
58+
// TODO: Assert that id_tag_ doesn't have the second highest bit set.
5859
return index ^ id_tag_;
5960
}
60-
static auto DecomposeWithBestEffort(int32_t tagged_index)
61-
-> std::pair<int32_t, int32_t> {
61+
62+
auto Remove(int32_t tagged_index) const -> int32_t {
63+
if (!HasTag(tagged_index)) {
64+
CARBON_DCHECK(tagged_index < initial_reserved_ids_,
65+
"This untagged index is outside the initial reserved ids "
66+
"and should have been tagged.");
67+
return tagged_index;
68+
}
69+
auto index = tagged_index ^ id_tag_;
70+
CARBON_DCHECK(index >= initial_reserved_ids_,
71+
"When removing tagging bits, found an index that "
72+
"shouldn't've been tagged in the first place.");
73+
return index;
74+
}
75+
76+
static auto HasTag(int32_t tagged_index) -> bool {
77+
return (llvm::reverseBits(2) & tagged_index) != 0;
78+
}
79+
80+
struct IrAndIndex {
81+
int32_t check_ir_id;
82+
int32_t index;
83+
};
84+
85+
static auto DecomposeWithBestEffort(int32_t tagged_index) -> IrAndIndex {
6286
if (tagged_index < 0) {
6387
return {-1, tagged_index};
6488
}
65-
if ((llvm::reverseBits(2) & tagged_index) == 0) {
89+
if (!HasTag(tagged_index)) {
6690
return {-1, tagged_index};
6791
}
6892
int length = 0;
@@ -86,25 +110,15 @@ struct IdTag {
86110
return {-1, tagged_index};
87111
}
88112
auto index_mask = llvm::maskTrailingOnes<uint32_t>(location);
89-
auto ir_id = (llvm::reverseBits(tagged_index & ~index_mask) >> 2) - 1;
113+
auto check_ir_id = (llvm::reverseBits(tagged_index & ~index_mask) >> 2) - 1;
90114
auto index = tagged_index & index_mask;
91-
return {ir_id, index};
92-
}
93-
auto Remove(int32_t tagged_index) const -> int32_t {
94-
if ((llvm::reverseBits(2) & tagged_index) == 0) {
95-
CARBON_DCHECK(tagged_index < initial_reserved_ids_,
96-
"This untagged index is outside the initial reserved ids "
97-
"and should have been tagged.");
98-
return tagged_index;
99-
}
100-
auto index = tagged_index ^ id_tag_;
101-
CARBON_DCHECK(index >= initial_reserved_ids_,
102-
"When removing tagging bits, found an index that "
103-
"shouldn't've been tagged in the first place.");
104-
return index;
115+
return {.check_ir_id = static_cast<int32_t>(check_ir_id),
116+
.index = static_cast<int32_t>(index)};
105117
}
106-
int32_t id_tag_;
107-
int32_t initial_reserved_ids_;
118+
119+
private:
120+
int32_t id_tag_ = 0;
121+
int32_t initial_reserved_ids_ = std::numeric_limits<int32_t>::max();
108122
};
109123

110124
// A simple wrapper for accumulating values, providing IDs to later retrieve the

toolchain/check/testdata/basics/raw_sem_ir/builtins.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// CHECK:STDOUT: filename: builtins.carbon
1616
// CHECK:STDOUT: sem_ir:
1717
// CHECK:STDOUT: import_irs:
18-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
19-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
18+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
19+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
2020
// CHECK:STDOUT: import_ir_insts: {}
2121
// CHECK:STDOUT: clang_decls: {}
2222
// CHECK:STDOUT: name_scopes:

toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ fn G(x: Cpp.X) {
3333
// CHECK:STDOUT: filename: import.carbon
3434
// CHECK:STDOUT: sem_ir:
3535
// CHECK:STDOUT: import_irs:
36-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
37-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
36+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
37+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
3838
// CHECK:STDOUT: import_ir_insts:
39-
// CHECK:STDOUT: import_ir_inst0: {ir_id: ir1, clang_source_loc_id: clang_source_loc0}
39+
// CHECK:STDOUT: import_ir_inst0: {ir_id: import_ir1, clang_source_loc_id: clang_source_loc0}
4040
// CHECK:STDOUT: clang_decls:
4141
// CHECK:STDOUT: clang_decl_id0: {key: "<translation unit>", inst_id: ir0.inst16}
4242
// CHECK:STDOUT: clang_decl_id1: {key: "struct X {}", inst_id: ir0.inst18}

toolchain/check/testdata/basics/raw_sem_ir/multifile.carbon

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn B() {
3131
// CHECK:STDOUT: filename: a.carbon
3232
// CHECK:STDOUT: sem_ir:
3333
// CHECK:STDOUT: import_irs:
34-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
35-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
34+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
35+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
3636
// CHECK:STDOUT: import_ir_insts: {}
3737
// CHECK:STDOUT: clang_decls: {}
3838
// CHECK:STDOUT: name_scopes:
@@ -88,12 +88,12 @@ fn B() {
8888
// CHECK:STDOUT: filename: b.carbon
8989
// CHECK:STDOUT: sem_ir:
9090
// CHECK:STDOUT: import_irs:
91-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
92-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
93-
// CHECK:STDOUT: ir2: {decl_id: ir1.inst15, is_export: false}
91+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
92+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
93+
// CHECK:STDOUT: import_ir2: {decl_id: ir1.inst15, is_export: false}
9494
// CHECK:STDOUT: import_ir_insts:
95-
// CHECK:STDOUT: import_ir_inst0: {ir_id: ir2, inst_id: ir0.inst15}
96-
// CHECK:STDOUT: import_ir_inst1: {ir_id: ir2, inst_id: ir0.inst15}
95+
// CHECK:STDOUT: import_ir_inst0: {ir_id: import_ir2, inst_id: ir0.inst15}
96+
// CHECK:STDOUT: import_ir_inst1: {ir_id: import_ir2, inst_id: ir0.inst15}
9797
// CHECK:STDOUT: clang_decls: {}
9898
// CHECK:STDOUT: name_scopes:
9999
// CHECK:STDOUT: name_scope0: {inst: inst14, parent_scope: name_scope<none>, has_error: false, extended_scopes: [], names: {name1: ir1.inst16, name0: ir1.inst17}}

toolchain/check/testdata/basics/raw_sem_ir/multifile_with_textual_ir.carbon

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn B() {
3131
// CHECK:STDOUT: filename: a.carbon
3232
// CHECK:STDOUT: sem_ir:
3333
// CHECK:STDOUT: import_irs:
34-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
35-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
34+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
35+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
3636
// CHECK:STDOUT: import_ir_insts: {}
3737
// CHECK:STDOUT: clang_decls: {}
3838
// CHECK:STDOUT: name_scopes:
@@ -107,12 +107,12 @@ fn B() {
107107
// CHECK:STDOUT: filename: b.carbon
108108
// CHECK:STDOUT: sem_ir:
109109
// CHECK:STDOUT: import_irs:
110-
// CHECK:STDOUT: ir0: {decl_id: inst<none>, is_export: false}
111-
// CHECK:STDOUT: ir1: {decl_id: inst<none>, is_export: false}
112-
// CHECK:STDOUT: ir2: {decl_id: ir1.inst15, is_export: false}
110+
// CHECK:STDOUT: import_ir0: {decl_id: inst<none>, is_export: false}
111+
// CHECK:STDOUT: import_ir1: {decl_id: inst<none>, is_export: false}
112+
// CHECK:STDOUT: import_ir2: {decl_id: ir1.inst15, is_export: false}
113113
// CHECK:STDOUT: import_ir_insts:
114-
// CHECK:STDOUT: import_ir_inst0: {ir_id: ir2, inst_id: ir0.inst15}
115-
// CHECK:STDOUT: import_ir_inst1: {ir_id: ir2, inst_id: ir0.inst15}
114+
// CHECK:STDOUT: import_ir_inst0: {ir_id: import_ir2, inst_id: ir0.inst15}
115+
// CHECK:STDOUT: import_ir_inst1: {ir_id: import_ir2, inst_id: ir0.inst15}
116116
// CHECK:STDOUT: clang_decls: {}
117117
// CHECK:STDOUT: name_scopes:
118118
// CHECK:STDOUT: name_scope0: {inst: inst14, parent_scope: name_scope<none>, has_error: false, extended_scopes: [], names: {name1: ir1.inst16, name0: ir1.inst17}}

0 commit comments

Comments
 (0)