Skip to content

Commit a81cb1b

Browse files
committed
[mlir][spirv] Allow specifying availability on enum attribute cases
Lots of SPIR-V ops take enum attributes and certain enum cases need extra capabilities or extensions to be available. This commit extends to allow specifying availability spec on enum cases. Extra utility functions are generated for the corresponding enum classes to return the availability requirement. The availability interface implemention for a SPIR-V op now goes over all enum attributes to collect the availability requirements. Reviewed By: mravishankar Differential Revision: https://reviews.llvm.org/D71947
1 parent 108daf7 commit a81cb1b

File tree

10 files changed

+328
-18
lines changed

10 files changed

+328
-18
lines changed

mlir/include/mlir/Dialect/SPIRV/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ mlir_tablegen(SPIRVEnums.h.inc -gen-enum-decls)
55
mlir_tablegen(SPIRVEnums.cpp.inc -gen-enum-defs)
66
add_public_tablegen_target(MLIRSPIRVEnumsIncGen)
77

8+
set(LLVM_TARGET_DEFINITIONS SPIRVBase.td)
9+
mlir_tablegen(SPIRVEnumAvailability.h.inc -gen-spirv-enum-avail-decls)
10+
mlir_tablegen(SPIRVEnumAvailability.cpp.inc -gen-spirv-enum-avail-defs)
11+
add_public_tablegen_target(MLIRSPIRVEnumAvailabilityIncGen)
12+
813
set(LLVM_TARGET_DEFINITIONS SPIRVOps.td)
914
mlir_tablegen(SPIRVAvailability.h.inc -gen-avail-interface-decls)
1015
mlir_tablegen(SPIRVAvailability.cpp.inc -gen-avail-interface-defs)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,13 @@ def SPV_CapabilityAttr :
467467
def SPV_AM_Logical : I32EnumAttrCase<"Logical", 0>;
468468
def SPV_AM_Physical32 : I32EnumAttrCase<"Physical32", 1>;
469469
def SPV_AM_Physical64 : I32EnumAttrCase<"Physical64", 2>;
470-
def SPV_AM_PhysicalStorageBuffer64 : I32EnumAttrCase<"PhysicalStorageBuffer64", 5348>;
470+
def SPV_AM_PhysicalStorageBuffer64 : I32EnumAttrCase<"PhysicalStorageBuffer64", 5348> {
471+
list<Availability> availability = [
472+
MinVersion<SPV_V_1_5>,
473+
Extension<[SPV_EXT_physical_storage_buffer, SPV_KHR_physical_storage_buffer]>,
474+
Capability<[SPV_C_PhysicalStorageBufferAddresses]>
475+
];
476+
}
471477

472478
def SPV_AddressingModelAttr :
473479
I32EnumAttr<"AddressingModel", "valid SPIR-V AddressingModel", [
@@ -944,7 +950,12 @@ def SPV_MemoryAccessAttr :
944950
def SPV_MM_Simple : I32EnumAttrCase<"Simple", 0>;
945951
def SPV_MM_GLSL450 : I32EnumAttrCase<"GLSL450", 1>;
946952
def SPV_MM_OpenCL : I32EnumAttrCase<"OpenCL", 2>;
947-
def SPV_MM_Vulkan : I32EnumAttrCase<"Vulkan", 3>;
953+
def SPV_MM_Vulkan : I32EnumAttrCase<"Vulkan", 3> {
954+
list<Availability> availability = [
955+
MinVersion<SPV_V_1_5>,
956+
Capability<[SPV_C_VulkanMemoryModel]>
957+
];
958+
}
948959

949960
def SPV_MemoryModelAttr :
950961
I32EnumAttr<"MemoryModel", "valid SPIR-V MemoryModel", [

mlir/include/mlir/Dialect/SPIRV/SPIRVTypes.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@
1717
#include "mlir/IR/TypeSupport.h"
1818
#include "mlir/IR/Types.h"
1919

20+
// Forward declare enum classes related to op availability. Their definitions
21+
// are in the TableGen'erated SPIRVEnums.h.inc and can be referenced by other
22+
// dclarations in SPIRVEnums.h.inc.
23+
namespace mlir {
24+
namespace spirv {
25+
enum class Version : uint32_t;
26+
enum class Extension;
27+
enum class Capability : uint32_t;
28+
} // namespace spirv
29+
} // namespace mlir
30+
2031
// Pull in all enum type definitions and utility function declarations
2132
#include "mlir/Dialect/SPIRV/SPIRVEnums.h.inc"
33+
// Pull in all enum type availability query function declarations
34+
#include "mlir/Dialect/SPIRV/SPIRVEnumAvailability.h.inc"
2235

2336
#include <tuple>
2437

mlir/include/mlir/TableGen/Attribute.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class EnumAttrCase : public Attribute {
135135

136136
// Returns the value of this enum attribute case.
137137
int64_t getValue() const;
138+
139+
// Returns the TableGen definition this EnumAttrCase was constructed from.
140+
const llvm::Record &getDef() const;
138141
};
139142

140143
// Wrapper class providing helper methods for accessing enum attributes defined
@@ -146,6 +149,8 @@ class EnumAttr : public Attribute {
146149
explicit EnumAttr(const llvm::Record &record);
147150
explicit EnumAttr(const llvm::DefInit *init);
148151

152+
static bool classof(const Attribute *attr);
153+
149154
// Returns true if this is a bit enum attribute.
150155
bool isBitEnum() const;
151156

mlir/lib/Dialect/SPIRV/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_llvm_library(MLIRSPIRV
1818
add_dependencies(MLIRSPIRV
1919
MLIRSPIRVAvailabilityIncGen
2020
MLIRSPIRVCanonicalizationIncGen
21+
MLIRSPIRVEnumAvailabilityIncGen
2122
MLIRSPIRVEnumsIncGen
2223
MLIRSPIRVOpsIncGen
2324
MLIRSPIRVOpUtilsGen

mlir/lib/Dialect/SPIRV/SPIRVTypes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ using namespace mlir::spirv;
2222

2323
// Pull in all enum utility function definitions
2424
#include "mlir/Dialect/SPIRV/SPIRVEnums.cpp.inc"
25+
// Pull in all enum type availability query function definitions
26+
#include "mlir/Dialect/SPIRV/SPIRVEnumAvailability.cpp.inc"
2527

2628
//===----------------------------------------------------------------------===//
2729
// ArrayType

mlir/lib/TableGen/Attribute.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ int64_t tblgen::EnumAttrCase::getValue() const {
155155
return def->getValueAsInt("value");
156156
}
157157

158+
const llvm::Record &tblgen::EnumAttrCase::getDef() const { return *def; }
159+
158160
tblgen::EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) {
159161
assert(isSubClassOf("EnumAttrInfo") &&
160162
"must be subclass of TableGen 'EnumAttr' class");
@@ -165,6 +167,10 @@ tblgen::EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {}
165167
tblgen::EnumAttr::EnumAttr(const llvm::DefInit *init)
166168
: EnumAttr(init->getDef()) {}
167169

170+
bool tblgen::EnumAttr::classof(const Attribute *attr) {
171+
return attr->isSubClassOf("EnumAttrInfo");
172+
}
173+
168174
bool tblgen::EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); }
169175

170176
StringRef tblgen::EnumAttr::getEnumClassName() const {

mlir/test/Dialect/SPIRV/TestAvailability.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ void TestAvailability::runOnFunction() {
3030
if (op->getDialect() != spvDialect)
3131
return WalkResult::advance();
3232

33+
auto opName = op->getName();
3334
auto &os = llvm::outs();
3435

3536
if (auto minVersion = dyn_cast<spirv::QueryMinVersionInterface>(op))
36-
os << " min version: "
37+
os << opName << " min version: "
3738
<< spirv::stringifyVersion(minVersion.getMinVersion()) << "\n";
3839

3940
if (auto maxVersion = dyn_cast<spirv::QueryMaxVersionInterface>(op))
40-
os << " max version: "
41+
os << opName << " max version: "
4142
<< spirv::stringifyVersion(maxVersion.getMaxVersion()) << "\n";
4243

4344
if (auto extension = dyn_cast<spirv::QueryExtensionInterface>(op)) {
44-
os << " extensions: [";
45+
os << opName << " extensions: [";
4546
for (const auto &exts : extension.getExtensions()) {
4647
os << " [";
4748
interleaveComma(exts, os, [&](spirv::Extension ext) {
@@ -53,7 +54,7 @@ void TestAvailability::runOnFunction() {
5354
}
5455

5556
if (auto capability = dyn_cast<spirv::QueryCapabilityInterface>(op)) {
56-
os << " capabilities: [";
57+
os << opName << " capabilities: [";
5758
for (const auto &caps : capability.getCapabilities()) {
5859
os << " [";
5960
interleaveComma(caps, os, [&](spirv::Capability cap) {

mlir/test/Dialect/SPIRV/availability.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ func @subgroup_ballot(%predicate: i1) -> vector<4xi32> {
2929
%0 = spv.GroupNonUniformBallot "Workgroup" %predicate : vector<4xi32>
3030
return %0: vector<4xi32>
3131
}
32+
33+
// CHECK-LABEL: module_logical_glsl450
34+
func @module_logical_glsl450() {
35+
// CHECK: spv.module min version: V_1_0
36+
// CHECK: spv.module max version: V_1_5
37+
// CHECK: spv.module extensions: [ ]
38+
// CHECK: spv.module capabilities: [ ]
39+
spv.module "Logical" "GLSL450" { }
40+
return
41+
}
42+
43+
// CHECK-LABEL: module_physical_storage_buffer64_vulkan
44+
func @module_physical_storage_buffer64_vulkan() {
45+
// CHECK: spv.module min version: V_1_5
46+
// CHECK: spv.module max version: V_1_5
47+
// CHECK: spv.module extensions: [ [SPV_EXT_physical_storage_buffer, SPV_KHR_physical_storage_buffer] ]
48+
// CHECK: spv.module capabilities: [ [PhysicalStorageBufferAddresses] [VulkanMemoryModel] ]
49+
spv.module "PhysicalStorageBuffer64" "Vulkan" { }
50+
return
51+
}

0 commit comments

Comments
 (0)