Skip to content

Commit 8ec1161

Browse files
authored
[Codegen, BasicBlockSections] Avoid cloning blocks which have their machine block address taken. (#94296)
These blocks usually show up in the form of branches within inline assembly. Since it's hard to rewire them, we fully omit paths with such blocks from path cloning.
1 parent fde6a37 commit 8ec1161

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

llvm/lib/CodeGen/BasicBlockPathCloning.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ bool IsValidCloning(const MachineFunction &MF,
119119
return false;
120120
}
121121
}
122+
if (PathBB->isMachineBlockAddressTaken()) {
123+
// Avoid cloning blocks which have their address taken since we can't
124+
// rewire branches to those blocks as easily (e.g., branches within
125+
// inline assembly).
126+
WithColor::warning()
127+
<< "block #" << BBID
128+
<< " has its machine block address taken in function "
129+
<< MF.getName() << "\n";
130+
return false;
131+
}
122132
}
123133

124134
if (I != ClonePath.size() - 1 && !PathBB->empty() &&

llvm/test/CodeGen/X86/basic-block-sections-cloning-invalid.ll

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ declare void @effect(i32 zeroext)
1313
; RUN: echo 'v1' > %t2
1414
; RUN: echo 'f foo' >> %t2
1515
; RUN: echo 'p 0 2 3' >> %t2
16-
; RUN: echo 'p 0 1 3' >> %t2
17-
; RUN: echo 'c 0 1.1 3.2 2.1 3.1 1' >> %t2
16+
; RUN: echo 'p 0 1 2 3' >> %t2
17+
; RUN: echo 'c 0 1.1 2.2 3.2 2.1 3.1 1' >> %t2
1818
; RUN: llc < %s -mtriple=x86_64-pc-linux -O0 -function-sections -basic-block-sections=%t2 2> %t2.err | FileCheck %s --check-prefixes=PATH
1919
; RUN: FileCheck %s --check-prefixes=WARN1 < %t2.err
2020
; RUN: echo 'v1' > %t3
@@ -23,6 +23,14 @@ declare void @effect(i32 zeroext)
2323
; RUN: echo 'c 0 100.1 1' >> %t3
2424
; RUN: llc < %s -mtriple=x86_64-pc-linux -O0 -function-sections -basic-block-sections=%t3 2> %t3.err | FileCheck %s
2525
; RUN: FileCheck %s --check-prefixes=WARN2 < %t3.err
26+
; RUN: echo 'v1' > %t4
27+
; RUN: echo 'f foo' >> %t4
28+
; RUN: echo 'p 1 6' >> %t4
29+
; RUN: echo 'c 0 1 6.1' >> %t4
30+
; RUN: llc < %s -mtriple=x86_64-pc-linux -O0 -function-sections -basic-block-sections=%t4 2> %t4.err | FileCheck %s
31+
; RUN: FileCheck %s --check-prefixes=WARN3 < %t4.err
32+
33+
2634

2735
define void @foo(i1 %a, i1 %b, i1 %c, i1 %d) {
2836
b0:
@@ -31,23 +39,29 @@ b0:
3139

3240
b1: ; preds = %b0
3341
call void @effect(i32 1)
34-
br i1 %b, label %b2, label %b3
42+
br i1 %b, label %b2, label %b6
3543

3644
b2: ; preds = %b1
3745
call void @effect(i32 2)
3846
br label %b3
3947

40-
b3: ; preds = %b0, %b1, %b2
48+
b3: ; preds = %b0, %b2
4149
call void @effect(i32 3)
4250
br i1 %c, label %b4, label %b5
4351

4452
b4: ; preds = %b3
4553
call void @effect(i32 4)
46-
br i1 %d, label %b5, label %cold
54+
callbr void asm sideeffect "je ${0:l}", "!i,~{dirflag},~{fpsr},~{flags}"()
55+
to label %b5 [label %b6]
4756

4857
b5: ; preds = %b3, %b4
4958
call void @effect(i32 5)
5059
ret void
60+
61+
b6: ; preds = %b1, %b4
62+
call void @effect(i32 6)
63+
ret void
64+
5165
cold:
5266
call void @effect(i32 6) ; preds = %b4
5367
ret void
@@ -59,7 +73,7 @@ cold:
5973

6074
; CHECK: je .LBB0_3
6175
; PATH: # %bb.7: # %b1
62-
; PATH: # %bb.8: # %b3
76+
; PATH: # %bb.8: # %b2
6377
; PATH: jne .LBB0_4
6478
; CHECK: # %bb.1: # %b1
6579
; CHECK: jne foo.cold
@@ -69,4 +83,5 @@ cold:
6983
;; Check the warnings
7084
; WARN1: warning: block #2 is not a successor of block #0 in function foo
7185
; WARN2: warning: no block with id 100 in function foo
86+
; WARN3: warning: block #6 has its machine block address taken in function foo
7287

0 commit comments

Comments
 (0)