Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccf49b9

Browse files
committed
[GlobalISel] support widen unmerge if WideTy > SrcTy
Summary: Widening G_UNMERGE_VALUES to a type which is larger than the original source type is the same as widening it to the same type as the source type: in both cases, G_UNMERGE_VALUES has to be replaced with bit arithmetic which. Although the arithmetic itself is independent of whether the source type is smaller or equal to the widen type, widening the source type to the widen type should result in less artifacts being emitted, since this is the type that the user explicitly requested. Reviewers: arsenm, dsanders, aemerson, aditya_nandakumar Reviewed By: arsenm, dsanders Subscribers: jvesely, wdng, nhaehnle, rovka, hiraditya, volkan, kerbowa, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D76494
1 parent 0cc124c commit ccf49b9

File tree

3 files changed

+121
-33
lines changed

3 files changed

+121
-33
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -1390,18 +1390,27 @@ LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx,
13901390
if (!DstTy.isScalar())
13911391
return UnableToLegalize;
13921392

1393-
if (WideTy.getSizeInBits() == SrcTy.getSizeInBits()) {
1393+
if (WideTy.getSizeInBits() >= SrcTy.getSizeInBits()) {
13941394
if (SrcTy.isPointer()) {
13951395
const DataLayout &DL = MIRBuilder.getDataLayout();
13961396
if (DL.isNonIntegralAddressSpace(SrcTy.getAddressSpace())) {
1397-
LLVM_DEBUG(dbgs() << "Not casting non-integral address space integer\n");
1397+
LLVM_DEBUG(
1398+
dbgs() << "Not casting non-integral address space integer\n");
13981399
return UnableToLegalize;
13991400
}
14001401

14011402
SrcTy = LLT::scalar(SrcTy.getSizeInBits());
14021403
SrcReg = MIRBuilder.buildPtrToInt(SrcTy, SrcReg).getReg(0);
14031404
}
14041405

1406+
// Widen SrcTy to WideTy. This does not affect the result, but since the
1407+
// user requested this size, it is probably better handled than SrcTy and
1408+
// should reduce the total number of legalization artifacts
1409+
if (WideTy.getSizeInBits() > SrcTy.getSizeInBits()) {
1410+
SrcTy = WideTy;
1411+
SrcReg = MIRBuilder.buildAnyExt(WideTy, SrcReg).getReg(0);
1412+
}
1413+
14051414
// Theres no unmerge type to target. Directly extract the bits from the
14061415
// source type
14071416
unsigned DstSize = DstTy.getSizeInBits();
@@ -1417,10 +1426,6 @@ LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx,
14171426
return Legalized;
14181427
}
14191428

1420-
// TODO
1421-
if (WideTy.getSizeInBits() > SrcTy.getSizeInBits())
1422-
return UnableToLegalize;
1423-
14241429
// Extend the source to a wider type.
14251430
LLT LCMTy = getLCMType(SrcTy, WideTy);
14261431

llvm/test/CodeGen/AMDGPU/GlobalISel/legalize-unmerge-values.mir

+61-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2-
# RUN: llc -mtriple=amdgcn-- -O0 -run-pass=legalizer -global-isel-abort=0 -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=amdgcn-- -O0 -run-pass=legalizer -o - %s | FileCheck %s
33

44
---
55
name: test_unmerge_s32_s64
@@ -694,14 +694,21 @@ body: |
694694
liveins: $vgpr0
695695
; CHECK-LABEL: name: test_unmerge_s1_s3
696696
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
697-
; CHECK: [[TRUNC:%[0-9]+]]:_(s3) = G_TRUNC [[COPY]](s32)
698-
; CHECK: [[UV:%[0-9]+]]:_(s1), [[UV1:%[0-9]+]]:_(s1), [[UV2:%[0-9]+]]:_(s1) = G_UNMERGE_VALUES [[TRUNC]](s3)
699-
; CHECK: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[UV]](s1)
700-
; CHECK: [[ANYEXT1:%[0-9]+]]:_(s32) = G_ANYEXT [[UV1]](s1)
701-
; CHECK: [[ANYEXT2:%[0-9]+]]:_(s32) = G_ANYEXT [[UV2]](s1)
702-
; CHECK: $vgpr0 = COPY [[ANYEXT]](s32)
703-
; CHECK: $vgpr1 = COPY [[ANYEXT1]](s32)
704-
; CHECK: $vgpr2 = COPY [[ANYEXT2]](s32)
697+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
698+
; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 65535
699+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
700+
; CHECK: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY1]], [[C1]]
701+
; CHECK: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[AND]], [[C]](s32)
702+
; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 2
703+
; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
704+
; CHECK: [[AND1:%[0-9]+]]:_(s32) = G_AND [[COPY2]], [[C1]]
705+
; CHECK: [[LSHR1:%[0-9]+]]:_(s32) = G_LSHR [[AND1]], [[C2]](s32)
706+
; CHECK: [[COPY3:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
707+
; CHECK: [[COPY4:%[0-9]+]]:_(s32) = COPY [[LSHR]](s32)
708+
; CHECK: [[COPY5:%[0-9]+]]:_(s32) = COPY [[LSHR1]](s32)
709+
; CHECK: $vgpr0 = COPY [[COPY3]](s32)
710+
; CHECK: $vgpr1 = COPY [[COPY4]](s32)
711+
; CHECK: $vgpr2 = COPY [[COPY5]](s32)
705712
%0:_(s32) = COPY $vgpr0
706713
%1:_(s3) = G_TRUNC %0
707714
%2:_(s1), %3:_(s1), %4:_(s1) = G_UNMERGE_VALUES %1
@@ -720,24 +727,51 @@ body: |
720727
liveins: $vgpr0
721728
; CHECK-LABEL: name: test_unmerge_s1_s8
722729
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
723-
; CHECK: [[TRUNC:%[0-9]+]]:_(s8) = G_TRUNC [[COPY]](s32)
724-
; CHECK: [[UV:%[0-9]+]]:_(s1), [[UV1:%[0-9]+]]:_(s1), [[UV2:%[0-9]+]]:_(s1), [[UV3:%[0-9]+]]:_(s1), [[UV4:%[0-9]+]]:_(s1), [[UV5:%[0-9]+]]:_(s1), [[UV6:%[0-9]+]]:_(s1), [[UV7:%[0-9]+]]:_(s1) = G_UNMERGE_VALUES [[TRUNC]](s8)
725-
; CHECK: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[UV]](s1)
726-
; CHECK: [[ANYEXT1:%[0-9]+]]:_(s32) = G_ANYEXT [[UV1]](s1)
727-
; CHECK: [[ANYEXT2:%[0-9]+]]:_(s32) = G_ANYEXT [[UV2]](s1)
728-
; CHECK: [[ANYEXT3:%[0-9]+]]:_(s32) = G_ANYEXT [[UV3]](s1)
729-
; CHECK: [[ANYEXT4:%[0-9]+]]:_(s32) = G_ANYEXT [[UV4]](s1)
730-
; CHECK: [[ANYEXT5:%[0-9]+]]:_(s32) = G_ANYEXT [[UV5]](s1)
731-
; CHECK: [[ANYEXT6:%[0-9]+]]:_(s32) = G_ANYEXT [[UV6]](s1)
732-
; CHECK: [[ANYEXT7:%[0-9]+]]:_(s32) = G_ANYEXT [[UV7]](s1)
733-
; CHECK: $vgpr0 = COPY [[ANYEXT]](s32)
734-
; CHECK: $vgpr1 = COPY [[ANYEXT1]](s32)
735-
; CHECK: $vgpr2 = COPY [[ANYEXT2]](s32)
736-
; CHECK: $vgpr3 = COPY [[ANYEXT3]](s32)
737-
; CHECK: $vgpr4 = COPY [[ANYEXT4]](s32)
738-
; CHECK: $vgpr5 = COPY [[ANYEXT5]](s32)
739-
; CHECK: $vgpr6 = COPY [[ANYEXT6]](s32)
740-
; CHECK: $vgpr7 = COPY [[ANYEXT7]](s32)
730+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
731+
; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 65535
732+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
733+
; CHECK: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY1]], [[C1]]
734+
; CHECK: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[AND]], [[C]](s32)
735+
; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 2
736+
; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
737+
; CHECK: [[AND1:%[0-9]+]]:_(s32) = G_AND [[COPY2]], [[C1]]
738+
; CHECK: [[LSHR1:%[0-9]+]]:_(s32) = G_LSHR [[AND1]], [[C2]](s32)
739+
; CHECK: [[C3:%[0-9]+]]:_(s32) = G_CONSTANT i32 3
740+
; CHECK: [[COPY3:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
741+
; CHECK: [[AND2:%[0-9]+]]:_(s32) = G_AND [[COPY3]], [[C1]]
742+
; CHECK: [[LSHR2:%[0-9]+]]:_(s32) = G_LSHR [[AND2]], [[C3]](s32)
743+
; CHECK: [[C4:%[0-9]+]]:_(s32) = G_CONSTANT i32 4
744+
; CHECK: [[COPY4:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
745+
; CHECK: [[AND3:%[0-9]+]]:_(s32) = G_AND [[COPY4]], [[C1]]
746+
; CHECK: [[LSHR3:%[0-9]+]]:_(s32) = G_LSHR [[AND3]], [[C4]](s32)
747+
; CHECK: [[C5:%[0-9]+]]:_(s32) = G_CONSTANT i32 5
748+
; CHECK: [[COPY5:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
749+
; CHECK: [[AND4:%[0-9]+]]:_(s32) = G_AND [[COPY5]], [[C1]]
750+
; CHECK: [[LSHR4:%[0-9]+]]:_(s32) = G_LSHR [[AND4]], [[C5]](s32)
751+
; CHECK: [[C6:%[0-9]+]]:_(s32) = G_CONSTANT i32 6
752+
; CHECK: [[COPY6:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
753+
; CHECK: [[AND5:%[0-9]+]]:_(s32) = G_AND [[COPY6]], [[C1]]
754+
; CHECK: [[LSHR5:%[0-9]+]]:_(s32) = G_LSHR [[AND5]], [[C6]](s32)
755+
; CHECK: [[C7:%[0-9]+]]:_(s32) = G_CONSTANT i32 7
756+
; CHECK: [[COPY7:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
757+
; CHECK: [[AND6:%[0-9]+]]:_(s32) = G_AND [[COPY7]], [[C1]]
758+
; CHECK: [[LSHR6:%[0-9]+]]:_(s32) = G_LSHR [[AND6]], [[C7]](s32)
759+
; CHECK: [[COPY8:%[0-9]+]]:_(s32) = COPY [[COPY]](s32)
760+
; CHECK: [[COPY9:%[0-9]+]]:_(s32) = COPY [[LSHR]](s32)
761+
; CHECK: [[COPY10:%[0-9]+]]:_(s32) = COPY [[LSHR1]](s32)
762+
; CHECK: [[COPY11:%[0-9]+]]:_(s32) = COPY [[LSHR2]](s32)
763+
; CHECK: [[COPY12:%[0-9]+]]:_(s32) = COPY [[LSHR3]](s32)
764+
; CHECK: [[COPY13:%[0-9]+]]:_(s32) = COPY [[LSHR4]](s32)
765+
; CHECK: [[COPY14:%[0-9]+]]:_(s32) = COPY [[LSHR5]](s32)
766+
; CHECK: [[COPY15:%[0-9]+]]:_(s32) = COPY [[LSHR6]](s32)
767+
; CHECK: $vgpr0 = COPY [[COPY8]](s32)
768+
; CHECK: $vgpr1 = COPY [[COPY9]](s32)
769+
; CHECK: $vgpr2 = COPY [[COPY10]](s32)
770+
; CHECK: $vgpr3 = COPY [[COPY11]](s32)
771+
; CHECK: $vgpr4 = COPY [[COPY12]](s32)
772+
; CHECK: $vgpr5 = COPY [[COPY13]](s32)
773+
; CHECK: $vgpr6 = COPY [[COPY14]](s32)
774+
; CHECK: $vgpr7 = COPY [[COPY15]](s32)
741775
%0:_(s32) = COPY $vgpr0
742776
%1:_(s8) = G_TRUNC %0
743777
%2:_(s1), %3:_(s1), %4:_(s1), %5:_(s1), %6:_(s1), %7:_(s1), %8:_(s1), %9:_(s1) = G_UNMERGE_VALUES %1

llvm/unittests/CodeGen/GlobalISel/LegalizerHelperTest.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -2516,4 +2516,53 @@ TEST_F(GISelMITest, LowerBSWAP) {
25162516
EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
25172517
}
25182518

2519+
// Test widening of G_UNMERGE_VALUES
2520+
TEST_F(GISelMITest, WidenUnmerge) {
2521+
setUp();
2522+
if (!TM)
2523+
return;
2524+
2525+
DefineLegalizerInfo(A, {});
2526+
2527+
// Check that widening G_UNMERGE_VALUES to a larger type than the source type
2528+
// works as expected
2529+
LLT P0{LLT::pointer(0, 64)};
2530+
LLT S32{LLT::scalar(32)};
2531+
LLT S96{LLT::scalar(96)};
2532+
2533+
auto IntToPtr = B.buildIntToPtr(P0, Copies[0]);
2534+
auto UnmergePtr = B.buildUnmerge(S32, IntToPtr);
2535+
auto UnmergeScalar = B.buildUnmerge(S32, Copies[0]);
2536+
2537+
AInfo Info(MF->getSubtarget());
2538+
DummyGISelObserver Observer;
2539+
LegalizerHelper Helper(*MF, Info, Observer, B);
2540+
2541+
// Perform Legalization
2542+
EXPECT_EQ(LegalizerHelper::LegalizeResult::Legalized,
2543+
Helper.widenScalar(*UnmergePtr, 0, S96));
2544+
2545+
EXPECT_EQ(LegalizerHelper::LegalizeResult::Legalized,
2546+
Helper.widenScalar(*UnmergeScalar, 0, S96));
2547+
2548+
const auto *CheckStr = R"(
2549+
CHECK: [[COPY:%[0-9]+]]:_(s64) = COPY
2550+
CHECK: [[PTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[COPY]]
2551+
CHECK: [[INT:%[0-9]+]]:_(s64) = G_PTRTOINT [[PTR]]
2552+
CHECK: [[ANYEXT:%[0-9]+]]:_(s96) = G_ANYEXT [[INT]]
2553+
CHECK: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[ANYEXT]]
2554+
CHECK: [[C:%[0-9]+]]:_(s96) = G_CONSTANT i96 32
2555+
CHECK: [[LSHR:%[0-9]+]]:_(s96) = G_LSHR [[ANYEXT]]:_, [[C]]
2556+
CHECK: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[LSHR]]
2557+
CHECK: [[ANYEXT:%[0-9]+]]:_(s96) = G_ANYEXT [[COPY]]
2558+
CHECK: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[ANYEXT]]
2559+
CHECK: [[C:%[0-9]+]]:_(s96) = G_CONSTANT i96 32
2560+
CHECK: [[LSHR:%[0-9]+]]:_(s96) = G_LSHR [[ANYEXT]]:_, [[C]]
2561+
CHECK: [[TRUNC1:%[0-9]+]]:_(s32) = G_TRUNC [[LSHR]]
2562+
)";
2563+
2564+
// Check
2565+
EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
2566+
}
2567+
25192568
} // namespace

0 commit comments

Comments
 (0)