-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Dump CheckIRId more explicitly for InstId tags #6172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
Based on #6168, first commit is |
toolchain/base/value_store.h
Outdated
} | ||
|
||
struct IrAndIndex { | ||
int32_t check_ir_id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't this use CheckIRId
? TBH I'm even a little confused why int32_t
for index, it seems like this could either be InstId
, or a template like:
template <typename IdT>
static auto DecomposeWithBestEffort(IdT id) -> std::pair<SemIR::CheckIRId, IdT> {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because this is in base/ not in sem_ir/. I wanted to make it use CheckIRId also but layering. We would have to move ValueStore to sem_ir/.
I also see on the PR introducing this that @zygoloid was thinking about future ValueStore usage where the tag would not even be a CheckIRId.
So maybe I should actually not call this check_ir_id at all here either, I will generalize it.
I think we could consider adding a type InstId::TagType = CheckIRId
and then we could template the IdTag class and make things more typesafe - with a fallback to int32_t (or maybe a sentinel type?) for id types that don't specify a tag type. Maybe we could have a specialization of IdTag that more obviously does nothing in those cases.
toolchain/base/value_store.h
Outdated
int32_t index; | ||
}; | ||
|
||
static auto DecomposeWithBestEffort(int32_t tagged_index) -> IrAndIndex { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's worth adding comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe @dwblaikie is going to follow up with some comments here
toolchain/base/value_store.h
Outdated
return (llvm::reverseBits(2) & tagged_index) != 0; | ||
} | ||
|
||
struct IrAndIndex { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we usually write "IR" instead of "Ir"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
toolchain/base/value_store.h
Outdated
// zero-based, to make it a bit less likely this doesn't collide with | ||
// anything else (though with the second-highest-bit-tagging this might not | ||
// be needed). | ||
id_tag_ = llvm::reverseBits((((id_index + 1) << 1) | 1) << 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're editing constructors, perhaps this should be done via initializer list instead of assignment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
toolchain/sem_ir/ids.cpp
Outdated
IdBase::Print(out); | ||
} else { | ||
out << "ir" << ir_id << ".inst" << simple_index; | ||
out << CheckIRId::Label << check_ir_id << "." << Label << inst_id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe construct a CheckIRId
here and use it for formatting? That way too, if we add special formatting (e.g. for Cpp
) it would automatically get it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea, thanks
int32_t id_tag_ = 0; | ||
int32_t initial_reserved_ids_ = std::numeric_limits<int32_t>::max(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you switch away from the prior setup? It seemed safer, since there are only two constructors and both must set different values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think setting things on the fields is generally safer and preferred. It's easy to miss a field in the other form. If you do, this way they all get initialized to some default value at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
toolchain/base/value_store.h
Outdated
} | ||
|
||
struct IrAndIndex { | ||
int32_t check_ir_id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because this is in base/ not in sem_ir/. I wanted to make it use CheckIRId also but layering. We would have to move ValueStore to sem_ir/.
I also see on the PR introducing this that @zygoloid was thinking about future ValueStore usage where the tag would not even be a CheckIRId.
So maybe I should actually not call this check_ir_id at all here either, I will generalize it.
I think we could consider adding a type InstId::TagType = CheckIRId
and then we could template the IdTag class and make things more typesafe - with a fallback to int32_t (or maybe a sentinel type?) for id types that don't specify a tag type. Maybe we could have a specialization of IdTag that more obviously does nothing in those cases.
toolchain/base/value_store.h
Outdated
int32_t index; | ||
}; | ||
|
||
static auto DecomposeWithBestEffort(int32_t tagged_index) -> IrAndIndex { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe @dwblaikie is going to follow up with some comments here
int32_t id_tag_ = 0; | ||
int32_t initial_reserved_ids_ = std::numeric_limits<int32_t>::max(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think setting things on the fields is generally safer and preferred. It's easy to miss a field in the other form. If you do, this way they all get initialized to some default value at least.
toolchain/base/value_store.h
Outdated
return (llvm::reverseBits(2) & tagged_index) != 0; | ||
} | ||
|
||
struct IrAndIndex { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
toolchain/base/value_store.h
Outdated
// zero-based, to make it a bit less likely this doesn't collide with | ||
// anything else (though with the second-highest-bit-tagging this might not | ||
// be needed). | ||
id_tag_ = llvm::reverseBits((((id_index + 1) << 1) | 1) << 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
toolchain/sem_ir/ids.cpp
Outdated
IdBase::Print(out); | ||
} else { | ||
out << "ir" << ir_id << ".inst" << simple_index; | ||
out << CheckIRId::Label << check_ir_id << "." << Label << inst_id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea, thanks
We swap the labels for ImportIRId and CheckIRId, so that CheckIRId the short name "ir" as this is now part of other ids (included as a tag), and is dumped as part of the id. Currently this is only for InstId but other Ids are coming.
Then we make the dumping process of the CheckIRId a bit more typesafe by having the IdTag return a CheckIRId, and we print it in the usual way so that any special casing in the implementation of CheckIRId::Print will be used.
TODOs are left behind to improve the typesafety for IdTag.