Skip to content

[SandboxIR] Fix CmpInst::create() when it gets folded #123408

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
Jan 17, 2025
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
13 changes: 6 additions & 7 deletions llvm/include/llvm/SandboxIR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2478,13 +2478,12 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
public:
using Predicate = llvm::CmpInst::Predicate;

static CmpInst *create(Predicate Pred, Value *S1, Value *S2,
InsertPosition Pos, Context &Ctx,
const Twine &Name = "");
static CmpInst *createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2,
const Instruction *FlagsSource,
InsertPosition Pos, Context &Ctx,
const Twine &Name = "");
static Value *create(Predicate Pred, Value *S1, Value *S2, InsertPosition Pos,
Context &Ctx, const Twine &Name = "");
static Value *createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2,
const Instruction *FlagsSource,
InsertPosition Pos, Context &Ctx,
const Twine &Name = "");
void setPredicate(Predicate P);
void swapOperands();

Expand Down
33 changes: 19 additions & 14 deletions llvm/lib/SandboxIR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,21 +926,26 @@ void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate) {
}
}

CmpInst *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
Value *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMI = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
if (dyn_cast<llvm::ICmpInst>(LLVMI))
return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMI));
return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMI));
}
CmpInst *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2,
const Instruction *F,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
CmpInst *Inst = create(P, S1, S2, Pos, Ctx, Name);
cast<llvm::CmpInst>(Inst->Val)->copyIRFlags(F->Val);
return Inst;
auto *LLVMV = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
// It may have been folded into a constant.
if (auto *LLVMC = dyn_cast<llvm::Constant>(LLVMV))
return Ctx.getOrCreateConstant(LLVMC);
if (isa<llvm::ICmpInst>(LLVMV))
return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMV));
return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMV));
}

Value *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2,
const Instruction *F, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
Value *V = create(P, S1, S2, Pos, Ctx, Name);
if (auto *C = dyn_cast<Constant>(V))
return C;
cast<llvm::CmpInst>(V->Val)->copyIRFlags(F->Val);
return V;
}

Type *CmpInst::makeCmpResultType(Type *OpndType) {
Expand Down
35 changes: 28 additions & 7 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5841,9 +5841,9 @@ define void @foo(i32 %i0, i32 %i1) {
EXPECT_EQ(ICmp->getSignedPredicate(), LLVMICmp->getSignedPredicate());
EXPECT_EQ(ICmp->getUnsignedPredicate(), LLVMICmp->getUnsignedPredicate());
}
auto *NewCmp =
auto *NewCmp = cast<sandboxir::CmpInst>(
sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0),
F.getArg(1), BB->begin(), Ctx, "NewCmp");
F.getArg(1), BB->begin(), Ctx, "NewCmp"));
EXPECT_EQ(NewCmp, &*BB->begin());
EXPECT_EQ(NewCmp->getPredicate(), llvm::CmpInst::ICMP_ULE);
EXPECT_EQ(NewCmp->getOperand(0), F.getArg(0));
Expand All @@ -5856,6 +5856,16 @@ define void @foo(i32 %i0, i32 %i1) {
sandboxir::Type *RT =
sandboxir::CmpInst::makeCmpResultType(F.getArg(0)->getType());
EXPECT_TRUE(RT->isIntegerTy(1)); // Only one bit in a single comparison

{
// Check create() when operands are constant.
auto *Const42 =
sandboxir::ConstantInt::get(sandboxir::Type::getInt32Ty(Ctx), 42);
auto *NewConstCmp =
sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, Const42, Const42,
BB->begin(), Ctx, "NewConstCmp");
EXPECT_TRUE(isa<sandboxir::Constant>(NewConstCmp));
}
}

TEST_F(SandboxIRTest, FCmpInst) {
Expand Down Expand Up @@ -5906,8 +5916,8 @@ define void @foo(float %f0, float %f1) {
CopyFrom->setFastMathFlags(FastMathFlags::getFast());

// create with default flags
auto *NewFCmp = sandboxir::CmpInst::create(
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), It1, Ctx, "NewFCmp");
auto *NewFCmp = cast<sandboxir::CmpInst>(sandboxir::CmpInst::create(
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), It1, Ctx, "NewFCmp"));
EXPECT_EQ(NewFCmp->getPredicate(), llvm::CmpInst::FCMP_ONE);
EXPECT_EQ(NewFCmp->getOperand(0), F.getArg(0));
EXPECT_EQ(NewFCmp->getOperand(1), F.getArg(1));
Expand All @@ -5917,9 +5927,10 @@ define void @foo(float %f0, float %f1) {
FastMathFlags DefaultFMF = NewFCmp->getFastMathFlags();
EXPECT_TRUE(CopyFrom->getFastMathFlags() != DefaultFMF);
// create with copied flags
auto *NewFCmpFlags = sandboxir::CmpInst::createWithCopiedFlags(
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, It1, Ctx,
"NewFCmpFlags");
auto *NewFCmpFlags =
cast<sandboxir::CmpInst>(sandboxir::CmpInst::createWithCopiedFlags(
llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, It1, Ctx,
"NewFCmpFlags"));
EXPECT_FALSE(NewFCmpFlags->getFastMathFlags() !=
CopyFrom->getFastMathFlags());
EXPECT_EQ(NewFCmpFlags->getPredicate(), llvm::CmpInst::FCMP_ONE);
Expand All @@ -5928,6 +5939,16 @@ define void @foo(float %f0, float %f1) {
#ifndef NDEBUG
EXPECT_EQ(NewFCmpFlags->getName(), "NewFCmpFlags");
#endif // NDEBUG

{
// Check create() when operands are constant.
auto *Const42 =
sandboxir::ConstantFP::get(sandboxir::Type::getFloatTy(Ctx), 42.0);
auto *NewConstCmp =
sandboxir::CmpInst::create(llvm::CmpInst::FCMP_ULE, Const42, Const42,
BB->begin(), Ctx, "NewConstCmp");
EXPECT_TRUE(isa<sandboxir::Constant>(NewConstCmp));
}
}

TEST_F(SandboxIRTest, UnreachableInst) {
Expand Down
Loading