-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PGO] Add ability to mark cold functions as optsize/minsize/optnone (#…
…69030) The performance of cold functions shouldn't matter too much, so if we care about binary sizes, add an option to mark cold functions as optsize/minsize for binary size, or optnone for compile times [1]. Clang patch will be in a future patch. This is intended to replace `shouldOptimizeForSize(Function&, ...)`. We've seen multiple cases where calls to this expensive function, if not careful, can blow up compile times. I will clean up users of that function in a followup patch. Initial version: https://reviews.llvm.org/D149800 [1] https://discourse.llvm.org/t/rfc-new-feature-proposal-de-optimizing-cold-functions-using-pgo-info/56388
- Loading branch information
Showing
13 changed files
with
253 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
llvm/include/llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===- PGOForceFunctionAttrs.h - --------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOFORCEFUNCTIONATTRS_H | ||
#define LLVM_TRANSFORMS_INSTRUMENTATION_PGOFORCEFUNCTIONATTRS_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Support/PGOOptions.h" | ||
|
||
namespace llvm { | ||
|
||
struct PGOForceFunctionAttrsPass | ||
: public PassInfoMixin<PGOForceFunctionAttrsPass> { | ||
PGOForceFunctionAttrsPass(PGOOptions::ColdFuncOpt ColdType) | ||
: ColdType(ColdType) {} | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); | ||
|
||
private: | ||
PGOOptions::ColdFuncOpt ColdType; | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOFORCEFUNCTIONATTRS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
llvm/lib/Transforms/Instrumentation/PGOForceFunctionAttrs.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h" | ||
#include "llvm/Analysis/BlockFrequencyInfo.h" | ||
#include "llvm/Analysis/ProfileSummaryInfo.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
using namespace llvm; | ||
|
||
static bool shouldRunOnFunction(Function &F, ProfileSummaryInfo &PSI, | ||
FunctionAnalysisManager &FAM) { | ||
if (F.isDeclaration()) | ||
return false; | ||
// Respect existing attributes. | ||
if (F.hasOptNone() || F.hasOptSize() || F.hasMinSize()) | ||
return false; | ||
if (F.hasFnAttribute(Attribute::Cold)) | ||
return true; | ||
if (!PSI.hasProfileSummary()) | ||
return false; | ||
BlockFrequencyInfo &BFI = FAM.getResult<BlockFrequencyAnalysis>(F); | ||
return PSI.isFunctionColdInCallGraph(&F, BFI); | ||
} | ||
|
||
PreservedAnalyses PGOForceFunctionAttrsPass::run(Module &M, | ||
ModuleAnalysisManager &AM) { | ||
if (ColdType == PGOOptions::ColdFuncOpt::Default) | ||
return PreservedAnalyses::all(); | ||
ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); | ||
FunctionAnalysisManager &FAM = | ||
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); | ||
bool MadeChange = false; | ||
for (Function &F : M) { | ||
if (!shouldRunOnFunction(F, PSI, FAM)) | ||
continue; | ||
MadeChange = true; | ||
switch (ColdType) { | ||
case PGOOptions::ColdFuncOpt::Default: | ||
llvm_unreachable("bailed out for default above"); | ||
break; | ||
case PGOOptions::ColdFuncOpt::OptSize: | ||
F.addFnAttr(Attribute::OptimizeForSize); | ||
break; | ||
case PGOOptions::ColdFuncOpt::MinSize: | ||
F.addFnAttr(Attribute::MinSize); | ||
break; | ||
case PGOOptions::ColdFuncOpt::OptNone: | ||
F.addFnAttr(Attribute::OptimizeNone); | ||
F.addFnAttr(Attribute::NoInline); | ||
break; | ||
} | ||
} | ||
return MadeChange ? PreservedAnalyses::none() : PreservedAnalyses::all(); | ||
} |
102 changes: 102 additions & 0 deletions
102
llvm/test/Instrumentation/PGOForceFunctionAttrs/basic.ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
; RUN: opt < %s -passes=pgo-force-function-attrs -pgo-kind=pgo-instr-use-pipeline -S -pgo-cold-func-opt=default | FileCheck %s --check-prefixes=NONE,CHECK | ||
; RUN: opt < %s -passes=pgo-force-function-attrs -pgo-kind=pgo-instr-use-pipeline -S -pgo-cold-func-opt=optsize | FileCheck %s --check-prefixes=OPTSIZE,CHECK | ||
; RUN: opt < %s -passes=pgo-force-function-attrs -pgo-kind=pgo-instr-use-pipeline -S -pgo-cold-func-opt=minsize | FileCheck %s --check-prefixes=MINSIZE,CHECK | ||
; RUN: opt < %s -passes=pgo-force-function-attrs -pgo-kind=pgo-instr-use-pipeline -S -pgo-cold-func-opt=optnone | FileCheck %s --check-prefixes=OPTNONE,CHECK | ||
|
||
; Should be no changes without profile data | ||
; RUN: opt < %s -passes=pgo-force-function-attrs -S -pgo-cold-func-opt=minsize | FileCheck %s --check-prefixes=NONE,CHECK | ||
|
||
; NONE-NOT: Function Attrs: | ||
; OPTSIZE: Function Attrs: optsize{{$}} | ||
; MINSIZE: Function Attrs: minsize{{$}} | ||
; OPTNONE: Function Attrs: noinline optnone{{$}} | ||
; CHECK: define void @cold() | ||
|
||
; CHECK: Function Attrs: optsize{{$}} | ||
; CHECK-NEXT: define void @cold_optsize() | ||
|
||
; CHECK: Function Attrs: minsize{{$}} | ||
; CHECK-NEXT: define void @cold_minsize() | ||
|
||
; CHECK: Function Attrs: noinline optnone{{$}} | ||
; CHECK-NEXT: define void @cold_optnone() | ||
|
||
; NONE: Function Attrs: cold{{$}} | ||
; OPTSIZE: Function Attrs: cold optsize{{$}} | ||
; MINSIZE: Function Attrs: cold minsize{{$}} | ||
; OPTNONE: Function Attrs: cold noinline optnone{{$}} | ||
; CHECK-NEXT: define void @cold_attr() | ||
|
||
; CHECK-NOT: Function Attrs: {{.*}}optsize | ||
; CHECK-NOT: Function Attrs: {{.*}}minsize | ||
; CHECK-NOT: Function Attrs: {{.*}}optnone | ||
|
||
@s = global i32 0 | ||
|
||
define void @cold() !prof !27 { | ||
store i32 1, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
define void @cold_optsize() optsize !prof !27 { | ||
store i32 1, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
define void @cold_minsize() minsize !prof !27 { | ||
store i32 1, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
define void @cold_optnone() noinline optnone !prof !27 { | ||
store i32 1, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
define void @cold_attr() cold { | ||
store i32 1, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
define void @hot() !prof !28 { | ||
%l = load i32, ptr @s, align 4 | ||
%add = add nsw i32 %l, 4 | ||
store i32 %add, ptr @s, align 4 | ||
ret void | ||
} | ||
|
||
attributes #0 = { optsize } | ||
attributes #1 = { minsize } | ||
attributes #2 = { noinline optnone } | ||
|
||
!llvm.module.flags = !{!0} | ||
|
||
!0 = !{i32 1, !"ProfileSummary", !1} | ||
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9} | ||
!2 = !{!"ProfileFormat", !"InstrProf"} | ||
!3 = !{!"TotalCount", i64 9040} | ||
!4 = !{!"MaxCount", i64 9000} | ||
!5 = !{!"MaxInternalCount", i64 0} | ||
!6 = !{!"MaxFunctionCount", i64 9000} | ||
!7 = !{!"NumCounts", i64 5} | ||
!8 = !{!"NumFunctions", i64 5} | ||
!9 = !{!"DetailedSummary", !10} | ||
!10 = !{!11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26} | ||
!11 = !{i32 10000, i64 9000, i32 1} | ||
!12 = !{i32 100000, i64 9000, i32 1} | ||
!13 = !{i32 200000, i64 9000, i32 1} | ||
!14 = !{i32 300000, i64 9000, i32 1} | ||
!15 = !{i32 400000, i64 9000, i32 1} | ||
!16 = !{i32 500000, i64 9000, i32 1} | ||
!17 = !{i32 600000, i64 9000, i32 1} | ||
!18 = !{i32 700000, i64 9000, i32 1} | ||
!19 = !{i32 800000, i64 9000, i32 1} | ||
!20 = !{i32 900000, i64 9000, i32 1} | ||
!21 = !{i32 950000, i64 9000, i32 1} | ||
!22 = !{i32 990000, i64 9000, i32 1} | ||
!23 = !{i32 999000, i64 10, i32 5} | ||
!24 = !{i32 999900, i64 10, i32 5} | ||
!25 = !{i32 999990, i64 10, i32 5} | ||
!26 = !{i32 999999, i64 10, i32 5} | ||
!27 = !{!"function_entry_count", i64 10} | ||
!28 = !{!"function_entry_count", i64 9000} |
Oops, something went wrong.