Skip to content

Commit bf0fc43

Browse files
authored
[SYCL][PI] Add versioning for plugins (#6216)
Following on from #5412
1 parent 90aaba8 commit bf0fc43

File tree

12 files changed

+104
-29
lines changed

12 files changed

+104
-29
lines changed

sycl/include/CL/sycl/detail/pi.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,28 @@
5353

5454
#define _PI_STRING_HELPER(a) #a
5555
#define _PI_CONCAT(a, b) _PI_STRING_HELPER(a.b)
56+
#define _PI_TRIPLE_CONCAT(a, b, c) _PI_STRING_HELPER(a.b.c)
57+
58+
// This is the macro that plugins should all use to define their version.
59+
// _PI_PLUGIN_VERSION_STRING will be printed when environment variable
60+
// SYCL_PI_TRACE is set to 1. PluginVersion should be defined for each plugin
61+
// in plugins/*/pi_*.hpp. PluginVersion should be incremented with each change
62+
// to the plugin.
63+
#define _PI_PLUGIN_VERSION_STRING(PluginVersion) \
64+
_PI_TRIPLE_CONCAT(_PI_H_VERSION_MAJOR, _PI_H_VERSION_MINOR, PluginVersion)
65+
5666
#define _PI_H_VERSION_STRING \
5767
_PI_CONCAT(_PI_H_VERSION_MAJOR, _PI_H_VERSION_MINOR)
5868

69+
// This will be used to check the major versions of plugins versus the major
70+
// versions of PI.
71+
#define _PI_STRING_SUBSTITUTE(X) _PI_STRING_HELPER(X)
72+
#define _PI_PLUGIN_VERSION_CHECK(PI_API_VERSION, PI_PLUGIN_VERSION) \
73+
if (strncmp(PI_API_VERSION, PI_PLUGIN_VERSION, \
74+
sizeof(_PI_STRING_SUBSTITUTE(_PI_H_VERSION_MAJOR))) < 0) { \
75+
return PI_ERROR_INVALID_OPERATION; \
76+
}
77+
5978
// NOTE: This file presents a maping of OpenCL to PI enums, constants and
6079
// typedefs. The general approach taken was to replace `CL_` prefix with `PI_`.
6180
// Please consider this when adding or modifying values, as the strict value
@@ -1786,9 +1805,9 @@ struct _pi_plugin {
17861805
// Some choices are:
17871806
// - Use of integers to keep major and minor version.
17881807
// - Keeping char* Versions.
1789-
char PiVersion[10];
1808+
char PiVersion[20];
17901809
// Plugin edits this.
1791-
char PluginVersion[10];
1810+
char PluginVersion[20];
17921811
char *Targets;
17931812
struct FunctionPointers {
17941813
#define _PI_API(api) decltype(::api) *api;

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,15 +5257,11 @@ pi_result cuda_piextUSMGetMemAllocInfo(pi_context context, const void *ptr,
52575257
// pi_level_zero.cpp for reference) Currently this is just a NOOP.
52585258
pi_result cuda_piTearDown(void *) { return PI_SUCCESS; }
52595259

5260-
const char SupportedVersion[] = _PI_H_VERSION_STRING;
5260+
const char SupportedVersion[] = _PI_CUDA_PLUGIN_VERSION_STRING;
52615261

52625262
pi_result piPluginInit(pi_plugin *PluginInit) {
5263-
int CompareVersions = strcmp(PluginInit->PiVersion, SupportedVersion);
5264-
if (CompareVersions < 0) {
5265-
// PI interface supports lower version of PI.
5266-
// TODO: Take appropriate actions.
5267-
return PI_ERROR_INVALID_OPERATION;
5268-
}
5263+
// Check that the major version matches in PiVersion and SupportedVersion
5264+
_PI_PLUGIN_VERSION_CHECK(PluginInit->PiVersion, SupportedVersion);
52695265

52705266
// PI interface supports higher version or the same version.
52715267
size_t PluginVersionSize = sizeof(PluginInit->PluginVersion);

sycl/plugins/cuda/pi_cuda.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#ifndef PI_CUDA_HPP
1919
#define PI_CUDA_HPP
2020

21+
// This version should be incremented for any change made to this file or its
22+
// corresponding .cpp file.
23+
#define _PI_CUDA_PLUGIN_VERSION 1
24+
25+
#define _PI_CUDA_PLUGIN_VERSION_STRING \
26+
_PI_PLUGIN_VERSION_STRING(_PI_CUDA_PLUGIN_VERSION)
27+
2128
#include "CL/sycl/detail/pi.h"
2229
#include <array>
2330
#include <atomic>

sycl/plugins/esimd_emulator/pi_esimd_emulator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,16 +1981,21 @@ pi_result piTearDown(void *) {
19811981
return PI_SUCCESS;
19821982
}
19831983

1984+
const char SupportedVersion[] = _PI_ESIMD_PLUGIN_VERSION_STRING;
1985+
19841986
pi_result piPluginInit(pi_plugin *PluginInit) {
19851987
if (PluginInit == nullptr) {
19861988
return PI_ERROR_INVALID_VALUE;
19871989
}
19881990

1991+
// Check that the major version matches in PiVersion and SupportedVersion
1992+
_PI_PLUGIN_VERSION_CHECK(PluginInit->PiVersion, SupportedVersion);
1993+
19891994
size_t PluginVersionSize = sizeof(PluginInit->PluginVersion);
19901995
if (strlen(_PI_H_VERSION_STRING) >= PluginVersionSize) {
19911996
return PI_ERROR_INVALID_VALUE;
19921997
}
1993-
strncpy(PluginInit->PluginVersion, _PI_H_VERSION_STRING, PluginVersionSize);
1998+
strncpy(PluginInit->PluginVersion, SupportedVersion, PluginVersionSize);
19941999

19952000
PiESimdDeviceAccess = new sycl::detail::ESIMDEmuPluginOpaqueData();
19962001
// 'version' to be compared with 'ESIMD_EMULATOR_DEVICE_REQUIRED_VER' defined

sycl/plugins/esimd_emulator/pi_esimd_emulator.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
#include <malloc.h>
2525

26+
// This version should be incremented for any change made to this file or its
27+
// corresponding .cpp file.
28+
#define _PI_ESIMD_PLUGIN_VERSION 1
29+
30+
#define _PI_ESIMD_PLUGIN_VERSION_STRING \
31+
_PI_PLUGIN_VERSION_STRING(_PI_ESIMD_PLUGIN_VERSION)
32+
2633
namespace cm_support {
2734
#include <cm_rt.h>
2835
} // namespace cm_support

sycl/plugins/hip/pi_hip.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4914,15 +4914,11 @@ pi_result hip_piTearDown(void *PluginParameter) {
49144914
return PI_SUCCESS;
49154915
}
49164916

4917-
const char SupportedVersion[] = _PI_H_VERSION_STRING;
4917+
const char SupportedVersion[] = _PI_HIP_PLUGIN_VERSION_STRING;
49184918

49194919
pi_result piPluginInit(pi_plugin *PluginInit) {
4920-
int CompareVersions = strcmp(PluginInit->PiVersion, SupportedVersion);
4921-
if (CompareVersions < 0) {
4922-
// PI interface supports lower version of PI.
4923-
// TODO: Take appropriate actions.
4924-
return PI_ERROR_INVALID_OPERATION;
4925-
}
4920+
// Check that the major version matches in PiVersion and SupportedVersion
4921+
_PI_PLUGIN_VERSION_CHECK(PluginInit->PiVersion, SupportedVersion);
49264922

49274923
// PI interface supports higher version or the same version.
49284924
size_t PluginVersionSize = sizeof(PluginInit->PluginVersion);

sycl/plugins/hip/pi_hip.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#ifndef PI_HIP_HPP
1919
#define PI_HIP_HPP
2020

21+
// This version should be incremented for any change made to this file or its
22+
// corresponding .cpp file.
23+
#define _PI_HIP_PLUGIN_VERSION 1
24+
25+
#define _PI_HIP_PLUGIN_VERSION_STRING \
26+
_PI_PLUGIN_VERSION_STRING(_PI_HIP_PLUGIN_VERSION)
27+
2128
#include "CL/sycl/detail/pi.h"
2229
#include <array>
2330
#include <atomic>

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8067,16 +8067,21 @@ pi_result piextProgramSetSpecializationConstant(pi_program Prog,
80678067
return PI_SUCCESS;
80688068
}
80698069

8070+
const char SupportedVersion[] = _PI_LEVEL_ZERO_PLUGIN_VERSION_STRING;
8071+
80708072
pi_result piPluginInit(pi_plugin *PluginInit) {
80718073
PI_ASSERT(PluginInit, PI_ERROR_INVALID_VALUE);
80728074

8075+
// Check that the major version matches in PiVersion and SupportedVersion
8076+
_PI_PLUGIN_VERSION_CHECK(PluginInit->PiVersion, SupportedVersion);
8077+
80738078
// TODO: handle versioning/targets properly.
80748079
size_t PluginVersionSize = sizeof(PluginInit->PluginVersion);
80758080

8076-
PI_ASSERT(strlen(_PI_H_VERSION_STRING) < PluginVersionSize,
8081+
PI_ASSERT(strlen(_PI_LEVEL_ZERO_PLUGIN_VERSION_STRING) < PluginVersionSize,
80778082
PI_ERROR_INVALID_VALUE);
80788083

8079-
strncpy(PluginInit->PluginVersion, _PI_H_VERSION_STRING, PluginVersionSize);
8084+
strncpy(PluginInit->PluginVersion, SupportedVersion, PluginVersionSize);
80808085

80818086
#define _PI_API(api) \
80828087
(PluginInit->PiFunctionTable).api = (decltype(&::api))(&api);

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#ifndef PI_LEVEL_ZERO_HPP
1919
#define PI_LEVEL_ZERO_HPP
2020

21+
// This version should be incremented for any change made to this file or its
22+
// corresponding .cpp file.
23+
#define _PI_LEVEL_ZERO_PLUGIN_VERSION 1
24+
25+
#define _PI_LEVEL_ZERO_PLUGIN_VERSION_STRING \
26+
_PI_PLUGIN_VERSION_STRING(_PI_LEVEL_ZERO_PLUGIN_VERSION)
27+
2128
#include <CL/sycl/detail/pi.h>
2229
#include <atomic>
2330
#include <cassert>

sycl/plugins/opencl/pi_opencl.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <CL/sycl/detail/cl.h>
2020
#include <CL/sycl/detail/pi.h>
21+
#include <pi_opencl.hpp>
2122

2223
#include <algorithm>
2324
#include <cassert>
@@ -36,8 +37,6 @@
3637
return cast<pi_result>(reterr); \
3738
}
3839

39-
const char SupportedVersion[] = _PI_H_VERSION_STRING;
40-
4140
// Want all the needed casts be explicit, do not define conversion operators.
4241
template <class To, class From> To cast(From value) {
4342
// TODO: see if more sanity checks are possible.
@@ -1392,13 +1391,11 @@ pi_result piTearDown(void *PluginParameter) {
13921391
return PI_SUCCESS;
13931392
}
13941393

1394+
const char SupportedVersion[] = _PI_OPENCL_PLUGIN_VERSION_STRING;
1395+
13951396
pi_result piPluginInit(pi_plugin *PluginInit) {
1396-
int CompareVersions = strcmp(PluginInit->PiVersion, SupportedVersion);
1397-
if (CompareVersions < 0) {
1398-
// PI interface supports lower version of PI.
1399-
// TODO: Take appropriate actions.
1400-
return PI_ERROR_INVALID_OPERATION;
1401-
}
1397+
// Check that the major version matches in PiVersion and SupportedVersion
1398+
_PI_PLUGIN_VERSION_CHECK(PluginInit->PiVersion, SupportedVersion);
14021399

14031400
// PI interface supports higher version or the same version.
14041401
size_t PluginVersionSize = sizeof(PluginInit->PluginVersion);

sycl/plugins/opencl/pi_opencl.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//==---------- pi_opencl.hpp - OpenCL Plugin -------------------------------==//
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+
/// \defgroup sycl_pi_ocl OpenCL Plugin
9+
/// \ingroup sycl_pi
10+
11+
/// \file pi_opencl.hpp
12+
/// Declarations for vOpenCL Plugin. It is the interface between device-agnostic
13+
/// SYCL runtime layer and underlying OpenCL runtime.
14+
///
15+
/// \ingroup sycl_pi_ocl
16+
17+
#ifndef PI_OPENCL_HPP
18+
#define PI_OPENCL_HPP
19+
20+
// This version should be incremented for any change made to this file or its
21+
// corresponding .cpp file.
22+
#define _PI_OPENCL_PLUGIN_VERSION 1
23+
24+
#define _PI_OPENCL_PLUGIN_VERSION_STRING \
25+
_PI_PLUGIN_VERSION_STRING(_PI_OPENCL_PLUGIN_VERSION)
26+
27+
#endif // PI_OPENCL_HPP

sycl/source/detail/pi.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,14 @@ static void initializePlugins(std::vector<plugin> &Plugins) {
446446
GlobalPlugin = std::make_shared<plugin>(
447447
PluginInformation, backend::ext_intel_esimd_emulator, Library);
448448
}
449-
Plugins.emplace_back(
449+
plugin &NewPlugin = Plugins.emplace_back(
450450
plugin(PluginInformation, PluginNames[I].second, Library));
451451
if (trace(TraceLevel::PI_TRACE_BASIC))
452452
std::cerr << "SYCL_PI_TRACE[basic]: "
453453
<< "Plugin found and successfully loaded: "
454-
<< PluginNames[I].first << std::endl;
454+
<< PluginNames[I].first
455+
<< " [ PluginVersion: " << NewPlugin.getPiPlugin().PluginVersion
456+
<< " ]" << std::endl;
455457
}
456458

457459
#ifdef XPTI_ENABLE_INSTRUMENTATION

0 commit comments

Comments
 (0)