Skip to content

Commit 3e4da3c

Browse files
authored
[SYCL] Add support for assuming SYCL queries fit in int (#2215)
For good default performance, compiler needs to set -D_SYCL_ID_QUERIES_FIT_IN_INT_. This will add __builtin_assume() that says global_id/linear_id fits within MAX_INT. We also need to have an ability to override where programmer needs 31+bit in global_id/linear_id. This is provided via -f[no-]sycl-id-queries-fit-in-int
1 parent 257658c commit 3e4da3c

File tree

10 files changed

+57
-18
lines changed

10 files changed

+57
-18
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ LANGOPT(SYCLVersion , 32, 0, "Version of the SYCL standard used")
252252
LANGOPT(DeclareSPIRVBuiltins, 1, 0, "Declare SPIR-V builtin functions")
253253
LANGOPT(SYCLExplicitSIMD , 1, 0, "SYCL compilation with explicit SIMD extension")
254254
LANGOPT(EnableDAEInSpirKernels , 1, 0, "Enable Dead Argument Elimination in SPIR kernels")
255+
LANGOPT(
256+
SYCLValueFitInMaxInt, 1, 0,
257+
"SYCL compiler assumes value fits within MAX_INT for member function of "
258+
"get/operator[], get_id/operator[] and get_global_id/get_global_linear_id "
259+
"in SYCL class id, iterm and nd_iterm")
255260

256261
LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP")
257262

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,12 @@ def fsycl_device_code_split_EQ : Joined<["-"], "fsycl-device-code-split=">,
18741874
def fsycl_device_code_split : Flag<["-"], "fsycl-device-code-split">, Alias<fsycl_device_code_split_EQ>,
18751875
AliasArgs<["per_source"]>, Flags<[CC1Option, CoreOption]>,
18761876
HelpText<"Perform SYCL device code split in the per_source mode i.e. create a device code module for each source (translation unit)">;
1877+
def fsycl_id_queries_fit_in_int : Flag<["-"], "fsycl-id-queries-fit-in-int">,
1878+
Flags<[CC1Option, CoreOption]>, HelpText<"Assume that SYCL ID queries fit "
1879+
"within MAX_INT.">;
1880+
def fno_sycl_id_queries_fit_in_int : Flag<["-"], "fno-sycl-id-queries-fit-in-int">,
1881+
Flags<[CC1Option, CoreOption]>, HelpText<"Do not assume that SYCL ID queries "
1882+
"fit within MAX_INT.">;
18771883
def fsycl_use_bitcode : Flag<["-"], "fsycl-use-bitcode">,
18781884
Flags<[CC1Option, CoreOption]>, HelpText<"Use LLVM bitcode instead of SPIR-V in fat objects">;
18791885
def fno_sycl_use_bitcode : Flag<["-"], "fno-sycl-use-bitcode">,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,6 +4147,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
41474147
CmdArgs.push_back("-Wno-sycl-strict");
41484148
}
41494149
}
4150+
if (IsSYCL || UseSYCLTriple) {
4151+
// Set options for both host and device
4152+
if (Arg *A = Args.getLastArg(options::OPT_fsycl_id_queries_fit_in_int,
4153+
options::OPT_fno_sycl_id_queries_fit_in_int))
4154+
A->render(Args, CmdArgs);
4155+
}
41504156

41514157
if (IsSYCL) {
41524158
if (SYCLStdArg) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,9 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
25962596
}
25972597
Opts.SYCLExplicitSIMD = Args.hasArg(options::OPT_fsycl_esimd);
25982598
Opts.EnableDAEInSpirKernels = Args.hasArg(options::OPT_fenable_sycl_dae);
2599+
Opts.SYCLValueFitInMaxInt =
2600+
Args.hasFlag(options::OPT_fsycl_id_queries_fit_in_int,
2601+
options::OPT_fno_sycl_id_queries_fit_in_int, false);
25992602
}
26002603

26012604
Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
465465
// SYCL Version is set to a value when building SYCL applications
466466
if (LangOpts.SYCLVersion == 2017)
467467
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
468+
if (LangOpts.SYCLValueFitInMaxInt)
469+
Builder.defineMacro("__SYCL_ID_QUERIES_FIT_IN_INT__", "1");
468470
}
469471

470472
if (LangOpts.DeclareSPIRVBuiltins) {

clang/test/Driver/sycl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,15 @@
8080
// SYCL-HELP-FPGA: Use triple of 'spir64_fpga-unknown-unknown-sycldevice' to enable ahead of time compilation
8181
// SYCL-HELP-CPU: Emitting help information for opencl-aot
8282
// SYCL-HELP-CPU: Use triple of 'spir64_x86_64-unknown-unknown-sycldevice' to enable ahead of time compilation
83+
84+
// -fsycl-id-queries-fit-in-int
85+
// RUN: %clang -### -fsycl -fsycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=ID_QUERIES
86+
// RUN: %clang_cl -### -fsycl -fsycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=ID_QUERIES
87+
// RUN: %clang -### -fsycl-device-only -fsycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=ID_QUERIES
88+
// RUN: %clang_cl -### -fsycl-device-only -fsycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=ID_QUERIES
89+
// RUN: %clang -### -fsycl -fno-sycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=NO_ID_QUERIES
90+
// RUN: %clang_cl -### -fsycl -fno-sycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=NO_ID_QUERIES
91+
// RUN: %clang -### -fsycl-device-only -fno-sycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=NO_ID_QUERIES
92+
// RUN: %clang_cl -### -fsycl-device-only -fno-sycl-id-queries-fit-in-int %s 2>&1 | FileCheck %s --check-prefix=NO_ID_QUERIES
93+
// ID_QUERIES: "-fsycl-id-queries-fit-in-int"
94+
// NO_ID_QUERIES: "-fno-sycl-id-queries-fit-in-int"
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
// RUN: %clang_cc1 %s -E -dM | FileCheck %s
2-
// RUN: %clang_cc1 %s -fsycl -fsycl-is-host -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
3-
// RUN: %clang_cc1 %s -fsycl -fsycl-is-device -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
4-
// RUN: %clang_cc1 %s -fsycl -fsycl-is-device -sycl-std=1.2.1 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
5-
// RUNx: %clang_cc1 %s -fsycl -fsycl-is-device -E -dM -fms-compatibility | FileCheck --check-prefix=CHECK-MSVC %s
2+
// RUN: %clang_cc1 %s -fsycl -fsycl-id-queries-fit-in-int -fsycl-is-host -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
3+
// RUN: %clang_cc1 %s -fsycl -fsycl-id-queries-fit-in-int -fsycl-is-device -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
4+
// RUN: %clang_cc1 %s -fsycl -fsycl-id-queries-fit-in-int -fsycl-is-device -sycl-std=1.2.1 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD-DEVICE %s
5+
// RUNx: %clang_cc1 %s -fsycl -fsycl-id-queries-fit-in-int -fsycl-is-device -E -dM -fms-compatibility | FileCheck --check-prefix=CHECK-MSVC %s
6+
// RUN: %clang_cc1 -fno-sycl-id-queries-fit-in-int %s -E -dM | FileCheck \
7+
// RUN: --check-prefix=CHECK-NO-SYCL_FIT_IN_INT %s
68

79
// CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1
810
// CHECK-NOT:#define SYCL_EXTERNAL
911
// CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121
12+
// CHECK-NOT:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
1013

1114
// CHECK-SYCL-STD:#define CL_SYCL_LANGUAGE_VERSION 121
15+
// CHECK-SYCL-STD:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
1216

13-
// CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1
14-
// CHECK-SYCL:#define SYCL_EXTERNAL __attribute__((sycl_device))
17+
// CHECK-SYCL-STD-DEVICE:#define __SYCL_DEVICE_ONLY__ 1
18+
// CHECK-SYCL-STD-DEVICE:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
1519

1620
// CHECK-MSVC-NOT: __GNUC__
1721
// CHECK-MSVC-NOT: __STDC__
22+
// CHECK-MSVC: #define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
23+
24+
// CHECK-NO-SYCL_FIT_IN_INT-NOT:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1

sycl/include/CL/sycl/handler.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,16 @@ __SYCL_EXPORT device getDeviceFromHandler(handler &);
139139
#if defined(__SYCL_ID_QUERIES_FIT_IN_INT__)
140140
template <typename T> struct NotIntMsg;
141141

142-
// TODO reword for "`fsycl-id-queries-fit-in-int' optimization flag." when
143-
// implemented
144142
template <int Dims> struct NotIntMsg<range<Dims>> {
145-
constexpr static char *Msg = "Provided range is out of integer limits. "
146-
"Pass `-U__SYCL_ID_QUERIES_FIT_IN_INT__' to "
147-
"disable range check.";
143+
constexpr static const char *Msg =
144+
"Provided range is out of integer limits. Pass "
145+
"`-fno-sycl-id-queries-fit-in-int' to disable range check.";
148146
};
149147

150148
template <int Dims> struct NotIntMsg<id<Dims>> {
151-
constexpr static char *Msg = "Provided offset is out of integer limits. "
152-
"Pass `-U__SYCL_ID_QUERIES_FIT_IN_INT__' to "
153-
"disable offset check.";
149+
constexpr static const char *Msg =
150+
"Provided offset is out of integer limits. Pass "
151+
"`-fno-sycl-id-queries-fit-in-int' to disable offset check.";
154152
};
155153
#endif
156154

sycl/test/basic_tests/range_offset_fit_in_int.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -D__SYCL_ID_QUERIES_FIT_IN_INT__=1 %s -o %t.out
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -fsycl-id-queries-fit-in-int %s -o %t.out
22
// RUN: %CPU_RUN_PLACEHOLDER %t.out
33

44
#include <CL/sycl.hpp>
@@ -8,7 +8,7 @@ namespace S = cl::sycl;
88

99
void checkRangeException(S::runtime_error &E) {
1010
constexpr char Msg[] = "Provided range is out of integer limits. "
11-
"Pass `-U__SYCL_ID_QUERIES_FIT_IN_INT__' to "
11+
"Pass `-fno-sycl-id-queries-fit-in-int' to "
1212
"disable range check.";
1313

1414
std::cerr << E.what() << std::endl;
@@ -18,7 +18,7 @@ void checkRangeException(S::runtime_error &E) {
1818

1919
void checkOffsetException(S::runtime_error &E) {
2020
constexpr char Msg[] = "Provided offset is out of integer limits. "
21-
"Pass `-U__SYCL_ID_QUERIES_FIT_IN_INT__' to "
21+
"Pass `-fno-sycl-id-queries-fit-in-int' to "
2222
"disable offset check.";
2323

2424
std::cerr << E.what() << std::endl;

sycl/test/check_device_code/id_queries_fit_int.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clangxx -fsycl -Xclang -fsycl-is-host -O1 -c -S -emit-llvm -o %t.ll -D__SYCL_ID_QUERIES_FIT_IN_INT__=1 %s
1+
// RUN: %clangxx -fsycl -Xclang -fsycl-is-host -fsycl-id-queries-fit-in-int -O1 -c -S -emit-llvm -o %t.ll %s
22
// RUN: FileCheck %s --input-file %t.ll
33

44
#include <CL/sycl.hpp>

0 commit comments

Comments
 (0)