Skip to content

Commit 3830821

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 651a49c commit 3830821

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-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: 5 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).
@@ -347,6 +346,9 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
347346
auto YSectionID = Y.getSectionID();
348347
if (XSectionID != YSectionID)
349348
return MBBSectionOrder(XSectionID, YSectionID);
349+
// Make sure that the entry block is placed at the beginning.
350+
if (&X == &EntryBB || &Y == &EntryBB)
351+
return &X == &EntryBB;
350352
// If the two basic block are in the same section, the order is decided by
351353
// their position within the section.
352354
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
@@ -213,10 +213,6 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
213213
Twine("duplicate basic block id found '") + BasicBlockIDStr +
214214
"'");
215215

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

294287
FI->second.ClusterInfo.emplace_back(
295288
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)