Index: clang/include/clang/Basic/OpenCLExtensions.def =================================================================== --- clang/include/clang/Basic/OpenCLExtensions.def +++ clang/include/clang/Basic/OpenCLExtensions.def @@ -58,7 +58,6 @@ OPENCLEXT_INTERNAL(cles_khr_int64, 110, ~0U) // OpenCL 1.2. -OPENCLEXT_INTERNAL(cl_khr_depth_images, 120, ~0U) OPENCLEXT_INTERNAL(cl_khr_gl_msaa_sharing, 120, ~0U) // OpenCL 2.0. Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -829,6 +829,9 @@ virtual void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const = 0; + /// Appends the \#define for supported extensions. + virtual void getOpenCLExtensionDefines(const LangOptions &Opts, + MacroBuilder &Builder) const; /// Return information about target-specific builtins for /// the current primary target, and info about which builtins are non-portable Index: clang/lib/Basic/Targets.cpp =================================================================== --- clang/lib/Basic/Targets.cpp +++ clang/lib/Basic/Targets.cpp @@ -705,3 +705,21 @@ return Target.release(); } + +void TargetInfo::getOpenCLExtensionDefines(const LangOptions &Opts, + MacroBuilder &Builder) const { + // Add macros for extension that this target supports. + auto defineExtMacro = [&](llvm::StringRef Ext) { + if (getSupportedOpenCLOpts().isSupported(Ext, Opts)) + Builder.defineMacro(Ext); + }; + +#define OPENCLEXT(Ext) defineExtMacro(#Ext); +#include "clang/Basic/OpenCLExtensions.def" + + // Here we could define macros that are available unconditionally. + // Ideally only extensions that are implemented in the frontend or + // macros that can't be added in the implicit headers should be + // added here. + Builder.defineMacro("__opencl_c_int64"); +} Index: clang/lib/Frontend/InitPreprocessor.cpp =================================================================== --- clang/lib/Frontend/InitPreprocessor.cpp +++ clang/lib/Frontend/InitPreprocessor.cpp @@ -1111,10 +1111,8 @@ // OpenCL definitions. if (LangOpts.OpenCL) { -#define OPENCLEXT(Ext) \ - if (TI.getSupportedOpenCLOpts().isSupported(#Ext, LangOpts)) \ - Builder.defineMacro(#Ext); -#include "clang/Basic/OpenCLExtensions.def" + // Add OpenCL #defines for supported extensions. + TI.getOpenCLExtensionDefines(LangOpts, Builder); if (TI.getTriple().isSPIR()) Builder.defineMacro("__IMAGE_SUPPORT__"); Index: clang/lib/Headers/opencl-c.h =================================================================== --- clang/lib/Headers/opencl-c.h +++ clang/lib/Headers/opencl-c.h @@ -11,7 +11,8 @@ #include "opencl-c-base.h" -#if defined(__OPENCL_CPP_VERSION__) || (__OPENCL_C_VERSION__ >= CL_VERSION_2_0) +#if defined(__OPENCL_CPP_VERSION__) || (__OPENCL_C_VERSION__ == CL_VERSION_2_0) || \ + (__OPENCL_C_VERSION__ >= CL_VERSION_1_2 && defined(__SPIR__) && !defined(__undef_cl_khr_depth_images)) #ifndef cl_khr_depth_images #define cl_khr_depth_images #endif //cl_khr_depth_images Index: clang/test/Headers/opencl-c-header.cl =================================================================== --- clang/test/Headers/opencl-c-header.cl +++ clang/test/Headers/opencl-c-header.cl @@ -95,3 +95,16 @@ #pragma OPENCL EXTENSION cl_intel_planar_yuv : enable // CHECK-MOD: Reading modules + +// Check predefined extension macros + +#if defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ == 200 || \ + (__OPENCL_C_VERSION__ >= 120 && defined(__SPIR__) && !defined(__undef_cl_khr_depth_images)) +#ifndef cl_khr_depth_images +#error "Missing cl_khr_depth_images define" +#endif +#else +#ifdef cl_khr_depth_images +#error "Incorrect cl_khr_depth_images define" +#endif +#endif Index: clang/test/SemaOpenCL/extension-version.cl =================================================================== --- clang/test/SemaOpenCL/extension-version.cl +++ clang/test/SemaOpenCL/extension-version.cl @@ -9,11 +9,11 @@ // RUN: %clang_cc1 -x cl -cl-std=CL2.0 %s -verify -triple spir-unknown-unknown -Wpedantic-core-features -DTEST_CORE_FEATURES // RUN: %clang_cc1 -x cl -cl-std=clc++ %s -verify -triple spir-unknown-unknown -Wpedantic-core-features -DTEST_CORE_FEATURES -#if (defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= 200) && !defined(TEST_CORE_FEATURES) -// expected-no-diagnostics +// Extensions in all versions +#ifndef __opencl_c_int64 +#error "Missing __opencl_c_int64 predefined macro" #endif -// Extensions in all versions #ifndef cl_clang_storage_class_specifiers #error "Missing cl_clang_storage_class_specifiers define" #endif @@ -167,17 +167,14 @@ #endif #pragma OPENCL EXTENSION cl_amd_media_ops2 : enable -#if (defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= 120) -#ifndef cl_khr_depth_images -#error "Missing cl_khr_depth_images define" -#endif -#else -#ifdef cl_khr_depth_images -#error "Incorrect cl_khr_depth_images define" -#endif -// expected-warning@+2{{unsupported OpenCL extension 'cl_khr_depth_images' - ignoring}} -#endif +// FIXME: This pragma is meaningless and it doesn't add any functionality. +// However we will probably need to keep it for backward compatibility. +// In which case there is no need to guard its use by any version or a target +// setting as it does absolutely nothing anyway. +// The warning diagnostic should change however to something like - +// - pragma has no effect. #pragma OPENCL EXTENSION cl_khr_depth_images : enable +// expected-warning@-1{{unknown OpenCL extension 'cl_khr_depth_images' - ignoring}} #if (defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= 120) #ifndef cl_intel_subgroups