Skip to content

Commit 9ea7f73

Browse files
committed
[AArch64][GISel] Don't pointlessly lower G_TRUNC (llvm#81479)
If we have something like G_TRUNC from v2s32 to v2s16, then lowering this to a concat of two G_TRUNC s32 to s16 followed by G_TRUNC from v2s16 to v2s8 does not bring us any closer to legality. In fact, the first part of that is a G_BUILD_VECTOR whose legalization will produce a new G_TRUNC from v2s32 to v2s16, and both G_TRUNCs will then get combined to the original, causing a legalization cycle. Make the lowering condition more precise, by requiring that the original vector is >128 bits, which is I believe the only case where this specific splitting approach is useful. Note that this doesn't actually produce a legal result (the alwaysLegal is a lie, as before), but it will cause a proper globalisel abort instead of an infinite legalization loop. Fixes llvm#81244. (cherry picked from commit 070848c)
1 parent 7ff82b2 commit 9ea7f73

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,8 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
622622
.lowerIf([=](const LegalityQuery &Query) {
623623
LLT DstTy = Query.Types[0];
624624
LLT SrcTy = Query.Types[1];
625-
return DstTy.isVector() && (SrcTy.getSizeInBits() > 128 ||
626-
(DstTy.getScalarSizeInBits() * 2 <
627-
SrcTy.getScalarSizeInBits()));
625+
return DstTy.isVector() && SrcTy.getSizeInBits() > 128 &&
626+
DstTy.getScalarSizeInBits() * 2 <= SrcTy.getScalarSizeInBits();
628627
})
629628

630629
.alwaysLegal();

llvm/test/CodeGen/AArch64/GlobalISel/legalize-xtn.mir

+24
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,27 @@ body: |
529529
RET_ReallyLR implicit $q0
530530
531531
...
532+
533+
---
534+
name: pr81244
535+
tracksRegLiveness: true
536+
body: |
537+
bb.0:
538+
liveins: $d0
539+
; CHECK-LABEL: name: pr81244
540+
; CHECK: liveins: $d0
541+
; CHECK-NEXT: {{ $}}
542+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s32>) = COPY $d0
543+
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(<2 x s8>) = G_TRUNC [[COPY]](<2 x s32>)
544+
; CHECK-NEXT: [[CONCAT_VECTORS:%[0-9]+]]:_(<4 x s8>) = G_CONCAT_VECTORS [[TRUNC]](<2 x s8>), [[TRUNC]](<2 x s8>)
545+
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(<4 x s16>) = G_ANYEXT [[CONCAT_VECTORS]](<4 x s8>)
546+
; CHECK-NEXT: $d0 = COPY [[ANYEXT]](<4 x s16>)
547+
; CHECK-NEXT: RET_ReallyLR implicit $d0
548+
%0:_(<2 x s32>) = COPY $d0
549+
%1:_(<2 x s8>) = G_TRUNC %0(<2 x s32>)
550+
%2:_(<4 x s8>) = G_CONCAT_VECTORS %1(<2 x s8>), %1(<2 x s8>)
551+
%3:_(<4 x s16>) = G_ANYEXT %2(<4 x s8>)
552+
$d0 = COPY %3(<4 x s16>)
553+
RET_ReallyLR implicit $d0
554+
555+
...

0 commit comments

Comments
 (0)