Skip to content

Commit 652417b

Browse files
[SYCL] Set preprocessor macros in integration header (#4823)
Predefines which need to be set for custom host compilation must be defined in integration header. Signed-off-by: Elizabeth Andrews <[email protected]>
1 parent 62d0abb commit 652417b

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

clang/include/clang/Basic/Version.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#ifndef LLVM_CLANG_BASIC_VERSION_H
1616
#define LLVM_CLANG_BASIC_VERSION_H
1717

18+
#include "clang/Basic/LangOptions.h"
1819
#include "clang/Basic/Version.inc"
20+
#include "llvm/ADT/SmallVector.h"
1921
#include "llvm/ADT/StringRef.h"
2022

2123
namespace clang {
@@ -56,6 +58,11 @@ namespace clang {
5658
/// for use in the CPP __VERSION__ macro, which includes the clang version
5759
/// number, the repository version, and the vendor tag.
5860
std::string getClangFullCPPVersion();
61+
62+
/// Retrieves a string representing the SYCL standard version for use in
63+
/// the CL_SYCL_LANGUAGE_VERSION and SYCL_LANGUAGE_VERSION macros.
64+
llvm::SmallVector<std::pair<llvm::StringRef, llvm::StringRef>, 2>
65+
getSYCLVersionMacros(const LangOptions &LangOpts);
5966
}
6067

6168
#endif // LLVM_CLANG_BASIC_VERSION_H

clang/lib/Basic/Version.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,13 @@ std::string getClangFullCPPVersion() {
123123
return buf;
124124
}
125125

126+
llvm::SmallVector<std::pair<llvm::StringRef, llvm::StringRef>, 2>
127+
getSYCLVersionMacros(const LangOptions &LangOpts) {
128+
if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017)
129+
return {{"CL_SYCL_LANGUAGE_VERSION", "121"},
130+
{"SYCL_LANGUAGE_VERSION", "201707"}};
131+
if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020)
132+
return {{"SYCL_LANGUAGE_VERSION", "202001"}};
133+
llvm_unreachable("SYCL standard should be set");
134+
}
126135
} // end namespace clang

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,9 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
491491

492492
if (LangOpts.SYCLIsDevice || LangOpts.SYCLIsHost) {
493493
// SYCL Version is set to a value when building SYCL applications
494-
if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017) {
495-
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
496-
Builder.defineMacro("SYCL_LANGUAGE_VERSION", "201707");
497-
} else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020)
498-
Builder.defineMacro("SYCL_LANGUAGE_VERSION", "202001");
494+
for (const std::pair<StringRef, StringRef> &Macro :
495+
getSYCLVersionMacros(LangOpts))
496+
Builder.defineMacro(Macro.first, Macro.second);
499497

500498
if (LangOpts.SYCLValueFitInMaxInt)
501499
Builder.defineMacro("__SYCL_ID_QUERIES_FIT_IN_INT__");

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/Basic/Attributes.h"
2121
#include "clang/Basic/Builtins.h"
2222
#include "clang/Basic/Diagnostic.h"
23+
#include "clang/Basic/Version.h"
2324
#include "clang/Sema/Initialization.h"
2425
#include "clang/Sema/Sema.h"
2526
#include "llvm/ADT/APSInt.h"
@@ -4594,6 +4595,21 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
45944595
Policy.SuppressUnwrittenScope = true;
45954596
SYCLFwdDeclEmitter FwdDeclEmitter(O, S.getLangOpts());
45964597

4598+
// Predefines which need to be set for custom host compilation
4599+
// must be defined in integration header.
4600+
for (const std::pair<StringRef, StringRef> &Macro :
4601+
getSYCLVersionMacros(S.getLangOpts())) {
4602+
O << "#ifndef " << Macro.first << '\n';
4603+
O << "#define " << Macro.first << " " << Macro.second << '\n';
4604+
O << "#endif //" << Macro.first << "\n\n";
4605+
}
4606+
4607+
if (S.getLangOpts().SYCLDisableRangeRounding) {
4608+
O << "#ifndef __SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ \n";
4609+
O << "#define __SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ 1\n";
4610+
O << "#endif //__SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__\n\n";
4611+
}
4612+
45974613
if (SpecConsts.size() > 0) {
45984614
O << "// Forward declarations of templated spec constant types:\n";
45994615
for (const auto &SC : SpecConsts)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -sycl-std=2020 -fsycl-int-header=%t.h %s
2+
// RUN: FileCheck -input-file=%t.h %s --check-prefix=CHECK-SYCL2020
3+
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -sycl-std=2017 -fsycl-int-header=%t.h %s
4+
// RUN: FileCheck -input-file=%t.h %s --check-prefix=CHECK-SYCL2017
5+
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -fsycl-disable-range-rounding -fsycl-int-header=%t.h %s
6+
// RUN: FileCheck -input-file=%t.h %s --check-prefix=CHECK-RANGE
7+
// RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -fsycl-int-header=%t.h %s
8+
// RUN: FileCheck -input-file=%t.h %s --check-prefix=CHECK-NO-RANGE
9+
10+
// Test verifying predefines which need to be set for host and device compilation.
11+
// Preprocessor macros which are required when using custom host compiler must be
12+
// defined in integration header.
13+
14+
#include "Inputs/sycl.hpp"
15+
16+
int main() {
17+
cl::sycl::kernel_single_task<class first_kernel>([]() {});
18+
}
19+
// CHECK-SYCL2020: #ifndef SYCL_LANGUAGE_VERSION
20+
// CHECK-SYCL2020-NEXT: #define SYCL_LANGUAGE_VERSION 202001
21+
// CHECK-SYCL2020-NEXT: #endif //SYCL_LANGUAGE_VERSION
22+
// CHECK-SYCL2020-NOT: #define CL_SYCL_LANGUAGE_VERSION 121
23+
// CHECK-SYCL2020-NOT: #define SYCL_LANGUAGE_VERSION 201707
24+
25+
// CHECK-SYCL2017: #ifndef CL_SYCL_LANGUAGE_VERSION
26+
// CHECK-SYCL2017-NEXT: #define CL_SYCL_LANGUAGE_VERSION 121
27+
// CHECK-SYCL2017-NEXT: #endif //CL_SYCL_LANGUAGE_VERSION
28+
// CHECK-SYCL2017: #ifndef SYCL_LANGUAGE_VERSION
29+
// CHECK-SYCL2017-NEXT: #define SYCL_LANGUAGE_VERSION 201707
30+
// CHECK-SYCL2017-NEXT: #endif //SYCL_LANGUAGE_VERSION
31+
// CHECK-SYCL2017-NOT: #define SYCL_LANGUAGE_VERSION 202001
32+
33+
// CHECK-RANGE: #ifndef __SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__
34+
// CHECK-RANGE-NEXT: #define __SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ 1
35+
// CHECK-RANGE-NEXT: #endif //__SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__
36+
// CHECK-NO-RANGE-NOT: #define __SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING__ 1

sycl/include/CL/sycl/handler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ class __SYCL_EXPORT handler {
10621062
}
10631063

10641064
#ifdef SYCL_LANGUAGE_VERSION
1065-
#define __SYCL_KERNEL_ATTR__ __attribute__((sycl_kernel))
1065+
#define __SYCL_KERNEL_ATTR__ [[clang::sycl_kernel]]
10661066
#else
10671067
#define __SYCL_KERNEL_ATTR__
10681068
#endif

0 commit comments

Comments
 (0)