[llvm] [SandboxIR] Implement CmpInst, FCmpInst, and ICmpInst (PR #106301)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 30 12:33:04 PDT 2024
================
@@ -4392,6 +4392,159 @@ define void @foo(i32 %arg) {
EXPECT_EQ(NewPHI->getNumIncomingValues(), PHI->getNumIncomingValues());
}
+static void checkSwapOperands(sandboxir::Context &Ctx,
+ llvm::sandboxir::CmpInst *Cmp,
+ llvm::CmpInst *LLVMCmp) {
+ auto OrigOp0 = Cmp->getOperand(0);
+ auto OrigOp1 = Cmp->getOperand(1);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(0)), OrigOp0);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(1)), OrigOp1);
+ // This checks the dispatch mechanism in CmpInst, as well as
+ // the specific implementations.
+ Cmp->swapOperands();
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(1)), OrigOp0);
+ EXPECT_EQ(Ctx.getValue(LLVMCmp->getOperand(0)), OrigOp1);
+ // Undo it to keep the rest of the test consistent
+ Cmp->swapOperands();
+}
+
+static void checkCommonPredicates(sandboxir::CmpInst *Cmp,
+ llvm::CmpInst *LLVMCmp) {
+ // Check proper creation
+ auto Pred = Cmp->getPredicate();
+ auto LLVMPred = LLVMCmp->getPredicate();
+ EXPECT_EQ(Pred, LLVMPred);
+ // Check setPredicate
+ Cmp->setPredicate(llvm::CmpInst::FCMP_FALSE);
+ EXPECT_EQ(Cmp->getPredicate(), llvm::CmpInst::FCMP_FALSE);
+ EXPECT_EQ(LLVMCmp->getPredicate(), llvm::CmpInst::FCMP_FALSE);
+ Cmp->setPredicate(Pred);
+ EXPECT_EQ(LLVMCmp->getPredicate(), Pred);
+ // Ensure the accessors properly forward to the underlying implementation
+ EXPECT_STREQ(sandboxir::CmpInst::getPredicateName(Pred).data(),
+ llvm::CmpInst::getPredicateName(LLVMPred).data());
+ EXPECT_EQ(Cmp->isFPPredicate(), LLVMCmp->isFPPredicate());
+ EXPECT_EQ(Cmp->isIntPredicate(), LLVMCmp->isIntPredicate());
+ EXPECT_EQ(Cmp->getInversePredicate(), LLVMCmp->getInversePredicate());
+ EXPECT_EQ(Cmp->getOrderedPredicate(), LLVMCmp->getOrderedPredicate());
+ EXPECT_EQ(Cmp->getUnorderedPredicate(), LLVMCmp->getUnorderedPredicate());
+ EXPECT_EQ(Cmp->getSwappedPredicate(), LLVMCmp->getSwappedPredicate());
+ EXPECT_EQ(Cmp->isStrictPredicate(), LLVMCmp->isStrictPredicate());
+ EXPECT_EQ(Cmp->isNonStrictPredicate(), LLVMCmp->isNonStrictPredicate());
+ EXPECT_EQ(Cmp->isRelational(), LLVMCmp->isRelational());
+ if (Cmp->isRelational()) {
+ EXPECT_EQ(Cmp->getFlippedStrictnessPredicate(),
+ LLVMCmp->getFlippedStrictnessPredicate());
+ }
+ EXPECT_EQ(Cmp->isCommutative(), LLVMCmp->isCommutative());
+ EXPECT_EQ(Cmp->isTrueWhenEqual(), LLVMCmp->isTrueWhenEqual());
+ EXPECT_EQ(Cmp->isFalseWhenEqual(), LLVMCmp->isFalseWhenEqual());
+ EXPECT_EQ(sandboxir::CmpInst::isOrdered(Pred),
+ llvm::CmpInst::isOrdered(LLVMPred));
+ EXPECT_EQ(sandboxir::CmpInst::isUnordered(Pred),
+ llvm::CmpInst::isUnordered(LLVMPred));
+}
+
+TEST_F(SandboxIRTest, ICmpInst) {
+ SCOPED_TRACE("SandboxIRTest sandboxir::ICmpInst tests");
+ parseIR(C, R"IR(
+define void @foo(i32 %i0, i32 %i1) {
+ bb:
+ %ine = icmp ne i32 %i0, %i1
+ %iugt = icmp ugt i32 %i0, %i1
+ %iuge = icmp uge i32 %i0, %i1
+ %iult = icmp ult i32 %i0, %i1
+ %iule = icmp ule i32 %i0, %i1
+ %isgt = icmp sgt i32 %i0, %i1
+ %isle = icmp sle i32 %i0, %i1
+ %ieg = icmp eq i32 %i0, %i1
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ [[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
+
+ auto *LLVMBB = getBasicBlockByName(LLVMF, "bb");
+ auto LLVMIt = LLVMBB->begin();
+ auto *BB = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMBB));
+ auto It = BB->begin();
+ // Check classof()
+ while (auto *ICmp = dyn_cast<sandboxir::ICmpInst>(&*It++)) {
+ auto *LLVMICmp = cast<llvm::ICmpInst>(&*LLVMIt++);
+ checkSwapOperands(Ctx, ICmp, LLVMICmp);
+ checkCommonPredicates(ICmp, LLVMICmp);
+ EXPECT_EQ(ICmp->isSigned(), LLVMICmp->isSigned());
+ EXPECT_EQ(ICmp->isUnsigned(), LLVMICmp->isUnsigned());
+ EXPECT_EQ(ICmp->getSignedPredicate(), LLVMICmp->getSignedPredicate());
+ EXPECT_EQ(ICmp->getUnsignedPredicate(), LLVMICmp->getUnsignedPredicate());
+ }
+ auto *NewCmp =
+ sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0),
+ F.getArg(1), &*BB->begin(), Ctx, "");
+ EXPECT_EQ(NewCmp, &*BB->begin());
+}
+
+TEST_F(SandboxIRTest, FCmpInst) {
+ SCOPED_TRACE("SandboxIRTest sandboxir::FCmpInst tests");
+ parseIR(C, R"IR(
+define void @foo(float %f0, float %f1) {
+bb:
+ %ffalse = fcmp false float %f0, %f1
+ %foeq = fcmp oeq float %f0, %f1
+ %fogt = fcmp ogt float %f0, %f1
+ %folt = fcmp olt float %f0, %f1
+ %fole = fcmp ole float %f0, %f1
+ %fone = fcmp one float %f0, %f1
+ %ford = fcmp ord float %f0, %f1
+ %funo = fcmp uno float %f0, %f1
+ %fueq = fcmp ueq float %f0, %f1
+ %fugt = fcmp ugt float %f0, %f1
+ %fuge = fcmp uge float %f0, %f1
+ %fult = fcmp ult float %f0, %f1
+ %fule = fcmp ule float %f0, %f1
+ %fune = fcmp une float %f0, %f1
+ %ftrue = fcmp true float %f0, %f1
+ ret void
+bb1:
+ %copyfrom = fadd reassoc float %f0, 42.0
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ [[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
+
+ auto *LLVMBB = getBasicBlockByName(LLVMF, "bb");
+ auto LLVMIt = LLVMBB->begin();
+ auto *BB = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMBB));
+ auto It = BB->begin();
+ // Check classof()
+ while (auto *FCmp = dyn_cast<sandboxir::ICmpInst>(&*It++)) {
+ auto *LLVMFCmp = cast<llvm::ICmpInst>(&*LLVMIt++);
+ checkSwapOperands(Ctx, FCmp, LLVMFCmp);
+ checkCommonPredicates(FCmp, LLVMFCmp);
+ }
+
+ auto *LLVMBB1 = getBasicBlockByName(LLVMF, "bb1");
+ auto *BB1 = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMBB1));
+ auto It1 = BB1->begin();
+ auto *CopyFrom = &*It1++;
+ 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, "");
+ 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,
+ "");
----------------
vporpo wrote:
Let's name this "NewFCmpFlags"
https://github.com/llvm/llvm-project/pull/106301
More information about the llvm-commits
mailing list