Skip to content

[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 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 36 additions & 143 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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() if I is a pointer to a sub-class of CastInst. You can get it to work by casting it to sandboxir::CastInst *, like (gdb) ((sandboxir::CastInst *)I)->dump() which is annoying.

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 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 of

void FooInst::dump(raw_ostream &OS) const {
  dumpCommonPrefix(OS);
  dumpCommonSuffix(OS);
}

hurts.

can you test if B().f() in

struct A {
    void f();
};

struct B : A {
    using A::f;
};

works with gdb?

Copy link
Contributor

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 virtual dumpImpl() that may get overriden by the sub-classes if needed.

I added a using CastInst::dump in SIToFPInst, but gdb still complains: Couldn't find method llvm::sandboxir::SIToFPInst::dump.

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> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading