[PATCH] D109775: [FuncSpec] Fix isConstant check for globals

Sjoerd Meijer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 16 05:54:12 PDT 2021


SjoerdMeijer updated this revision to Diff 372909.
SjoerdMeijer added a comment.

Added an option that is off by default to specialise on addresses. Investigating a cost model for this is for another day,  I don't want to do this at this point.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109775/new/

https://reviews.llvm.org/D109775

Files:
  llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
  llvm/test/Transforms/FunctionSpecialization/function-specialization-nonconst-glob.ll


Index: llvm/test/Transforms/FunctionSpecialization/function-specialization-nonconst-glob.ll
===================================================================
--- llvm/test/Transforms/FunctionSpecialization/function-specialization-nonconst-glob.ll
+++ llvm/test/Transforms/FunctionSpecialization/function-specialization-nonconst-glob.ll
@@ -1,8 +1,14 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+
 ; RUN: opt -function-specialization -force-function-specialization -S < %s | FileCheck %s
+; RUN: opt -function-specialization -force-function-specialization -func-specialization-on-address=0 -S < %s | FileCheck %s
+; RUN: opt -function-specialization -force-function-specialization -func-specialization-on-address=1 -S < %s | FileCheck %s --check-prefix=ON-ADDRESS
+
+; Global B is not constant. We do not specialise on addresses unless we
+; enable that:
 
-; Global B is not constant. We do specialise on this global, showing we
-; specialise on its address.
+; ON-ADDRESS: call i32 @foo.1(i32 %x, i32* @A)
+; ON-ADDRESS: call i32 @foo.2(i32 %y, i32* @B)
 
 target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
 
@@ -18,7 +24,7 @@
 ; CHECK-NEXT:    [[CALL:%.*]] = call i32 @foo.1(i32 [[X]], i32* @A)
 ; CHECK-NEXT:    br label [[RETURN:%.*]]
 ; CHECK:       if.else:
-; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @foo.2(i32 [[Y:%.*]], i32* @B)
+; CHECK-NEXT:    [[CALL1:%.*]] = call i32 @foo(i32 [[Y:%.*]], i32* @B)
 ; CHECK-NEXT:    br label [[RETURN]]
 ; CHECK:       return:
 ; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i32 [ [[CALL]], [[IF_THEN]] ], [ [[CALL1]], [[IF_ELSE]] ]
@@ -42,7 +48,7 @@
 }
 
 define internal i32 @foo(i32 %x, i32* %b) {
-; CHECK-LABEL: @foo(
+; CHECK-LABEL: define internal i32 @foo(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = load i32, i32* [[B:%.*]], align 4
 ; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[X:%.*]], [[TMP0]]
@@ -61,9 +67,4 @@
 ; CHECK-NEXT:    ret i32 %add
 ; CHECK-NEXT:  }
 
-; CHECK-LABEL: define internal i32 @foo.2(i32 %x, i32* %b) {
-; CHECK-NEXT:  entry:
-; CHECK-NEXT:    %0 = load i32, i32* @B, align 4
-; CHECK-NEXT:    %add = add nsw i32 %x, %0
-; CHECK-NEXT:    ret i32 %add
-; CHECK-NEXT:  }
+; CHECK-NOT: define internal i32 @foo.2(
Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -81,6 +81,10 @@
                           cl::desc("Average loop iteration count cost"),
                           cl::init(10));
 
+static cl::opt<bool> SpecializeOnAddresses(
+    "func-specialization-on-address", cl::init(false), cl::Hidden,
+    cl::desc("Enable function specialization on the address of global values"));
+
 // TODO: This needs checking to see the impact on compile-times, which is why
 // this is off by default for now.
 static cl::opt<bool> EnableSpecializationForLiteralConstant(
@@ -672,9 +676,16 @@
 
       auto *V = CS.getArgOperand(A->getArgNo());
       // TrackValueOfGlobalVariable only tracks scalar global variables.
-      if (auto *GV = dyn_cast<GlobalVariable>(V))
+      if (auto *GV = dyn_cast<GlobalVariable>(V)) {
+        // Check if we want to specialize on the address of non-constant
+        // global values.
+        if (!GV->isConstant())
+          if (!SpecializeOnAddresses)
+            return false;
+
         if (!GV->getValueType()->isSingleValueType())
           return false;
+      }
 
       if (isa<Constant>(V) && (Solver.getLatticeValueFor(V).isConstant() ||
                                EnableSpecializationForLiteralConstant))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109775.372909.patch
Type: text/x-patch
Size: 3696 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210916/63908800/attachment-0001.bin>


More information about the llvm-commits mailing list