Index: cfe/trunk/include/clang/Basic/TargetInfo.h =================================================================== --- cfe/trunk/include/clang/Basic/TargetInfo.h +++ cfe/trunk/include/clang/Basic/TargetInfo.h @@ -46,6 +46,7 @@ class QualType; class SourceLocation; class SourceManager; +class Type; namespace Builtin { struct Info; } @@ -1050,10 +1051,8 @@ return getTargetOpts().SupportedOpenCLOptions; } - /// \brief Get OpenCL image type address space. - virtual LangAS::ID getOpenCLImageAddrSpace() const { - return LangAS::opencl_global; - } + /// \brief Get address space for OpenCL type. + virtual LangAS::ID getOpenCLTypeAddrSpace(const Type *T) const; /// \returns Target specific vtbl ptr address space. virtual unsigned getVtblPtrAddressSpace() const { Index: cfe/trunk/lib/AST/ASTContext.cpp =================================================================== --- cfe/trunk/lib/AST/ASTContext.cpp +++ cfe/trunk/lib/AST/ASTContext.cpp @@ -1631,6 +1631,7 @@ uint64_t Width = 0; unsigned Align = 8; bool AlignIsRequired = false; + unsigned AS = 0; switch (T->getTypeClass()) { #define TYPE(Class, Base) #define ABSTRACT_TYPE(Class, Base) @@ -1777,28 +1778,18 @@ Width = Target->getPointerWidth(0); Align = Target->getPointerAlign(0); break; - case BuiltinType::OCLSampler: { - auto AS = getTargetAddressSpace(LangAS::opencl_constant); - Width = Target->getPointerWidth(AS); - Align = Target->getPointerAlign(AS); - break; - } + case BuiltinType::OCLSampler: case BuiltinType::OCLEvent: case BuiltinType::OCLClkEvent: case BuiltinType::OCLQueue: case BuiltinType::OCLReserveID: - // Currently these types are pointers to opaque types. - Width = Target->getPointerWidth(0); - Align = Target->getPointerAlign(0); - break; #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ case BuiltinType::Id: #include "clang/Basic/OpenCLImageTypes.def" - { - auto AS = getTargetAddressSpace(Target->getOpenCLImageAddrSpace()); - Width = Target->getPointerWidth(AS); - Align = Target->getPointerAlign(AS); - } + AS = getTargetAddressSpace(Target->getOpenCLTypeAddrSpace(T)); + Width = Target->getPointerWidth(AS); + Align = Target->getPointerAlign(AS); + break; } break; case Type::ObjCObjectPointer: @@ -1806,8 +1797,7 @@ Align = Target->getPointerAlign(0); break; case Type::BlockPointer: { - unsigned AS = getTargetAddressSpace( - cast(T)->getPointeeType()); + AS = getTargetAddressSpace(cast(T)->getPointeeType()); Width = Target->getPointerWidth(AS); Align = Target->getPointerAlign(AS); break; @@ -1816,14 +1806,13 @@ case Type::RValueReference: { // alignof and sizeof should never enter this code path here, so we go // the pointer route. - unsigned AS = getTargetAddressSpace( - cast(T)->getPointeeType()); + AS = getTargetAddressSpace(cast(T)->getPointeeType()); Width = Target->getPointerWidth(AS); Align = Target->getPointerAlign(AS); break; } case Type::Pointer: { - unsigned AS = getTargetAddressSpace(cast(T)->getPointeeType()); + AS = getTargetAddressSpace(cast(T)->getPointeeType()); Width = Target->getPointerWidth(AS); Align = Target->getPointerAlign(AS); break; Index: cfe/trunk/lib/Basic/TargetInfo.cpp =================================================================== --- cfe/trunk/lib/Basic/TargetInfo.cpp +++ cfe/trunk/lib/Basic/TargetInfo.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/TargetInfo.h" +#include "clang/AST/Type.h" #include "clang/Basic/AddressSpaces.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/LangOptions.h" @@ -347,6 +348,30 @@ return true; } +LangAS::ID TargetInfo::getOpenCLTypeAddrSpace(const Type *T) const { + auto BT = dyn_cast(T); + + if (!BT) { + if (isa(T)) + return LangAS::opencl_global; + + return LangAS::Default; + } + + switch (BT->getKind()) { +#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ + case BuiltinType::Id: \ + return LangAS::opencl_global; +#include "clang/Basic/OpenCLImageTypes.def" + + case BuiltinType::OCLSampler: + return LangAS::opencl_constant; + + default: + return LangAS::Default; + } +} + //===----------------------------------------------------------------------===// Index: cfe/trunk/lib/Basic/Targets/AMDGPU.h =================================================================== --- cfe/trunk/lib/Basic/Targets/AMDGPU.h +++ cfe/trunk/lib/Basic/Targets/AMDGPU.h @@ -14,6 +14,7 @@ #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H #define LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H +#include "clang/AST/Type.h" #include "clang/Basic/TargetInfo.h" #include "clang/Basic/TargetOptions.h" #include "llvm/ADT/Triple.h" @@ -190,8 +191,21 @@ } } - LangAS::ID getOpenCLImageAddrSpace() const override { + LangAS::ID getOpenCLTypeAddrSpace(const Type *T) const override { + auto BT = dyn_cast(T); + + if (!BT) + return TargetInfo::getOpenCLTypeAddrSpace(T); + + switch (BT->getKind()) { +#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ + case BuiltinType::Id: \ return LangAS::opencl_constant; +#include "clang/Basic/OpenCLImageTypes.def" + + default: + return TargetInfo::getOpenCLTypeAddrSpace(T); + } } llvm::Optional getConstantAddressSpace() const override { Index: cfe/trunk/lib/CodeGen/CGOpenCLRuntime.h =================================================================== --- cfe/trunk/lib/CodeGen/CGOpenCLRuntime.h +++ cfe/trunk/lib/CodeGen/CGOpenCLRuntime.h @@ -48,9 +48,9 @@ virtual llvm::Type *convertOpenCLSpecificType(const Type *T); - virtual llvm::Type *getPipeType(); + virtual llvm::Type *getPipeType(const PipeType *T); - llvm::PointerType *getSamplerType(); + llvm::PointerType *getSamplerType(const Type *T); // \brief Returnes a value which indicates the size in bytes of the pipe // element. Index: cfe/trunk/lib/CodeGen/CGOpenCLRuntime.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CGOpenCLRuntime.cpp +++ cfe/trunk/lib/CodeGen/CGOpenCLRuntime.cpp @@ -35,8 +35,8 @@ "Not an OpenCL specific type!"); llvm::LLVMContext& Ctx = CGM.getLLVMContext(); - uint32_t ImgAddrSpc = CGM.getContext().getTargetAddressSpace( - CGM.getTarget().getOpenCLImageAddrSpace()); + uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace( + CGM.getTarget().getOpenCLTypeAddrSpace(T)); switch (cast(T)->getKind()) { default: llvm_unreachable("Unexpected opencl builtin type!"); @@ -45,29 +45,29 @@ case BuiltinType::Id: \ return llvm::PointerType::get( \ llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \ - ImgAddrSpc); + AddrSpc); #include "clang/Basic/OpenCLImageTypes.def" case BuiltinType::OCLSampler: - return getSamplerType(); + return getSamplerType(T); case BuiltinType::OCLEvent: - return llvm::PointerType::get(llvm::StructType::create( - Ctx, "opencl.event_t"), 0); + return llvm::PointerType::get( + llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc); case BuiltinType::OCLClkEvent: return llvm::PointerType::get( - llvm::StructType::create(Ctx, "opencl.clk_event_t"), 0); + llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc); case BuiltinType::OCLQueue: return llvm::PointerType::get( - llvm::StructType::create(Ctx, "opencl.queue_t"), 0); + llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc); case BuiltinType::OCLReserveID: return llvm::PointerType::get( - llvm::StructType::create(Ctx, "opencl.reserve_id_t"), 0); + llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc); } } -llvm::Type *CGOpenCLRuntime::getPipeType() { +llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) { if (!PipeTy){ - uint32_t PipeAddrSpc = - CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); + uint32_t PipeAddrSpc = CGM.getContext().getTargetAddressSpace( + CGM.getTarget().getOpenCLTypeAddrSpace(T)); PipeTy = llvm::PointerType::get(llvm::StructType::create( CGM.getLLVMContext(), "opencl.pipe_t"), PipeAddrSpc); } @@ -75,12 +75,12 @@ return PipeTy; } -llvm::PointerType *CGOpenCLRuntime::getSamplerType() { +llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) { if (!SamplerTy) SamplerTy = llvm::PointerType::get(llvm::StructType::create( CGM.getLLVMContext(), "opencl.sampler_t"), CGM.getContext().getTargetAddressSpace( - LangAS::opencl_constant)); + CGM.getTarget().getOpenCLTypeAddrSpace(T))); return SamplerTy; } Index: cfe/trunk/lib/CodeGen/CodeGenModule.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp @@ -4536,7 +4536,7 @@ CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF) { llvm::Constant *C = EmitConstantExpr(E, E->getType(), &CGF); - auto SamplerT = getOpenCLRuntime().getSamplerType(); + auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), Index: cfe/trunk/lib/CodeGen/CodeGenTypes.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CodeGenTypes.cpp +++ cfe/trunk/lib/CodeGen/CodeGenTypes.cpp @@ -635,7 +635,7 @@ break; } case Type::Pipe: { - ResultType = CGM.getOpenCLRuntime().getPipeType(); + ResultType = CGM.getOpenCLRuntime().getPipeType(cast(Ty)); break; } } Index: cfe/trunk/test/CodeGenOpenCL/opencl_types.cl =================================================================== --- cfe/trunk/test/CodeGenOpenCL/opencl_types.cl +++ cfe/trunk/test/CodeGenOpenCL/opencl_types.cl @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 %s -triple "spir-unknown-unknown" -emit-llvm -o - -O0 | FileCheck %s --check-prefix=CHECK-SPIR -// RUN: %clang_cc1 %s -triple "amdgcn--amdhsa" -emit-llvm -o - -O0 | FileCheck %s --check-prefix=CHECK-AMDGCN +// RUN: %clang_cc1 -cl-std=CL2.0 %s -triple "spir-unknown-unknown" -emit-llvm -o - -O0 | FileCheck %s --check-prefix=CHECK-SPIR +// RUN: %clang_cc1 -cl-std=CL2.0 %s -triple "amdgcn--amdhsa" -emit-llvm -o - -O0 | FileCheck %s --check-prefix=CHECK-AMDGCN #define CLK_ADDRESS_CLAMP_TO_EDGE 2 #define CLK_NORMALIZED_COORDS_TRUE 1 @@ -42,6 +42,12 @@ // CHECK-SPIR: alloca %opencl.sampler_t addrspace(2)* event_t evt; // CHECK-SPIR: alloca %opencl.event_t* + clk_event_t clk_evt; + // CHECK-SPIR: alloca %opencl.clk_event_t* + queue_t queue; + // CHECK-SPIR: alloca %opencl.queue_t* + reserve_id_t rid; + // CHECK-SPIR: alloca %opencl.reserve_id_t* // CHECK-SPIR: store %opencl.sampler_t addrspace(2)* fnc4smp(smp); // CHECK-SPIR: call {{.*}}void @fnc4smp(%opencl.sampler_t addrspace(2)* @@ -49,6 +55,10 @@ // CHECK-SPIR: call {{.*}}void @fnc4smp(%opencl.sampler_t addrspace(2)* } +kernel void foo_pipe(read_only pipe int p) {} +// CHECK-SPIR: @foo_pipe(%opencl.pipe_t addrspace(1)* %p) +// CHECK_AMDGCN: @foo_pipe(%opencl.pipe_t addrspace(1)* %p) + void __attribute__((overloadable)) bad1(image1d_t b, image2d_t c, image2d_t d) {} // CHECK-SPIR-LABEL: @{{_Z4bad114ocl_image1d_ro14ocl_image2d_roS0_|"\\01\?bad1@@\$\$J0YAXPAUocl_image1d_ro@@PAUocl_image2d_ro@@1@Z"}} // CHECK-AMDGCN-LABEL: @{{_Z4bad114ocl_image1d_ro14ocl_image2d_roS0_|"\\01\?bad1@@\$\$J0YAXPAUocl_image1d_ro@@PAUocl_image2d_ro@@1@Z"}}(%opencl.image1d_ro_t addrspace(2)*{{.*}}%opencl.image2d_ro_t addrspace(2)*{{.*}}%opencl.image2d_ro_t addrspace(2)*{{.*}})