[llvm] f11d8c8 - [FuncSpec][NFC] Improve the unittest coverage.

Alexandros Lamprineas via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 19 01:47:07 PDT 2023


Author: Alexandros Lamprineas
Date: 2023-06-19T09:43:26+01:00
New Revision: f11d8c88dde9720e47aae92d7611d5b98ce30d79

URL: https://github.com/llvm/llvm-project/commit/f11d8c88dde9720e47aae92d7611d5b98ce30d79
DIFF: https://github.com/llvm/llvm-project/commit/f11d8c88dde9720e47aae92d7611d5b98ce30d79.diff

LOG: [FuncSpec][NFC] Improve the unittest coverage.

The specialization bonus is zero in some unittests because the basic blocks
containing the users of the constant arguments are executed less frequently
than the entry block. Sinking them into loops solves that.

Differential Revision: https://reviews.llvm.org/D153230

Added: 
    

Modified: 
    llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp b/llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
index 9169d505df400..df44a16987721 100644
--- a/llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
+++ b/llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
@@ -98,6 +98,8 @@ TEST_F(FunctionSpecializationTest, SwitchInst) {
   const char *ModuleString = R"(
     define void @foo(i32 %a, i32 %b, i32 %i) {
     entry:
+      br label %loop
+    loop:
       switch i32 %i, label %default
       [ i32 1, label %case1
         i32 2, label %case2 ]
@@ -111,10 +113,10 @@ TEST_F(FunctionSpecializationTest, SwitchInst) {
       br label %bb2
     bb1:
       %4 = add i32 %0, %b
-      br label %default
+      br label %loop
     bb2:
       %5 = or i32 %2, %a
-      br label %default
+      br label %loop
     default:
       ret void
     }
@@ -128,6 +130,7 @@ TEST_F(FunctionSpecializationTest, SwitchInst) {
   Constant *One = ConstantInt::get(IntegerType::getInt32Ty(M.getContext()), 1);
 
   auto FuncIter = F->begin();
+  ++FuncIter;
   BasicBlock &Case1 = *++FuncIter;
   BasicBlock &Case2 = *++FuncIter;
   BasicBlock &BB1 = *++FuncIter;
@@ -139,28 +142,33 @@ TEST_F(FunctionSpecializationTest, SwitchInst) {
   Instruction &BrBB2 = Case2.back();
   Instruction &Add = BB1.front();
   Instruction &Or = BB2.front();
-  Instruction &BrDefault = BB2.back();
+  Instruction &BrLoop = BB2.back();
 
   // mul
   Cost Ref = getInstCost(Mul);
   Cost Bonus = Specializer.getSpecializationBonus(F->getArg(0), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // and + or + add
   Ref = getInstCost(And) + getInstCost(Or) + getInstCost(Add);
   Bonus = Specializer.getSpecializationBonus(F->getArg(1), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // sdiv + br + br
-  Ref = getInstCost(Sdiv) + getInstCost(BrBB2) + getInstCost(BrDefault);
+  Ref = getInstCost(Sdiv) + getInstCost(BrBB2) + getInstCost(BrLoop);
   Bonus = Specializer.getSpecializationBonus(F->getArg(2), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 }
 
 TEST_F(FunctionSpecializationTest, BranchInst) {
   const char *ModuleString = R"(
     define void @foo(i32 %a, i32 %b, i1 %cond) {
     entry:
+      br label %loop
+    loop:
       br i1 %cond, label %bb0, label %bb2
     bb0:
       %0 = mul i32 %a, 2
@@ -169,7 +177,7 @@ TEST_F(FunctionSpecializationTest, BranchInst) {
     bb1:
       %2 = add i32 %0, %b
       %3 = sdiv i32 8, 2
-      br label %bb2
+      br label %loop
     bb2:
       ret void
     }
@@ -184,6 +192,7 @@ TEST_F(FunctionSpecializationTest, BranchInst) {
   Constant *False = ConstantInt::getFalse(M.getContext());
 
   auto FuncIter = F->begin();
+  ++FuncIter;
   BasicBlock &BB0 = *++FuncIter;
   BasicBlock &BB1 = *++FuncIter;
 
@@ -192,23 +201,26 @@ TEST_F(FunctionSpecializationTest, BranchInst) {
   Instruction &BrBB1 = BB0.back();
   Instruction &Add = BB1.front();
   Instruction &Sdiv = *++BB1.begin();
-  Instruction &BrBB2 = BB1.back();
+  Instruction &BrLoop = BB1.back();
 
   // mul
   Cost Ref = getInstCost(Mul);
   Cost Bonus = Specializer.getSpecializationBonus(F->getArg(0), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // add
   Ref = getInstCost(Add);
   Bonus = Specializer.getSpecializationBonus(F->getArg(1), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // sub + br + sdiv + br
   Ref = getInstCost(Sub) + getInstCost(BrBB1) + getInstCost(Sdiv) +
-        getInstCost(BrBB2);
+        getInstCost(BrLoop);
   Bonus = Specializer.getSpecializationBonus(F->getArg(2), False, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 }
 
 TEST_F(FunctionSpecializationTest, Misc) {
@@ -245,14 +257,17 @@ TEST_F(FunctionSpecializationTest, Misc) {
   Cost Ref = getInstCost(Icmp) + getInstCost(Zext);
   Cost Bonus = Specializer.getSpecializationBonus(F->getArg(0), One, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // select
   Ref = getInstCost(Select);
   Bonus = Specializer.getSpecializationBonus(F->getArg(1), True, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 
   // gep + load
   Ref = getInstCost(Gep) + getInstCost(Load);
   Bonus = Specializer.getSpecializationBonus(F->getArg(2), GV, Visitor);
   EXPECT_EQ(Bonus, Ref);
+  EXPECT_TRUE(Bonus > 0);
 }


        


More information about the llvm-commits mailing list