Skip to content

Commit 5667aba

Browse files
author
Artem Gindinson
authored
[SYCL] Fix device code instrumentation (intel#4615)
- Fix the malfunctioning driver option for passing `-fsycl-instrument-device-code` to CC1 for SPIR-V-based targets. - Add the ITT device libraries when the CLI option is enabled, excluding these from `-fno-sycl-device-lib` effects. - Disable ITT annotations for ESIMD code. Tests are in intel/llvm-test-suite#484. Signed-off-by: Artem Gindinson <[email protected]>
1 parent d37744a commit 5667aba

File tree

8 files changed

+90
-49
lines changed

8 files changed

+90
-49
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,20 +4331,19 @@ class OffloadingActionBuilder final {
43314331

43324332
bool addSYCLDeviceLibs(const ToolChain *TC, ActionList &DeviceLinkObjects,
43334333
bool isSpirvAOT, bool isMSVCEnv) {
4334-
enum SYCLDeviceLibType {
4335-
sycl_devicelib_wrapper,
4336-
sycl_devicelib_fallback
4337-
};
43384334
struct DeviceLibOptInfo {
43394335
StringRef devicelib_name;
43404336
StringRef devicelib_option;
43414337
};
43424338

43434339
bool NoDeviceLibs = false;
43444340
int NumOfDeviceLibLinked = 0;
4345-
// Currently, all SYCL device libraries will be linked by default
4346-
llvm::StringMap<bool> devicelib_link_info = {
4347-
{"libc", true}, {"libm-fp32", true}, {"libm-fp64", true}};
4341+
// Currently, all SYCL device libraries will be linked by default. Linkage
4342+
// of "internal" libraries cannot be affected via -fno-sycl-device-lib.
4343+
llvm::StringMap<bool> devicelib_link_info = {{"libc", true},
4344+
{"libm-fp32", true},
4345+
{"libm-fp64", true},
4346+
{"internal", true}};
43484347
if (Arg *A = Args.getLastArg(options::OPT_fsycl_device_lib_EQ,
43494348
options::OPT_fno_sycl_device_lib_EQ)) {
43504349
if (A->getValues().size() == 0)
@@ -4357,11 +4356,16 @@ class OffloadingActionBuilder final {
43574356
for (StringRef Val : A->getValues()) {
43584357
if (Val == "all") {
43594358
for (const auto &K : devicelib_link_info.keys())
4360-
devicelib_link_info[K] = true && !NoDeviceLibs;
4359+
devicelib_link_info[K] =
4360+
true && (!NoDeviceLibs || K.equals("internal"));
43614361
break;
43624362
}
43634363
auto LinkInfoIter = devicelib_link_info.find(Val);
4364-
if (LinkInfoIter == devicelib_link_info.end()) {
4364+
if (LinkInfoIter == devicelib_link_info.end() ||
4365+
Val.equals("internal")) {
4366+
// TODO: Move the diagnostic to the SYCL section of
4367+
// Driver::CreateOffloadingDeviceToolChains() to minimize code
4368+
// duplication.
43654369
C.getDriver().Diag(diag::err_drv_unsupported_option_argument)
43664370
<< A->getOption().getName() << Val;
43674371
}
@@ -4375,30 +4379,34 @@ class OffloadingActionBuilder final {
43754379
SmallVector<SmallString<128>, 4> LibLocCandidates;
43764380
SYCLTC->SYCLInstallation.getSYCLDeviceLibPath(LibLocCandidates);
43774381
StringRef LibSuffix = isMSVCEnv ? ".obj" : ".o";
4378-
SmallVector<DeviceLibOptInfo, 5> sycl_device_wrapper_libs = {
4382+
using SYCLDeviceLibsList = SmallVector<DeviceLibOptInfo, 5>;
4383+
const SYCLDeviceLibsList sycl_device_wrapper_libs = {
43794384
{"libsycl-crt", "libc"},
43804385
{"libsycl-complex", "libm-fp32"},
43814386
{"libsycl-complex-fp64", "libm-fp64"},
43824387
{"libsycl-cmath", "libm-fp32"},
43834388
{"libsycl-cmath-fp64", "libm-fp64"}};
43844389
// For AOT compilation, we need to link sycl_device_fallback_libs as
43854390
// default too.
4386-
SmallVector<DeviceLibOptInfo, 5> sycl_device_fallback_libs = {
4391+
const SYCLDeviceLibsList sycl_device_fallback_libs = {
43874392
{"libsycl-fallback-cassert", "libc"},
43884393
{"libsycl-fallback-cstring", "libc"},
43894394
{"libsycl-fallback-complex", "libm-fp32"},
43904395
{"libsycl-fallback-complex-fp64", "libm-fp64"},
43914396
{"libsycl-fallback-cmath", "libm-fp32"},
43924397
{"libsycl-fallback-cmath-fp64", "libm-fp64"}};
4393-
auto addInputs = [&](SYCLDeviceLibType t) {
4394-
auto sycl_libs = (t == sycl_devicelib_wrapper)
4395-
? sycl_device_wrapper_libs
4396-
: sycl_device_fallback_libs;
4398+
// ITT annotation libraries are linked in separately whenever the device
4399+
// code instrumentation is enabled.
4400+
const SYCLDeviceLibsList sycl_device_annotation_libs = {
4401+
{"libsycl-itt-user-wrappers", "internal"},
4402+
{"libsycl-itt-compiler-wrappers", "internal"},
4403+
{"libsycl-itt-stubs", "internal"}};
4404+
auto addInputs = [&](const SYCLDeviceLibsList &LibsList) {
43974405
bool LibLocSelected = false;
43984406
for (const auto &LLCandidate : LibLocCandidates) {
43994407
if (LibLocSelected)
44004408
break;
4401-
for (const DeviceLibOptInfo &Lib : sycl_libs) {
4409+
for (const DeviceLibOptInfo &Lib : LibsList) {
44024410
if (!devicelib_link_info[Lib.devicelib_option])
44034411
continue;
44044412
SmallString<128> LibName(LLCandidate);
@@ -4421,9 +4429,11 @@ class OffloadingActionBuilder final {
44214429
}
44224430
}
44234431
};
4424-
addInputs(sycl_devicelib_wrapper);
4432+
addInputs(sycl_device_wrapper_libs);
44254433
if (isSpirvAOT)
4426-
addInputs(sycl_devicelib_fallback);
4434+
addInputs(sycl_device_fallback_libs);
4435+
if (Args.hasArg(options::OPT_fsycl_instrument_device_code))
4436+
addInputs(sycl_device_annotation_libs);
44274437
return NumOfDeviceLibLinked != 0;
44284438
}
44294439

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4660,6 +4660,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
46604660
CmdArgs.push_back("-fsycl-allow-func-ptr");
46614661
}
46624662

4663+
// Forward -fsycl-instrument-device-code option to cc1. This option will
4664+
// only be used for SPIR-V-based targets.
4665+
if (Arg *A =
4666+
Args.getLastArgNoClaim(options::OPT_fsycl_instrument_device_code))
4667+
if (Triple.isSPIR()) {
4668+
A->claim();
4669+
CmdArgs.push_back("-fsycl-instrument-device-code");
4670+
}
4671+
46634672
if (!SYCLStdArg) {
46644673
// The user had not pass SYCL version, thus we'll employ no-sycl-strict
46654674
// to allow address-space unqualified pointers in function params/return
@@ -6409,15 +6418,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
64096418
// Forward -cl options to -cc1
64106419
RenderOpenCLOptions(Args, CmdArgs, InputType);
64116420

6412-
// Forward -fsycl-instrument-device-code option to cc1. This option can only
6413-
// be used with spir triple.
6414-
if (Arg *A = Args.getLastArg(options::OPT_fsycl_instrument_device_code)) {
6415-
if (!Triple.isSPIR())
6416-
D.Diag(diag::err_drv_unsupported_opt_for_target)
6417-
<< A->getAsString(Args) << TripleStr;
6418-
CmdArgs.push_back("-fsycl-instrument-device-code");
6419-
}
6420-
64216421
if (IsHIP) {
64226422
if (Args.hasFlag(options::OPT_fhip_new_launch_api,
64236423
options::OPT_fno_hip_new_launch_api, true))

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,15 @@ void SYCL::constructLLVMForeachCommand(Compilation &C, const JobAction &JA,
146146
// The list should match pre-built SYCL device library files located in
147147
// compiler package. Once we add or remove any SYCL device library files,
148148
// the list should be updated accordingly.
149-
static llvm::SmallVector<StringRef, 10> SYCLDeviceLibList{
149+
static llvm::SmallVector<StringRef, 16> SYCLDeviceLibList{
150150
"crt",
151151
"cmath",
152152
"cmath-fp64",
153153
"complex",
154154
"complex-fp64",
155+
"itt-compiler-wrappers",
156+
"itt-stubs",
157+
"itt-user-wrappers",
155158
"fallback-cassert",
156159
"fallback-cstring",
157160
"fallback-cmath",

clang/test/Driver/sycl-device-lib-win.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@
9494

9595
/// test invalid value for -f[no-]sycl-device-lib
9696
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,dummy -### 2>&1 \
97-
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE
97+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE -DVal=dummy
9898
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=dummy,libm-fp32 -### 2>&1 \
99-
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE
100-
// SYCL_DEVICE_LIB_INVALID_VALUE: error: unsupported argument 'dummy' to option 'fsycl-device-lib='
101-
// SYCL_NO_DEVICE_LIB_INVALID_VALUE: error: unsupported argument 'dummy' to option 'fno-sycl-device-lib='
99+
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE -DVal=dummy
100+
// Do separate checks for the compiler-reserved "internal" value
101+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=internal -### 2>&1 \
102+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE -DVal=internal
103+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=internal -### 2>&1 \
104+
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE -DVal=internal
105+
// SYCL_DEVICE_LIB_INVALID_VALUE: error: unsupported argument '[[Val]]' to option 'fsycl-device-lib='
106+
// SYCL_NO_DEVICE_LIB_INVALID_VALUE: error: unsupported argument '[[Val]]' to option 'fno-sycl-device-lib='
102107

103108
/// ###########################################################################
104109
/// test llvm-link behavior for linking device libraries

clang/test/Driver/sycl-device-lib.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,16 @@
9595

9696
/// test invalid value for -f[no-]sycl-device-lib
9797
// RUN: %clangxx -fsycl %s -fsycl-device-lib=libc,dummy -### 2>&1 \
98-
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE
98+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE -DVal=dummy
9999
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=dummy,libm-fp32 -### 2>&1 \
100-
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE
101-
// SYCL_DEVICE_LIB_INVALID_VALUE: error: unsupported argument 'dummy' to option 'fsycl-device-lib='
102-
// SYCL_NO_DEVICE_LIB_INVALID_VALUE: error: unsupported argument 'dummy' to option 'fno-sycl-device-lib='
100+
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE -DVal=dummy
101+
// Do separate checks for the compiler-reserved "internal" value
102+
// RUN: %clangxx -fsycl %s -fsycl-device-lib=internal -### 2>&1 \
103+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_INVALID_VALUE -DVal=internal
104+
// RUN: %clangxx -fsycl %s -fno-sycl-device-lib=internal -### 2>&1 \
105+
// RUN: | FileCheck %s -check-prefix=SYCL_NO_DEVICE_LIB_INVALID_VALUE -DVal=internal
106+
// SYCL_DEVICE_LIB_INVALID_VALUE: error: unsupported argument '[[Val]]' to option 'fsycl-device-lib='
107+
// SYCL_NO_DEVICE_LIB_INVALID_VALUE: error: unsupported argument '[[Val]]' to option 'fno-sycl-device-lib='
103108

104109
/// ###########################################################################
105110
/// test llvm-link behavior for linking device libraries
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
/// Check that SPIR ITT instrumentation is disabled by default:
2-
// RUN: %clang -### %s 2>&1 \
3-
// RUN: | FileCheck -check-prefix=CHECK-DEFAULT %s
4-
// CHECK-DEFAULT-NOT: "-fsycl-instrument-device-code"
1+
/// Check if -fsycl-instrument-device-code is passed to device-side -cc1
2+
/// and if ITT device libraries are pulled in.
3+
/// The following conditions must be fulfilled:
4+
/// 1. A SPIR-V-based environment must be targetted
5+
/// 2. The corresponding Driver option must be enabled explicitly
56

6-
/// Check if "fsycl_instrument_device_code" is passed to -cc1:
7-
// RUN: %clang -### -fsycl-instrument-device-code %s 2>&1 \
8-
// RUN: | FileCheck -check-prefix=CHECK-ENABLED %s
9-
// CHECK-ENABLED: "-cc1"{{.*}} "-fsycl-instrument-device-code"
7+
// RUN: %clangxx -fsycl -fsycl-instrument-device-code -fsycl-targets=spir64 -### %s 2>&1 \
8+
// RUN: | FileCheck -check-prefixes=CHECK-SPIRV,CHECK-HOST %s
9+
/// -fno-sycl-device-lib mustn't affect the linkage of ITT libraries
10+
// RUN: %clangxx -fsycl -fsycl-instrument-device-code -fno-sycl-device-lib=all -fsycl-targets=spir64 -### %s 2>&1 \
11+
// RUN: | FileCheck -check-prefixes=CHECK-SPIRV %s
1012

11-
/// Check if "fsycl_instrument_device_code" usage with a non-spirv target
12-
/// results in an error.
13-
// RUN: %clang -### -fsycl-instrument-device-code --target=x86 %s 2>&1
14-
// expected-error{{unsupported option '-fsycl-instrument-device-code' for target 'x86_64-unknown-linux-gnu'}}
13+
// CHECK-SPIRV: "-cc1"{{.*}} "-fsycl-is-device"{{.*}} "-fsycl-instrument-device-code"
14+
// CHECK-SPIRV: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-inputs={{.*}}libsycl-itt-user-wrappers.{{o|obj}}" "-outputs={{.*}}libsycl-itt-user-wrappers-{{.*}}.{{o|obj}}" "-unbundle"
15+
// CHECK-SPIRV: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-inputs={{.*}}libsycl-itt-compiler-wrappers.{{o|obj}}" "-outputs={{.*}}libsycl-itt-compiler-wrappers-{{.*}}.{{o|obj}}" "-unbundle"
16+
// CHECK-SPIRV: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-inputs={{.*}}libsycl-itt-stubs.{{o|obj}}" "-outputs={{.*}}libsycl-itt-stubs-{{.*}}.{{o|obj}}" "-unbundle"
17+
// CHECK-SPIRV: llvm-link{{.*}} "-only-needed" "{{.*}}" "-o" "{{.*}}.bc" "--suppress-warnings"
18+
// CHECK-HOST-NOT: "-cc1"{{.*}} "-fsycl-is-host"{{.*}} "-fsycl-instrument-device-code"
19+
20+
// RUN: %clangxx -fsycl -fsycl-targets=spir64 -### %s 2>&1 \
21+
// RUN: | FileCheck -check-prefixes=CHECK-NONPASSED %s
22+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -fsycl-instrument-device-code -nocudalib -### %s 2>&1 \
23+
// RUN: | FileCheck -check-prefixes=CHECK-WARNING,CHECK-NONPASSED %s
24+
// CHECK-NONPASSED-NOT: "-fsycl-instrument-device-code"
25+
// CHECK-NONPASSED-NOT: "-inputs={{.*}}libsycl-itt-{{.*}}.{{o|obj}}"
26+
// CHECK-WARNING: warning: argument unused during compilation: '-fsycl-instrument-device-code'

llvm/lib/Transforms/Instrumentation/SPIRITTAnnotations.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ PreservedAnalyses SPIRITTAnnotationsPass::run(Module &M,
244244
SPIRV_GROUP_FMAX, SPIRV_GROUP_UMAX, SPIRV_GROUP_SMAX};
245245

246246
for (Function &F : M) {
247-
if (F.isDeclaration())
247+
// Do not annotate:
248+
// - declarations
249+
// - ESIMD functions (TODO: consider enabling instrumentation)
250+
if (F.isDeclaration() || F.getMetadata("sycl_explicit_simd"))
248251
continue;
249252

250253
// Work item start/finish annotations are only for SPIR kernels

sycl/test/esimd/vadd.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// Check that the code compiles with -O0 and -g
55
// RUN: %clangxx -I %sycl_include %s -o %t.out -fsycl -O0
66
// RUN: %clangxx -I %sycl_include %s -o %t.out -fsycl -O0 -g
7+
// Check that the code compiles with device code instrumentation enabled
8+
// RUN: %clangxx -I %sycl_include %s -o %t.out -fsycl \
9+
// RUN: -fsycl-instrument-device-code
710

811
#include <CL/sycl.hpp>
912
#include <sycl/ext/intel/experimental/esimd.hpp>

0 commit comments

Comments
 (0)