|
| 1 | +From a60b8f468119065f8a6cb4a16598263cb00de0b5 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Sven van Haastregt < [email protected]> |
| 3 | +Date: Mon, 16 Jan 2023 11:32:12 +0000 |
| 4 | +Subject: [PATCH] [OpenCL] Allow undefining header-only features |
| 5 | + |
| 6 | +`opencl-c-base.h` always defines 5 particular feature macros for |
| 7 | +SPIR-V, making it impossible to disable those features. |
| 8 | + |
| 9 | +To allow disabling any of those features, let the header recognize |
| 10 | +`__undef_<feature>` macros. The user can then pass the |
| 11 | +`-D__undef_<feature>` flag on the command line to disable a specific |
| 12 | +feature. The __undef macro could potentially also be set from |
| 13 | +`-cl-ext=-feature`, but for now only change the header and only |
| 14 | +provide __undef macros for the 5 features that are always enabled in |
| 15 | +`opencl-c-base.h`. |
| 16 | + |
| 17 | +Differential Revision: https://reviews.llvm.org/D141297 |
| 18 | +--- |
| 19 | + clang/lib/Headers/opencl-c-base.h | 19 ++++++++++++++++ |
| 20 | + clang/test/SemaOpenCL/features.cl | 37 ++++++++++++++++++++++--------- |
| 21 | + 2 files changed, 45 insertions(+), 11 deletions(-) |
| 22 | + |
| 23 | +diff --git a/clang/lib/Headers/opencl-c-base.h b/clang/lib/Headers/opencl-c-base.h |
| 24 | +index c433b4f7eb1af..fad2f9c0272bf 100644 |
| 25 | +--- a/clang/lib/Headers/opencl-c-base.h |
| 26 | ++++ b/clang/lib/Headers/opencl-c-base.h |
| 27 | +@@ -74,6 +74,25 @@ |
| 28 | + #define __opencl_c_atomic_scope_all_devices 1 |
| 29 | + #define __opencl_c_read_write_images 1 |
| 30 | + #endif // defined(__SPIR__) |
| 31 | ++ |
| 32 | ++// Undefine any feature macros that have been explicitly disabled using |
| 33 | ++// an __undef_<feature> macro. |
| 34 | ++#ifdef __undef___opencl_c_work_group_collective_functions |
| 35 | ++#undef __opencl_c_work_group_collective_functions |
| 36 | ++#endif |
| 37 | ++#ifdef __undef___opencl_c_atomic_order_seq_cst |
| 38 | ++#undef __opencl_c_atomic_order_seq_cst |
| 39 | ++#endif |
| 40 | ++#ifdef __undef___opencl_c_atomic_scope_device |
| 41 | ++#undef __opencl_c_atomic_scope_device |
| 42 | ++#endif |
| 43 | ++#ifdef __undef___opencl_c_atomic_scope_all_devices |
| 44 | ++#undef __opencl_c_atomic_scope_all_devices |
| 45 | ++#endif |
| 46 | ++#ifdef __undef___opencl_c_read_write_images |
| 47 | ++#undef __opencl_c_read_write_images |
| 48 | ++#endif |
| 49 | ++ |
| 50 | + #endif // (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300) |
| 51 | + |
| 52 | + #if !defined(__opencl_c_generic_address_space) |
| 53 | +diff --git a/clang/test/SemaOpenCL/features.cl b/clang/test/SemaOpenCL/features.cl |
| 54 | +index af058b5e69828..3f59b4ea3b5ae 100644 |
| 55 | +--- a/clang/test/SemaOpenCL/features.cl |
| 56 | ++++ b/clang/test/SemaOpenCL/features.cl |
| 57 | +@@ -26,6 +26,15 @@ |
| 58 | + // RUN: %clang_cc1 -triple spir-unknown-unknown %s -E -dM -o - -x cl -cl-std=clc++1.0 \ |
| 59 | + // RUN: | FileCheck -match-full-lines %s --check-prefix=NO-FEATURES |
| 60 | + |
| 61 | ++// For OpenCL C 3.0, header-only features can be disabled using macros. |
| 62 | ++// RUN: %clang_cc1 -triple spir-unknown-unknown %s -E -dM -o - -x cl -cl-std=CL3.0 -fdeclare-opencl-builtins -finclude-default-header \ |
| 63 | ++// RUN: -D__undef___opencl_c_work_group_collective_functions=1 \ |
| 64 | ++// RUN: -D__undef___opencl_c_atomic_order_seq_cst=1 \ |
| 65 | ++// RUN: -D__undef___opencl_c_atomic_scope_device=1 \ |
| 66 | ++// RUN: -D__undef___opencl_c_atomic_scope_all_devices=1 \ |
| 67 | ++// RUN: -D__undef___opencl_c_read_write_images=1 \ |
| 68 | ++// RUN: | FileCheck %s --check-prefix=NO-HEADERONLY-FEATURES |
| 69 | ++ |
| 70 | + // Note that __opencl_c_int64 is always defined assuming |
| 71 | + // always compiling for FULL OpenCL profile |
| 72 | + |
| 73 | +@@ -43,14 +52,20 @@ |
| 74 | + // FEATURES: #define __opencl_c_subgroups 1 |
| 75 | + |
| 76 | + // NO-FEATURES: #define __opencl_c_int64 1 |
| 77 | +-// NO-FEATURES-NOT: __opencl_c_3d_image_writes |
| 78 | +-// NO-FEATURES-NOT: __opencl_c_atomic_order_acq_rel |
| 79 | +-// NO-FEATURES-NOT: __opencl_c_atomic_order_seq_cst |
| 80 | +-// NO-FEATURES-NOT: __opencl_c_device_enqueue |
| 81 | +-// NO-FEATURES-NOT: __opencl_c_fp64 |
| 82 | +-// NO-FEATURES-NOT: __opencl_c_generic_address_space |
| 83 | +-// NO-FEATURES-NOT: __opencl_c_images |
| 84 | +-// NO-FEATURES-NOT: __opencl_c_pipes |
| 85 | +-// NO-FEATURES-NOT: __opencl_c_program_scope_global_variables |
| 86 | +-// NO-FEATURES-NOT: __opencl_c_read_write_images |
| 87 | +-// NO-FEATURES-NOT: __opencl_c_subgroups |
| 88 | ++// NO-FEATURES-NOT: #define __opencl_c_3d_image_writes |
| 89 | ++// NO-FEATURES-NOT: #define __opencl_c_atomic_order_acq_rel |
| 90 | ++// NO-FEATURES-NOT: #define __opencl_c_atomic_order_seq_cst |
| 91 | ++// NO-FEATURES-NOT: #define __opencl_c_device_enqueue |
| 92 | ++// NO-FEATURES-NOT: #define __opencl_c_fp64 |
| 93 | ++// NO-FEATURES-NOT: #define __opencl_c_generic_address_space |
| 94 | ++// NO-FEATURES-NOT: #define __opencl_c_images |
| 95 | ++// NO-FEATURES-NOT: #define __opencl_c_pipes |
| 96 | ++// NO-FEATURES-NOT: #define __opencl_c_program_scope_global_variables |
| 97 | ++// NO-FEATURES-NOT: #define __opencl_c_read_write_images |
| 98 | ++// NO-FEATURES-NOT: #define __opencl_c_subgroups |
| 99 | ++ |
| 100 | ++// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_work_group_collective_functions |
| 101 | ++// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_order_seq_cst |
| 102 | ++// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_scope_device |
| 103 | ++// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_scope_all_devices |
| 104 | ++// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_read_write_images |
0 commit comments