From 4b4bc1ea16dec58e828ec3a0154046b10ec69242 Mon Sep 17 00:00:00 2001 From: Konstantin Schwarz Date: Tue, 24 Aug 2021 15:13:39 +0200 Subject: [PATCH] [GlobalISel] Do not generate illegal G_SEXTLOADs after legalization The sext_inreg_of_load combine did not have the isLegalOrBeforeLegalizer check, leading to the generation of potentially illegal G_SEXTLOADs when run after legalization. Reviewed By: foad Differential Revision: https://reviews.llvm.org/D108626 --- llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp | 12 ++++++++++++ ...tlegalizer-combiner-sextload-from-sextinreg.mir | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 llvm/test/CodeGen/AMDGPU/GlobalISel/postlegalizer-combiner-sextload-from-sextinreg.mir diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp index 949ecac..34322cb 100644 --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -722,6 +722,18 @@ bool CombinerHelper::matchSextInRegOfLoad( // anyway for most targets. if (!isPowerOf2_32(NewSizeBits)) return false; + + const MachineMemOperand &MMO = LoadDef->getMMO(); + LegalityQuery::MemDesc MMDesc; + MMDesc.MemoryTy = LLT::scalar(NewSizeBits); + MMDesc.AlignInBits = MMO.getAlign().value() * 8; + MMDesc.Ordering = MMO.getSuccessOrdering(); + if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SEXTLOAD, + {MRI.getType(LoadDef->getDstReg()), + MRI.getType(LoadDef->getPointerReg())}, + {MMDesc}})) + return false; + MatchInfo = std::make_tuple(LoadDef->getDstReg(), NewSizeBits); return true; } diff --git a/llvm/test/CodeGen/AMDGPU/GlobalISel/postlegalizer-combiner-sextload-from-sextinreg.mir b/llvm/test/CodeGen/AMDGPU/GlobalISel/postlegalizer-combiner-sextload-from-sextinreg.mir new file mode 100644 index 0000000..067158e --- /dev/null +++ b/llvm/test/CodeGen/AMDGPU/GlobalISel/postlegalizer-combiner-sextload-from-sextinreg.mir @@ -0,0 +1,22 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -global-isel -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -run-pass=amdgpu-postlegalizer-combiner -verify-machineinstrs -o - %s | FileCheck %s + +# Post-legalizer should not generate illegal extending loads +--- +name: sextload_from_inreg +tracksRegLiveness: true +legalized: true +body: | + bb.0: + liveins: $vgpr0_vgpr1 + ; CHECK-LABEL: name: sextload_from_inreg + ; CHECK: liveins: $vgpr0_vgpr1 + ; CHECK: [[COPY:%[0-9]+]]:_(p1) = COPY $vgpr0_vgpr1 + ; CHECK: [[LOAD:%[0-9]+]]:_(s64) = G_LOAD [[COPY]](p1) :: (load (s64), addrspace 1) + ; CHECK: [[SEXT_INREG:%[0-9]+]]:_(s64) = G_SEXT_INREG [[LOAD]], 8 + ; CHECK: $vgpr0_vgpr1 = COPY [[SEXT_INREG]](s64) + %0:_(p1) = COPY $vgpr0_vgpr1 + %1:_(s64) = G_LOAD %0 :: (load (s64), align 8, addrspace 1) + %2:_(s64) = G_SEXT_INREG %1, 8 + $vgpr0_vgpr1 = COPY %2 +... -- 2.7.4