Skip to content

Commit 585f612

Browse files
quic-akaryakiLukacma
authored andcommitted
[Hexagon] Incorrect MIR after "hexinsert" pass (llvm#164021)
The "hexinsert" pass tries to replace bitfield operations with Hexagon `insert` instructions. To limit memory usage, there is a limit on the internal table size. When the limit is reached, the state is not correctly cleaned up so defs from from new `insert` instructions are not deleted after processing the basic block. Later, these defs can be incorrectly used in other basic blocks even they are not reachable. Then compiler will crash with: *** Bad machine code: Virtual register defs don't dominate all uses. *** Fixes: llvm#163774
1 parent 3dfe1d8 commit 585f612

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

llvm/lib/Target/Hexagon/HexagonGenInsert.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ void HexagonGenInsert::collectInBlock(MachineBasicBlock *B,
920920
// successors have been processed.
921921
RegisterSet BlockDefs, InsDefs;
922922
for (MachineInstr &MI : *B) {
923+
// Stop if the map size is too large.
924+
if (IFMap.size() >= MaxIFMSize)
925+
break;
926+
923927
InsDefs.clear();
924928
getInstrDefs(&MI, InsDefs);
925929
// Leave those alone. They are more transparent than "insert".
@@ -942,8 +946,8 @@ void HexagonGenInsert::collectInBlock(MachineBasicBlock *B,
942946

943947
findRecordInsertForms(VR, AVs);
944948
// Stop if the map size is too large.
945-
if (IFMap.size() > MaxIFMSize)
946-
return;
949+
if (IFMap.size() >= MaxIFMSize)
950+
break;
947951
}
948952
}
949953

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; Check that llc does not abort, which happened due to incorrect MIR.
2+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=1 < %s
3+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=2 < %s
4+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=3 < %s
5+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=4 < %s
6+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=5 < %s
7+
8+
; Look for this symptom, in case llc does not check invalid IR.
9+
; CHECK-NOT: insert(%14,%5,#5,#5)
10+
11+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=1 -debug-only=hexinsert -stop-after hexinsert < %s 2>&1 | FileCheck %s
12+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=2 -debug-only=hexinsert -stop-after hexinsert < %s 2>&1 | FileCheck %s
13+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=3 -debug-only=hexinsert -stop-after hexinsert < %s 2>&1 | FileCheck %s
14+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=4 -debug-only=hexinsert -stop-after hexinsert < %s 2>&1 | FileCheck %s
15+
; RUN: llc -O2 -mtriple=hexagon -insert-max-ifmap=5 -debug-only=hexinsert -stop-after hexinsert < %s 2>&1 | FileCheck %s
16+
17+
define i32 @f(i32 %0, i32 %1, i32 %2) {
18+
entry:
19+
switch i32 %0, label %common.ret1 [
20+
i32 8907, label %3
21+
i32 4115, label %6
22+
]
23+
24+
common.ret1:
25+
%common.ret1.op = phi i32 [ %5, %3 ], [ %526, %6 ], [ 0, %entry ]
26+
ret i32 %common.ret1.op
27+
28+
3:
29+
%4 = shl i32 %2, 5
30+
%5 = and i32 %4, 992
31+
br label %common.ret1
32+
33+
6:
34+
%7 = shl i32 %0, 10
35+
%8 = and i32 %7, 7168
36+
%9 = shl i32 %0, 5
37+
%10 = and i32 %9, 992
38+
%11 = or i32 %10, %8
39+
%12 = and i32 %0, 1
40+
%13 = or i32 %11, %12
41+
%14 = shl i32 %1, 1
42+
%15 = and i32 %14, 2031616
43+
%526 = or i32 %13, %15
44+
br label %common.ret1
45+
}

0 commit comments

Comments
 (0)