Skip to content

Commit 95db31e

Browse files
authored
Treat specifying a function in the bbsection profile without any directive as noop. (#167359)
1 parent 8b1cc2d commit 95db31e

File tree

7 files changed

+45
-90
lines changed

7 files changed

+45
-90
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
!world
1+
v1
2+
f world
3+
c 0

clang/test/CodeGen/basic-block-sections.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ int another(int a) {
3030
//
3131
// BB_WORLD: .section .text.world,"ax",@progbits{{$}}
3232
// BB_WORLD: world:
33-
// BB_WORLD: .section .text.world,"ax",@progbits,unique
34-
// BB_WORLD: world.__part.1:
33+
// BB_ALL: .section .text.world,"ax",@progbits,unique
34+
// BB_ALL: world.__part.1:
35+
// BB_LIST: .section .text.split.world,"ax",@progbits
36+
// BB_LIST: world.cold:
3537
// BB_ALL: .section .text.another,"ax",@progbits
3638
// BB_ALL: another.__part.1:
3739
// BB_LIST-NOT: .section .text.another,"ax",@progbits

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,13 @@ class BasicBlockSectionsProfileReader {
6868

6969
BasicBlockSectionsProfileReader() = default;
7070

71-
// Returns true if basic block sections profile exist for function \p
72-
// FuncName.
71+
// Returns true if function \p FuncName is hot based on the basic block
72+
// section profile.
7373
bool isFunctionHot(StringRef FuncName) const;
7474

75-
// Returns a pair with first element representing whether basic block sections
76-
// profile exist for the function \p FuncName, and the second element
77-
// representing the basic block sections profile (cluster info) for this
78-
// function. If the first element is true and the second element is empty, it
79-
// means unique basic block sections are desired for all basic blocks of the
80-
// function.
81-
std::pair<bool, SmallVector<BBClusterInfo>>
75+
// Returns the cluster info for the function \p FuncName. Returns an empty
76+
// vector if function has no cluster info.
77+
SmallVector<BBClusterInfo>
8278
getClusterInfoForFunction(StringRef FuncName) const;
8379

8480
// Returns the path clonings for the given function.
@@ -190,7 +186,7 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
190186

191187
bool isFunctionHot(StringRef FuncName) const;
192188

193-
std::pair<bool, SmallVector<BBClusterInfo>>
189+
SmallVector<BBClusterInfo>
194190
getClusterInfoForFunction(StringRef FuncName) const;
195191

196192
SmallVector<SmallVector<unsigned>>

llvm/lib/CodeGen/BasicBlockSections.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ updateBranches(MachineFunction &MF,
183183
// clusters are ordered in increasing order of their IDs, with the "Exception"
184184
// and "Cold" succeeding all other clusters.
185185
// FuncClusterInfo represents the cluster information for basic blocks. It
186-
// maps from BBID of basic blocks to their cluster information. If this is
187-
// empty, it means unique sections for all basic blocks in the function.
186+
// maps from BBID of basic blocks to their cluster information.
188187
static void
189188
assignSections(MachineFunction &MF,
190189
const DenseMap<UniqueBBID, BBClusterInfo> &FuncClusterInfo) {
@@ -197,10 +196,8 @@ assignSections(MachineFunction &MF,
197196
for (auto &MBB : MF) {
198197
// With the 'all' option, every basic block is placed in a unique section.
199198
// With the 'list' option, every basic block is placed in a section
200-
// associated with its cluster, unless we want individual unique sections
201-
// for every basic block in this function (if FuncClusterInfo is empty).
202-
if (MF.getTarget().getBBSectionsType() == llvm::BasicBlockSection::All ||
203-
FuncClusterInfo.empty()) {
199+
// associated with its cluster.
200+
if (MF.getTarget().getBBSectionsType() == llvm::BasicBlockSection::All) {
204201
// If unique sections are desired for all basic blocks of the function, we
205202
// set every basic block's section ID equal to its original position in
206203
// the layout (which is equal to its number). This ensures that basic
@@ -308,22 +305,22 @@ bool BasicBlockSections::handleBBSections(MachineFunction &MF) {
308305
if (BBSectionsType == BasicBlockSection::List &&
309306
hasInstrProfHashMismatch(MF))
310307
return false;
311-
// Renumber blocks before sorting them. This is useful for accessing the
312-
// original layout positions and finding the original fallthroughs.
313-
MF.RenumberBlocks();
314308

315309
DenseMap<UniqueBBID, BBClusterInfo> FuncClusterInfo;
316310
if (BBSectionsType == BasicBlockSection::List) {
317-
auto [HasProfile, ClusterInfo] =
318-
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
319-
.getClusterInfoForFunction(MF.getName());
320-
if (!HasProfile)
311+
auto ClusterInfo = getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
312+
.getClusterInfoForFunction(MF.getName());
313+
if (ClusterInfo.empty())
321314
return false;
322315
for (auto &BBClusterInfo : ClusterInfo) {
323316
FuncClusterInfo.try_emplace(BBClusterInfo.BBID, BBClusterInfo);
324317
}
325318
}
326319

320+
// Renumber blocks before sorting them. This is useful for accessing the
321+
// original layout positions and finding the original fallthroughs.
322+
MF.RenumberBlocks();
323+
327324
MF.setBBSectionsType(BBSectionsType);
328325
assignSections(MF, FuncClusterInfo);
329326

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,24 @@ BasicBlockSectionsProfileReader::parseUniqueBBID(StringRef S) const {
5858
}
5959

6060
bool BasicBlockSectionsProfileReader::isFunctionHot(StringRef FuncName) const {
61-
return getClusterInfoForFunction(FuncName).first;
61+
return !getClusterInfoForFunction(FuncName).empty();
6262
}
6363

64-
std::pair<bool, SmallVector<BBClusterInfo>>
64+
SmallVector<BBClusterInfo>
6565
BasicBlockSectionsProfileReader::getClusterInfoForFunction(
6666
StringRef FuncName) const {
6767
auto R = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
68-
return R != ProgramPathAndClusterInfo.end()
69-
? std::pair(true, R->second.ClusterInfo)
70-
: std::pair(false, SmallVector<BBClusterInfo>());
68+
return R != ProgramPathAndClusterInfo.end() ? R->second.ClusterInfo
69+
: SmallVector<BBClusterInfo>();
7170
}
7271

7372
SmallVector<SmallVector<unsigned>>
7473
BasicBlockSectionsProfileReader::getClonePathsForFunction(
7574
StringRef FuncName) const {
76-
return ProgramPathAndClusterInfo.lookup(getAliasName(FuncName)).ClonePaths;
75+
auto R = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
76+
return R != ProgramPathAndClusterInfo.end()
77+
? R->second.ClonePaths
78+
: SmallVector<SmallVector<unsigned>>();
7779
}
7880

7981
uint64_t BasicBlockSectionsProfileReader::getEdgeCount(
@@ -494,7 +496,7 @@ bool BasicBlockSectionsProfileReaderWrapperPass::isFunctionHot(
494496
return BBSPR.isFunctionHot(FuncName);
495497
}
496498

497-
std::pair<bool, SmallVector<BBClusterInfo>>
499+
SmallVector<BBClusterInfo>
498500
BasicBlockSectionsProfileReaderWrapperPass::getClusterInfoForFunction(
499501
StringRef FuncName) const {
500502
return BBSPR.getClusterInfoForFunction(FuncName);
Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
;; Check the basic block sections list option.
2-
;; version 0 profile:
3-
; RUN: echo '!_Z3foob' > %t1
1+
;; Check that specifying the function in the basic block sections profile
2+
;; without any other directives is a noop.
43
;;
5-
;; version 1 profile:
6-
; RUN: echo 'v1' > %t2
7-
; RUN: echo 'f _Z3foob' >> %t2
4+
;; Specify the bb sections profile:
5+
; RUN: echo 'v1' > %t
6+
; RUN: echo 'f _Z3foob' >> %t
87
;;
9-
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t1 -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-FUNCTION-SECTION
10-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t1 -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-NO-FUNCTION-SECTION
11-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t1 -unique-basic-block-section-names --bbsections-guided-section-prefix=false | FileCheck %s -check-prefix=LINUX-SECTIONS-NO-GUIDED-PREFIX
12-
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t2 -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-FUNCTION-SECTION
13-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t2 -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-NO-FUNCTION-SECTION
14-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t2 -unique-basic-block-section-names --bbsections-guided-section-prefix=false | FileCheck %s -check-prefix=LINUX-SECTIONS-NO-GUIDED-PREFIX
8+
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t > %bbsections
9+
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections > %orig
10+
; RUN: diff -u %orig %bbsections
1511

1612
define i32 @_Z3foob(i1 zeroext %0) nounwind {
1713
%2 = alloca i32, align 4
@@ -41,45 +37,3 @@ define i32 @_Z3foob(i1 zeroext %0) nounwind {
4137

4238
declare i32 @_Z3barv() #1
4339
declare i32 @_Z3bazv() #1
44-
45-
define i32 @_Z3zipb(i1 zeroext %0) nounwind {
46-
%2 = alloca i32, align 4
47-
%3 = alloca i8, align 1
48-
%4 = zext i1 %0 to i8
49-
store i8 %4, ptr %3, align 1
50-
%5 = load i8, ptr %3, align 1
51-
%6 = trunc i8 %5 to i1
52-
%7 = zext i1 %6 to i32
53-
%8 = icmp sgt i32 %7, 0
54-
br i1 %8, label %9, label %11
55-
56-
9: ; preds = %1
57-
%10 = call i32 @_Z3barv()
58-
store i32 %10, ptr %2, align 4
59-
br label %13
60-
61-
11: ; preds = %1
62-
%12 = call i32 @_Z3bazv()
63-
store i32 %12, ptr %2, align 4
64-
br label %13
65-
66-
13: ; preds = %11, %9
67-
%14 = load i32, ptr %2, align 4
68-
ret i32 %14
69-
}
70-
71-
; LINUX-SECTIONS-NO-GUIDED-PREFIX: .section .text._Z3foob,"ax",@progbits
72-
; LINUX-SECTIONS: .section .text.hot._Z3foob,"ax",@progbits
73-
; LINUX-SECTIONS: _Z3foob:
74-
; LINUX-SECTIONS: .section .text.hot._Z3foob._Z3foob.__part.1,"ax",@progbits
75-
; LINUX-SECTIONS: _Z3foob.__part.1:
76-
; LINUX-SECTIONS: .section .text.hot._Z3foob._Z3foob.__part.2,"ax",@progbits
77-
; LINUX-SECTIONS: _Z3foob.__part.2:
78-
; LINUX-SECTIONS: .section .text.hot._Z3foob._Z3foob.__part.3,"ax",@progbits
79-
; LINUX-SECTIONS: _Z3foob.__part.3:
80-
81-
; LINUX-SECTIONS-FUNCTION-SECTION: .section .text._Z3zipb,"ax",@progbits
82-
; LINUX-SECTIONS-NO-FUNCTION-SECTION-NOT: .section .text{{.*}}._Z3zipb,"ax",@progbits
83-
; LINUX-SECTIONS: _Z3zipb:
84-
; LINUX-SECTIONS-NOT: .section .text{{.*}}._Z3zipb.__part.{{[0-9]+}},"ax",@progbits
85-
; LINUX-SECTIONS-NOT: _Z3zipb.__part.{{[0-9]+}}:

llvm/test/CodeGen/X86/basic-block-sections-source-drift.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
; RUN: echo "!foo" > %t.order.txt
2-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t.order.txt | FileCheck --check-prefix=SOURCE-DRIFT %s
3-
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t.order.txt -bbsections-detect-source-drift=false | FileCheck --check-prefix=HASH-CHECK-DISABLED %s
1+
; RUN: echo "v1" > %t
2+
; RUN: echo "f foo" >> %t
3+
; RUN: echo "c 0" >> %t
4+
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t | FileCheck --check-prefix=SOURCE-DRIFT %s
5+
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t -bbsections-detect-source-drift=false | FileCheck --check-prefix=HASH-CHECK-DISABLED %s
46

57
define dso_local i32 @foo(i1 zeroext %0, i1 zeroext %1) !annotation !1 {
68
br i1 %0, label %5, label %3

0 commit comments

Comments
 (0)