[PATCH] D30845: Fix array sizes where address space is not yet known

Konstantin Zhuravlyov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 10 13:51:24 PST 2017


kzhuravl created this revision.
Herald added a subscriber: wdng.

For variables in generic address spaces, for example:

  unsigned char V[6442450944];
  ...

the address space is not yet known when we get into *getConstantArrayType*, it is 0. AMDGCN target's address space 0 has 32 bits pointers, so when we call *getPointerWidth* with 0, the array size is trimmed to 32 bits, which is not right.


https://reviews.llvm.org/D30845

Files:
  include/clang/Basic/TargetInfo.h
  lib/AST/ASTContext.cpp
  lib/Basic/Targets.cpp
  test/CodeGenOpenCL/amdgcn-large-globals.cl


Index: test/CodeGenOpenCL/amdgcn-large-globals.cl
===================================================================
--- test/CodeGenOpenCL/amdgcn-large-globals.cl
+++ test/CodeGenOpenCL/amdgcn-large-globals.cl
@@ -0,0 +1,12 @@
+// REQUIRES: amdgpu-registered-target
+// RUN: %clang_cc1 -cl-std=CL2.0 -triple amdgcn-unknown-unknown -S -emit-llvm -o - %s | FileCheck %s
+
+// CHECK: @One = common local_unnamed_addr addrspace(1) global [6442450944 x i8] zeroinitializer, align 1
+unsigned char One[6442450944];
+// CHECK: @Two = common local_unnamed_addr addrspace(1) global [6442450944 x i32] zeroinitializer, align 4
+global unsigned int Two[6442450944];
+ 
+kernel void large_globals(unsigned int id) {
+  One[id] = id;
+  Two[id + 1] = id + 1;
+}
Index: lib/Basic/Targets.cpp
===================================================================
--- lib/Basic/Targets.cpp
+++ lib/Basic/Targets.cpp
@@ -2084,6 +2084,10 @@
     }
   }
 
+  uint64_t getPreferredPointerWidth(unsigned AddrSpace) const override {
+    return 64;
+  }
+
   uint64_t getMaxPointerWidth() const override {
     return getTriple().getArch() == llvm::Triple::amdgcn ? 64 : 32;
   }
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -2692,8 +2692,8 @@
   // Convert the array size into a canonical width matching the pointer size for
   // the target.
   llvm::APInt ArySize(ArySizeIn);
-  ArySize =
-    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
+  ArySize = ArySize.zextOrTrunc(
+      Target->getPreferredPointerWidth(getTargetAddressSpace(EltTy)));
 
   llvm::FoldingSetNodeID ID;
   ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
Index: include/clang/Basic/TargetInfo.h
===================================================================
--- include/clang/Basic/TargetInfo.h
+++ include/clang/Basic/TargetInfo.h
@@ -296,6 +296,13 @@
     return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
   }
 
+  /// \brief Return the "preferred" width of pointers on this target, for the
+  /// specified address space.  This can be different from "getPointerWidth" in
+  /// cases where the final address space is not yet known.
+  virtual uint64_t getPreferredPointerWidth(unsigned AddrSpace) const {
+    return getPointerWidth(AddrSpace);
+  }
+
   /// \brief Return the maximum width of pointers on this target.
   virtual uint64_t getMaxPointerWidth() const {
     return PointerWidth;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30845.91404.patch
Type: text/x-patch
Size: 2537 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170310/e27fca46/attachment.bin>


More information about the cfe-commits mailing list