Skip to content

Commit 91a79d5

Browse files
authored
[SYCL] Move static global variables to function scope (intel#4322)
Move static global variables to function scope to avoid initialization races on usages.
1 parent 0c96549 commit 91a79d5

File tree

7 files changed

+46
-34
lines changed

7 files changed

+46
-34
lines changed

sycl/source/detail/allowlist.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
148148
// check that values of keys, which should have some fixed format, are
149149
// valid. E.g., for BackendName key, the allowed values are only ones
150150
// described in SyclBeMap
151-
ValidateEnumValues(BackendNameKeyName, SyclBeMap);
152-
ValidateEnumValues(DeviceTypeKeyName, SyclDeviceTypeMap);
151+
ValidateEnumValues(BackendNameKeyName, getSyclBeMap());
152+
ValidateEnumValues(DeviceTypeKeyName, getSyclDeviceTypeMap());
153153

154154
if (Key == DeviceVendorIdKeyName) {
155155
// DeviceVendorId should have hex format
@@ -310,7 +310,7 @@ void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
310310

311311
// get BackendName value and put it to DeviceDesc
312312
sycl::backend Backend = Plugin.getBackend();
313-
for (const auto &SyclBe : SyclBeMap) {
313+
for (const auto &SyclBe : getSyclBeMap()) {
314314
if (SyclBe.second == Backend) {
315315
DeviceDesc.emplace(BackendNameKeyName, SyclBe.first);
316316
break;
@@ -336,7 +336,7 @@ void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
336336
sizeof(RT::PiDeviceType),
337337
&PiDevType, nullptr);
338338
sycl::info::device_type DeviceType = pi::cast<info::device_type>(PiDevType);
339-
for (const auto &SyclDeviceType : SyclDeviceTypeMap) {
339+
for (const auto &SyclDeviceType : getSyclDeviceTypeMap()) {
340340
if (SyclDeviceType.second == DeviceType) {
341341
const auto &DeviceTypeValue = SyclDeviceType.first;
342342
DeviceDesc[DeviceTypeKeyName] = DeviceTypeValue;

sycl/source/detail/config.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ void dumpConfig() {
111111
#undef CONFIG
112112
}
113113

114-
} // __SYCL_INLINE_NAMESPACE(cl)
115-
} // namespace sycl
114+
// Array is used by SYCL_DEVICE_FILTER and SYCL_DEVICE_ALLOWLIST
115+
const std::array<std::pair<std::string, info::device_type>, 5> &
116+
getSyclDeviceTypeMap() {
117+
static const std::array<std::pair<std::string, info::device_type>, 5>
118+
SyclDeviceTypeMap = {{{"host", info::device_type::host},
119+
{"cpu", info::device_type::cpu},
120+
{"gpu", info::device_type::gpu},
121+
{"acc", info::device_type::accelerator},
122+
{"*", info::device_type::all}}};
123+
return SyclDeviceTypeMap;
124+
}
125+
126+
// Array is used by SYCL_DEVICE_FILTER and SYCL_DEVICE_ALLOWLIST
127+
const std::array<std::pair<std::string, backend>, 6> &getSyclBeMap() {
128+
static const std::array<std::pair<std::string, backend>, 6> SyclBeMap = {
129+
{{"host", backend::host},
130+
{"opencl", backend::opencl},
131+
{"level_zero", backend::level_zero},
132+
{"cuda", backend::cuda},
133+
{"rocm", backend::rocm},
134+
{"*", backend::all}}};
135+
return SyclBeMap;
136+
}
137+
116138
} // namespace detail
139+
} // namespace sycl
140+
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/config.hpp

+3-13
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,11 @@ template <> class SYCLConfig<SYCL_PI_TRACE> {
177177
};
178178

179179
// Array is used by SYCL_DEVICE_FILTER and SYCL_DEVICE_ALLOWLIST
180-
static const std::array<std::pair<std::string, info::device_type>, 5>
181-
SyclDeviceTypeMap = {{{"host", info::device_type::host},
182-
{"cpu", info::device_type::cpu},
183-
{"gpu", info::device_type::gpu},
184-
{"acc", info::device_type::accelerator},
185-
{"*", info::device_type::all}}};
180+
const std::array<std::pair<std::string, info::device_type>, 5> &
181+
getSyclDeviceTypeMap();
186182

187183
// Array is used by SYCL_DEVICE_FILTER and SYCL_DEVICE_ALLOWLIST
188-
static const std::array<std::pair<std::string, backend>, 6> SyclBeMap = {
189-
{{"host", backend::host},
190-
{"opencl", backend::opencl},
191-
{"level_zero", backend::level_zero},
192-
{"cuda", backend::cuda},
193-
{"rocm", backend::rocm},
194-
{"*", backend::all}}};
184+
const std::array<std::pair<std::string, backend>, 6> &getSyclBeMap();
195185

196186
template <> class SYCLConfig<SYCL_DEVICE_FILTER> {
197187
using BaseT = SYCLConfigBase<SYCL_DEVICE_FILTER>;

sycl/source/detail/device_filter.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ device_filter::device_filter(const std::string &FilterString) {
3030

3131
// Handle the optional 1st field of the filter, backend
3232
// Check if the first entry matches with a known backend type
33-
auto It =
34-
std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement);
33+
auto It = std::find_if(std::begin(getSyclBeMap()), std::end(getSyclBeMap()),
34+
findElement);
3535
// If no match is found, set the backend type backend::all
3636
// which actually means 'any backend' will be a match.
37-
if (It == SyclBeMap.end())
37+
if (It == getSyclBeMap().end())
3838
Backend = backend::all;
3939
else {
4040
Backend = It->second;
@@ -49,11 +49,11 @@ device_filter::device_filter(const std::string &FilterString) {
4949
if (Cursor >= FilterString.size()) {
5050
DeviceType = info::device_type::all;
5151
} else {
52-
auto Iter = std::find_if(std::begin(SyclDeviceTypeMap),
53-
std::end(SyclDeviceTypeMap), findElement);
52+
auto Iter = std::find_if(std::begin(getSyclDeviceTypeMap()),
53+
std::end(getSyclDeviceTypeMap()), findElement);
5454
// If no match is found, set device_type 'all',
5555
// which actually means 'any device_type' will be a match.
56-
if (Iter == SyclDeviceTypeMap.end())
56+
if (Iter == getSyclDeviceTypeMap().end())
5757
DeviceType = info::device_type::all;
5858
else {
5959
DeviceType = Iter->second;

sycl/unittests/allowlist/ParseAllowList.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <detail/allowlist.hpp>
10-
#include <detail/config.hpp> // for SyclBeMap and SyclDeviceTypeMap
10+
#include <detail/config.hpp> // for getSyclBeMap() and getSyclDeviceTypeMap()
1111

1212
#include <gtest/gtest.h>
1313

@@ -157,7 +157,7 @@ TEST(ParseAllowListTests, CheckMissingClosedDoubleCurlyBracesAreHandled) {
157157

158158
TEST(ParseAllowListTests, CheckAllValidBackendNameValuesAreProcessed) {
159159
std::string AllowList;
160-
for (const auto &SyclBe : sycl::detail::SyclBeMap) {
160+
for (const auto &SyclBe : sycl::detail::getSyclBeMap()) {
161161
if (!AllowList.empty())
162162
AllowList += "|";
163163
AllowList += "BackendName:" + SyclBe.first;
@@ -173,7 +173,7 @@ TEST(ParseAllowListTests, CheckAllValidBackendNameValuesAreProcessed) {
173173

174174
TEST(ParseAllowListTests, CheckAllValidDeviceTypeValuesAreProcessed) {
175175
std::string AllowList;
176-
for (const auto &SyclDeviceType : sycl::detail::SyclDeviceTypeMap) {
176+
for (const auto &SyclDeviceType : sycl::detail::getSyclDeviceTypeMap()) {
177177
if (!AllowList.empty())
178178
AllowList += "|";
179179
AllowList += "DeviceType:" + SyclDeviceType.first;

sycl/unittests/pi/EnqueueMemTest.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ class EnqueueMemTest : public testing::TestWithParam<detail::plugin> {
105105
}
106106
};
107107

108-
static std::vector<detail::plugin> Plugins = pi::initializeAndRemoveInvalid();
109-
110108
INSTANTIATE_TEST_CASE_P(
111-
EnqueueMemTestImpl, EnqueueMemTest, testing::ValuesIn(Plugins),
109+
EnqueueMemTestImpl, EnqueueMemTest,
110+
testing::ValuesIn(pi::initializeAndRemoveInvalid()),
112111
[](const testing::TestParamInfo<EnqueueMemTest::ParamType> &info) {
113112
return pi::GetBackendString(info.param.getBackend());
114113
});

sycl/unittests/pi/PlatformTest.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ class PlatformTest : public testing::TestWithParam<detail::plugin> {
6262
}
6363
};
6464

65-
static std::vector<detail::plugin> Plugins = pi::initializeAndRemoveInvalid();
66-
6765
INSTANTIATE_TEST_CASE_P(
68-
PlatformTestImpl, PlatformTest, testing::ValuesIn(Plugins),
66+
PlatformTestImpl, PlatformTest,
67+
testing::ValuesIn(pi::initializeAndRemoveInvalid()),
6968
[](const testing::TestParamInfo<PlatformTest::ParamType> &info) {
7069
return pi::GetBackendString(info.param.getBackend());
7170
});

0 commit comments

Comments
 (0)