[clang] [clang][AMDGPU] Disable OpenCL images on targets without image-insts (PR #242675)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 14 09:57:06 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Daniil Fukalov (dfukalov)

<details>
<summary>Changes</summary>

Clang unconditionally enabled OpenCL image options on AMDGPU and only checked __opencl_c_images in OpenCL C 3.0+, accepting image and sampler types on AMDGCN targets without image instructions (such as gfx942, gfx950, and gfx1250).

Add TargetInfo::hasOpenCLImageSupport() to gate those options and reject image and sampler types in Sema when unsupported, while keeping them enabled for R600 and CPU-less AMDGCN builds.

Fixes: SC1-7154
Assisted-by: GitHub Copilot

---
Full diff: https://github.com/llvm/llvm-project/pull/242675.diff


5 Files Affected:

- (modified) clang/include/clang/Basic/TargetInfo.h (+3) 
- (modified) clang/lib/Basic/Targets/AMDGPU.h (+13-7) 
- (modified) clang/lib/Sema/SemaType.cpp (+6) 
- (added) clang/test/Preprocessor/amdgpu-opencl-image-macros.cl (+31) 
- (added) clang/test/SemaOpenCL/amdgpu-image-support.cl (+52) 


``````````diff
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index a6523e8eaaa437..93ae707e58ba50 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1973,6 +1973,9 @@ class TargetInfo : public TransferrableTargetInfo,
   /// Whether to support HIP image/texture API's.
   virtual bool hasHIPImageSupport() const { return true; }
 
+  /// Whether to support OpenCL image and sampler types and features.
+  virtual bool hasOpenCLImageSupport() const { return true; }
+
   /// The first value in the pair is the minimum offset between two objects to
   /// avoid false sharing (destructive interference). The second value in the
   /// pair is maximum size of contiguous memory to promote true sharing
diff --git a/clang/lib/Basic/Targets/AMDGPU.h b/clang/lib/Basic/Targets/AMDGPU.h
index bf4ad80b291ec2..6e413b74c8fe90 100644
--- a/clang/lib/Basic/Targets/AMDGPU.h
+++ b/clang/lib/Basic/Targets/AMDGPU.h
@@ -316,17 +316,18 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
       Opts["cl_khr_fp16"] = true;
       Opts["cl_khr_int64_base_atomics"] = true;
       Opts["cl_khr_int64_extended_atomics"] = true;
-      Opts["cl_khr_mipmap_image"] = true;
-      Opts["cl_khr_mipmap_image_writes"] = true;
       Opts["cl_khr_subgroups"] = true;
       Opts["cl_amd_media_ops"] = true;
       Opts["cl_amd_media_ops2"] = true;
 
-      // FIXME: Check subtarget for image support.
-      Opts["__opencl_c_images"] = true;
-      Opts["__opencl_c_3d_image_writes"] = true;
-      Opts["__opencl_c_read_write_images"] = true;
-      Opts["cl_khr_3d_image_writes"] = true;
+      if (hasOpenCLImageSupport()) {
+        Opts["cl_khr_mipmap_image"] = true;
+        Opts["cl_khr_mipmap_image_writes"] = true;
+        Opts["__opencl_c_images"] = true;
+        Opts["__opencl_c_3d_image_writes"] = true;
+        Opts["__opencl_c_read_write_images"] = true;
+        Opts["cl_khr_3d_image_writes"] = true;
+      }
       Opts["__opencl_c_program_scope_global_variables"] = true;
       Opts["__opencl_c_atomic_order_acq_rel"] = true;
       Opts["__opencl_c_atomic_order_seq_cst"] = true;
@@ -492,6 +493,11 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
 
   bool hasHIPImageSupport() const override { return HasImage; }
 
+  bool hasOpenCLImageSupport() const override {
+    return !getTriple().isAMDGCN() || GPUKind == llvm::AMDGPU::GK_NONE ||
+           HasImage;
+  }
+
   std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
     // This is imprecise as the value can vary between 64, 128 (even 256!) bytes
     // depending on the level of cache and the target architecture. We select
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e16630ce9cd844..e59fd2e1938192 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1413,6 +1413,8 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
     const auto &OpenCLOptions = S.getOpenCLOptions();
     bool IsOpenCLC30Compatible =
         S.getLangOpts().getOpenCLCompatibleVersion() >= 300;
+    // OpenCL C v1.2 s6.1.3 / v2.0 s6.1.3 - Image types are only supported if
+    // the device supports images.
     // OpenCL C v3.0 s6.3.3 - OpenCL image types require __opencl_c_images
     // support.
     // OpenCL C v3.0 s6.2.1 - OpenCL 3d image write types requires support
@@ -1426,6 +1428,10 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
       S.Diag(DS.getTypeSpecTypeLoc(), diag::err_opencl_requires_extension)
           << 0 << Result << "__opencl_c_images";
       declarator.setInvalidType();
+    } else if ((Result->isImageType() || Result->isSamplerT()) &&
+               !S.Context.getTargetInfo().hasOpenCLImageSupport()) {
+      S.Diag(DS.getTypeSpecTypeLoc(), diag::err_type_unsupported) << Result;
+      declarator.setInvalidType();
     } else if (Result->isOCLImage3dWOType() &&
                !OpenCLOptions.isSupported("cl_khr_3d_image_writes",
                                           S.getLangOpts())) {
diff --git a/clang/test/Preprocessor/amdgpu-opencl-image-macros.cl b/clang/test/Preprocessor/amdgpu-opencl-image-macros.cl
new file mode 100644
index 00000000000000..c676e5d34d3509
--- /dev/null
+++ b/clang/test/Preprocessor/amdgpu-opencl-image-macros.cl
@@ -0,0 +1,31 @@
+// Check that the OpenCL image feature/extension macros are only defined on
+// AMDGPU targets with image instructions.
+
+// RUN: %clang_cc1 -triple amdgpu-- -cl-std=CL2.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=IMAGES %s
+// RUN: %clang_cc1 -triple amdgpu9.0a-- -cl-std=CL2.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=IMAGES %s
+// RUN: %clang_cc1 -triple amdgpu11.00-- -cl-std=CL3.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefixes=IMAGES,IMAGES-CL30 %s
+// RUN: %clang_cc1 -triple amdgpu9.42-- -cl-std=CL2.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=NO-IMAGES %s
+// RUN: %clang_cc1 -triple amdgpu9.50-- -cl-std=CL2.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=NO-IMAGES %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=CL2.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=NO-IMAGES %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=CL3.0 -E -dM %s \
+// RUN:   | FileCheck --check-prefix=NO-IMAGES %s
+
+// IMAGES-DAG: #define __opencl_c_images 1
+// IMAGES-DAG: #define __opencl_c_read_write_images 1
+// IMAGES-DAG: #define cl_khr_3d_image_writes 1
+// IMAGES-DAG: #define cl_khr_mipmap_image 1
+// IMAGES-DAG: #define cl_khr_mipmap_image_writes 1
+// IMAGES-CL30-DAG: #define __opencl_c_3d_image_writes 1
+
+// NO-IMAGES-NOT: #define __opencl_c_3d_image_writes
+// NO-IMAGES-NOT: #define __opencl_c_images
+// NO-IMAGES-NOT: #define __opencl_c_read_write_images
+// NO-IMAGES-NOT: #define cl_khr_3d_image_writes
+// NO-IMAGES-NOT: #define cl_khr_mipmap_image
+// NO-IMAGES-NOT: #define cl_khr_mipmap_image_writes
diff --git a/clang/test/SemaOpenCL/amdgpu-image-support.cl b/clang/test/SemaOpenCL/amdgpu-image-support.cl
new file mode 100644
index 00000000000000..e67f949dd47f44
--- /dev/null
+++ b/clang/test/SemaOpenCL/amdgpu-image-support.cl
@@ -0,0 +1,52 @@
+// Check that OpenCL image and sampler types are only available on AMDGPU
+// targets with image instructions.
+
+// No GPU specified (device libraries), GPUs with image instructions, and R600.
+// RUN: %clang_cc1 -triple amdgpu-- -cl-std=CL2.0 -verify=img -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu9.0a-- -cl-std=CL2.0 -verify=img -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu11.00-- -cl-std=CL3.0 -verify=img -fsyntax-only %s
+// RUN: %clang_cc1 -triple r600-unknown-unknown -target-cpu cypress -cl-std=CL1.2 \
+// RUN:   -verify=img -fsyntax-only %s
+
+// GPUs without image instructions, before OpenCL C 3.0.
+// RUN: %clang_cc1 -triple amdgpu9.42-- -cl-std=CL1.2 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu9.42-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu9.50-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu9.4-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.51-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.5-- -cl-std=CL2.0 -verify=noimg -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=clc++1.0 -verify=noimg -fsyntax-only %s
+
+// GPUs without image instructions, OpenCL C 3.0 and C++ for OpenCL 2021.
+// RUN: %clang_cc1 -triple amdgpu9.42-- -cl-std=CL3.0 -verify=nofeature -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=CL3.0 -verify=nofeature -fsyntax-only %s
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=clc++2021 -verify=nofeature -fsyntax-only %s
+
+// Forcing the feature on does not make the types available.
+// RUN: %clang_cc1 -triple amdgpu12.50-- -cl-std=CL3.0 -cl-ext=+__opencl_c_images \
+// RUN:   -verify=noimg -fsyntax-only %s
+
+// img-no-diagnostics
+
+void test_ro(read_only image2d_t img);
+// noimg-error at -1 {{'__read_only image2d_t' is not supported on this target}}
+// nofeature-error at -2 {{use of type '__read_only image2d_t' requires __opencl_c_images support}}
+
+void test_wo(write_only image2d_t img);
+// noimg-error at -1 {{'__write_only image2d_t' is not supported on this target}}
+// nofeature-error at -2 {{use of type '__write_only image2d_t' requires __opencl_c_images support}}
+
+void test_sampler(sampler_t s);
+// noimg-error at -1 {{'sampler_t' is not supported on this target}}
+// nofeature-error at -2 {{use of type 'sampler_t' requires __opencl_c_images support}}
+
+#if defined(__OPENCL_CPP_VERSION__) || (__OPENCL_C_VERSION__ >= 200)
+void test_rw(read_write image2d_t img);
+// noimg-error at -1 {{'__read_write image2d_t' is not supported on this target}}
+// nofeature-error at -2 {{use of type '__read_write image2d_t' requires __opencl_c_images support}}
+
+void test_3d_wo(write_only image3d_t img);
+// noimg-error at -1 {{'__write_only image3d_t' is not supported on this target}}
+// nofeature-error at -2 {{use of type '__write_only image3d_t' requires __opencl_c_images support}}
+#endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/242675


More information about the cfe-commits mailing list