Skip to content

Commit 78c5e3f

Browse files
author
Artem Gindinson
authored
Make feature macro undefs conditional on -cl-ext input (#426)
Commits d6563b0, de262c9 and d5a2638 have provided a resolution for `opencl-c-base.h` defining OCL C 3.0 feature macros unconditionally, which interfered with Compute Runtime's platform-dependent definitions of extension lists. An issue with this approach is that `opencl-c-base.h` provides additional defines based on `__opencl_c_atomic_scope_all_devices` being set: ``` memory_scope_all_svm_devices = __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES, memory_scope_all_devices = memory_scope_all_svm_devices, ``` (from https://github.com/llvm/llvm-project/blob/release/16.x/clang/lib/Headers/opencl-c-base.h#L387-L392) Simply erasing the macro definition from `opencl-c-base.h` leads to memory scope values' erasure, as the command line definitions aren't available at that moment due to headers' inclusion order. Original commit d6563b0 did mention an alternative approach, which accounts for the above issue: > Once https://reviews.llvm.org/D141297 (LLVM 16) is integrated, an alternative approach could be to pass `-D__undef_<feature_macro>` <...> at the OCL Clang's level (upon detecting the lack of extension-defining inputs to `-cl-ext`). Implement just that in scope of the current commit. Said LLORG patch simply needs to be applied for CClang 14-15 instead of the original "unconditional undef" one. Signed-off-by: Artem Gindinson <[email protected]>
1 parent d5a2638 commit 78c5e3f

File tree

3 files changed

+124
-36
lines changed

3 files changed

+124
-36
lines changed

options_compile.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Copyright (c) Intel Corporation (2009-2017).
2020
#include "options.h"
2121

2222
#include "clang/Driver/Options.h"
23+
#include "llvm/ADT/SmallSet.h"
2324
#include "llvm/Option/Arg.h"
2425
#include "llvm/Option/ArgList.h"
2526
#include "llvm/Support/ManagedStatic.h"
@@ -277,11 +278,30 @@ std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,
277278
it->second = enabled;
278279
}
279280
};
281+
llvm::SmallSet<llvm::StringRef, 32> parsedOclCFeatures;
280282
std::for_each(effectiveArgs.begin(), effectiveArgs.end(),
281283
[&](const ArgsVector::value_type &a) {
282284
if (a.find("-cl-ext=") == 0)
283285
parseClExt(a);
286+
else if (a.find("-D__opencl_c_") == 0)
287+
parsedOclCFeatures.insert(a);
284288
});
289+
290+
// "opencl-c-base.h" unconditionally enables a list of so-called "optional
291+
// core" language features. We need to undef those that aren't explicitly
292+
// defined within the compilation command (which would suggest that the
293+
// target platform supports the corresponding feature).
294+
const char* optionalCoreOclCFeaturesList[] = {
295+
"__opencl_c_work_group_collective_functions",
296+
"__opencl_c_atomic_order_seq_cst",
297+
"__opencl_c_atomic_scope_device",
298+
"__opencl_c_atomic_scope_all_devices",
299+
"__opencl_c_read_write_images" };
300+
for (std::string OclCFeature : optionalCoreOclCFeaturesList) {
301+
if (!parsedOclCFeatures.contains(std::string("-D") + OclCFeature))
302+
effectiveArgs.push_back(std::string("-D__undef_") + OclCFeature);
303+
}
304+
285305
// extension is enabled in PCH but disabled or not specifed in options =>
286306
// disable pch
287307
bool useModules =

patches/clang/0004-Do-not-define-CL30-support-macros-in-OpenCL-C-base.patch

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)