Skip to content

Commit 06664fd

Browse files
hazzlimlabrinea
andauthored
[FuncSpec] Enable SpecializeLiteralConstant by default (#113442)
Enable specialization on literal constant arguments by default in Function Specialization. --------- Co-authored-by: Alexandros Lamprineas <[email protected]>
1 parent 98c8d64 commit 06664fd

13 files changed

+182
-28
lines changed

llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
// - Perhaps a post-inlining function specialization pass could be more
6565
// aggressive on literal constants.
6666
//
67+
// Limitations:
68+
// ------------
69+
// - We are unable to consider specializations of functions called from indirect
70+
// callsites whose pointer operand has a lattice value that is known to be
71+
// constant, either from IPSCCP or previous iterations of FuncSpec. This is
72+
// because SCCP has not yet replaced the uses of the known constant.
73+
//
6774
// References:
6875
// -----------
6976
// 2021 LLVM Dev Mtg “Introducing function specialisation, and can we enable

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,11 @@ static cl::opt<bool> SpecializeOnAddress(
8484
"funcspec-on-address", cl::init(false), cl::Hidden, cl::desc(
8585
"Enable function specialization on the address of global values"));
8686

87-
// Disabled by default as it can significantly increase compilation times.
88-
//
89-
// https://llvm-compile-time-tracker.com
90-
// https://github.com/nikic/llvm-compile-time-tracker
9187
static cl::opt<bool> SpecializeLiteralConstant(
92-
"funcspec-for-literal-constant", cl::init(false), cl::Hidden, cl::desc(
93-
"Enable specialization of functions that take a literal constant as an "
94-
"argument"));
88+
"funcspec-for-literal-constant", cl::init(true), cl::Hidden,
89+
cl::desc(
90+
"Enable specialization of functions that take a literal constant as an "
91+
"argument"));
9592

9693
bool InstCostVisitor::canEliminateSuccessor(BasicBlock *BB, BasicBlock *Succ,
9794
DenseSet<BasicBlock *> &DeadBlocks) {
@@ -682,10 +679,11 @@ bool FunctionSpecializer::run() {
682679
(RequireMinSize && Metrics.NumInsts < MinFunctionSize))
683680
continue;
684681

685-
// TODO: For now only consider recursive functions when running multiple
686-
// times. This should change if specialization on literal constants gets
687-
// enabled.
688-
if (!Inserted && !Metrics.isRecursive && !SpecializeLiteralConstant)
682+
// When specialization on literal constants is disabled, only consider
683+
// recursive functions when running multiple times to save wasted analysis,
684+
// as we will not be able to specialize on any newly found literal constant
685+
// return values.
686+
if (!SpecializeLiteralConstant && !Inserted && !Metrics.isRecursive)
689687
continue;
690688

691689
int64_t Sz = *Metrics.NumInsts.getValue();

llvm/test/DebugInfo/Generic/ipsccp-remap-assign-id.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: opt -passes=ipsccp %s -S -o - | FileCheck %s
2-
; RUN: opt --try-experimental-debuginfo-iterators -passes=ipsccp %s -S -o - | FileCheck %s
1+
; RUN: opt -passes=ipsccp -funcspec-for-literal-constant=false %s -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes=ipsccp -funcspec-for-literal-constant=false %s -S -o - | FileCheck %s
33

44
;; Check the dbg.assign DIAssignID operand gets remapped after cloning.
55

llvm/test/Transforms/FunctionSpecialization/compiler-crash-58759.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S --passes="default<O3>" < %s | FileCheck %s
1+
; RUN: opt -S --passes="default<O3>" -funcspec-for-literal-constant=false < %s | FileCheck %s
22

33
define dso_local i32 @g0(i32 noundef %x) local_unnamed_addr {
44
entry:

llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
; Note that this test case shows that function specialization pass would
55
; transform the function even if no specialization happened.
66

7-
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization -S < %s | FileCheck %s
7+
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization -funcspec-for-literal-constant=false -S < %s | FileCheck %s
88

99
%struct = type { i8, i16, i32, i64, i64}
1010
@Global = internal constant %struct {i8 0, i16 1, i32 2, i64 3, i64 4}

llvm/test/Transforms/FunctionSpecialization/function-specialization2.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs --version 5
2-
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -force-specialization -S < %s | FileCheck %s
3-
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -funcspec-max-iters=1 -force-specialization -S < %s | FileCheck %s --check-prefix=ONE-ITER
4-
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -funcspec-max-iters=0 -force-specialization -S < %s | FileCheck %s --check-prefix=DISABLED
2+
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -funcspec-for-literal-constant=false -force-specialization -S < %s | FileCheck %s
3+
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -funcspec-for-literal-constant=false -funcspec-max-iters=1 -force-specialization -S < %s | FileCheck %s --check-prefix=ONE-ITER
4+
; RUN: opt -passes="ipsccp<func-spec>,deadargelim" -funcspec-for-literal-constant=false -funcspec-max-iters=0 -force-specialization -S < %s | FileCheck %s --check-prefix=DISABLED
55

66

77
define internal i32 @func(ptr %0, i32 %1, ptr nocapture %2) {

llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization \
2-
; RUN: -funcspec-max-clones=2 -S < %s | FileCheck %s
2+
; RUN: -funcspec-for-literal-constant=false -funcspec-max-clones=2 \
3+
; RUN: -S < %s | FileCheck %s
34

45
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization \
5-
; RUN: -funcspec-max-clones=1 -S < %s | FileCheck %s --check-prefix=CONST1
6+
; RUN: -funcspec-for-literal-constant=false -funcspec-max-clones=1 \
7+
; RUN: -S < %s | FileCheck %s --check-prefix=CONST1
68

79
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
810

llvm/test/Transforms/FunctionSpecialization/get-possible-constants.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S --passes="ipsccp<func-spec>" < %s | FileCheck %s
1+
; RUN: opt -S --passes="ipsccp<func-spec>" -funcspec-for-literal-constant=false < %s | FileCheck %s
22
define dso_local i32 @p0(i32 noundef %x) {
33
entry:
44
%add = add nsw i32 %x, 1

llvm/test/Transforms/FunctionSpecialization/global-rank.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S --passes="ipsccp<func-spec>" -funcspec-max-clones=1 < %s | FileCheck %s
1+
; RUN: opt -S --passes="ipsccp<func-spec>" -funcspec-for-literal-constant=false -funcspec-max-clones=1 < %s | FileCheck %s
22
define internal i32 @f(i32 noundef %x, ptr nocapture noundef readonly %p, ptr nocapture noundef readonly %q) noinline {
33
entry:
44
%call = tail call i32 %p(i32 noundef %x)

llvm/test/Transforms/FunctionSpecialization/identical-specializations.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs --version 5
2-
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization -S < %s | FileCheck %s
2+
; RUN: opt -passes="ipsccp<func-spec>" -force-specialization -funcspec-for-literal-constant=false -S < %s | FileCheck %s
33

44
define i64 @main(i64 %x, i64 %y, i1 %flag) {
55
entry:

0 commit comments

Comments
 (0)