Skip to content

Commit 5d55d8e

Browse files
isaacaultKornevNikita
authored andcommitted
[UR] Fix Loader Negative Prefilters (#17528)
This commit fixes #17086.
1 parent e0cf4fa commit 5d55d8e

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

unified-runtime/scripts/core/manifests.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#
2+
# Copyright (C) 2025 Intel Corporation
3+
#
4+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
5+
# See LICENSE.TXT
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
#
8+
# See YaML.md for syntax definition
9+
#
10+
# NOTE: device_types must be explictly listed. Refrain from using
11+
# $X_DEVICE_TYPE_ALL. See https://github.com/intel/llvm/issues/17527.
112
--- #--------------------------------------------------------------------------
213
type: header
314
desc: "Intel $OneApi Unified Runtime adapter manifests"
@@ -7,7 +18,11 @@ type: manifest
718
name: opencl
819
backend: $X_ADAPTER_BACKEND_OPENCL
920
device_types:
10-
- $X_DEVICE_TYPE_ALL
21+
- $X_DEVICE_TYPE_CPU
22+
- $X_DEVICE_TYPE_GPU
23+
- $X_DEVICE_TYPE_FPGA
24+
- $X_DEVICE_TYPE_MCA
25+
- $X_DEVICE_TYPE_VPU
1126
--- #--------------------------------------------------------------------------
1227
type: manifest
1328
name: cuda
@@ -25,13 +40,21 @@ type: manifest
2540
name: level_zero
2641
backend: $X_ADAPTER_BACKEND_LEVEL_ZERO
2742
device_types:
28-
- $X_DEVICE_TYPE_ALL
43+
- $X_DEVICE_TYPE_CPU
44+
- $X_DEVICE_TYPE_GPU
45+
- $X_DEVICE_TYPE_FPGA
46+
- $X_DEVICE_TYPE_MCA
47+
- $X_DEVICE_TYPE_VPU
2948
--- #--------------------------------------------------------------------------
3049
type: manifest
3150
name: level_zero_v2
3251
backend: $X_ADAPTER_BACKEND_LEVEL_ZERO
3352
device_types:
34-
- $X_DEVICE_TYPE_ALL
53+
- $X_DEVICE_TYPE_CPU
54+
- $X_DEVICE_TYPE_GPU
55+
- $X_DEVICE_TYPE_FPGA
56+
- $X_DEVICE_TYPE_MCA
57+
- $X_DEVICE_TYPE_VPU
3558
--- #--------------------------------------------------------------------------
3659
type: manifest
3760
name: native_cpu

unified-runtime/source/loader/ur_adapter_registry.hpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace fs = filesystem;
2424

2525
namespace ur_loader {
2626

27+
struct ur_device_tuple {
28+
ur_adapter_backend_t backend;
29+
ur_device_type_t device;
30+
};
31+
2732
// Helper struct representing a ONEAPI_DEVICE_SELECTOR filter term.
2833
struct FilterTerm {
2934
std::string backend;
@@ -37,7 +42,7 @@ struct FilterTerm {
3742
{"native_cpu", UR_ADAPTER_BACKEND_NATIVE_CPU},
3843
};
3944

40-
bool matchesBackend(const ur_adapter_manifest &manifest) const {
45+
bool matchesBackend(const ur_adapter_backend_t &match_backend) const {
4146
if (backend.front() == '*') {
4247
return true;
4348
}
@@ -49,7 +54,7 @@ struct FilterTerm {
4954
backend);
5055
return false;
5156
}
52-
if (backendIter->second == manifest.backend) {
57+
if (backendIter->second == match_backend) {
5358
return true;
5459
}
5560
return false;
@@ -60,12 +65,7 @@ struct FilterTerm {
6065
{"gpu", UR_DEVICE_TYPE_GPU},
6166
{"fpga", UR_DEVICE_TYPE_FPGA}};
6267

63-
bool matchesDevices(const ur_adapter_manifest &manifest) const {
64-
// If the adapter can report all device types then it matches.
65-
if (std::find(manifest.device_types.begin(), manifest.device_types.end(),
66-
UR_DEVICE_TYPE_ALL) != manifest.device_types.end()) {
67-
return true;
68-
}
68+
bool matchesDevices(const ur_device_type_t &match_device) const {
6969
for (auto deviceString : devices) {
7070
// We don't have a way to determine anything about device indices or
7171
// sub-devices at this stage so just match any numeric value we get.
@@ -79,20 +79,19 @@ struct FilterTerm {
7979
deviceString);
8080
continue;
8181
}
82-
if (std::find(manifest.device_types.begin(), manifest.device_types.end(),
83-
deviceIter->second) != manifest.device_types.end()) {
82+
if (deviceIter->second == match_device) {
8483
return true;
8584
}
8685
}
8786
return false;
8887
}
8988

90-
bool matches(const ur_adapter_manifest &manifest) const {
91-
if (!matchesBackend(manifest)) {
89+
bool matches(const ur_device_tuple &device_tuple) const {
90+
if (!matchesBackend(device_tuple.backend)) {
9291
return false;
9392
}
9493

95-
return matchesDevices(manifest);
94+
return matchesDevices(device_tuple.device);
9695
}
9796
};
9897

@@ -280,22 +279,31 @@ class AdapterRegistry {
280279
if (PositiveFilter) {
281280
positiveFilters.push_back({backend, termPair.second});
282281
} else {
283-
// To preserve the behaviour of the original pre-filter implementation,
284-
// we interpret all negative filters as backend only. This isn't
285-
// correct, see https://github.com/intel/llvm/issues/17086
286-
negativeFilters.push_back({backend, {"*"}});
282+
negativeFilters.push_back({backend, termPair.second});
287283
}
288284
}
289285

286+
// If ONEAPI_DEVICE_SELECTOR only specified negative filters then we
287+
// implicitly add a positive filter accepting all backends and devices.
288+
if (positiveFilters.empty()) {
289+
positiveFilters.push_back({"*", {"*"}});
290+
}
291+
290292
for (const auto &manifest : ur_adapter_manifests) {
291-
auto matchesFilter = [manifest](const FilterTerm &f) -> bool {
292-
return f.matches(manifest);
293-
};
294-
if (std::any_of(positiveFilters.begin(), positiveFilters.end(),
295-
matchesFilter) &&
296-
std::none_of(negativeFilters.begin(), negativeFilters.end(),
297-
matchesFilter)) {
298-
adapterNames.insert(manifest.library);
293+
// Check each device in the manifest.
294+
for (const auto &device : manifest.device_types) {
295+
ur_device_tuple single_device = {manifest.backend, device};
296+
297+
auto matchesFilter = [single_device](const FilterTerm &f) -> bool {
298+
return f.matches(single_device);
299+
};
300+
301+
if (std::any_of(positiveFilters.begin(), positiveFilters.end(),
302+
matchesFilter) &&
303+
std::none_of(negativeFilters.begin(), negativeFilters.end(),
304+
matchesFilter)) {
305+
adapterNames.insert(manifest.library);
306+
}
299307
}
300308
}
301309

unified-runtime/source/loader/ur_manifests.hpp

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)