Skip to content

Commit 1fa1a14

Browse files
committed
[BasicBlockSections] Always keep the entry block in the beginning of the function.
BasicBlockSections must enforce placing the entry block at the beginning of the function regardless of the basic block sections profile.
1 parent f3d534c commit 1fa1a14

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ struct MBBSectionID {
7474
MBBSectionID(SectionType T) : Type(T), Number(0) {}
7575
};
7676

77-
// This structure represents the information for a basic block.
77+
// This structure represents the information for a basic block pertaining to
78+
// the basic block sections profile.
7879
struct UniqueBBID {
7980
unsigned BaseID;
80-
// sections profile).
8181
unsigned CloneID;
8282
};
8383

llvm/lib/CodeGen/BasicBlockSections.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
318318
MF.setBBSectionsType(BBSectionsType);
319319
assignSections(MF, FuncClusterInfo);
320320

321-
// We make sure that the cluster including the entry basic block precedes all
322-
// other clusters.
323-
auto EntryBBSectionID = MF.front().getSectionID();
321+
const MachineBasicBlock &EntryBB = MF.front();
322+
auto EntryBBSectionID = EntryBB.getSectionID();
324323

325324
// Helper function for ordering BB sections as follows:
326325
// * Entry section (section including the entry block).
@@ -341,12 +340,17 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
341340
// contiguous and ordered accordingly. Furthermore, clusters are ordered in
342341
// increasing order of their section IDs, with the exception and the
343342
// cold section placed at the end of the function.
343+
// Also, we force the entry block of the function to be placed at the
344+
// beginning of the function, regardless of the requested order.
344345
auto Comparator = [&](const MachineBasicBlock &X,
345346
const MachineBasicBlock &Y) {
346347
auto XSectionID = X.getSectionID();
347348
auto YSectionID = Y.getSectionID();
348349
if (XSectionID != YSectionID)
349350
return MBBSectionOrder(XSectionID, YSectionID);
351+
// Make sure that the entry block is placed at the beginning.
352+
if (&X == &EntryBB || &Y == &EntryBB)
353+
return &X == &EntryBB;
350354
// If the two basic block are in the same section, the order is decided by
351355
// their position within the section.
352356
if (XSectionID.Type == MBBSectionID::SectionType::Default)

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
214214
Twine("duplicate basic block id found '") + BasicBlockIDStr +
215215
"'");
216216

217-
if (!BasicBlockID->BaseID && CurrentPosition)
218-
return createProfileParseError(
219-
"entry BB (0) does not begin a cluster.");
220-
221217
FI->second.ClusterInfo.emplace_back(BBClusterInfo{
222218
*std::move(BasicBlockID), CurrentCluster, CurrentPosition++});
223219
}
@@ -288,9 +284,6 @@ Error BasicBlockSectionsProfileReader::ReadV0Profile() {
288284
if (!FuncBBIDs.insert(BBID).second)
289285
return createProfileParseError(
290286
Twine("duplicate basic block id found '") + BBIDStr + "'");
291-
if (BBID == 0 && CurrentPosition)
292-
return createProfileParseError(
293-
"entry BB (0) does not begin a cluster");
294287

295288
FI->second.ClusterInfo.emplace_back(
296289
BBClusterInfo({{static_cast<unsigned>(BBID), 0},

llvm/test/CodeGen/X86/basic-block-sections-clusters-error.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
; RUN: echo '!!1' >> %t1
66
; RUN: not --crash llc < %s -O0 -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t1 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR1
77
; CHECK-ERROR1: LLVM ERROR: invalid profile {{.*}} at line 3: duplicate basic block id found '1'
8-
; RUN: echo '!dummy1' > %t2
9-
; RUN: echo '!!4 0' >> %t2
10-
; RUN: not --crash llc < %s -O0 -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t2 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR2
11-
; CHECK-ERROR2: LLVM ERROR: invalid profile {{.*}} at line 2: entry BB (0) does not begin a cluster
128
; RUN: echo '!dummy1' > %t3
139
; RUN: echo '!!-1' >> %t3
1410
; RUN: not --crash llc < %s -O0 -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t3 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR3
@@ -48,7 +44,7 @@
4844
; RUN: echo 'f dummy1' >> %t11
4945
; RUN: echo 'c 0 1.a' >> %t11
5046
; RUN: not --crash llc < %s -O0 -mtriple=x86_64 -function-sections -basic-block-sections=%t11 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR11
51-
; CHECK-ERROR11: LLVM ERROR: invalid profile {{.*}} at line 3: unable to parse clone id: 'a'
47+
; CHECK-ERROR11: LLVM ERROR: invalid profile {{.*}} at line 3: unable to parse clone id: 'a'
5248
; RUN: echo 'v1' > %t12
5349
; RUN: echo 'f dummy1' >> %t12
5450
; RUN: echo 'c 0 1' >> %t12
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; COM: Tests to verify that the entry basic block is always placed at the beginning of its section.
2+
; RUN: echo 'v1' > %t1
3+
; RUN: echo 'f foo' >> %t1
4+
; RUN: echo 'c2 0' >> %t1
5+
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t1 -O0 | FileCheck %s -check-prefix=LINUX-SECTIONS1
6+
7+
; RUN: echo 'v1' > %t2
8+
; RUN: echo 'f foo' >> %t2
9+
; RUN: echo 'c2' >> %t2
10+
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t2 -O0 | FileCheck %s -check-prefix=LINUX-SECTIONS2
11+
12+
13+
define void @foo(i1 %a, i1 %b) {
14+
b0:
15+
br i1 %a, label %b1, label %b2
16+
17+
b1: ; preds = %b0
18+
ret void
19+
20+
b2: ; preds = %b0
21+
ret void
22+
}
23+
24+
;; Check that %b0 is emitted at the beginning of the function.
25+
; LINUX-SECTIONS1: .section .text.foo,"ax",@progbits
26+
; LINUX-SECTIONS1: foo:
27+
; LINUX-SECTIONS1: # %bb.0: # %b0
28+
; LINUX-SECTIONS1: jne foo.cold
29+
; LINUX-SECTIONS1: # %bb.2: # %b2
30+
; LINUX-SECTIONS1: retq
31+
; LINUX-SECTIONS1: .section .text.split.foo,"ax",@progbits
32+
; LINUX-SECTIONS1: foo.cold: # %b1
33+
; LINUX-SECTIONS1: retq
34+
35+
; LINUX-SECTIONS2: .section .text.foo,"ax",@progbits
36+
; LINUX-SECTIONS2: foo:
37+
; LINUX-SECTIONS2: # %bb.0: # %b0
38+
; LINUX-SECTIONS2: je foo.__part.0
39+
; LINUX-SECTIONS2: # %bb.1: # %b1
40+
; LINUX-SECTIONS2: retq
41+
; LINUX-SECTIONS2: .section .text.foo,"ax",@progbits
42+
; LINUX-SECTIONS2: foo.__part.0: # %b2
43+
; LINUX-SECTIONS2: retq

0 commit comments

Comments
 (0)