[PATCH] D47580: AMDGPU: Preserve metadata when widening loads

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 31 03:31:37 PDT 2018


arsenm created this revision.
arsenm added reviewers: rampitec, kzhuravl.
Herald added subscribers: t-tye, tpr, dstuttard, yaxunl, nhaehnle, wdng.

https://reviews.llvm.org/D47580

Files:
  lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
  test/CodeGen/AMDGPU/widen_extending_scalar_loads.ll


Index: test/CodeGen/AMDGPU/widen_extending_scalar_loads.ll
===================================================================
--- test/CodeGen/AMDGPU/widen_extending_scalar_loads.ll
+++ test/CodeGen/AMDGPU/widen_extending_scalar_loads.ll
@@ -189,4 +189,39 @@
   ret void
 }
 
+; OPT-LABEL: @constant_load_i16_align4_range(
+; OPT: load i32, i32 addrspace(4)* %1, !range !0
+define amdgpu_kernel void @constant_load_i16_align4_range(i32 addrspace(1)* %out, i16 addrspace(4)* %in) #0 {
+  %ld = load i16, i16 addrspace(4)* %in, align 4, !range !0
+  %ext = sext i16 %ld to i32
+  store i32 %ext, i32 addrspace(1)* %out
+  ret void
+}
+
+; OPT-LABEL: @constant_load_i16_align4_range_max(
+; OPT: load i32, i32 addrspace(4)* %1, !range !1
+define amdgpu_kernel void @constant_load_i16_align4_range_max(i32 addrspace(1)* %out, i16 addrspace(4)* %in) #0 {
+  %ld = load i16, i16 addrspace(4)* %in, align 4, !range !1
+  %ext = sext i16 %ld to i32
+  store i32 %ext, i32 addrspace(1)* %out
+  ret void
+}
+
+; OPT-LABEL: @constant_load_i16_align4_invariant
+; OPT: load i32, i32 addrspace(4)* %1, !invariant.load !2
+define amdgpu_kernel void @constant_load_i16_align4_invariant(i32 addrspace(1)* %out, i16 addrspace(4)* %in) #0 {
+  %ld = load i16, i16 addrspace(4)* %in, align 4, !invariant.load !2
+  %ext = sext i16 %ld to i32
+  store i32 %ext, i32 addrspace(1)* %out
+  ret void
+}
+
 attributes #0 = { nounwind }
+
+; OPT: !0 = !{i32 5, i32 500}
+; OPT: !1 = !{i32 5, i32 65535}
+; OPT: !2 = !{}
+
+!0 = !{i16 5, i16 500}
+!1 = !{i16 5, i16 -1}
+!2 = !{}
Index: lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
===================================================================
--- lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
+++ lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
@@ -465,7 +465,7 @@
   return Changed;
 }
 
-bool AMDGPUCodeGenPrepare::visitLoadInst(LoadInst  &I) {
+bool AMDGPUCodeGenPrepare::visitLoadInst(LoadInst &I) {
   if ((I.getPointerAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS ||
        I.getPointerAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT) &&
       canWidenScalarExtLoad(I)) {
@@ -475,7 +475,24 @@
     Type *I32Ty = Builder.getInt32Ty();
     Type *PT = PointerType::get(I32Ty, I.getPointerAddressSpace());
     Value *BitCast= Builder.CreateBitCast(I.getPointerOperand(), PT);
-    Value *WidenLoad = Builder.CreateLoad(BitCast);
+    LoadInst *WidenLoad = Builder.CreateLoad(BitCast);
+    WidenLoad->copyMetadata(I);
+
+    // If we have range metadata, we need to convert the type.
+    if (auto *Range = WidenLoad->getMetadata(LLVMContext::MD_range)) {
+      ConstantInt *Lower =
+        mdconst::extract<ConstantInt>(Range->getOperand(0));
+      ConstantInt *Upper =
+        mdconst::extract<ConstantInt>(Range->getOperand(1));
+
+      Metadata *LowAndHigh[] = {
+        ConstantAsMetadata::get(ConstantInt::get(I32Ty, Lower->getZExtValue())),
+        ConstantAsMetadata::get(ConstantInt::get(I32Ty, Upper->getZExtValue()))
+      };
+
+      WidenLoad->setMetadata(LLVMContext::MD_range,
+                             MDNode::get(Mod->getContext(), LowAndHigh));
+    }
 
     int TySize = Mod->getDataLayout().getTypeSizeInBits(I.getType());
     Type *IntNTy = Builder.getIntNTy(TySize);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47580.149258.patch
Type: text/x-patch
Size: 3233 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180531/4216b59d/attachment.bin>


More information about the llvm-commits mailing list