-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PGO] Add ability to mark cold functions as optsize/minsize/optnone #69030
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e52a811
[PGO] Add ability to mark cold functions as optsize/minsize/optnone
aeubanks e1ae305
rename a bunch of stuff
aeubanks 7b885fd
run pass in CSFDO use pipeline
aeubanks 02e6603
small fixes
aeubanks 33022e8
always run pass, also apply attrs to cold functions
aeubanks c3a1e7a
Merge remote-tracking branch 'upstream/main' into cold
aeubanks 38b397d
respect existing attribute
aeubanks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if functions originally has
AlwaysInline
attribute?More generally, what if function originally has a conflicting attribute from what's being set here?