Skip to content

Commit be18b1d

Browse files
authored
[SYCL] Add ITT annotation instructions (#3299)
This patch introduces InstrumentalAnnotationsPass pass that adds ITT instrumentations for barrier and barrier-like functions and atomic instructions. The pass is being included to compiler flow only when "-fsycl-instrument-device-code" option passed to clang driver. Following annotations were added: - __itt_offload_wi_start_wrapper Notify tools work-item execution has started - __itt_offload_wi_resume_wrapper Notify tools work-item execution resumed (e.g. after barrier) - __itt_offload_wi_finish_wrapper Notify tools work-item execution has finished - __itt_offload_wg_barrier Notify tools work-item has reached a barrier - __itt_offload_atomic_op_start Atomic operation markup - __itt_offload_atomic_op_finish Atomic operation markup Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
1 parent 25b482b commit be18b1d

File tree

17 files changed

+855
-0
lines changed

17 files changed

+855
-0
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,8 @@ SYCLLowerIR/ @kbobrovs @DenisBakhvalov
114114
esimd/ @kbobrovs @DenisBakhvalov
115115
sycl/include/CL/sycl/INTEL/esimd.hpp @kbobrovs @DenisBakhvalov
116116
sycl/doc/extensions/ExplicitSIMD/ @kbobrovs
117+
118+
# ITT annotations
119+
llvm/lib/Transforms/Instrumentation/SPIRITTAnnotations.cpp @MrSidims
120+
llvm/include/llvm/Transforms/Instrumentation/SPIRITTAnnotations.h @MrSidims
121+

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ CODEGENOPT(PassByValueIsNoAlias, 1, 0)
417417
/// according to the field declaring type width.
418418
CODEGENOPT(AAPCSBitfieldWidth, 1, 1)
419419

420+
// Whether to instrument SPIR device code with ITT annotations
421+
CODEGENOPT(SPIRITTAnnotations, 1, 0)
422+
420423
#undef CODEGENOPT
421424
#undef ENUM_CODEGENOPT
422425
#undef VALUE_CODEGENOPT

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,10 @@ def fsycl_device_code_lower_esimd : Flag<["-"], "fsycl-device-code-lower-esimd">
24182418
Flags<[CC1Option, CoreOption]>, HelpText<"Lower ESIMD-specific constructs">;
24192419
def fno_sycl_device_code_lower_esimd : Flag<["-"], "fno-sycl-device-code-lower-esimd">,
24202420
Flags<[CC1Option, CoreOption]>, HelpText<"Do not lower ESIMD-specific constructs">;
2421+
def fsycl_instrument_device_code : Flag<["-"], "fsycl-instrument-device-code">,
2422+
Group<sycl_Group>, Flags<[CC1Option, CoreOption]>,
2423+
HelpText<"Add ITT instrumentation intrinsics calls">,
2424+
MarshallingInfoFlag<CodeGenOpts<"SPIRITTAnnotations">>;
24212425
defm sycl_id_queries_fit_in_int: OptInFFlag<"sycl-id-queries-fit-in-int", "Assume", "Do not assume", " that SYCL ID queries fit within MAX_INT.", [CC1Option,CoreOption], LangOpts<"SYCLValueFitInMaxInt">>;
24222426
def fsycl_use_bitcode : Flag<["-"], "fsycl-use-bitcode">,
24232427
Flags<[CC1Option, CoreOption]>, HelpText<"Use LLVM bitcode instead of SPIR-V in fat objects">;

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7373
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
7474
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
75+
#include "llvm/Transforms/Instrumentation/SPIRITTAnnotations.h"
7576
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
7677
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
7778
#include "llvm/Transforms/ObjCARC.h"
@@ -948,6 +949,16 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
948949
LangOpts.EnableDAEInSpirKernels)
949950
PerModulePasses.add(createDeadArgEliminationSYCLPass());
950951

952+
// Add SPIRITTAnnotations pass to the pass manager if
953+
// -fsycl-instrument-device-code option was passed. This option can be
954+
// used only with spir triple.
955+
if (CodeGenOpts.SPIRITTAnnotations) {
956+
if (!llvm::Triple(TheModule->getTargetTriple()).isSPIR())
957+
llvm::report_fatal_error(
958+
"ITT annotations can only by added to a module with spir target");
959+
PerModulePasses.add(createSPIRITTAnnotationsPass());
960+
}
961+
951962
switch (Action) {
952963
case Backend_EmitNothing:
953964
break;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5881,6 +5881,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
58815881
if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false))
58825882
Args.AddLastArg(CmdArgs, options::OPT_sycl_std_EQ);
58835883

5884+
// Forward -fsycl-instrument-device-code option to cc1. This option can only
5885+
// be used with spir triple.
5886+
if (Arg *A = Args.getLastArg(options::OPT_fsycl_instrument_device_code)) {
5887+
if (!Triple.isSPIR())
5888+
D.Diag(diag::err_drv_unsupported_opt_for_target)
5889+
<< A->getAsString(Args) << TripleStr;
5890+
CmdArgs.push_back("-fsycl-instrument-device-code");
5891+
}
5892+
58845893
if (IsHIP) {
58855894
if (Args.hasFlag(options::OPT_fhip_new_launch_api,
58865895
options::OPT_fno_hip_new_launch_api, true))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Check if start/finish ITT annotations are being added during compilation of
2+
/// SYCL device code
3+
4+
// RUN: %clang_cc1 -fsycl-is-device -fsycl-instrument-device-code -triple spir64-unknown-unknown-sycldevice -emit-llvm %s -o - | FileCheck %s
5+
6+
// CHECK: kernel_function
7+
// CHECK-NEXT: entry:
8+
// CHECK-NEXT: call void @__itt_offload_wi_start_wrapper()
9+
// CHECK: call void @__itt_offload_wi_finish_wrapper()
10+
// CHECK-NEXT: ret void
11+
12+
#include "Inputs/sycl.hpp"
13+
14+
int main() {
15+
cl::sycl::accessor<int, 1, cl::sycl::access::mode::read_write> accessorA;
16+
cl::sycl::kernel_single_task<class kernel_function>(
17+
[=]() {
18+
accessorA.use();
19+
});
20+
return 0;
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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"
5+
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"
10+
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'}}

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ void initializeStripSymbolsPass(PassRegistry&);
429429
void initializeStructurizeCFGLegacyPassPass(PassRegistry &);
430430
void initializeSYCLLowerWGScopeLegacyPassPass(PassRegistry &);
431431
void initializeSYCLLowerESIMDLegacyPassPass(PassRegistry &);
432+
void initializeSPIRITTAnnotationsLegacyPassPass(PassRegistry &);
432433
void initializeESIMDLowerLoadStorePass(PassRegistry &);
433434
void initializeESIMDLowerVecArgLegacyPassPass(PassRegistry &);
434435
void initializeTailCallElimPass(PassRegistry&);

llvm/include/llvm/LinkAllPasses.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Transforms/InstCombine/InstCombine.h"
4949
#include "llvm/Transforms/Instrumentation.h"
5050
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
51+
#include "llvm/Transforms/Instrumentation/SPIRITTAnnotations.h"
5152
#include "llvm/Transforms/ObjCARC.h"
5253
#include "llvm/Transforms/Scalar.h"
5354
#include "llvm/Transforms/Scalar/GVN.h"
@@ -204,6 +205,7 @@ namespace {
204205
(void)llvm::createSYCLLowerESIMDPass();
205206
(void)llvm::createESIMDLowerLoadStorePass();
206207
(void)llvm::createESIMDLowerVecArgPass();
208+
(void)llvm::createSPIRITTAnnotationsPass();
207209
std::string buf;
208210
llvm::raw_string_ostream os(buf);
209211
(void) llvm::createPrintModulePass(os);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----- SPIRITTAnnotations.h - SPIR Instrumental Annotations Pass ------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A transformation pass which adds instrumental calls to annotate SPIR
10+
// synchronization instructions. This can be used for kernel profiling.
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include "llvm/IR/Module.h"
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
class SPIRITTAnnotationsPass : public PassInfoMixin<SPIRITTAnnotationsPass> {
21+
public:
22+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
23+
};
24+
25+
ModulePass *createSPIRITTAnnotationsPass();
26+
27+
} // namespace llvm

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_llvm_component_library(LLVMInstrumentation
1515
PGOMemOPSizeOpt.cpp
1616
PoisonChecking.cpp
1717
SanitizerCoverage.cpp
18+
SPIRITTAnnotations.cpp
1819
ValueProfileCollector.cpp
1920
ThreadSanitizer.cpp
2021
HWAddressSanitizer.cpp

0 commit comments

Comments
 (0)