r206478 - Bug 18567: Fix constantexpr pointer casts with address spaces.

Matt Arsenault Matthew.Arsenault at amd.com
Thu Apr 17 10:45:37 PDT 2014


Author: arsenm
Date: Thu Apr 17 12:45:37 2014
New Revision: 206478

URL: http://llvm.org/viewvc/llvm-project?rev=206478&view=rev
Log:
Bug 18567: Fix constantexpr pointer casts with address spaces.

Getting a pointer into a struct at a non-zero offset would try to
use the default address space.

Added:
    cfe/trunk/test/CodeGenOpenCL/address-space-constant-initializers.cl
Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=206478&r1=206477&r2=206478&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Thu Apr 17 12:45:37 2014
@@ -1063,7 +1063,9 @@ llvm::Constant *CodeGenModule::EmitConst
 
       // Apply offset if necessary.
       if (!Offset->isNullValue()) {
-        llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
+        unsigned AS = C->getType()->getPointerAddressSpace();
+        llvm::Type *CharPtrTy = Int8Ty->getPointerTo(AS);
+        llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, CharPtrTy);
         Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset);
         C = llvm::ConstantExpr::getPointerCast(Casted, C->getType());
       }

Added: cfe/trunk/test/CodeGenOpenCL/address-space-constant-initializers.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenOpenCL/address-space-constant-initializers.cl?rev=206478&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenOpenCL/address-space-constant-initializers.cl (added)
+++ cfe/trunk/test/CodeGenOpenCL/address-space-constant-initializers.cl Thu Apr 17 12:45:37 2014
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -ffake-address-space-map -emit-llvm -o - | FileCheck %s
+
+typedef struct {
+    int i;
+    float f; // At non-zero offset.
+} ArrayStruct;
+
+__constant ArrayStruct constant_array_struct = { 0, 0.0f };
+
+typedef struct {
+    __constant float* constant_float_ptr;
+} ConstantArrayPointerStruct;
+
+// CHECK: %struct.ConstantArrayPointerStruct = type { float addrspace(3)* }
+// CHECK: addrspace(3) global %struct.ConstantArrayPointerStruct { float addrspace(3)* bitcast (i8 addrspace(3)* getelementptr (i8 addrspace(3)* bitcast (%struct.ArrayStruct addrspace(3)* @constant_array_struct to i8 addrspace(3)*), i64 4) to float addrspace(3)*) }
+// Bug  18567
+__constant ConstantArrayPointerStruct constant_array_pointer_struct = {
+    &constant_array_struct.f
+};
+





More information about the cfe-commits mailing list