Skip to content

Commit b5dd1fe

Browse files
authored
VirtRegRewriter: Fix verifier errors after regalloc failures (#128280)
1 parent 98542a3 commit b5dd1fe

7 files changed

+27
-24
lines changed

llvm/lib/CodeGen/VirtRegMap.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ void VirtRegRewriter::rewrite() {
598598
SmallVector<Register, 8> SuperDefs;
599599
SmallVector<Register, 8> SuperKills;
600600

601+
const bool IsValidAlloc = !MF->getProperties().hasProperty(
602+
MachineFunctionProperties::Property::FailedRegAlloc);
603+
601604
for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
602605
MBBI != MBBE; ++MBBI) {
603606
LLVM_DEBUG(MBBI->print(dbgs(), Indexes));
@@ -617,9 +620,7 @@ void VirtRegRewriter::rewrite() {
617620
assert(Register(PhysReg).isPhysical());
618621

619622
RewriteRegs.insert(PhysReg);
620-
assert((!MRI->isReserved(PhysReg) ||
621-
MF->getProperties().hasProperty(
622-
MachineFunctionProperties::Property::FailedRegAlloc)) &&
623+
assert((!MRI->isReserved(PhysReg) || !IsValidAlloc) &&
623624
"Reserved register assignment");
624625

625626
// Preserve semantics of sub-register operands.
@@ -695,7 +696,14 @@ void VirtRegRewriter::rewrite() {
695696
// Rewrite. Note we could have used MachineOperand::substPhysReg(), but
696697
// we need the inlining here.
697698
MO.setReg(PhysReg);
698-
MO.setIsRenamable(true);
699+
700+
// Defend against generating invalid flags in allocation failure
701+
// scenarios. We have have assigned a register which was undefined, or a
702+
// reserved register which cannot be renamable.
703+
if (LLVM_LIKELY(IsValidAlloc))
704+
MO.setIsRenamable(true);
705+
else if (MO.isUse())
706+
MO.setIsUndef(true);
699707
}
700708

701709
// Add any missing super-register kills after rewriting the whole

llvm/test/CodeGen/AMDGPU/illegal-eviction-assert.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
...
1919

20-
# CHECK: S_NOP 0, implicit-def renamable $vgpr4_vgpr5_vgpr6_vgpr7_vgpr8_vgpr9_vgpr10_vgpr11_vgpr12_vgpr13_vgpr14_vgpr15_vgpr16_vgpr17_vgpr18_vgpr19, implicit-def renamable $vgpr20_vgpr21_vgpr22_vgpr23_vgpr24_vgpr25_vgpr26_vgpr27, implicit-def renamable $vgpr0_vgpr1_vgpr2_vgpr3, implicit-def renamable $vgpr28_vgpr29_vgpr30_vgpr31, implicit-def renamable $vgpr0_vgpr1_vgpr2_vgpr3
21-
# CHECK: S_NOP 0, implicit killed renamable $vgpr4_vgpr5_vgpr6_vgpr7_vgpr8_vgpr9_vgpr10_vgpr11_vgpr12_vgpr13_vgpr14_vgpr15_vgpr16_vgpr17_vgpr18_vgpr19, implicit killed renamable $vgpr20_vgpr21_vgpr22_vgpr23_vgpr24_vgpr25_vgpr26_vgpr27, implicit killed renamable $vgpr0_vgpr1_vgpr2_vgpr3, implicit killed renamable $vgpr28_vgpr29_vgpr30_vgpr31, implicit killed renamable $vgpr0_vgpr1_vgpr2_vgpr3
20+
# CHECK: S_NOP 0, implicit-def $vgpr4_vgpr5_vgpr6_vgpr7_vgpr8_vgpr9_vgpr10_vgpr11_vgpr12_vgpr13_vgpr14_vgpr15_vgpr16_vgpr17_vgpr18_vgpr19, implicit-def $vgpr20_vgpr21_vgpr22_vgpr23_vgpr24_vgpr25_vgpr26_vgpr27, implicit-def $vgpr0_vgpr1_vgpr2_vgpr3, implicit-def $vgpr28_vgpr29_vgpr30_vgpr31, implicit-def $vgpr0_vgpr1_vgpr2_vgpr3
21+
# CHECK: S_NOP 0, implicit killed undef $vgpr4_vgpr5_vgpr6_vgpr7_vgpr8_vgpr9_vgpr10_vgpr11_vgpr12_vgpr13_vgpr14_vgpr15_vgpr16_vgpr17_vgpr18_vgpr19, implicit killed undef $vgpr20_vgpr21_vgpr22_vgpr23_vgpr24_vgpr25_vgpr26_vgpr27, implicit killed undef $vgpr0_vgpr1_vgpr2_vgpr3, implicit killed undef $vgpr28_vgpr29_vgpr30_vgpr31, implicit killed undef $vgpr0_vgpr1_vgpr2_vgpr3
2222

2323
---
2424
name: foo

llvm/test/CodeGen/AMDGPU/issue48473.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# %25 to $sgpr60_sgpr61_sgpr62_sgpr63_sgpr64_sgpr65_sgpr66_sgpr67
4444

4545
# CHECK-LABEL: name: issue48473
46-
# CHECK: S_NOP 0, implicit killed renamable $sgpr0_sgpr1_sgpr2_sgpr3, implicit killed renamable $sgpr12_sgpr13_sgpr14_sgpr15, implicit killed renamable $sgpr16_sgpr17_sgpr18_sgpr19_sgpr20_sgpr21_sgpr22_sgpr23, implicit killed renamable $sgpr24_sgpr25_sgpr26_sgpr27_sgpr28_sgpr29_sgpr30_sgpr31, implicit killed renamable $sgpr84_sgpr85_sgpr86_sgpr87, implicit killed renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43, implicit killed renamable $sgpr4_sgpr5_sgpr6_sgpr7, implicit killed renamable $sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51, implicit killed renamable $sgpr88_sgpr89_sgpr90_sgpr91, implicit killed renamable $sgpr76_sgpr77_sgpr78_sgpr79_sgpr80_sgpr81_sgpr82_sgpr83, implicit killed renamable $sgpr0_sgpr1_sgpr2_sgpr3, implicit killed renamable $sgpr52_sgpr53_sgpr54_sgpr55_sgpr56_sgpr57_sgpr58_sgpr59, implicit killed renamable $sgpr92_sgpr93_sgpr94_sgpr95, implicit killed renamable $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75, implicit renamable $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75, implicit killed renamable $sgpr96_sgpr97_sgpr98_sgpr99, implicit killed renamable $sgpr8_sgpr9_sgpr10_sgpr11, implicit killed renamable $sgpr60_sgpr61_sgpr62_sgpr63_sgpr64_sgpr65_sgpr66_sgpr67
46+
# CHECK: S_NOP 0, implicit killed undef $sgpr0_sgpr1_sgpr2_sgpr3, implicit killed undef $sgpr12_sgpr13_sgpr14_sgpr15, implicit killed undef $sgpr16_sgpr17_sgpr18_sgpr19_sgpr20_sgpr21_sgpr22_sgpr23, implicit killed undef $sgpr24_sgpr25_sgpr26_sgpr27_sgpr28_sgpr29_sgpr30_sgpr31, implicit killed undef $sgpr84_sgpr85_sgpr86_sgpr87, implicit killed undef $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43, implicit killed undef $sgpr4_sgpr5_sgpr6_sgpr7, implicit killed undef $sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51, implicit killed undef $sgpr88_sgpr89_sgpr90_sgpr91, implicit killed undef $sgpr76_sgpr77_sgpr78_sgpr79_sgpr80_sgpr81_sgpr82_sgpr83, implicit killed undef $sgpr0_sgpr1_sgpr2_sgpr3, implicit killed undef $sgpr52_sgpr53_sgpr54_sgpr55_sgpr56_sgpr57_sgpr58_sgpr59, implicit killed undef $sgpr92_sgpr93_sgpr94_sgpr95, implicit killed undef $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75, implicit undef $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75, implicit killed undef $sgpr96_sgpr97_sgpr98_sgpr99, implicit killed undef $sgpr8_sgpr9_sgpr10_sgpr11, implicit killed undef $sgpr60_sgpr61_sgpr62_sgpr63_sgpr64_sgpr65_sgpr66_sgpr67
4747

4848
---
4949
name: issue48473

llvm/test/CodeGen/AMDGPU/ran-out-of-registers-error-all-regs-reserved.ll

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=greedy -verify-machineinstrs=0 -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
2-
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=basic -verify-machineinstrs=0 -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
3-
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=fast -verify-machineinstrs=0 -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
4-
5-
; FIXME: Should pass verifier after failure.
1+
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=greedy -verify-machineinstrs -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
2+
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=basic -verify-machineinstrs -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
3+
; TODO: Fix verifier error after fast
4+
; TODO: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -vgpr-regalloc=fast -verify-machineinstrs -filetype=null %s 2>&1 | FileCheck -implicit-check-not=error %s
65

76
declare <32 x i32> @llvm.amdgcn.mfma.i32.32x32x4i8(i32, i32, <32 x i32>, i32 immarg, i32 immarg, i32 immarg)
87

llvm/test/CodeGen/AMDGPU/regalloc-failure-overlapping-insert-assert.mir

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# RUN: not llc -mtriple=amdgcn -mcpu=gfx908 -verify-machineinstrs=0 -start-before=greedy,1 -stop-after=virtregrewriter,2 %s -o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
2-
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx908 -verify-machineinstrs -start-before=greedy,1 -stop-after=virtregrewriter,2 %s -o /dev/null 2>&1 | FileCheck -check-prefixes=ERR,VERIFIER %s
1+
# RUN: not llc -mtriple=amdgcn -mcpu=gfx908 -verify-machineinstrs -start-before=greedy,1 -stop-after=virtregrewriter,2 %s -o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
32

4-
# FIXME: We should not produce a verifier error after erroring
3+
# Make sure there's no machine verifier error after failure.
54

65
# ERR: error: inline assembly requires more registers than available
7-
# VERIFIER: *** Bad machine code: Using an undefined physical register ***
86

97
# This testcase cannot be compiled with the enforced register
108
# budget. Previously, tryLastChanceRecoloring would assert here. It

llvm/test/CodeGen/AMDGPU/remaining-virtual-register-operands.ll

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
; RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -verify-machineinstrs < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -verify-machineinstrs < %s 2>&1 | FileCheck %s
22

33
; This testcase fails register allocation at the same time it performs
44
; virtual register splitting (by introducing VGPR to AGPR copies). We
55
; still need to enqueue and allocate the newly split vregs after the
66
; failure.
77

8-
; The machine verifier complains about usage of register
9-
; which is marked as killed in previous instruction.
10-
; This happens due to when register allocator is out of registers
11-
; it takes the first avialable register.
8+
; The machine verifier should not complain about usage of register
9+
; which is marked as killed in previous instruction. This happens due
10+
; to when register allocator is out of registers it takes the first
11+
; avialable register.
1212

1313
; CHECK: error: <unknown>:0:0: ran out of registers during register allocation
14-
; CHECK: Bad machine code: Using an undefined physical register
1514
define amdgpu_kernel void @alloc_failure_with_split_vregs(float %v0, float %v1) #0 {
1615
%agpr0 = call float asm sideeffect "; def $0", "=${a0}"()
1716
%agpr.vec = insertelement <16 x float> undef, float %agpr0, i32 0

llvm/test/CodeGen/X86/inline-asm-assertion.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
; RUN: not llc -verify-machineinstrs -O0 < %s 2>&1 | FileCheck %s
2-
; RUN: not --crash llc -verify-machineinstrs -O2 < %s 2>&1 | FileCheck %s --check-prefix=CHECK-O2
2+
; RUN: not llc -verify-machineinstrs -O2 < %s 2>&1 | FileCheck %s
33
; CHECK: error: inline assembly requires more registers than available
44
; CHECK: .size main, .Lfunc_end0-main
5-
; CHECK-O2: error: inline assembly requires more registers than available
65

76
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
87
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)