Author: stulova Date: Tue Nov 29 11:01:19 2016 New Revision: 288163 URL: http://llvm.org/viewvc/llvm-project?rev=288163&view=rev Log: [OpenCL] Prevent generation of globals in non-constant AS for OpenCL.
Avoid using shortcut for const qualified non-constant address space aggregate variables while generating them on the stack such that the alloca object is used instead of a global variable containing initializer. Review: https://reviews.llvm.org/D27109 Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp cfe/trunk/test/CodeGenOpenCL/constant-addr-space-globals.cl Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=288163&r1=288162&r2=288163&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGDecl.cpp (original) +++ cfe/trunk/lib/CodeGen/CGDecl.cpp Tue Nov 29 11:01:19 2016 @@ -948,8 +948,12 @@ CodeGenFunction::EmitAutoVarAlloca(const // If the variable's a const type, and it's neither an NRVO // candidate nor a __block variable and has no mutable members, // emit it as a global instead. - if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef && - CGM.isTypeConstant(Ty, true)) { + // Exception is if a variable is located in non-constant address space + // in OpenCL. + if ((!getLangOpts().OpenCL || + Ty.getAddressSpace() == LangAS::opencl_constant) && + (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef && + CGM.isTypeConstant(Ty, true))) { EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); // Signal this condition to later callbacks. Modified: cfe/trunk/test/CodeGenOpenCL/constant-addr-space-globals.cl URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenOpenCL/constant-addr-space-globals.cl?rev=288163&r1=288162&r2=288163&view=diff ============================================================================== --- cfe/trunk/test/CodeGenOpenCL/constant-addr-space-globals.cl (original) +++ cfe/trunk/test/CodeGenOpenCL/constant-addr-space-globals.cl Tue Nov 29 11:01:19 2016 @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -cl-opt-disable -ffake-address-space-map -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 %s -triple "spir64-unknown-unknown" -cl-opt-disable -ffake-address-space-map -emit-llvm -o - | FileCheck %s // CHECK: @array = addrspace({{[0-9]+}}) constant __constant float array[2] = {0.0f, 1.0f}; @@ -6,3 +6,22 @@ __constant float array[2] = {0.0f, 1.0f} kernel void test(global float *out) { *out = array[0]; } + +// Test that we don't use directly initializers for const aggregates +// but create a copy in the original address space (unless a variable itself is +// in the constant address space). + +void foo(constant const int *p1, const int *p2, const int *p3); +// CHECK: @k.arr1 = internal addrspace(3) constant [3 x i32] [i32 1, i32 2, i32 3] +// CHECK: @k.arr2 = private unnamed_addr addrspace(3) constant [3 x i32] [i32 4, i32 5, i32 6] +// CHECK: @k.arr3 = private unnamed_addr addrspace(3) constant [3 x i32] [i32 7, i32 8, i32 9] +kernel void k(void) { + // CHECK-NOT: %arr1 = alloca [3 x i32] + constant const int arr1[] = {1, 2, 3}; + // CHECK: %arr2 = alloca [3 x i32] + const int arr2[] = {4, 5, 6}; + // CHECK: %arr3 = alloca [3 x i32] + int arr3[] = {7, 8, 9}; + + foo(arr1, arr2, arr3); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits