Skip to content

Commit a60b8f4

Browse files
committed
[OpenCL] Allow undefining header-only features
`opencl-c-base.h` always defines 5 particular feature macros for SPIR-V, making it impossible to disable those features. To allow disabling any of those features, let the header recognize `__undef_<feature>` macros. The user can then pass the `-D__undef_<feature>` flag on the command line to disable a specific feature. The __undef macro could potentially also be set from `-cl-ext=-feature`, but for now only change the header and only provide __undef macros for the 5 features that are always enabled in `opencl-c-base.h`. Differential Revision: https://reviews.llvm.org/D141297
1 parent 15ad244 commit a60b8f4

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

clang/lib/Headers/opencl-c-base.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@
7474
#define __opencl_c_atomic_scope_all_devices 1
7575
#define __opencl_c_read_write_images 1
7676
#endif // defined(__SPIR__)
77+
78+
// Undefine any feature macros that have been explicitly disabled using
79+
// an __undef_<feature> macro.
80+
#ifdef __undef___opencl_c_work_group_collective_functions
81+
#undef __opencl_c_work_group_collective_functions
82+
#endif
83+
#ifdef __undef___opencl_c_atomic_order_seq_cst
84+
#undef __opencl_c_atomic_order_seq_cst
85+
#endif
86+
#ifdef __undef___opencl_c_atomic_scope_device
87+
#undef __opencl_c_atomic_scope_device
88+
#endif
89+
#ifdef __undef___opencl_c_atomic_scope_all_devices
90+
#undef __opencl_c_atomic_scope_all_devices
91+
#endif
92+
#ifdef __undef___opencl_c_read_write_images
93+
#undef __opencl_c_read_write_images
94+
#endif
95+
7796
#endif // (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
7897

7998
#if !defined(__opencl_c_generic_address_space)

clang/test/SemaOpenCL/features.cl

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
// RUN: %clang_cc1 -triple spir-unknown-unknown %s -E -dM -o - -x cl -cl-std=clc++1.0 \
2727
// RUN: | FileCheck -match-full-lines %s --check-prefix=NO-FEATURES
2828

29+
// For OpenCL C 3.0, header-only features can be disabled using macros.
30+
// RUN: %clang_cc1 -triple spir-unknown-unknown %s -E -dM -o - -x cl -cl-std=CL3.0 -fdeclare-opencl-builtins -finclude-default-header \
31+
// RUN: -D__undef___opencl_c_work_group_collective_functions=1 \
32+
// RUN: -D__undef___opencl_c_atomic_order_seq_cst=1 \
33+
// RUN: -D__undef___opencl_c_atomic_scope_device=1 \
34+
// RUN: -D__undef___opencl_c_atomic_scope_all_devices=1 \
35+
// RUN: -D__undef___opencl_c_read_write_images=1 \
36+
// RUN: | FileCheck %s --check-prefix=NO-HEADERONLY-FEATURES
37+
2938
// Note that __opencl_c_int64 is always defined assuming
3039
// always compiling for FULL OpenCL profile
3140

@@ -43,14 +52,20 @@
4352
// FEATURES: #define __opencl_c_subgroups 1
4453

4554
// NO-FEATURES: #define __opencl_c_int64 1
46-
// NO-FEATURES-NOT: __opencl_c_3d_image_writes
47-
// NO-FEATURES-NOT: __opencl_c_atomic_order_acq_rel
48-
// NO-FEATURES-NOT: __opencl_c_atomic_order_seq_cst
49-
// NO-FEATURES-NOT: __opencl_c_device_enqueue
50-
// NO-FEATURES-NOT: __opencl_c_fp64
51-
// NO-FEATURES-NOT: __opencl_c_generic_address_space
52-
// NO-FEATURES-NOT: __opencl_c_images
53-
// NO-FEATURES-NOT: __opencl_c_pipes
54-
// NO-FEATURES-NOT: __opencl_c_program_scope_global_variables
55-
// NO-FEATURES-NOT: __opencl_c_read_write_images
56-
// NO-FEATURES-NOT: __opencl_c_subgroups
55+
// NO-FEATURES-NOT: #define __opencl_c_3d_image_writes
56+
// NO-FEATURES-NOT: #define __opencl_c_atomic_order_acq_rel
57+
// NO-FEATURES-NOT: #define __opencl_c_atomic_order_seq_cst
58+
// NO-FEATURES-NOT: #define __opencl_c_device_enqueue
59+
// NO-FEATURES-NOT: #define __opencl_c_fp64
60+
// NO-FEATURES-NOT: #define __opencl_c_generic_address_space
61+
// NO-FEATURES-NOT: #define __opencl_c_images
62+
// NO-FEATURES-NOT: #define __opencl_c_pipes
63+
// NO-FEATURES-NOT: #define __opencl_c_program_scope_global_variables
64+
// NO-FEATURES-NOT: #define __opencl_c_read_write_images
65+
// NO-FEATURES-NOT: #define __opencl_c_subgroups
66+
67+
// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_work_group_collective_functions
68+
// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_order_seq_cst
69+
// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_scope_device
70+
// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_atomic_scope_all_devices
71+
// NO-HEADERONLY-FEATURES-NOT: #define __opencl_c_read_write_images

0 commit comments

Comments
 (0)