[llvm] 801c2b9 - [FuncSpec] Add an option to specializing literal constant

Chuanqi Xu via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 29 20:30:14 PDT 2021


Author: Chuanqi Xu
Date: 2021-06-30T11:26:44+08:00
New Revision: 801c2b9bbaad778fd4f9fb25b4ab2bd8742a5a3b

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

LOG: [FuncSpec] Add an option to specializing literal constant

Now the option is off by default. Since we are not sure if this option
would make the compile time increase aggressively. Although we tested it
on SPEC2017, we may need to test more to make it on by default.

Reviewed By: SjoerdMeijer

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

Added: 
    llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers.ll

Modified: 
    llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 8c1a78a88ec5a..f61f4312b7776 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -12,7 +12,7 @@
 //
 // Current limitations:
 // - It does not handle specialization of recursive functions,
-// - It does not yet handle integer constants, and integer ranges,
+// - It does not yet handle integer ranges.
 // - Only 1 argument per function is specialised,
 // - The cost-model could be further looked into,
 // - We are not yet caching analysis results.
@@ -64,6 +64,10 @@ static cl::opt<unsigned>
                           cl::desc("Average loop iteration count cost"),
                           cl::init(10));
 
+static cl::opt<bool> EnableSpecializationForLiteralConstant(
+    "function-specialization-for-literal-constant", cl::init(false), cl::Hidden,
+    cl::desc("Make function specialization available for literal constant."));
+
 // Helper to check if \p LV is either overdefined or a constant int.
 static bool isOverdefined(const ValueLatticeElement &LV) {
   return !LV.isUnknownOrUndef() && !LV.isConstant();
@@ -485,17 +489,11 @@ class FunctionSpecializer {
         }
       }
 
-      // Get the lattice value for the value the call site passes to the
-      // argument. If this value is not constant, move on to the next call
-      // site. Additionally, set the AllConstant flag to false.
-      if (V != A && !Solver.getLatticeValueFor(V).isConstant()) {
+      if (isa<Constant>(V) && (Solver.getLatticeValueFor(V).isConstant() ||
+                               EnableSpecializationForLiteralConstant))
+        Constants.push_back(cast<Constant>(V));
+      else
         AllConstant = false;
-        continue;
-      }
-
-      // Add the constant to the set.
-      if (auto *C = dyn_cast<Constant>(CS.getArgOperand(A->getArgNo())))
-        Constants.push_back(C);
     }
 
     // If the argument can only take on constant values, AllConstant will be

diff  --git a/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers.ll b/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers.ll
new file mode 100644
index 0000000000000..598f73691ba74
--- /dev/null
+++ b/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers.ll
@@ -0,0 +1,44 @@
+; RUN: opt -function-specialization -function-specialization-for-literal-constant=true -S < %s | FileCheck %s
+
+; Check that the literal constant parameter could be specialized.
+; CHECK: @foo.1(
+; CHECK: @foo.2(
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+
+declare i32 @getValue()
+declare i1 @getCond()
+
+define internal i32 @foo(i1 %break_cond) {
+entry:
+  br label %loop.entry
+
+loop.entry:
+  br label %loop2.entry
+
+loop2.entry:
+  br label %loop2.body
+
+loop2.body:
+  %value = call i32 @getValue()
+  br i1 %break_cond, label %loop2.end, label %return
+
+loop2.end:
+  %cond.end = call i1 @getCond()
+  br i1 %cond.end, label %loop2.entry, label %loop.end
+
+loop.end:
+  %cond2.end = call i1 @getCond()
+  br i1 %cond2.end, label %loop.entry, label %return
+
+return:
+  ret i32 %value
+}
+
+define dso_local i32 @bar(i32 %x, i32 %y) {
+entry:
+  %retval.1 = call i32 @foo(i1 1)
+  %retval.2 = call i32 @foo(i1 0)
+  %retval = add nsw i32 %retval.1, %retval.2
+  ret i32 %retval
+}
\ No newline at end of file


        


More information about the llvm-commits mailing list