-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SandboxIR][NFC] Introduce templated CastInstImpl to simplify subclasses #101427
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1433,88 +1433,58 @@ class CastInst : public Instruction { | |
#endif | ||
}; | ||
|
||
class SIToFPInst final : public CastInst { | ||
// Helper class to simplify stamping out CastInst subclasses. | ||
template <Instruction::Opcode Op> class CastInstImpl : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Opcode::SIToFP; | ||
return false; | ||
const Twine &Name = "") { | ||
return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name); | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final; | ||
LLVM_DUMP_METHOD void dump() const final; | ||
#endif // NDEBUG | ||
}; | ||
|
||
class FPToUIInst final : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Opcode::FPToUI; | ||
return false; | ||
Context &Ctx, const Twine &Name = "") { | ||
return create(Src, DestTy, InsertBefore->getIterator(), | ||
InsertBefore->getParent(), Ctx, Name); | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final; | ||
LLVM_DUMP_METHOD void dump() const final; | ||
#endif // NDEBUG | ||
}; | ||
|
||
class FPToSIInst final : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
Context &Ctx, const Twine &Name = "") { | ||
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name); | ||
} | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Opcode::FPToSI; | ||
return I->getOpcode() == Op; | ||
return false; | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final; | ||
LLVM_DUMP_METHOD void dump() const final; | ||
#endif // NDEBUG | ||
}; | ||
|
||
class IntToPtrInst final : public CastInst { | ||
class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {}; | ||
class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {}; | ||
class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {}; | ||
class IntToPtrInst final : public CastInstImpl<Instruction::Opcode::IntToPtr> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clang-format shrug |
||
}; | ||
class PtrToIntInst final : public CastInstImpl<Instruction::Opcode::PtrToInt> { | ||
}; | ||
class BitCastInst final : public CastInstImpl<Instruction::Opcode::BitCast> {}; | ||
class AddrSpaceCastInst final | ||
: public CastInstImpl<Instruction::Opcode::AddrSpaceCast> { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Opcode::IntToPtr; | ||
return false; | ||
/// \Returns the pointer operand. | ||
Value *getPointerOperand() { return getOperand(0); } | ||
/// \Returns the pointer operand. | ||
const Value *getPointerOperand() const { | ||
return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand(); | ||
} | ||
/// \Returns the operand index of the pointer operand. | ||
static unsigned getPointerOperandIndex() { return 0u; } | ||
/// \Returns the address space of the pointer operand. | ||
unsigned getSrcAddressSpace() const { | ||
return getPointerOperand()->getType()->getPointerAddressSpace(); | ||
} | ||
/// \Returns the address space of the result. | ||
unsigned getDestAddressSpace() const { | ||
return getType()->getPointerAddressSpace(); | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final; | ||
LLVM_DUMP_METHOD void dump() const final; | ||
#endif // NDEBUG | ||
}; | ||
|
||
class PHINode final : public Instruction { | ||
|
@@ -1611,83 +1581,6 @@ class PHINode final : public Instruction { | |
LLVM_DUMP_METHOD void dump() const override; | ||
#endif | ||
}; | ||
class PtrToIntInst final : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
return isa<Instruction>(From) && | ||
cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt; | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final; | ||
LLVM_DUMP_METHOD void dump() const final; | ||
#endif // NDEBUG | ||
}; | ||
|
||
class BitCastInst : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Instruction::Opcode::BitCast; | ||
return false; | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const override; | ||
LLVM_DUMP_METHOD void dump() const override; | ||
#endif | ||
}; | ||
|
||
class AddrSpaceCastInst : public CastInst { | ||
public: | ||
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt, | ||
BasicBlock *WhereBB, Context &Ctx, | ||
const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore, | ||
Context &Ctx, const Twine &Name = ""); | ||
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, | ||
Context &Ctx, const Twine &Name = ""); | ||
|
||
static bool classof(const Value *From) { | ||
if (auto *I = dyn_cast<Instruction>(From)) | ||
return I->getOpcode() == Opcode::AddrSpaceCast; | ||
return false; | ||
} | ||
/// \Returns the pointer operand. | ||
Value *getPointerOperand() { return getOperand(0); } | ||
/// \Returns the pointer operand. | ||
const Value *getPointerOperand() const { | ||
return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand(); | ||
} | ||
/// \Returns the operand index of the pointer operand. | ||
static unsigned getPointerOperandIndex() { return 0u; } | ||
/// \Returns the address space of the pointer operand. | ||
unsigned getSrcAddressSpace() const { | ||
return getPointerOperand()->getType()->getPointerAddressSpace(); | ||
} | ||
/// \Returns the address space of the result. | ||
unsigned getDestAddressSpace() const { | ||
return getType()->getPointerAddressSpace(); | ||
} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const override; | ||
LLVM_DUMP_METHOD void dump() const override; | ||
#endif | ||
}; | ||
|
||
/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to | ||
/// an OpaqueInstr. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would keep these for now until we find a better fix for all of them. The issue is that gdb won't work with
I->dump()
ifI
is a pointer to a sub-class of CastInst. You can get it to work by casting it tosandboxir::CastInst *
, like(gdb) ((sandboxir::CastInst *)I)->dump()
which is annoying.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 looks like all
dump()
methods are the same right now, with multi-instructions being different in the future. there's gotta be a better way of deduplicating some of the logic, seeing so many copies ofhurts.
can you test if
B().f()
inworks with gdb?
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.
Yeah there is a ton of duplication. What might work is a non-virtual
Value::dump()
that calls virtualdumpImpl()
that may get overriden by the sub-classes if needed.I added a
using CastInst::dump
inSIToFPInst
, but gdb still complains:Couldn't find method llvm::sandboxir::SIToFPInst::dump
.