Skip to content

Commit e672f51

Browse files
committed
[mlir][spirv] Add a field for client API in target environment
SPIR-V can be directly consumed by APIs like Vulkan and OpenCL, where we can use the capability list to diffferentiate. It can also be used as a compilation target to transcompile to shading languages like WGSL to target WebGPU. We have no way to tell that with just the capability list, so we cannot perform certain transformations only applicable to those targets thus far. So this commit add a field in the target environment to indicate the client API for such purposes. Reviewed By: kuhar Differential Revision: https://reviews.llvm.org/D138732
1 parent 410c1f6 commit e672f51

5 files changed

Lines changed: 101 additions & 32 deletions

File tree

‎mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAttributes.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ class TargetEnvAttr
138138
using Base::Base;
139139

140140
/// Gets a TargetEnvAttr instance.
141-
static TargetEnvAttr get(VerCapExtAttr triple, Vendor vendorID,
142-
DeviceType deviceType, uint32_t deviceId,
143-
ResourceLimitsAttr limits);
141+
static TargetEnvAttr get(VerCapExtAttr triple, ResourceLimitsAttr limits,
142+
ClientAPI clientAPI = ClientAPI::Unknown,
143+
Vendor vendorID = Vendor::Unknown,
144+
DeviceType deviceType = DeviceType::Unknown,
145+
uint32_t deviceId = kUnknownDeviceID);
144146

145147
/// Returns the attribute kind's name (without the 'spirv.' prefix).
146148
static StringRef getKindName();
@@ -161,6 +163,9 @@ class TargetEnvAttr
161163
/// Returns the target capabilities as an integer array attribute.
162164
ArrayAttr getCapabilitiesAttr();
163165

166+
/// Returns the client API.
167+
ClientAPI getClientAPI() const;
168+
164169
/// Returns the vendor ID.
165170
Vendor getVendorID() const;
166171

‎mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def SPIRV_DT_IntegratedGPU : I32EnumAttrCase<"IntegratedGPU", 2>;
267267
// An accelerator other than GPU or CPU
268268
def SPIRV_DT_Other : I32EnumAttrCase<"Other", 3>;
269269
// Information missing.
270-
def SPIRV_DT_Unknown : I32EnumAttrCase<"Unknown", 4>;
270+
def SPIRV_DT_Unknown : I32EnumAttrCase<"Unknown", 0xffffffff>;
271271

272272
def SPIRV_DeviceTypeAttr : SPIRV_I32EnumAttr<
273273
"DeviceType", "valid SPIR-V device types", "device_type", [
@@ -283,7 +283,7 @@ def SPIRV_V_Intel : I32EnumAttrCase<"Intel", 4>;
283283
def SPIRV_V_NVIDIA : I32EnumAttrCase<"NVIDIA", 5>;
284284
def SPIRV_V_Qualcomm : I32EnumAttrCase<"Qualcomm", 6>;
285285
def SPIRV_V_SwiftShader : I32EnumAttrCase<"SwiftShader", 7>;
286-
def SPIRV_V_Unknown : I32EnumAttrCase<"Unknown", 0xff>;
286+
def SPIRV_V_Unknown : I32EnumAttrCase<"Unknown", 0xffffffff>;
287287

288288
def SPIRV_VendorAttr : SPIRV_I32EnumAttr<
289289
"Vendor", "recognized SPIR-V vendor strings", "vendor", [
@@ -292,6 +292,18 @@ def SPIRV_VendorAttr : SPIRV_I32EnumAttr<
292292
SPIRV_V_Unknown
293293
]>;
294294

295+
def SPIRV_CA_Metal : I32EnumAttrCase<"Metal", 0>;
296+
def SPIRV_CA_OpenCL : I32EnumAttrCase<"OpenCL", 1>;
297+
def SPIRV_CA_Vulkan : I32EnumAttrCase<"Vulkan", 2>;
298+
def SPIRV_CA_WebGPU : I32EnumAttrCase<"WebGPU", 3>;
299+
def SPIRV_CA_Unknown : I32EnumAttrCase<"Unknown", 0xffffffff>;
300+
301+
def SPIRV_ClientAPIAttr : SPIRV_I32EnumAttr<
302+
"ClientAPI", "recognized SPIR-V client APIs", "client_api", [
303+
SPIRV_CA_Metal, SPIRV_CA_OpenCL, SPIRV_CA_Vulkan, SPIRV_CA_WebGPU,
304+
SPIRV_CA_Unknown
305+
]>;
306+
295307
//===----------------------------------------------------------------------===//
296308
// SPIR-V extension definitions
297309
//===----------------------------------------------------------------------===//

‎mlir/lib/Dialect/SPIRV/IR/SPIRVAttributes.cpp‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,31 @@ struct VerCapExtAttributeStorage : public AttributeStorage {
8282
};
8383

8484
struct TargetEnvAttributeStorage : public AttributeStorage {
85-
using KeyTy = std::tuple<Attribute, Vendor, DeviceType, uint32_t, Attribute>;
85+
using KeyTy =
86+
std::tuple<Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute>;
8687

87-
TargetEnvAttributeStorage(Attribute triple, Vendor vendorID,
88-
DeviceType deviceType, uint32_t deviceID,
89-
Attribute limits)
90-
: triple(triple), limits(limits), vendorID(vendorID),
91-
deviceType(deviceType), deviceID(deviceID) {}
88+
TargetEnvAttributeStorage(Attribute triple, ClientAPI clientAPI,
89+
Vendor vendorID, DeviceType deviceType,
90+
uint32_t deviceID, Attribute limits)
91+
: triple(triple), limits(limits), clientAPI(clientAPI),
92+
vendorID(vendorID), deviceType(deviceType), deviceID(deviceID) {}
9293

9394
bool operator==(const KeyTy &key) const {
94-
return key ==
95-
std::make_tuple(triple, vendorID, deviceType, deviceID, limits);
95+
return key == std::make_tuple(triple, clientAPI, vendorID, deviceType,
96+
deviceID, limits);
9697
}
9798

9899
static TargetEnvAttributeStorage *
99100
construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
100101
return new (allocator.allocate<TargetEnvAttributeStorage>())
101102
TargetEnvAttributeStorage(std::get<0>(key), std::get<1>(key),
102103
std::get<2>(key), std::get<3>(key),
103-
std::get<4>(key));
104+
std::get<4>(key), std::get<5>(key));
104105
}
105106

106107
Attribute triple;
107108
Attribute limits;
109+
ClientAPI clientAPI;
108110
Vendor vendorID;
109111
DeviceType deviceType;
110112
uint32_t deviceID;
@@ -282,14 +284,13 @@ spirv::VerCapExtAttr::verify(function_ref<InFlightDiagnostic()> emitError,
282284
// TargetEnvAttr
283285
//===----------------------------------------------------------------------===//
284286

285-
spirv::TargetEnvAttr spirv::TargetEnvAttr::get(spirv::VerCapExtAttr triple,
286-
Vendor vendorID,
287-
DeviceType deviceType,
288-
uint32_t deviceID,
289-
ResourceLimitsAttr limits) {
287+
spirv::TargetEnvAttr spirv::TargetEnvAttr::get(
288+
spirv::VerCapExtAttr triple, ResourceLimitsAttr limits, ClientAPI clientAPI,
289+
Vendor vendorID, DeviceType deviceType, uint32_t deviceID) {
290290
assert(triple && limits && "expected valid triple and limits");
291291
MLIRContext *context = triple.getContext();
292-
return Base::get(context, triple, vendorID, deviceType, deviceID, limits);
292+
return Base::get(context, triple, clientAPI, vendorID, deviceType, deviceID,
293+
limits);
293294
}
294295

295296
StringRef spirv::TargetEnvAttr::getKindName() { return "target_env"; }
@@ -318,6 +319,10 @@ ArrayAttr spirv::TargetEnvAttr::getCapabilitiesAttr() {
318319
return getTripleAttr().getCapabilitiesAttr();
319320
}
320321

322+
spirv::ClientAPI spirv::TargetEnvAttr::getClientAPI() const {
323+
return getImpl()->clientAPI;
324+
}
325+
321326
spirv::Vendor spirv::TargetEnvAttr::getVendorID() const {
322327
return getImpl()->vendorID;
323328
}
@@ -523,6 +528,22 @@ static Attribute parseTargetEnvAttr(DialectAsmParser &parser) {
523528
if (parser.parseAttribute(tripleAttr) || parser.parseComma())
524529
return {};
525530

531+
auto clientAPI = spirv::ClientAPI::Unknown;
532+
if (succeeded(parser.parseOptionalKeyword("api"))) {
533+
if (parser.parseEqual())
534+
return {};
535+
auto loc = parser.getCurrentLocation();
536+
StringRef apiStr;
537+
if (parser.parseKeyword(&apiStr))
538+
return {};
539+
if (auto apiSymbol = spirv::symbolizeClientAPI(apiStr))
540+
clientAPI = *apiSymbol;
541+
else
542+
parser.emitError(loc, "unknown client API: ") << apiStr;
543+
if (parser.parseComma())
544+
return {};
545+
}
546+
526547
// Parse [vendor[:device-type[:device-id]]]
527548
Vendor vendorID = Vendor::Unknown;
528549
DeviceType deviceType = DeviceType::Unknown;
@@ -531,22 +552,20 @@ static Attribute parseTargetEnvAttr(DialectAsmParser &parser) {
531552
auto loc = parser.getCurrentLocation();
532553
StringRef vendorStr;
533554
if (succeeded(parser.parseOptionalKeyword(&vendorStr))) {
534-
if (auto vendorSymbol = spirv::symbolizeVendor(vendorStr)) {
555+
if (auto vendorSymbol = spirv::symbolizeVendor(vendorStr))
535556
vendorID = *vendorSymbol;
536-
} else {
557+
else
537558
parser.emitError(loc, "unknown vendor: ") << vendorStr;
538-
}
539559

540560
if (succeeded(parser.parseOptionalColon())) {
541561
loc = parser.getCurrentLocation();
542562
StringRef deviceTypeStr;
543563
if (parser.parseKeyword(&deviceTypeStr))
544564
return {};
545-
if (auto deviceTypeSymbol = spirv::symbolizeDeviceType(deviceTypeStr)) {
565+
if (auto deviceTypeSymbol = spirv::symbolizeDeviceType(deviceTypeStr))
546566
deviceType = *deviceTypeSymbol;
547-
} else {
567+
else
548568
parser.emitError(loc, "unknown device type: ") << deviceTypeStr;
549-
}
550569

551570
if (succeeded(parser.parseOptionalColon())) {
552571
loc = parser.getCurrentLocation();
@@ -563,8 +582,8 @@ static Attribute parseTargetEnvAttr(DialectAsmParser &parser) {
563582
if (parser.parseAttribute(limitsAttr) || parser.parseGreater())
564583
return {};
565584

566-
return spirv::TargetEnvAttr::get(tripleAttr, vendorID, deviceType, deviceID,
567-
limitsAttr);
585+
return spirv::TargetEnvAttr::get(tripleAttr, limitsAttr, clientAPI, vendorID,
586+
deviceType, deviceID);
568587
}
569588

570589
Attribute SPIRVDialect::parseAttribute(DialectAsmParser &parser,
@@ -616,6 +635,9 @@ static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer) {
616635
static void print(spirv::TargetEnvAttr targetEnv, DialectAsmPrinter &printer) {
617636
printer << spirv::TargetEnvAttr::getKindName() << "<#spirv.";
618637
print(targetEnv.getTripleAttr(), printer);
638+
auto clientAPI = targetEnv.getClientAPI();
639+
if (clientAPI != spirv::ClientAPI::Unknown)
640+
printer << ", api=" << clientAPI;
619641
spirv::Vendor vendorID = targetEnv.getVendorID();
620642
spirv::DeviceType deviceType = targetEnv.getDeviceType();
621643
uint32_t deviceID = targetEnv.getDeviceID();

‎mlir/lib/Dialect/SPIRV/IR/TargetAndABI.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "mlir/Dialect/SPIRV/IR/TargetAndABI.h"
10+
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
1011
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
1112
#include "mlir/IR/Builders.h"
1213
#include "mlir/IR/FunctionInterfaces.h"
@@ -170,10 +171,10 @@ spirv::TargetEnvAttr spirv::getDefaultTargetEnv(MLIRContext *context) {
170171
auto triple = spirv::VerCapExtAttr::get(spirv::Version::V_1_0,
171172
{spirv::Capability::Shader},
172173
ArrayRef<Extension>(), context);
173-
return spirv::TargetEnvAttr::get(triple, spirv::Vendor::Unknown,
174-
spirv::DeviceType::Unknown,
175-
spirv::TargetEnvAttr::kUnknownDeviceID,
176-
spirv::getDefaultResourceLimits(context));
174+
return spirv::TargetEnvAttr::get(
175+
triple, spirv::getDefaultResourceLimits(context),
176+
spirv::ClientAPI::Unknown, spirv::Vendor::Unknown,
177+
spirv::DeviceType::Unknown, spirv::TargetEnvAttr::kUnknownDeviceID);
177178
}
178179

179180
spirv::TargetEnvAttr spirv::lookupTargetEnv(Operation *op) {

‎mlir/test/Dialect/SPIRV/IR/target-and-abi.mlir‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ func.func @target_env() attributes {
118118

119119
// -----
120120

121+
func.func @target_env_client_api() attributes {
122+
// CHECK: spirv.target_env = #spirv.target_env<
123+
// CHECK-SAME: #spirv.vce<v1.0, [], []>,
124+
// CHECK-SAME: api=Metal,
125+
// CHECK-SAME: #spirv.resource_limits<>>
126+
spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [], []>, api=Metal, #spirv.resource_limits<>>
127+
} { return }
128+
129+
// -----
130+
131+
func.func @target_env_client_api() attributes {
132+
// CHECK: spirv.target_env = #spirv.target_env
133+
// CHECK-NOT: api=
134+
spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [], []>, api=Unknown, #spirv.resource_limits<>>
135+
} { return }
136+
137+
// -----
138+
121139
func.func @target_env_vendor_id() attributes {
122140
// CHECK: spirv.target_env = #spirv.target_env<
123141
// CHECK-SAME: #spirv.vce<v1.0, [], []>,
@@ -148,6 +166,17 @@ func.func @target_env_vendor_id_device_type_device_id() attributes {
148166

149167
// -----
150168

169+
func.func @target_env_client_api_vendor_id_device_type_device_id() attributes {
170+
// CHECK: spirv.target_env = #spirv.target_env<
171+
// CHECK-SAME: #spirv.vce<v1.0, [], []>,
172+
// CHECK-SAME: api=Vulkan,
173+
// CHECK-SAME: Qualcomm:IntegratedGPU:100925441,
174+
// CHECK-SAME: #spirv.resource_limits<>>
175+
spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [], []>, api=Vulkan, Qualcomm:IntegratedGPU:0x6040001, #spirv.resource_limits<>>
176+
} { return }
177+
178+
// -----
179+
151180
func.func @target_env_extra_fields() attributes {
152181
// expected-error @+3 {{expected '>'}}
153182
spirv.target_env = #spirv.target_env<

0 commit comments

Comments
 (0)