Skip to content

Commit

Permalink
Revert "[mlir][spirv] Enhance structure type member decoration handling"
Browse files Browse the repository at this point in the history
This reverts commit 5d74df5.

This broke the MSVC build:  <bits/stdint-uintn.h> isn't available on Windows
  • Loading branch information
joker-eph committed Jun 12, 2020
1 parent 707836e commit 6f0ce46
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 174 deletions.
51 changes: 14 additions & 37 deletions mlir/include/mlir/Dialect/SPIRV/SPIRVTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "mlir/IR/TypeSupport.h"
#include "mlir/IR/Types.h"

#include <bits/stdint-uintn.h>
#include <tuple>

// Forward declare enum classes related to op availability. Their definitions
Expand Down Expand Up @@ -277,40 +276,22 @@ class StructType : public Type::TypeBase<StructType, CompositeType,
public:
using Base::Base;

// Type for specifying the offset of the struct members
using OffsetInfo = uint32_t;

// Type for specifying the decoration(s) on struct members
struct MemberDecorationInfo {
uint32_t memberIndex : 31;
uint32_t hasValue : 1;
Decoration decoration;
uint32_t decorationValue;

MemberDecorationInfo(uint32_t index, uint32_t hasValue,
Decoration decoration, uint32_t decorationValue)
: memberIndex(index), hasValue(hasValue), decoration(decoration),
decorationValue(decorationValue) {}

bool operator==(const MemberDecorationInfo &other) const {
return (this->memberIndex == other.memberIndex) &&
(this->decoration == other.decoration) &&
(this->decorationValue == other.decorationValue);
}
// Layout information used for members in a struct in SPIR-V
//
// TODO(ravishankarm) : For now this only supports the offset type, so uses
// uint64_t value to represent the offset, with
// std::numeric_limit<uint64_t>::max indicating no offset. Change this to
// something that can hold all the information needed for different member
// types
using LayoutInfo = uint64_t;

bool operator<(const MemberDecorationInfo &other) const {
return this->memberIndex < other.memberIndex ||
(this->memberIndex == other.memberIndex &&
static_cast<uint32_t>(this->decoration) <
static_cast<uint32_t>(other.decoration));
}
};
using MemberDecorationInfo = std::pair<uint32_t, spirv::Decoration>;

static bool kindof(unsigned kind) { return kind == TypeKind::Struct; }

/// Construct a StructType with at least one member.
static StructType get(ArrayRef<Type> memberTypes,
ArrayRef<OffsetInfo> offsetInfo = {},
ArrayRef<LayoutInfo> layoutInfo = {},
ArrayRef<MemberDecorationInfo> memberDecorations = {});

/// Construct a struct with no members.
Expand Down Expand Up @@ -342,9 +323,9 @@ class StructType : public Type::TypeBase<StructType, CompositeType,

ElementTypeRange getElementTypes() const;

bool hasOffset() const;
bool hasLayout() const;

uint64_t getMemberOffset(unsigned) const;
uint64_t getOffset(unsigned) const;

// Returns in `allMemberDecorations` the spirv::Decorations (apart from
// Offset) associated with all members of the StructType.
Expand All @@ -353,19 +334,15 @@ class StructType : public Type::TypeBase<StructType, CompositeType,

// Returns in `memberDecorations` all the spirv::Decorations (apart from
// Offset) associated with the `i`-th member of the StructType.
void getMemberDecorations(unsigned i,
SmallVectorImpl<StructType::MemberDecorationInfo>
&memberDecorations) const;
void getMemberDecorations(
unsigned i, SmallVectorImpl<spirv::Decoration> &memberDecorations) const;

void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions,
Optional<spirv::StorageClass> storage = llvm::None);
void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities,
Optional<spirv::StorageClass> storage = llvm::None);
};

llvm::hash_code
hash_value(const StructType::MemberDecorationInfo &memberDecorationInfo);

// SPIR-V cooperative matrix type
class CooperativeMatrixNVType
: public Type::TypeBase<CooperativeMatrixNVType, CompositeType,
Expand Down
9 changes: 4 additions & 5 deletions mlir/lib/Dialect/SPIRV/LayoutUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ VulkanLayoutUtils::decorateType(spirv::StructType structType,
}

SmallVector<Type, 4> memberTypes;
SmallVector<spirv::StructType::OffsetInfo, 4> offsetInfo;
SmallVector<Size, 4> layoutInfo;
SmallVector<spirv::StructType::MemberDecorationInfo, 4> memberDecorations;

Size structMemberOffset = 0;
Expand All @@ -46,8 +46,7 @@ VulkanLayoutUtils::decorateType(spirv::StructType structType,
decorateType(structType.getElementType(i), memberSize, memberAlignment);
structMemberOffset = llvm::alignTo(structMemberOffset, memberAlignment);
memberTypes.push_back(memberType);
offsetInfo.push_back(
static_cast<spirv::StructType::OffsetInfo>(structMemberOffset));
layoutInfo.push_back(structMemberOffset);
// If the member's size is the max value, it must be the last member and it
// must be a runtime array.
assert(memberSize != std::numeric_limits<Size>().max() ||
Expand All @@ -67,7 +66,7 @@ VulkanLayoutUtils::decorateType(spirv::StructType structType,
size = llvm::alignTo(structMemberOffset, maxMemberAlignment);
alignment = maxMemberAlignment;
structType.getMemberDecorations(memberDecorations);
return spirv::StructType::get(memberTypes, offsetInfo, memberDecorations);
return spirv::StructType::get(memberTypes, layoutInfo, memberDecorations);
}

Type VulkanLayoutUtils::decorateType(Type type, VulkanLayoutUtils::Size &size,
Expand Down Expand Up @@ -169,7 +168,7 @@ bool VulkanLayoutUtils::isLegalType(Type type) {
case spirv::StorageClass::StorageBuffer:
case spirv::StorageClass::PushConstant:
case spirv::StorageClass::PhysicalStorageBuffer:
return structType.hasOffset() || !structType.getNumElements();
return structType.hasLayout() || !structType.getNumElements();
default:
return true;
}
Expand Down
70 changes: 26 additions & 44 deletions mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,31 +535,30 @@ static Type parseImageType(SPIRVDialect const &dialect,
static ParseResult parseStructMemberDecorations(
SPIRVDialect const &dialect, DialectAsmParser &parser,
ArrayRef<Type> memberTypes,
SmallVectorImpl<StructType::OffsetInfo> &offsetInfo,
SmallVectorImpl<StructType::LayoutInfo> &layoutInfo,
SmallVectorImpl<StructType::MemberDecorationInfo> &memberDecorationInfo) {

// Check if the first element is offset.
llvm::SMLoc offsetLoc = parser.getCurrentLocation();
StructType::OffsetInfo offset = 0;
OptionalParseResult offsetParseResult = parser.parseOptionalInteger(offset);
if (offsetParseResult.hasValue()) {
if (failed(*offsetParseResult))
llvm::SMLoc layoutLoc = parser.getCurrentLocation();
StructType::LayoutInfo layout = 0;
OptionalParseResult layoutParseResult = parser.parseOptionalInteger(layout);
if (layoutParseResult.hasValue()) {
if (failed(*layoutParseResult))
return failure();

if (offsetInfo.size() != memberTypes.size() - 1) {
return parser.emitError(offsetLoc,
"offset specification must be given for "
"all members");
if (layoutInfo.size() != memberTypes.size() - 1) {
return parser.emitError(
layoutLoc, "layout specification must be given for all members");
}
offsetInfo.push_back(offset);
layoutInfo.push_back(layout);
}

// Check for no spirv::Decorations.
if (succeeded(parser.parseOptionalRSquare()))
return success();

// If there was an offset, make sure to parse the comma.
if (offsetParseResult.hasValue() && parser.parseComma())
// If there was a layout, make sure to parse the comma.
if (layoutParseResult.hasValue() && parser.parseComma())
return failure();

// Check for spirv::Decorations.
Expand All @@ -568,23 +567,9 @@ static ParseResult parseStructMemberDecorations(
if (!memberDecoration)
return failure();

// Parse member decoration value if it exists.
if (succeeded(parser.parseOptionalEqual())) {
auto memberDecorationValue =
parseAndVerifyInteger<uint32_t>(dialect, parser);

if (!memberDecorationValue)
return failure();

memberDecorationInfo.emplace_back(
static_cast<uint32_t>(memberTypes.size() - 1), 1,
memberDecoration.getValue(), memberDecorationValue.getValue());
} else {
memberDecorationInfo.emplace_back(
static_cast<uint32_t>(memberTypes.size() - 1), 0,
memberDecoration.getValue(), 0);
}

memberDecorationInfo.emplace_back(
static_cast<uint32_t>(memberTypes.size() - 1),
memberDecoration.getValue());
} while (succeeded(parser.parseOptionalComma()));

return parser.parseRSquare();
Expand All @@ -602,7 +587,7 @@ static Type parseStructType(SPIRVDialect const &dialect,
return StructType::getEmpty(dialect.getContext());

SmallVector<Type, 4> memberTypes;
SmallVector<StructType::OffsetInfo, 4> offsetInfo;
SmallVector<StructType::LayoutInfo, 4> layoutInfo;
SmallVector<StructType::MemberDecorationInfo, 4> memberDecorationInfo;

do {
Expand All @@ -612,21 +597,21 @@ static Type parseStructType(SPIRVDialect const &dialect,
memberTypes.push_back(memberType);

if (succeeded(parser.parseOptionalLSquare())) {
if (parseStructMemberDecorations(dialect, parser, memberTypes, offsetInfo,
if (parseStructMemberDecorations(dialect, parser, memberTypes, layoutInfo,
memberDecorationInfo)) {
return Type();
}
}
} while (succeeded(parser.parseOptionalComma()));

if (!offsetInfo.empty() && memberTypes.size() != offsetInfo.size()) {
if (!layoutInfo.empty() && memberTypes.size() != layoutInfo.size()) {
parser.emitError(parser.getNameLoc(),
"offset specification must be given for all members");
"layout specification must be given for all members");
return Type();
}
if (parser.parseGreater())
return Type();
return StructType::get(memberTypes, offsetInfo, memberDecorationInfo);
return StructType::get(memberTypes, layoutInfo, memberDecorationInfo);
}

// spirv-type ::= array-type
Expand Down Expand Up @@ -694,20 +679,17 @@ static void print(StructType type, DialectAsmPrinter &os) {
os << "struct<";
auto printMember = [&](unsigned i) {
os << type.getElementType(i);
SmallVector<spirv::StructType::MemberDecorationInfo, 0> decorations;
SmallVector<spirv::Decoration, 0> decorations;
type.getMemberDecorations(i, decorations);
if (type.hasOffset() || !decorations.empty()) {
if (type.hasLayout() || !decorations.empty()) {
os << " [";
if (type.hasOffset()) {
os << type.getMemberOffset(i);
if (type.hasLayout()) {
os << type.getOffset(i);
if (!decorations.empty())
os << ", ";
}
auto eachFn = [&os](spirv::StructType::MemberDecorationInfo decoration) {
os << stringifyDecoration(decoration.decoration);
if (decoration.hasValue) {
os << "=" << decoration.decorationValue;
}
auto eachFn = [&os](spirv::Decoration decoration) {
os << stringifyDecoration(decoration);
};
llvm::interleaveComma(decorations, os, eachFn);
os << "]";
Expand Down
Loading

0 comments on commit 6f0ce46

Please sign in to comment.