Skip to content

Commit b3726b9

Browse files
committed
review
1 parent df40a92 commit b3726b9

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

toolchain/base/value_store.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,15 @@ struct IdTag {
3636
IdTag() = default;
3737

3838
explicit IdTag(int32_t id_index, int32_t initial_reserved_ids)
39-
: initial_reserved_ids_(initial_reserved_ids) {
40-
// Shift down by 1 to get out of the high bit to avoid using any negative
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).
47-
id_tag_ = llvm::reverseBits((((id_index + 1) << 1) | 1) << 1);
48-
}
49-
50-
auto GetCheckIRId() const -> int32_t {
51-
return (llvm::reverseBits(id_tag_) >> 2) - 1;
52-
}
39+
: // Shift down by 1 to get out of the high bit to avoid using any
40+
// negative ids, since they have special uses. Shift down by another 1
41+
// to free up the second highest bit for a marker to indicate whether
42+
// the index is tagged (& needs to be untagged) or not. Add one to the
43+
// index so it's not zero-based, to make it a bit less likely this
44+
// doesn't collide with anything else (though with the
45+
// second-highest-bit-tagging this might not be needed).
46+
id_tag_(llvm::reverseBits((((id_index + 1) << 1) | 1) << 1)),
47+
initial_reserved_ids_(initial_reserved_ids) {}
5348

5449
auto Apply(int32_t index) const -> int32_t {
5550
if (index < initial_reserved_ids_) {
@@ -73,16 +68,24 @@ struct IdTag {
7368
return index;
7469
}
7570

71+
// Gets the value unique to this IdTag instance that is added to indices in
72+
// Apply, and removed in Remove.
73+
auto GetContainerTag() const -> int32_t {
74+
return (llvm::reverseBits(id_tag_) >> 2) - 1;
75+
}
76+
77+
// Returns whether `tagged_index` has an IdTag applied to it, from this IdTag
78+
// instance or any other one.
7679
static auto HasTag(int32_t tagged_index) -> bool {
7780
return (llvm::reverseBits(2) & tagged_index) != 0;
7881
}
7982

80-
struct IrAndIndex {
81-
int32_t check_ir_id;
83+
struct TagAndIndex {
84+
int32_t tag;
8285
int32_t index;
8386
};
8487

85-
static auto DecomposeWithBestEffort(int32_t tagged_index) -> IrAndIndex {
88+
static auto DecomposeWithBestEffort(int32_t tagged_index) -> TagAndIndex {
8689
if (tagged_index < 0) {
8790
return {-1, tagged_index};
8891
}
@@ -110,9 +113,9 @@ struct IdTag {
110113
return {-1, tagged_index};
111114
}
112115
auto index_mask = llvm::maskTrailingOnes<uint32_t>(location);
113-
auto check_ir_id = (llvm::reverseBits(tagged_index & ~index_mask) >> 2) - 1;
116+
auto tag = (llvm::reverseBits(tagged_index & ~index_mask) >> 2) - 1;
114117
auto index = tagged_index & index_mask;
115-
return {.check_ir_id = static_cast<int32_t>(check_ir_id),
118+
return {.tag = static_cast<int32_t>(tag),
116119
.index = static_cast<int32_t>(index)};
117120
}
118121

@@ -299,7 +302,7 @@ class ValueStore
299302
"Untagged index was outside of container range. Possibly tagged "
300303
"index {0}. Best-effort decomposition: CheckIRId: {1}, index: {2}. "
301304
"The IdTag for this container is: {3}",
302-
id.index, ir_id, decomposed_index, tag_.GetCheckIRId());
305+
id.index, ir_id, decomposed_index, tag_.GetContainerTag());
303306
}
304307
#endif
305308
return index;

toolchain/sem_ir/inst.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ struct LocIdAndInst {
438438
};
439439

440440
// Provides a ValueStore wrapper for an API specific to instructions.
441+
//
442+
// InstIds in this store are tagged by an IdTag using the File's CheckIRId as
443+
// the tag value.
441444
class InstStore {
442445
public:
443446
using IdType = InstId;

0 commit comments

Comments
 (0)