Skip to content

[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

Merged
merged 1 commit into from
Jul 30, 2024
Merged

[SandboxIR] Implement BitCastInst #101227

merged 1 commit into from
Jul 30, 2024

Conversation

vporpo
Copy link
Contributor

@vporpo vporpo commented Jul 30, 2024

This patch implements sandboxir::BitCastInst which mirrors llvm::BitCastInst.

Context &Ctx, const Twine &Name = "");

static bool classof(const Value *From) {
return isa<Instruction>(From) &&

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;

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."

Copy link
Contributor Author

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);

Copy link
Contributor

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)?

Copy link
Contributor Author

@vporpo vporpo Jul 30, 2024

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.

Copy link
Contributor

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?

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.

Copy link
Contributor Author

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::BitCastInsts 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.

Copy link
Contributor

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

Copy link
Contributor Author

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.
@vporpo vporpo merged commit e59c832 into llvm:main Jul 30, 2024
4 of 6 checks passed

static bool classof(const Value *From) {
if (auto *I = dyn_cast<Instruction>(From))
return I->getOpcode() == Instruction::Opcode::BitCast;
Copy link
Member

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.

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants