-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[SandboxIR] Implement BitCastInst #101227
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
Conversation
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
return isa<Instruction>(From) && |
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 isa + cast is forbidden.
if (Instruction *I = dyn_cast<Instruction>(From))
return I->getOpcode() == Instruction::Opcode::BitCast;
return false;
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.
"Note that you should not use an isa<> test followed by a cast<>, for that use the dyn_cast<> operator."
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.
isa
+ cast
is pretty common for these exact cases where you want to early return if the instruction is of the wrong type. Just grep for it in llvm/lib
, you will find lots of examples that look exactly like this.
The alternative would look like:
auto *I = dyn_cast<Instruction>(From);
return I == nullptr ? false : I->getOpcode() == Instruction::Opcode::BitCast;
which is not as elegant and probably slower because of the extra control flow within dyn_cast
.
"Note that you should not use an isa<> test followed by a cast<>, for that use the dyn_cast<> operator."
I think this is meant to discourage things like:
Foo *I = nullptr;
if (isa<Foo>(V))
I = cast<Foo>(V);
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 both ways will probably optimize to the same code given they're doing the same thing.
can you do return isa<BitCastInst>(From)
?
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.
Do you mean isa<llvm::BitCastInst>(From->Val)
? Yes that should work.
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.
but this isn't a multi-instruction? other multi-instructions will have their own implementation of classof
that will have to take into account multiple LLVM instructions?
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.
The LLVM Programmer’s Manual
says that you should use dyn_cast. I would follow that.
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 not, but From
could be in a multi-Instruction. And it is theoretically possible to have two separate multi-instructions that contain llvm::BitCastInst
s as their Val
values. Which is why I think that using From->Val
's class is not the right approach. It is safer to get the SandboxIR instruction that points to From->Val
and get it's opcode.
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.
oh I see, I keep getting confused between LLVM IR and SandboxIR, I haven't spent long enough staring at SandboxIR code to get the namespaces straight in my head
I do slightly prefer tschuett's original suggestion over the current version, it's a bit less redundant even though it's an extra line
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.
OK I will update it to the dyn_cast
one.
This patch implements sandboxir::BitCastInst which mirrors llvm::BitCastInst.
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Instruction::Opcode::BitCast; |
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.
Please use dyn_cast for PtrToInt "classof" check too. I wasn't careful to check the use of isa+cast there.
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 will push an NFC to make this consistent with the rest. Though I still think that the isa + cast pattern looks a bit nicer as it is less verbose.
This patch implements sandboxir::BitCastInst which mirrors llvm::BitCastInst.