Skip to content

Commit 1dbb932

Browse files
authored
GlobalISel: Relax verifier between physreg and typed vreg (llvm#159281)
Accept mismatched register size and type size if the type is legal for the register class. For AMDGPU boolean registers have 2 possible interpretations depending on the use context type. e.g., these are both equally valid: %0:_(s1) = COPY $vcc %1:_(s64) = COPY $vcc vcc is a 64-bit register, which can be interpreted as a 1-bit or 64-bit value depending on the use context. SelectionDAG has never required exact match between the register size and the used value type. You can assign a type with a smaller size to a larger register class. Relax the verifier to match. There are several hacks holding together these copies in various places, and this is preparation to remove one of them. The x86 test change is from what I would consider an X86 usage bug. X86 defines an FR32 register class and F16 register class, but the F16 register class is functionally an alias of F32 with the same members and size. There's no need to have the F16 class.
1 parent 9690a71 commit 1dbb932

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-26
lines changed

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,20 +2376,24 @@ void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
23762376

23772377
// If we have only one valid type, this is likely a copy between a virtual
23782378
// and physical register.
2379-
TypeSize SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
2380-
TypeSize DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
2379+
TypeSize SrcSize = TypeSize::getZero();
2380+
TypeSize DstSize = TypeSize::getZero();
23812381
if (SrcReg.isPhysical() && DstTy.isValid()) {
23822382
const TargetRegisterClass *SrcRC =
23832383
TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy);
2384-
if (SrcRC)
2385-
SrcSize = TRI->getRegSizeInBits(*SrcRC);
2384+
if (!SrcRC)
2385+
SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
2386+
} else {
2387+
SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
23862388
}
23872389

23882390
if (DstReg.isPhysical() && SrcTy.isValid()) {
23892391
const TargetRegisterClass *DstRC =
23902392
TRI->getMinimalPhysRegClassLLT(DstReg, SrcTy);
2391-
if (DstRC)
2392-
DstSize = TRI->getRegSizeInBits(*DstRC);
2393+
if (!DstRC)
2394+
DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
2395+
} else {
2396+
DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
23932397
}
23942398

23952399
// The next two checks allow COPY between physical and virtual registers,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# RUN: not --crash llc -mtriple=amdgcn -run-pass=none -filetype=null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" %s
2+
3+
---
4+
name: test_valid_copies
5+
tracksRegLiveness: true
6+
body: |
7+
bb.0:
8+
liveins: $vgpr0, $vcc
9+
%0:_(s32) = COPY $vgpr0
10+
%1:_(s16) = COPY $vgpr0
11+
%2:_(s64) = COPY $vcc
12+
%3:_(s1) = COPY $vcc
13+
$vgpr0 = COPY %0
14+
$vgpr0 = COPY %0
15+
$vcc = COPY %2
16+
$vcc = COPY %3
17+
...
18+
19+
---
20+
name: test_invalid_copies
21+
tracksRegLiveness: true
22+
body: |
23+
bb.0:
24+
liveins: $vgpr0_vgpr1, $vgpr2, $vcc
25+
26+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
27+
; CHECK: - instruction: %0:_(s32) = COPY $vgpr0_vgpr1
28+
%0:_(s32) = COPY $vgpr0_vgpr1
29+
30+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
31+
; CHECK: - instruction: %1:_(s64) = COPY $vgpr2
32+
%1:_(s64) = COPY $vgpr2
33+
34+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
35+
; CHECK: - instruction: %2:_(s32) = COPY $vcc
36+
%2:_(s32) = COPY $vcc
37+
38+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
39+
; CHECK: - instruction: %3:_(s8) = COPY $vgpr2
40+
%3:_(s8) = COPY $vgpr2
41+
42+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
43+
; CHECK: - instruction: $vgpr0_vgpr1 = COPY %0:_(s32)
44+
$vgpr0_vgpr1 = COPY %0
45+
46+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
47+
; CHECK: - instruction: $vgpr2 = COPY %1:_(s64)
48+
$vgpr2 = COPY %1
49+
50+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
51+
; CHECK: - instruction: $vcc = COPY %2:_(s32)
52+
$vcc = COPY %2
53+
54+
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
55+
; CHECK: - instruction: $vgpr2 = COPY %3:_(s8)
56+
$vgpr2 = COPY %3
57+
58+
...

llvm/test/MachineVerifier/test_copy_physregs_x86.mir

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,26 @@ body: |
2929
liveins: $xmm0, $xmm1, $xmm2, $xmm3
3030
3131
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
32-
; CHECK: - instruction: %0:_(s16) = COPY $xmm0
33-
%0:_(s16) = COPY $xmm0
32+
; CHECK: - instruction: %0:_(<4 x s16>) = COPY $xmm1
33+
%0:_(<4 x s16>) = COPY $xmm1
3434
3535
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
36-
; CHECK: - instruction: %1:_(<4 x s16>) = COPY $xmm1
37-
%1:_(<4 x s16>) = COPY $xmm1
36+
; CHECK: - instruction: %1:_(s256) = COPY $xmm2
37+
%1:_(s256) = COPY $xmm2
3838
3939
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
40-
; CHECK: - instruction: %2:_(s256) = COPY $xmm2
41-
%2:_(s256) = COPY $xmm2
40+
; CHECK: - instruction: %2:_(<8 x s32>) = COPY $xmm3
41+
%2:_(<8 x s32>) = COPY $xmm3
4242
4343
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
44-
; CHECK: - instruction: %3:_(<8 x s32>) = COPY $xmm3
45-
%3:_(<8 x s32>) = COPY $xmm3
44+
; CHECK: - instruction: $xmm1 = COPY %0:_(<4 x s16>)
45+
$xmm1 = COPY %0
4646
4747
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
48-
; CHECK: - instruction: $xmm0 = COPY %0:_(s16)
49-
$xmm0 = COPY %0
50-
51-
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
52-
; CHECK: - instruction: $xmm1 = COPY %1:_(<4 x s16>)
53-
$xmm1 = COPY %1
48+
; CHECK: - instruction: $xmm2 = COPY %1:_(s256)
49+
$xmm2 = COPY %1
5450
5551
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
56-
; CHECK: - instruction: $xmm2 = COPY %2:_(s256)
57-
$xmm2 = COPY %2
58-
59-
; CHECK: *** Bad machine code: Copy Instruction is illegal with mismatching sizes ***
60-
; CHECK: - instruction: $xmm3 = COPY %3:_(<8 x s32>)
61-
$xmm3 = COPY %3
52+
; CHECK: - instruction: $xmm3 = COPY %2:_(<8 x s32>)
53+
$xmm3 = COPY %2
6254
...

0 commit comments

Comments
 (0)