forked from OSchip/llvm-project
[mlir][spirv] NFC: put SPIR-V attributes in separate files
Differential Revision: https://reviews.llvm.org/D75871
This commit is contained in:
parent
9414db1090
commit
c818c3cc96
|
@ -0,0 +1,137 @@
|
|||
//===- SPIRVAttributes.h - SPIR-V attribute declarations -------*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file declares SPIR-V dialect specific attributes.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H
|
||||
#define MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H
|
||||
|
||||
#include "mlir/IR/Attributes.h"
|
||||
#include "mlir/Support/LLVM.h"
|
||||
|
||||
namespace mlir {
|
||||
// Pull in SPIR-V attribute definitions for target and ABI.
|
||||
#include "mlir/Dialect/SPIRV/TargetAndABI.h.inc"
|
||||
|
||||
namespace spirv {
|
||||
enum class Capability : uint32_t;
|
||||
enum class Extension;
|
||||
enum class Version : uint32_t;
|
||||
|
||||
namespace detail {
|
||||
struct TargetEnvAttributeStorage;
|
||||
struct VerCapExtAttributeStorage;
|
||||
} // namespace detail
|
||||
|
||||
/// SPIR-V dialect-specific attribute kinds.
|
||||
namespace AttrKind {
|
||||
enum Kind {
|
||||
TargetEnv = Attribute::FIRST_SPIRV_ATTR, /// Target environment
|
||||
VerCapExt, /// (version, extension, capability) triple
|
||||
};
|
||||
} // namespace AttrKind
|
||||
|
||||
/// An attribute that specifies the SPIR-V (version, capabilities, extensions)
|
||||
/// triple.
|
||||
class VerCapExtAttr
|
||||
: public Attribute::AttrBase<VerCapExtAttr, Attribute,
|
||||
detail::VerCapExtAttributeStorage> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
/// Gets a VerCapExtAttr instance.
|
||||
static VerCapExtAttr get(Version version, ArrayRef<Capability> capabilities,
|
||||
ArrayRef<Extension> extensions,
|
||||
MLIRContext *context);
|
||||
static VerCapExtAttr get(IntegerAttr version, ArrayAttr capabilities,
|
||||
ArrayAttr extensions);
|
||||
|
||||
/// Returns the attribute kind's name (without the 'spv.' prefix).
|
||||
static StringRef getKindName();
|
||||
|
||||
/// Returns the version.
|
||||
Version getVersion();
|
||||
|
||||
struct ext_iterator final
|
||||
: public llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
Extension (*)(Attribute)> {
|
||||
explicit ext_iterator(ArrayAttr::iterator it);
|
||||
};
|
||||
using ext_range = llvm::iterator_range<ext_iterator>;
|
||||
|
||||
/// Returns the extensions.
|
||||
ext_range getExtensions();
|
||||
/// Returns the extensions as a string array attribute.
|
||||
ArrayAttr getExtensionsAttr();
|
||||
|
||||
struct cap_iterator final
|
||||
: public llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
Capability (*)(Attribute)> {
|
||||
explicit cap_iterator(ArrayAttr::iterator it);
|
||||
};
|
||||
using cap_range = llvm::iterator_range<cap_iterator>;
|
||||
|
||||
/// Returns the capabilities.
|
||||
cap_range getCapabilities();
|
||||
/// Returns the capabilities as an integer array attribute.
|
||||
ArrayAttr getCapabilitiesAttr();
|
||||
|
||||
static bool kindof(unsigned kind) { return kind == AttrKind::VerCapExt; }
|
||||
|
||||
static LogicalResult verifyConstructionInvariants(Location loc,
|
||||
IntegerAttr version,
|
||||
ArrayAttr capabilities,
|
||||
ArrayAttr extensions);
|
||||
};
|
||||
|
||||
/// An attribute that specifies the target version, allowed extensions and
|
||||
/// capabilities, and resource limits. These information describles a SPIR-V
|
||||
/// target environment.
|
||||
class TargetEnvAttr
|
||||
: public Attribute::AttrBase<TargetEnvAttr, Attribute,
|
||||
detail::TargetEnvAttributeStorage> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
/// Gets a TargetEnvAttr instance.
|
||||
static TargetEnvAttr get(VerCapExtAttr triple, DictionaryAttr limits);
|
||||
|
||||
/// Returns the attribute kind's name (without the 'spv.' prefix).
|
||||
static StringRef getKindName();
|
||||
|
||||
/// Returns the (version, capabilities, extensions) triple attribute.
|
||||
VerCapExtAttr getTripleAttr();
|
||||
|
||||
/// Returns the target version.
|
||||
Version getVersion();
|
||||
|
||||
/// Returns the target extensions.
|
||||
VerCapExtAttr::ext_range getExtensions();
|
||||
/// Returns the target extensions as a string array attribute.
|
||||
ArrayAttr getExtensionsAttr();
|
||||
|
||||
/// Returns the target capabilities.
|
||||
VerCapExtAttr::cap_range getCapabilities();
|
||||
/// Returns the target capabilities as an integer array attribute.
|
||||
ArrayAttr getCapabilitiesAttr();
|
||||
|
||||
/// Returns the target resource limits.
|
||||
ResourceLimitsAttr getResourceLimits();
|
||||
|
||||
static bool kindof(unsigned kind) { return kind == AttrKind::TargetEnv; }
|
||||
|
||||
static LogicalResult verifyConstructionInvariants(Location loc,
|
||||
VerCapExtAttr triple,
|
||||
DictionaryAttr limits);
|
||||
};
|
||||
} // namespace spirv
|
||||
} // namespace mlir
|
||||
|
||||
#endif // MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H
|
|
@ -13,130 +13,14 @@
|
|||
#ifndef MLIR_DIALECT_SPIRV_TARGETANDABI_H
|
||||
#define MLIR_DIALECT_SPIRV_TARGETANDABI_H
|
||||
|
||||
#include "mlir/IR/Attributes.h"
|
||||
#include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
|
||||
#include "mlir/Support/LLVM.h"
|
||||
|
||||
namespace mlir {
|
||||
class OpBuilder;
|
||||
class Operation;
|
||||
class Value;
|
||||
|
||||
// Pull in SPIR-V attribute definitions.
|
||||
#include "mlir/Dialect/SPIRV/TargetAndABI.h.inc"
|
||||
|
||||
namespace spirv {
|
||||
enum class Capability : uint32_t;
|
||||
enum class Extension;
|
||||
enum class StorageClass : uint32_t;
|
||||
enum class Version : uint32_t;
|
||||
|
||||
namespace detail {
|
||||
struct TargetEnvAttributeStorage;
|
||||
struct VerCapExtAttributeStorage;
|
||||
} // namespace detail
|
||||
|
||||
/// SPIR-V dialect-specific attribute kinds.
|
||||
// TODO(antiagainst): move to a more suitable place if we have more attributes.
|
||||
namespace AttrKind {
|
||||
enum Kind {
|
||||
TargetEnv = Attribute::FIRST_SPIRV_ATTR, /// Target environment
|
||||
VerCapExt, /// (version, extension, capability) triple
|
||||
};
|
||||
} // namespace AttrKind
|
||||
|
||||
/// An attribute that specifies the SPIR-V (version, capabilities, extensions)
|
||||
/// triple.
|
||||
class VerCapExtAttr
|
||||
: public Attribute::AttrBase<VerCapExtAttr, Attribute,
|
||||
detail::VerCapExtAttributeStorage> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
/// Gets a VerCapExtAttr instance.
|
||||
static VerCapExtAttr get(Version version, ArrayRef<Capability> capabilities,
|
||||
ArrayRef<Extension> extensions,
|
||||
MLIRContext *context);
|
||||
static VerCapExtAttr get(IntegerAttr version, ArrayAttr capabilities,
|
||||
ArrayAttr extensions);
|
||||
|
||||
/// Returns the attribute kind's name (without the 'spv.' prefix).
|
||||
static StringRef getKindName();
|
||||
|
||||
/// Returns the version.
|
||||
Version getVersion();
|
||||
|
||||
struct ext_iterator final
|
||||
: public llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
Extension (*)(Attribute)> {
|
||||
explicit ext_iterator(ArrayAttr::iterator it);
|
||||
};
|
||||
using ext_range = llvm::iterator_range<ext_iterator>;
|
||||
|
||||
/// Returns the extensions.
|
||||
ext_range getExtensions();
|
||||
/// Returns the extensions as a string array attribute.
|
||||
ArrayAttr getExtensionsAttr();
|
||||
|
||||
struct cap_iterator final
|
||||
: public llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
Capability (*)(Attribute)> {
|
||||
explicit cap_iterator(ArrayAttr::iterator it);
|
||||
};
|
||||
using cap_range = llvm::iterator_range<cap_iterator>;
|
||||
|
||||
/// Returns the capabilities.
|
||||
cap_range getCapabilities();
|
||||
/// Returns the capabilities as an integer array attribute.
|
||||
ArrayAttr getCapabilitiesAttr();
|
||||
|
||||
static bool kindof(unsigned kind) { return kind == AttrKind::VerCapExt; }
|
||||
|
||||
static LogicalResult verifyConstructionInvariants(Location loc,
|
||||
IntegerAttr version,
|
||||
ArrayAttr capabilities,
|
||||
ArrayAttr extensions);
|
||||
};
|
||||
|
||||
/// An attribute that specifies the target version, allowed extensions and
|
||||
/// capabilities, and resource limits. These information describles a SPIR-V
|
||||
/// target environment.
|
||||
class TargetEnvAttr
|
||||
: public Attribute::AttrBase<TargetEnvAttr, Attribute,
|
||||
detail::TargetEnvAttributeStorage> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
/// Gets a TargetEnvAttr instance.
|
||||
static TargetEnvAttr get(VerCapExtAttr triple, DictionaryAttr limits);
|
||||
|
||||
/// Returns the attribute kind's name (without the 'spv.' prefix).
|
||||
static StringRef getKindName();
|
||||
|
||||
/// Returns the (version, capabilities, extensions) triple attribute.
|
||||
VerCapExtAttr getTripleAttr();
|
||||
|
||||
/// Returns the target version.
|
||||
Version getVersion();
|
||||
|
||||
/// Returns the target extensions.
|
||||
VerCapExtAttr::ext_range getExtensions();
|
||||
/// Returns the target extensions as a string array attribute.
|
||||
ArrayAttr getExtensionsAttr();
|
||||
|
||||
/// Returns the target capabilities.
|
||||
VerCapExtAttr::cap_range getCapabilities();
|
||||
/// Returns the target capabilities as an integer array attribute.
|
||||
ArrayAttr getCapabilitiesAttr();
|
||||
|
||||
/// Returns the target resource limits.
|
||||
ResourceLimitsAttr getResourceLimits();
|
||||
|
||||
static bool kindof(unsigned kind) { return kind == AttrKind::TargetEnv; }
|
||||
|
||||
static LogicalResult verifyConstructionInvariants(Location loc,
|
||||
VerCapExtAttr triple,
|
||||
DictionaryAttr limits);
|
||||
};
|
||||
|
||||
/// Returns the attribute name for specifying argument ABI information.
|
||||
StringRef getInterfaceVarABIAttrName();
|
||||
|
|
|
@ -4,6 +4,7 @@ add_public_tablegen_target(MLIRSPIRVCanonicalizationIncGen)
|
|||
|
||||
add_mlir_dialect_library(MLIRSPIRV
|
||||
LayoutUtils.cpp
|
||||
SPIRVAttributes.cpp
|
||||
SPIRVCanonicalization.cpp
|
||||
SPIRVDialect.cpp
|
||||
SPIRVOps.cpp
|
||||
|
|
|
@ -0,0 +1,220 @@
|
|||
//===- SPIRVAttributes.cpp - SPIR-V attribute definitions -----------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "mlir/Dialect/SPIRV/SPIRVAttributes.h"
|
||||
#include "mlir/Dialect/SPIRV/SPIRVTypes.h"
|
||||
#include "mlir/IR/Builders.h"
|
||||
|
||||
using namespace mlir;
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// DictionaryDict derived attributes
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace mlir {
|
||||
#include "mlir/Dialect/SPIRV/TargetAndABI.cpp.inc"
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Attribute storage classes
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace spirv {
|
||||
namespace detail {
|
||||
struct VerCapExtAttributeStorage : public AttributeStorage {
|
||||
using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
|
||||
|
||||
VerCapExtAttributeStorage(Attribute version, Attribute capabilities,
|
||||
Attribute extensions)
|
||||
: version(version), capabilities(capabilities), extensions(extensions) {}
|
||||
|
||||
bool operator==(const KeyTy &key) const {
|
||||
return std::get<0>(key) == version && std::get<1>(key) == capabilities &&
|
||||
std::get<2>(key) == extensions;
|
||||
}
|
||||
|
||||
static VerCapExtAttributeStorage *
|
||||
construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
|
||||
return new (allocator.allocate<VerCapExtAttributeStorage>())
|
||||
VerCapExtAttributeStorage(std::get<0>(key), std::get<1>(key),
|
||||
std::get<2>(key));
|
||||
}
|
||||
|
||||
Attribute version;
|
||||
Attribute capabilities;
|
||||
Attribute extensions;
|
||||
};
|
||||
|
||||
struct TargetEnvAttributeStorage : public AttributeStorage {
|
||||
using KeyTy = std::pair<Attribute, Attribute>;
|
||||
|
||||
TargetEnvAttributeStorage(Attribute triple, Attribute limits)
|
||||
: triple(triple), limits(limits) {}
|
||||
|
||||
bool operator==(const KeyTy &key) const {
|
||||
return key.first == triple && key.second == limits;
|
||||
}
|
||||
|
||||
static TargetEnvAttributeStorage *
|
||||
construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
|
||||
return new (allocator.allocate<TargetEnvAttributeStorage>())
|
||||
TargetEnvAttributeStorage(key.first, key.second);
|
||||
}
|
||||
|
||||
Attribute triple;
|
||||
Attribute limits;
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace spirv
|
||||
} // namespace mlir
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// VerCapExtAttr
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
spirv::VerCapExtAttr spirv::VerCapExtAttr::get(
|
||||
spirv::Version version, ArrayRef<spirv::Capability> capabilities,
|
||||
ArrayRef<spirv::Extension> extensions, MLIRContext *context) {
|
||||
Builder b(context);
|
||||
|
||||
auto versionAttr = b.getI32IntegerAttr(static_cast<uint32_t>(version));
|
||||
|
||||
SmallVector<Attribute, 4> capAttrs;
|
||||
capAttrs.reserve(capabilities.size());
|
||||
for (spirv::Capability cap : capabilities)
|
||||
capAttrs.push_back(b.getI32IntegerAttr(static_cast<uint32_t>(cap)));
|
||||
|
||||
SmallVector<Attribute, 4> extAttrs;
|
||||
extAttrs.reserve(extensions.size());
|
||||
for (spirv::Extension ext : extensions)
|
||||
extAttrs.push_back(b.getStringAttr(spirv::stringifyExtension(ext)));
|
||||
|
||||
return get(versionAttr, b.getArrayAttr(capAttrs), b.getArrayAttr(extAttrs));
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr spirv::VerCapExtAttr::get(IntegerAttr version,
|
||||
ArrayAttr capabilities,
|
||||
ArrayAttr extensions) {
|
||||
assert(version && capabilities && extensions);
|
||||
MLIRContext *context = version.getContext();
|
||||
return Base::get(context, spirv::AttrKind::VerCapExt, version, capabilities,
|
||||
extensions);
|
||||
}
|
||||
|
||||
StringRef spirv::VerCapExtAttr::getKindName() { return "vce"; }
|
||||
|
||||
spirv::Version spirv::VerCapExtAttr::getVersion() {
|
||||
return static_cast<spirv::Version>(
|
||||
getImpl()->version.cast<IntegerAttr>().getValue().getZExtValue());
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::ext_iterator::ext_iterator(ArrayAttr::iterator it)
|
||||
: llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
spirv::Extension (*)(Attribute)>(
|
||||
it, [](Attribute attr) {
|
||||
return *symbolizeExtension(attr.cast<StringAttr>().getValue());
|
||||
}) {}
|
||||
|
||||
spirv::VerCapExtAttr::ext_range spirv::VerCapExtAttr::getExtensions() {
|
||||
auto range = getExtensionsAttr().getValue();
|
||||
return {ext_iterator(range.begin()), ext_iterator(range.end())};
|
||||
}
|
||||
|
||||
ArrayAttr spirv::VerCapExtAttr::getExtensionsAttr() {
|
||||
return getImpl()->extensions.cast<ArrayAttr>();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::cap_iterator::cap_iterator(ArrayAttr::iterator it)
|
||||
: llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
spirv::Capability (*)(Attribute)>(
|
||||
it, [](Attribute attr) {
|
||||
return *symbolizeCapability(
|
||||
attr.cast<IntegerAttr>().getValue().getZExtValue());
|
||||
}) {}
|
||||
|
||||
spirv::VerCapExtAttr::cap_range spirv::VerCapExtAttr::getCapabilities() {
|
||||
auto range = getCapabilitiesAttr().getValue();
|
||||
return {cap_iterator(range.begin()), cap_iterator(range.end())};
|
||||
}
|
||||
|
||||
ArrayAttr spirv::VerCapExtAttr::getCapabilitiesAttr() {
|
||||
return getImpl()->capabilities.cast<ArrayAttr>();
|
||||
}
|
||||
|
||||
LogicalResult spirv::VerCapExtAttr::verifyConstructionInvariants(
|
||||
Location loc, IntegerAttr version, ArrayAttr capabilities,
|
||||
ArrayAttr extensions) {
|
||||
if (!version.getType().isSignlessInteger(32))
|
||||
return emitError(loc, "expected 32-bit integer for version");
|
||||
|
||||
if (!llvm::all_of(capabilities.getValue(), [](Attribute attr) {
|
||||
if (auto intAttr = attr.dyn_cast<IntegerAttr>())
|
||||
if (spirv::symbolizeCapability(intAttr.getValue().getZExtValue()))
|
||||
return true;
|
||||
return false;
|
||||
}))
|
||||
return emitError(loc, "unknown capability in capability list");
|
||||
|
||||
if (!llvm::all_of(extensions.getValue(), [](Attribute attr) {
|
||||
if (auto strAttr = attr.dyn_cast<StringAttr>())
|
||||
if (spirv::symbolizeExtension(strAttr.getValue()))
|
||||
return true;
|
||||
return false;
|
||||
}))
|
||||
return emitError(loc, "unknown extension in extension list");
|
||||
|
||||
return success();
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// TargetEnvAttr
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
spirv::TargetEnvAttr spirv::TargetEnvAttr::get(spirv::VerCapExtAttr triple,
|
||||
DictionaryAttr limits) {
|
||||
assert(triple && limits && "expected valid triple and limits");
|
||||
MLIRContext *context = triple.getContext();
|
||||
return Base::get(context, spirv::AttrKind::TargetEnv, triple, limits);
|
||||
}
|
||||
|
||||
StringRef spirv::TargetEnvAttr::getKindName() { return "target_env"; }
|
||||
|
||||
spirv::VerCapExtAttr spirv::TargetEnvAttr::getTripleAttr() {
|
||||
return getImpl()->triple.cast<spirv::VerCapExtAttr>();
|
||||
}
|
||||
|
||||
spirv::Version spirv::TargetEnvAttr::getVersion() {
|
||||
return getTripleAttr().getVersion();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::ext_range spirv::TargetEnvAttr::getExtensions() {
|
||||
return getTripleAttr().getExtensions();
|
||||
}
|
||||
|
||||
ArrayAttr spirv::TargetEnvAttr::getExtensionsAttr() {
|
||||
return getTripleAttr().getExtensionsAttr();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::cap_range spirv::TargetEnvAttr::getCapabilities() {
|
||||
return getTripleAttr().getCapabilities();
|
||||
}
|
||||
|
||||
ArrayAttr spirv::TargetEnvAttr::getCapabilitiesAttr() {
|
||||
return getTripleAttr().getCapabilitiesAttr();
|
||||
}
|
||||
|
||||
spirv::ResourceLimitsAttr spirv::TargetEnvAttr::getResourceLimits() {
|
||||
return getImpl()->limits.cast<spirv::ResourceLimitsAttr>();
|
||||
}
|
||||
|
||||
LogicalResult spirv::TargetEnvAttr::verifyConstructionInvariants(
|
||||
Location loc, spirv::VerCapExtAttr triple, DictionaryAttr limits) {
|
||||
if (!limits.isa<spirv::ResourceLimitsAttr>())
|
||||
return emitError(loc, "expected spirv::ResourceLimitsAttr for limits");
|
||||
|
||||
return success();
|
||||
}
|
|
@ -15,217 +15,6 @@
|
|||
|
||||
using namespace mlir;
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// DictionaryDict derived attributes
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace mlir {
|
||||
#include "mlir/Dialect/SPIRV/TargetAndABI.cpp.inc"
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Attribute storage classes
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
namespace spirv {
|
||||
namespace detail {
|
||||
struct VerCapExtAttributeStorage : public AttributeStorage {
|
||||
using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
|
||||
|
||||
VerCapExtAttributeStorage(Attribute version, Attribute capabilities,
|
||||
Attribute extensions)
|
||||
: version(version), capabilities(capabilities), extensions(extensions) {}
|
||||
|
||||
bool operator==(const KeyTy &key) const {
|
||||
return std::get<0>(key) == version && std::get<1>(key) == capabilities &&
|
||||
std::get<2>(key) == extensions;
|
||||
}
|
||||
|
||||
static VerCapExtAttributeStorage *
|
||||
construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
|
||||
return new (allocator.allocate<VerCapExtAttributeStorage>())
|
||||
VerCapExtAttributeStorage(std::get<0>(key), std::get<1>(key),
|
||||
std::get<2>(key));
|
||||
}
|
||||
|
||||
Attribute version;
|
||||
Attribute capabilities;
|
||||
Attribute extensions;
|
||||
};
|
||||
|
||||
struct TargetEnvAttributeStorage : public AttributeStorage {
|
||||
using KeyTy = std::pair<Attribute, Attribute>;
|
||||
|
||||
TargetEnvAttributeStorage(Attribute triple, Attribute limits)
|
||||
: triple(triple), limits(limits) {}
|
||||
|
||||
bool operator==(const KeyTy &key) const {
|
||||
return key.first == triple && key.second == limits;
|
||||
}
|
||||
|
||||
static TargetEnvAttributeStorage *
|
||||
construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
|
||||
return new (allocator.allocate<TargetEnvAttributeStorage>())
|
||||
TargetEnvAttributeStorage(key.first, key.second);
|
||||
}
|
||||
|
||||
Attribute triple;
|
||||
Attribute limits;
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace spirv
|
||||
} // namespace mlir
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// VerCapExtAttr
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
spirv::VerCapExtAttr spirv::VerCapExtAttr::get(
|
||||
spirv::Version version, ArrayRef<spirv::Capability> capabilities,
|
||||
ArrayRef<spirv::Extension> extensions, MLIRContext *context) {
|
||||
Builder b(context);
|
||||
|
||||
auto versionAttr = b.getI32IntegerAttr(static_cast<uint32_t>(version));
|
||||
|
||||
SmallVector<Attribute, 4> capAttrs;
|
||||
capAttrs.reserve(capabilities.size());
|
||||
for (spirv::Capability cap : capabilities)
|
||||
capAttrs.push_back(b.getI32IntegerAttr(static_cast<uint32_t>(cap)));
|
||||
|
||||
SmallVector<Attribute, 4> extAttrs;
|
||||
extAttrs.reserve(extensions.size());
|
||||
for (spirv::Extension ext : extensions)
|
||||
extAttrs.push_back(b.getStringAttr(spirv::stringifyExtension(ext)));
|
||||
|
||||
return get(versionAttr, b.getArrayAttr(capAttrs), b.getArrayAttr(extAttrs));
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr spirv::VerCapExtAttr::get(IntegerAttr version,
|
||||
ArrayAttr capabilities,
|
||||
ArrayAttr extensions) {
|
||||
assert(version && capabilities && extensions);
|
||||
MLIRContext *context = version.getContext();
|
||||
return Base::get(context, spirv::AttrKind::VerCapExt, version, capabilities,
|
||||
extensions);
|
||||
}
|
||||
|
||||
StringRef spirv::VerCapExtAttr::getKindName() { return "vce"; }
|
||||
|
||||
spirv::Version spirv::VerCapExtAttr::getVersion() {
|
||||
return static_cast<spirv::Version>(
|
||||
getImpl()->version.cast<IntegerAttr>().getValue().getZExtValue());
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::ext_iterator::ext_iterator(ArrayAttr::iterator it)
|
||||
: llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
spirv::Extension (*)(Attribute)>(
|
||||
it, [](Attribute attr) {
|
||||
return *symbolizeExtension(attr.cast<StringAttr>().getValue());
|
||||
}) {}
|
||||
|
||||
spirv::VerCapExtAttr::ext_range spirv::VerCapExtAttr::getExtensions() {
|
||||
auto range = getExtensionsAttr().getValue();
|
||||
return {ext_iterator(range.begin()), ext_iterator(range.end())};
|
||||
}
|
||||
|
||||
ArrayAttr spirv::VerCapExtAttr::getExtensionsAttr() {
|
||||
return getImpl()->extensions.cast<ArrayAttr>();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::cap_iterator::cap_iterator(ArrayAttr::iterator it)
|
||||
: llvm::mapped_iterator<ArrayAttr::iterator,
|
||||
spirv::Capability (*)(Attribute)>(
|
||||
it, [](Attribute attr) {
|
||||
return *symbolizeCapability(
|
||||
attr.cast<IntegerAttr>().getValue().getZExtValue());
|
||||
}) {}
|
||||
|
||||
spirv::VerCapExtAttr::cap_range spirv::VerCapExtAttr::getCapabilities() {
|
||||
auto range = getCapabilitiesAttr().getValue();
|
||||
return {cap_iterator(range.begin()), cap_iterator(range.end())};
|
||||
}
|
||||
|
||||
ArrayAttr spirv::VerCapExtAttr::getCapabilitiesAttr() {
|
||||
return getImpl()->capabilities.cast<ArrayAttr>();
|
||||
}
|
||||
|
||||
LogicalResult spirv::VerCapExtAttr::verifyConstructionInvariants(
|
||||
Location loc, IntegerAttr version, ArrayAttr capabilities,
|
||||
ArrayAttr extensions) {
|
||||
if (!version.getType().isSignlessInteger(32))
|
||||
return emitError(loc, "expected 32-bit integer for version");
|
||||
|
||||
if (!llvm::all_of(capabilities.getValue(), [](Attribute attr) {
|
||||
if (auto intAttr = attr.dyn_cast<IntegerAttr>())
|
||||
if (spirv::symbolizeCapability(intAttr.getValue().getZExtValue()))
|
||||
return true;
|
||||
return false;
|
||||
}))
|
||||
return emitError(loc, "unknown capability in capability list");
|
||||
|
||||
if (!llvm::all_of(extensions.getValue(), [](Attribute attr) {
|
||||
if (auto strAttr = attr.dyn_cast<StringAttr>())
|
||||
if (spirv::symbolizeExtension(strAttr.getValue()))
|
||||
return true;
|
||||
return false;
|
||||
}))
|
||||
return emitError(loc, "unknown extension in extension list");
|
||||
|
||||
return success();
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// TargetEnvAttr
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
spirv::TargetEnvAttr spirv::TargetEnvAttr::get(spirv::VerCapExtAttr triple,
|
||||
DictionaryAttr limits) {
|
||||
assert(triple && limits && "expected valid triple and limits");
|
||||
MLIRContext *context = triple.getContext();
|
||||
return Base::get(context, spirv::AttrKind::TargetEnv, triple, limits);
|
||||
}
|
||||
|
||||
StringRef spirv::TargetEnvAttr::getKindName() { return "target_env"; }
|
||||
|
||||
spirv::VerCapExtAttr spirv::TargetEnvAttr::getTripleAttr() {
|
||||
return getImpl()->triple.cast<spirv::VerCapExtAttr>();
|
||||
}
|
||||
|
||||
spirv::Version spirv::TargetEnvAttr::getVersion() {
|
||||
return getTripleAttr().getVersion();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::ext_range spirv::TargetEnvAttr::getExtensions() {
|
||||
return getTripleAttr().getExtensions();
|
||||
}
|
||||
|
||||
ArrayAttr spirv::TargetEnvAttr::getExtensionsAttr() {
|
||||
return getTripleAttr().getExtensionsAttr();
|
||||
}
|
||||
|
||||
spirv::VerCapExtAttr::cap_range spirv::TargetEnvAttr::getCapabilities() {
|
||||
return getTripleAttr().getCapabilities();
|
||||
}
|
||||
|
||||
ArrayAttr spirv::TargetEnvAttr::getCapabilitiesAttr() {
|
||||
return getTripleAttr().getCapabilitiesAttr();
|
||||
}
|
||||
|
||||
spirv::ResourceLimitsAttr spirv::TargetEnvAttr::getResourceLimits() {
|
||||
return getImpl()->limits.cast<spirv::ResourceLimitsAttr>();
|
||||
}
|
||||
|
||||
LogicalResult spirv::TargetEnvAttr::verifyConstructionInvariants(
|
||||
Location loc, spirv::VerCapExtAttr triple, DictionaryAttr limits) {
|
||||
if (!limits.isa<spirv::ResourceLimitsAttr>())
|
||||
return emitError(loc, "expected spirv::ResourceLimitsAttr for limits");
|
||||
|
||||
return success();
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Utility functions
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
StringRef spirv::getInterfaceVarABIAttrName() {
|
||||
return "spv.interface_var_abi";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue