Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -214,6 +214,12 @@ unsigned MaxOpenCLWorkGroupSize; + bool SupportsOpenCLImages = false; + + /// Maximum supported device OpenCL version, corresponding to the values + /// expected for __OPENCL_VERSION__. If 0, the macro is not defined. + unsigned MaxOpenCLSupportedVersion = 0; + // TargetInfo Constructor. Default initializes all fields. TargetInfo(const llvm::Triple &T); @@ -667,6 +673,16 @@ unsigned getMaxOpenCLWorkGroupSize() const { return MaxOpenCLWorkGroupSize; } + /// Returns if if OpenCL images are supported. + bool hasOpenCLImageSupport() const { + return SupportsOpenCLImages; + } + + /// Return value to report for __OPENCL_VERSION__ + unsigned getMaxOpenCLSupportedVersion() const { + return MaxOpenCLSupportedVersion; + } + /// Return the alignment (in bits) of the thrown exception object. This is /// only meaningful for targets that allocate C++ exceptions in a system /// runtime, such as those using the Itanium C++ ABI. Index: clang/lib/Basic/Targets/AMDGPU.h =================================================================== --- clang/lib/Basic/Targets/AMDGPU.h +++ clang/lib/Basic/Targets/AMDGPU.h @@ -70,6 +70,10 @@ !!(GPUFeatures & llvm::AMDGPU::FEATURE_LDEXP); } + bool hasFlatAddressSpace() const { + return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FLAT_ADDRESS_SPACE); + } + static bool isAMDGCN(const llvm::Triple &TT) { return TT.getArch() == llvm::Triple::amdgcn; } Index: clang/lib/Basic/Targets/AMDGPU.cpp =================================================================== --- clang/lib/Basic/Targets/AMDGPU.cpp +++ clang/lib/Basic/Targets/AMDGPU.cpp @@ -304,6 +304,15 @@ } MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64; + + SupportsOpenCLImages = true; + + // CL2.0 features require flat instruction support. ROCm supports CL2.0, but + // Clover does not. + const bool SupportsCL2 = hasFlatAddressSpace() && + (Triple.getOS() == llvm::Triple::AMDHSA || + Triple.getOS() == llvm::Triple::AMDPAL); + MaxOpenCLSupportedVersion = SupportsCL2 ? 200 : 120; } void AMDGPUTargetInfo::adjust(LangOptions &Opts) { Index: clang/lib/Basic/Targets/SPIR.h =================================================================== --- clang/lib/Basic/Targets/SPIR.h +++ clang/lib/Basic/Targets/SPIR.h @@ -54,6 +54,8 @@ // Define available target features // These must be defined in sorted order! NoAsmVariants = true; + SupportsOpenCLImages = true; + MaxOpenCLSupportedVersion = 200; } void getTargetDefines(const LangOptions &Opts, Index: clang/lib/Frontend/InitPreprocessor.cpp =================================================================== --- clang/lib/Frontend/InitPreprocessor.cpp +++ clang/lib/Frontend/InitPreprocessor.cpp @@ -449,6 +449,12 @@ llvm_unreachable("Unsupported OpenCL version"); } } + + if (TI.getMaxOpenCLSupportedVersion() != 0) { + Builder.defineMacro("__OPENCL_VERSION__", + Twine(TI.getMaxOpenCLSupportedVersion())); + } + Builder.defineMacro("CL_VERSION_1_0", "100"); Builder.defineMacro("CL_VERSION_1_1", "110"); Builder.defineMacro("CL_VERSION_1_2", "120"); @@ -1112,7 +1118,7 @@ Builder.defineMacro(#Ext); #include "clang/Basic/OpenCLExtensions.def" - if (TI.getTriple().isSPIR()) + if (TI.hasOpenCLImageSupport()) Builder.defineMacro("__IMAGE_SUPPORT__"); } Index: clang/test/Preprocessor/predefined-macros.c =================================================================== --- clang/test/Preprocessor/predefined-macros.c +++ clang/test/Preprocessor/predefined-macros.c @@ -174,6 +174,31 @@ // RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spir-unknown-unknown \ // RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIR // CHECK-SPIR: #define __IMAGE_SUPPORT__ 1 +// CHECK-SPIR: #define __OPENCL_VERSION__ 200{{$}} + +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-unknown \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-GFX6 +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-unknown -target-cpu gfx600 \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-GFX6 +// CHECK-AMDGCN-GFX6: #define __IMAGE_SUPPORT__ 1 +// CHECK-AMDGCN-GFX6: #define __OPENCL_VERSION__ 120{{$}} + +// No set OS or mesa3d, assume CL1.2 +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-unknown -target-cpu gfx700 \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-GFX7 +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-mesa3d -target-cpu gfx700 \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-GFX7 +// CHECK-AMDGCN-GFX7: #define __IMAGE_SUPPORT__ 1 +// CHECK-AMDGCN-GFX7: #define __OPENCL_VERSION__ 120{{$}} + +// Assume CL2.0 support for HSA and PAL +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-amdhsa -target-cpu gfx700 \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-AMDHSAPAL +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple amdgcn-unknown-amdpal -target-cpu gfx700 \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-AMDGCN-AMDHSAPAL +// CHECK-AMDGCN-AMDHSAPAL: #define __IMAGE_SUPPORT__ 1 +// CHECK-AMDGCN-AMDHSAPAL: #define __OPENCL_VERSION__ 200{{$}} + // RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \ // RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP Index: llvm/include/llvm/Support/TargetParser.h =================================================================== --- llvm/include/llvm/Support/TargetParser.h +++ llvm/include/llvm/Support/TargetParser.h @@ -154,7 +154,10 @@ FEATURE_FAST_DENORMAL_F32 = 1 << 5, // Wavefront 32 is available. - FEATURE_WAVE32 = 1 << 6 + FEATURE_WAVE32 = 1 << 6, + + // Flat instructions are available. + FEATURE_FLAT_ADDRESS_SPACE = 1 << 7, }; StringRef getArchNameAMDGCN(GPUKind AK); Index: llvm/lib/Support/TargetParser.cpp =================================================================== --- llvm/lib/Support/TargetParser.cpp +++ llvm/lib/Support/TargetParser.cpp @@ -72,36 +72,36 @@ {{"oland"}, {"gfx601"}, GK_GFX601, FEATURE_NONE}, {{"pitcairn"}, {"gfx601"}, GK_GFX601, FEATURE_NONE}, {{"verde"}, {"gfx601"}, GK_GFX601, FEATURE_NONE}, - {{"gfx700"}, {"gfx700"}, GK_GFX700, FEATURE_NONE}, - {{"kaveri"}, {"gfx700"}, GK_GFX700, FEATURE_NONE}, - {{"gfx701"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32}, - {{"hawaii"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32}, - {{"gfx702"}, {"gfx702"}, GK_GFX702, FEATURE_FAST_FMA_F32}, - {{"gfx703"}, {"gfx703"}, GK_GFX703, FEATURE_NONE}, - {{"kabini"}, {"gfx703"}, GK_GFX703, FEATURE_NONE}, - {{"mullins"}, {"gfx703"}, GK_GFX703, FEATURE_NONE}, - {{"gfx704"}, {"gfx704"}, GK_GFX704, FEATURE_NONE}, - {{"bonaire"}, {"gfx704"}, GK_GFX704, FEATURE_NONE}, - {{"gfx801"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"carrizo"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx802"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32}, - {{"iceland"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32}, - {{"tonga"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32}, - {{"gfx803"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32}, - {{"fiji"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32}, - {{"polaris10"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32}, - {{"polaris11"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32}, - {{"gfx810"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32}, - {{"stoney"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32}, - {{"gfx900"}, {"gfx900"}, GK_GFX900, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx902"}, {"gfx902"}, GK_GFX902, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx904"}, {"gfx904"}, GK_GFX904, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx906"}, {"gfx906"}, GK_GFX906, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx908"}, {"gfx908"}, GK_GFX908, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx909"}, {"gfx909"}, GK_GFX909, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32}, - {{"gfx1010"}, {"gfx1010"}, GK_GFX1010, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32}, - {{"gfx1011"}, {"gfx1011"}, GK_GFX1011, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32}, - {{"gfx1012"}, {"gfx1012"}, GK_GFX1012, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32}, + {{"gfx700"}, {"gfx700"}, GK_GFX700, FEATURE_FLAT_ADDRESS_SPACE}, + {{"kaveri"}, {"gfx700"}, GK_GFX700, FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx701"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"hawaii"}, {"gfx701"}, GK_GFX701, FEATURE_FAST_FMA_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx702"}, {"gfx702"}, GK_GFX702, FEATURE_FAST_FMA_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx703"}, {"gfx703"}, GK_GFX703, FEATURE_FLAT_ADDRESS_SPACE}, + {{"kabini"}, {"gfx703"}, GK_GFX703, FEATURE_FLAT_ADDRESS_SPACE}, + {{"mullins"}, {"gfx703"}, GK_GFX703, FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx704"}, {"gfx704"}, GK_GFX704, FEATURE_FLAT_ADDRESS_SPACE}, + {{"bonaire"}, {"gfx704"}, GK_GFX704, FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx801"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"carrizo"}, {"gfx801"}, GK_GFX801, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx802"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"iceland"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"tonga"}, {"gfx802"}, GK_GFX802, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx803"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"fiji"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"polaris10"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"polaris11"}, {"gfx803"}, GK_GFX803, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx810"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"stoney"}, {"gfx810"}, GK_GFX810, FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx900"}, {"gfx900"}, GK_GFX900, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx902"}, {"gfx902"}, GK_GFX902, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx904"}, {"gfx904"}, GK_GFX904, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx906"}, {"gfx906"}, GK_GFX906, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx908"}, {"gfx908"}, GK_GFX908, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx909"}, {"gfx909"}, GK_GFX909, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx1010"}, {"gfx1010"}, GK_GFX1010, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx1011"}, {"gfx1011"}, GK_GFX1011, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_FLAT_ADDRESS_SPACE}, + {{"gfx1012"}, {"gfx1012"}, GK_GFX1012, FEATURE_FAST_FMA_F32|FEATURE_FAST_DENORMAL_F32|FEATURE_WAVE32|FEATURE_FLAT_ADDRESS_SPACE}, }; const GPUInfo *getArchEntry(AMDGPU::GPUKind AK, ArrayRef Table) {