[PATCH] D153036: [FuncSpec] Add Freeze and CallBase to the InstCostVisitor.

Alexandros Lamprineas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 15 08:24:38 PDT 2023


labrinea created this revision.
labrinea added reviewers: ChuanqiXu, chill.
Herald added subscribers: hoy, ormris, hiraditya.
Herald added a project: All.
labrinea requested review of this revision.
Herald added a project: LLVM.

Allows constant folding of such instructions when estimating user bonus.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153036

Files:
  llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
  llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
  llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp


Index: llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
===================================================================
--- llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
+++ llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
@@ -215,13 +215,17 @@
   const char *ModuleString = R"(
     @g = constant [2 x i32] zeroinitializer, align 4
 
+    declare i32 @llvm.smax.i32(i32, i32)
+
     define i32 @foo(i8 %a, i1 %cond, ptr %b) {
       %cmp = icmp eq i8 %a, 10
       %ext = zext i1 %cmp to i32
       %sel = select i1 %cond, i32 %ext, i32 1
       %gep = getelementptr i32, ptr %b, i32 %sel
       %ld = load i32, ptr %gep
-      ret i32 %ld
+      %fr = freeze i32 %ld
+      %smax = call i32 @llvm.smax.i32(i32 %fr, i32 1)
+      ret i32 %smax
     }
   )";
 
@@ -240,6 +244,8 @@
   Instruction &Select = *BlockIter++;
   Instruction &Gep = *BlockIter++;
   Instruction &Load = *BlockIter++;
+  Instruction &Freeze = *BlockIter++;
+  Instruction &Smax = *BlockIter++;
 
   // icmp + zext
   Cost Ref = getInstCost(Icmp) + getInstCost(Zext);
@@ -251,8 +257,9 @@
   Bonus = Specializer.getSpecializationBonus(F->getArg(1), True, Visitor);
   EXPECT_EQ(Bonus, Ref);
 
-  // gep + load
-  Ref = getInstCost(Gep) + getInstCost(Load);
+  // gep + load + freeze + smax
+  Ref = getInstCost(Gep) + getInstCost(Load) + getInstCost(Freeze) +
+        getInstCost(Smax);
   Bonus = Specializer.getSpecializationBonus(F->getArg(2), GV, Visitor);
   EXPECT_EQ(Bonus, Ref);
 }
Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -222,6 +222,34 @@
   return estimateBasicBlocks(WorkList, KnownConstants, Solver, BFI, TTI);
 }
 
+Constant *InstCostVisitor::visitFreezeInst(FreezeInst &I) {
+  if (isGuaranteedNotToBeUndefOrPoison(LastVisited->second))
+    return LastVisited->second;
+  return nullptr;
+}
+
+Constant *InstCostVisitor::visitCallBase(CallBase &I) {
+  Function *F = I.getCalledFunction();
+  if (!F || !canConstantFoldCallTo(&I, F))
+    return nullptr;
+
+  SmallVector<Constant *, 8> Operands;
+  Operands.reserve(I.getNumOperands());
+
+  for (unsigned Idx = 0, E = I.getNumOperands() - 1; Idx != E; ++Idx) {
+    Value *V = I.getOperand(Idx);
+    auto *C = dyn_cast<Constant>(V);
+    if (!C)
+      C = findConstantFor(V, KnownConstants);
+    if (!C)
+      return nullptr;
+    Operands.push_back(C);
+  }
+
+  auto Ops = ArrayRef(Operands.begin(), Operands.end());
+  return ConstantFoldCall(&I, F, Ops);
+}
+
 Constant *InstCostVisitor::visitLoadInst(LoadInst &I) {
   if (isa<ConstantPointerNull>(LastVisited->second))
     return nullptr;
Index: llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
===================================================================
--- llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
+++ llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
@@ -143,6 +143,8 @@
   Cost estimateBranchInst(BranchInst &I);
 
   Constant *visitInstruction(Instruction &I) { return nullptr; }
+  Constant *visitFreezeInst(FreezeInst &I);
+  Constant *visitCallBase(CallBase &I);
   Constant *visitLoadInst(LoadInst &I);
   Constant *visitGetElementPtrInst(GetElementPtrInst &I);
   Constant *visitSelectInst(SelectInst &I);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153036.531767.patch
Type: text/x-patch
Size: 3402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230615/19f91b35/attachment.bin>


More information about the llvm-commits mailing list