Skip to content

Commit 5c57233

Browse files
committed
Address review comments
- Rename local variable (s/CodeSizeCost/SpecSize) - Rename struct member (s/CodeSizeCost/CodeSize) and update comment - Remove early exit from specialization creation loop - Remove redundant prefix from regression test name - Remove regression test for exceeding FuncGrowth
1 parent d289ffa commit 5c57233

File tree

4 files changed

+56
-112
lines changed

4 files changed

+56
-112
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,16 @@ struct Spec {
131131
// Profitability of the specialization.
132132
unsigned Score;
133133

134-
// Cost of the specialization, in terms of codesize.
135-
unsigned CodeSizeCost;
134+
// Number of instructions in the specialization.
135+
unsigned CodeSize;
136136

137137
// List of call sites, matching this specialization.
138138
SmallVector<CallBase *> CallSites;
139139

140-
Spec(Function *F, const SpecSig &S, unsigned Score, unsigned CodeSizeCost)
141-
: F(F), Sig(S), Score(Score), CodeSizeCost(CodeSizeCost) {}
142-
Spec(Function *F, const SpecSig &&S, unsigned Score, unsigned CodeSizeCost)
143-
: F(F), Sig(S), Score(Score), CodeSizeCost(CodeSizeCost) {}
140+
Spec(Function *F, const SpecSig &S, unsigned Score, unsigned CodeSize)
141+
: F(F), Sig(S), Score(Score), CodeSize(CodeSize) {}
142+
Spec(Function *F, const SpecSig &&S, unsigned Score, unsigned CodeSize)
143+
: F(F), Sig(S), Score(Score), CodeSize(CodeSize) {}
144144
};
145145

146146
class InstCostVisitor : public InstVisitor<InstCostVisitor, Constant *> {

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,9 @@ bool FunctionSpecializer::run() {
772772
for (unsigned I = 0; I < NSpecs; ++I) {
773773
Spec &S = AllSpecs[BestSpecs[I]];
774774

775-
// Check that creating this specialization doesn't exceed the maximum
776-
// codesize growth.
777-
unsigned FuncSize = getCostValue(FunctionMetrics[S.F].NumInsts);
778-
if ((FunctionGrowth[S.F] + S.CodeSizeCost) / FuncSize > MaxCodeSizeGrowth)
779-
continue;
780-
FunctionGrowth[S.F] += S.CodeSizeCost;
775+
// Accumulate the codesize growth for the function, now we are creating the
776+
// specialization.
777+
FunctionGrowth[S.F] += S.CodeSize;
781778

782779
S.Clone = createSpecialization(S.F, S.Sig);
783780

@@ -933,7 +930,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
933930
CodeSize += Visitor.getCodeSizeSavingsFromPendingPHIs();
934931

935932
unsigned CodeSizeSavings = getCostValue(CodeSize);
936-
unsigned CodeSizeCost = FuncSize - CodeSizeSavings;
933+
unsigned SpecSize = FuncSize - CodeSizeSavings;
937934

938935
auto IsProfitable = [&]() -> bool {
939936
// No check required.
@@ -970,7 +967,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
970967
if (LatencySavings < MinLatencySavings * FuncSize / 100)
971968
return false;
972969
// Maximum codesize growth.
973-
if ((FunctionGrowth[F] + CodeSizeCost) / FuncSize > MaxCodeSizeGrowth)
970+
if ((FunctionGrowth[F] + SpecSize) / FuncSize > MaxCodeSizeGrowth)
974971
return false;
975972

976973
Score += std::max(CodeSizeSavings, LatencySavings);
@@ -982,7 +979,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
982979
continue;
983980

984981
// Create a new specialisation entry.
985-
auto &Spec = AllSpecs.emplace_back(F, S, Score, CodeSizeCost);
982+
auto &Spec = AllSpecs.emplace_back(F, S, Score, SpecSize);
986983
if (CS.getFunction() != F)
987984
Spec.CallSites.push_back(&CS);
988985
const unsigned Index = AllSpecs.size() - 1;

llvm/test/Transforms/FunctionSpecialization/function-specialization-maxgrowth.ll

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; 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>" -funcspec-min-function-size=1 \
3+
; RUN: -funcspec-for-literal-constant=true \
4+
; RUN: -funcspec-min-codesize-savings=50 \
5+
; RUN: -funcspec-min-latency-savings=50 \
6+
; RUN: -funcspec-max-codesize-growth=1 \
7+
; RUN: -S < %s | FileCheck %s
8+
9+
; Verify that we are able to specialize a function successfully after analysis
10+
; of other specializations that are found to not be profitable.
11+
define void @test_specialize_after_failed_analysis(i32 %n) {
12+
entry:
13+
%notspec0 = call i32 @add(i32 0, i32 %n)
14+
%notspec1 = call i32 @add(i32 1, i32 %n)
15+
%spec = call i32 @add(i32 1, i32 1)
16+
ret void
17+
}
18+
19+
define i32 @add(i32 %x, i32 %y) {
20+
entry:
21+
%res = add i32 %x, %y
22+
ret i32 %res
23+
}
24+
; CHECK-LABEL: define void @test_specialize_after_failed_analysis(
25+
; CHECK-SAME: i32 [[N:%.*]]) {
26+
; CHECK-NEXT: [[ENTRY:.*:]]
27+
; CHECK-NEXT: [[NOTSPEC0:%.*]] = call i32 @add(i32 0, i32 [[N]])
28+
; CHECK-NEXT: [[NOTSPEC1:%.*]] = call i32 @add(i32 1, i32 [[N]])
29+
; CHECK-NEXT: [[SPEC:%.*]] = call i32 @add.specialized.1(i32 1, i32 1)
30+
; CHECK-NEXT: ret void
31+
;
32+
;
33+
; CHECK-LABEL: define i32 @add(
34+
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
35+
; CHECK-NEXT: [[ENTRY:.*:]]
36+
; CHECK-NEXT: [[RES:%.*]] = add i32 [[X]], [[Y]]
37+
; CHECK-NEXT: ret i32 [[RES]]
38+
;
39+
;
40+
; CHECK-LABEL: define internal i32 @add.specialized.1(
41+
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
42+
; CHECK-NEXT: [[ENTRY:.*:]]
43+
; CHECK-NEXT: ret i32 poison
44+
;

0 commit comments

Comments
 (0)