[llvm] [SandboxIR][NFC] Factor out common test for CastInst subclasses (PR #101410)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 15:41:36 PDT 2024


================
@@ -1620,142 +1620,95 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
   }
 }
 
-TEST_F(SandboxIRTest, SIToFPInst) {
-  parseIR(C, R"IR(
-define void @foo(i32 %arg) {
-  %sitofp = sitofp i32 %arg to float
-  ret void
-}
-)IR");
+/// CastInst's subclasses are very similar so we can use a common test function
+/// for them.
+template <typename SubclassT, sandboxir::Instruction::Opcode OpcodeT>
+void testCastInst(llvm::Module *M, Type *SrcTy, Type *DstTy) {
   Function &LLVMF = *M->getFunction("foo");
-  sandboxir::Context Ctx(C);
+  sandboxir::Context Ctx(M->getContext());
   sandboxir::Function *F = Ctx.createFunction(&LLVMF);
   unsigned ArgIdx = 0;
   auto *Arg = F->getArg(ArgIdx++);
   auto *BB = &*F->begin();
   auto It = BB->begin();
-  Type *Ti32 = Type::getInt32Ty(C);
-  Type *Tfloat = Type::getFloatTy(C);
 
-  auto *SIToFP = cast<sandboxir::SIToFPInst>(&*It++);
-  EXPECT_EQ(SIToFP->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
-  EXPECT_EQ(SIToFP->getSrcTy(), Ti32);
-  EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
+  auto *CI = cast<SubclassT>(&*It++);
+  EXPECT_EQ(CI->getOpcode(), OpcodeT);
+  EXPECT_EQ(CI->getSrcTy(), SrcTy);
+  EXPECT_EQ(CI->getDestTy(), DstTy);
   auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
 
   {
     // Check create() WhereIt, WhereBB
-    auto *NewI = cast<sandboxir::SIToFPInst>(
-        sandboxir::SIToFPInst::create(Arg, Tfloat, /*WhereIt=*/BB->end(),
-                                      /*WhereBB=*/BB, Ctx, "SIToFP"));
+    auto *NewI =
+        cast<SubclassT>(SubclassT::create(Arg, DstTy, /*WhereIt=*/BB->end(),
+                                          /*WhereBB=*/BB, Ctx, "NewCI"));
     // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
+    EXPECT_EQ(NewI->getOpcode(), OpcodeT);
     // Check getSrcTy().
     EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
     // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Tfloat);
+    EXPECT_EQ(NewI->getDestTy(), DstTy);
     // Check instr position.
     EXPECT_EQ(NewI->getNextNode(), nullptr);
     EXPECT_EQ(NewI->getPrevNode(), Ret);
+    // Check instr name.
+    EXPECT_EQ(NewI->getName(), "NewCI");
   }
   {
     // Check create() InsertBefore.
-    auto *NewI = cast<sandboxir::SIToFPInst>(
-        sandboxir::SIToFPInst::create(Arg, Tfloat,
-                                      /*InsertBefore=*/Ret, Ctx, "SIToFP"));
+    auto *NewI =
+        cast<SubclassT>(SubclassT::create(Arg, DstTy,
+                                          /*InsertBefore=*/Ret, Ctx, "NewCI"));
     // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
+    EXPECT_EQ(NewI->getOpcode(), OpcodeT);
     // Check getSrcTy().
     EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
     // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Tfloat);
+    EXPECT_EQ(NewI->getDestTy(), DstTy);
     // Check instr position.
     EXPECT_EQ(NewI->getNextNode(), Ret);
   }
   {
     // Check create() InsertAtEnd.
-    auto *NewI = cast<sandboxir::SIToFPInst>(
-        sandboxir::SIToFPInst::create(Arg, Tfloat,
-                                      /*InsertAtEnd=*/BB, Ctx, "SIToFP"));
+    auto *NewI =
+        cast<SubclassT>(SubclassT::create(Arg, DstTy,
+                                          /*InsertAtEnd=*/BB, Ctx, "NewCI"));
     // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
+    EXPECT_EQ(NewI->getOpcode(), OpcodeT);
     // Check getSrcTy().
     EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
     // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Tfloat);
+    EXPECT_EQ(NewI->getDestTy(), DstTy);
     // Check instr position.
     EXPECT_EQ(NewI->getNextNode(), nullptr);
     EXPECT_EQ(NewI->getParent(), BB);
   }
 }
 
+TEST_F(SandboxIRTest, SIToFPInst) {
+  parseIR(C, R"IR(
+define void @foo(i32 %arg) {
+  %sitofp = sitofp i32 %arg to float
+  ret void
+}
+)IR");
+  testCastInst<sandboxir::SIToFPInst, sandboxir::Instruction::Opcode::SIToFP>(
+      M.get(),
+      /*SrcTy=*/Type::getInt32Ty(C),
+      /*DstTy=*/Type::getFloatTy(C));
+}
+
 TEST_F(SandboxIRTest, FPToUIInst) {
   parseIR(C, R"IR(
 define void @foo(float %arg) {
   %fptoui = fptoui float %arg to i32
   ret void
 }
 )IR");
-  Function &LLVMF = *M->getFunction("foo");
-  sandboxir::Context Ctx(C);
-  sandboxir::Function *F = Ctx.createFunction(&LLVMF);
-  unsigned ArgIdx = 0;
-  auto *Arg = F->getArg(ArgIdx++);
-  auto *BB = &*F->begin();
-  auto It = BB->begin();
-  Type *Ti32 = Type::getInt32Ty(C);
-  Type *Tfloat = Type::getFloatTy(C);
-
-  auto *FPToUI = cast<sandboxir::FPToUIInst>(&*It++);
-  EXPECT_EQ(FPToUI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
-  EXPECT_EQ(FPToUI->getSrcTy(), Tfloat);
-  EXPECT_EQ(FPToUI->getDestTy(), Ti32);
-  auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+  testCastInst<sandboxir::FPToUIInst, sandboxir::Instruction::Opcode::FPToUI>(
 
-  {
-    // Check create() WhereIt, WhereBB
-    auto *NewI = cast<sandboxir::FPToUIInst>(
-        sandboxir::FPToUIInst::create(Arg, Ti32, /*WhereIt=*/BB->end(),
-                                      /*WhereBB=*/BB, Ctx, "FPToUI"));
-    // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
-    // Check getSrcTy().
-    EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
-    // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Ti32);
-    // Check instr position.
-    EXPECT_EQ(NewI->getNextNode(), nullptr);
-    EXPECT_EQ(NewI->getPrevNode(), Ret);
-  }
-  {
-    // Check create() InsertBefore.
-    auto *NewI = cast<sandboxir::FPToUIInst>(
-        sandboxir::FPToUIInst::create(Arg, Ti32,
-                                      /*InsertBefore=*/Ret, Ctx, "FPToUI"));
-    // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
-    // Check getSrcTy().
-    EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
-    // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Ti32);
-    // Check instr position.
-    EXPECT_EQ(NewI->getNextNode(), Ret);
-  }
-  {
-    // Check create() InsertAtEnd.
-    auto *NewI = cast<sandboxir::FPToUIInst>(
-        sandboxir::FPToUIInst::create(Arg, Ti32,
-                                      /*InsertAtEnd=*/BB, Ctx, "FPToUI"));
-    // Check getOpcode().
-    EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
-    // Check getSrcTy().
-    EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
-    // Check getDestTy().
-    EXPECT_EQ(NewI->getDestTy(), Ti32);
-    // Check instr position.
-    EXPECT_EQ(NewI->getNextNode(), nullptr);
-    EXPECT_EQ(NewI->getParent(), BB);
-  }
+      M.get(), /*SrcTy=*/Type::getFloatTy(C), /*DstTy=*/Type::getInt32Ty(C));
----------------
aeubanks wrote:

then this can be `*M`

https://github.com/llvm/llvm-project/pull/101410


More information about the llvm-commits mailing list