[polly] r310448 - [PPCGCodeGeneration] Compute element size in bytes for arrays correctly.

Siddharth Bhat via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 9 01:29:16 PDT 2017


Author: bollu
Date: Wed Aug  9 01:29:16 2017
New Revision: 310448

URL: http://llvm.org/viewvc/llvm-project?rev=310448&view=rev
Log:
[PPCGCodeGeneration] Compute element size in bytes for arrays correctly.

Previously, we used to compute this with `elementSizeInBits / 8`. This
would yield an element size of 0 when the array had element size < 8 in
bits.

To fix this, ask data layout what the size in bytes should be.

Differential Revision: https://reviews.llvm.org/D36459

Added:
    polly/trunk/test/GPGPU/array-with-elem-type-smaller-than-byte.ll
Modified:
    polly/trunk/lib/CodeGen/PPCGCodeGeneration.cpp

Modified: polly/trunk/lib/CodeGen/PPCGCodeGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGen/PPCGCodeGeneration.cpp?rev=310448&r1=310447&r2=310448&view=diff
==============================================================================
--- polly/trunk/lib/CodeGen/PPCGCodeGeneration.cpp (original)
+++ polly/trunk/lib/CodeGen/PPCGCodeGeneration.cpp Wed Aug  9 01:29:16 2017
@@ -778,6 +778,19 @@ void GPUNodeBuilder::allocateDeviceArray
           ArraySize,
           Builder.CreateMul(Offset,
                             Builder.getInt64(ScopArray->getElemSizeInBytes())));
+    const SCEV *SizeSCEV = SE.getSCEV(ArraySize);
+    // It makes no sense to have an array of size 0. The CUDA API will
+    // throw an error anyway if we invoke `cuMallocManaged` with size `0`. We
+    // choose to be defensive and catch this at the compile phase. It is
+    // most likely that we are doing something wrong with size computation.
+    if (SizeSCEV->isZero()) {
+      errs() << getUniqueScopName(&S)
+             << " has computed array size 0: " << *ArraySize
+             << " | for array: " << *(ScopArray->getBasePtr())
+             << ". This is illegal, exiting.\n";
+      report_fatal_error("array size was computed to be 0");
+    }
+
     Value *DevArray = createCallAllocateMemoryForDevice(ArraySize);
     DevArray->setName(DevArrayName);
     DeviceAllocations[ScopArray] = DevArray;
@@ -2905,7 +2918,7 @@ public:
 
       PPCGArray.space = Array->getSpace().release();
       PPCGArray.type = strdup(TypeName.c_str());
-      PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
+      PPCGArray.size = DL->getTypeAllocSize(Array->getElementType());
       PPCGArray.name = strdup(Array->getName().c_str());
       PPCGArray.extent = nullptr;
       PPCGArray.n_index = Array->getNumberOfDimensions();

Added: polly/trunk/test/GPGPU/array-with-elem-type-smaller-than-byte.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/GPGPU/array-with-elem-type-smaller-than-byte.ll?rev=310448&view=auto
==============================================================================
--- polly/trunk/test/GPGPU/array-with-elem-type-smaller-than-byte.ll (added)
+++ polly/trunk/test/GPGPU/array-with-elem-type-smaller-than-byte.ll Wed Aug  9 01:29:16 2017
@@ -0,0 +1,50 @@
+; RUN: opt %loadPolly -S -polly-codegen-ppcg \
+; RUN: -polly-use-llvm-names < %s
+; ModuleID = 'test/GPGPU/zero-size-array.ll'
+
+; REQUIRES: pollyacc
+
+target datalayout = "e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f128:128:128-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+target triple = "x86_64-unknown-linux-gnu"
+
+
+; We used to divide the element size by 8 to arrive at the 'actual' size
+; of an array element. This used to cause arrays that have an element size
+; of less than 8 to collapse to size 0. This test makes sure that it does
+; not happen anymore.
+
+; f(int *niters_ptr, int *arr[0]) {
+;     const int inters = *niters_ptr;
+;     for(int i = 0; i < niters; i++) {
+;       arr[0][i + 1] = 0
+;     }
+; }
+
+; Function Attrs: nounwind uwtable
+define void @f(i32* noalias %niters.ptr, [0 x i32]* noalias %arr) #0 {
+entry:
+  %niters = load i32, i32* %niters.ptr, align 4
+  br label %loop.body
+
+loop.body:                                             ; preds = %loop.body, %entry
+  %indvar = phi i32 [ %indvar.next, %loop.body ], [ 1, %entry ]
+  %indvar.sext = sext i32 %indvar to i64
+  %arr.slot = getelementptr [0 x i32], [0 x i32]* %arr, i64 0, i64 %indvar.sext
+  store i32 0, i32* %arr.slot, align 4
+  %tmp8 = icmp eq i32 %indvar, %niters
+  %indvar.next = add i32 %indvar, 1
+  br i1 %tmp8, label %loop.exit, label %loop.body
+
+loop.exit:                                    ; preds = %loop.body
+  %tmp10 = icmp sgt i32 undef, 0
+  br label %auxiliary.loop
+
+auxiliary.loop:                                            ; preds = %"101", %loop.exit
+  %tmp11 = phi i1 [ %tmp10, %loop.exit ], [ undef, %auxiliary.loop ]
+  br i1 undef, label %auxiliary.loop, label %exit
+
+exit:                              ; preds = %auxiliary.loop
+  ret void
+}
+
+attributes #0 = { nounwind uwtable }




More information about the llvm-commits mailing list