2019-05-26 05:43:20 -07:00
|
|
|
//===- SPIRVOps.cpp - MLIR SPIR-V operations ------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file defines the operations in the SPIR-V dialect.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-16 05:06:57 -07:00
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVOps.h"
|
2019-05-26 05:43:20 -07:00
|
|
|
|
2019-08-20 13:33:41 -07:00
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVDialect.h"
|
2019-07-16 05:06:57 -07:00
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVTypes.h"
|
2019-05-29 10:47:16 -07:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-07-11 11:41:04 -07:00
|
|
|
#include "mlir/IR/Function.h"
|
2019-05-29 10:47:16 -07:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2019-05-26 05:43:20 -07:00
|
|
|
#include "mlir/IR/StandardTypes.h"
|
2019-09-10 17:47:37 -07:00
|
|
|
#include "mlir/Support/Functional.h"
|
2019-07-30 14:14:28 -07:00
|
|
|
#include "mlir/Support/StringExtras.h"
|
2019-09-24 19:24:33 -07:00
|
|
|
#include "llvm/ADT/bit.h"
|
2019-05-26 05:43:20 -07:00
|
|
|
|
2019-05-29 10:47:16 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
2019-07-02 06:02:20 -07:00
|
|
|
// TODO(antiagainst): generate these strings using ODS.
|
2019-07-03 18:12:52 -07:00
|
|
|
static constexpr const char kAlignmentAttrName[] = "alignment";
|
2019-08-30 12:17:21 -07:00
|
|
|
static constexpr const char kBranchWeightAttrName[] = "branch_weights";
|
2019-09-16 15:39:16 -07:00
|
|
|
static constexpr const char kCallee[] = "callee";
|
2019-08-20 13:33:41 -07:00
|
|
|
static constexpr const char kDefaultValueAttrName[] = "default_value";
|
2019-09-21 10:18:00 -07:00
|
|
|
static constexpr const char kExecutionScopeAttrName[] = "execution_scope";
|
2019-08-17 10:19:48 -07:00
|
|
|
static constexpr const char kFnNameAttrName[] = "fn";
|
2019-07-12 06:14:53 -07:00
|
|
|
static constexpr const char kIndicesAttrName[] = "indices";
|
2019-08-17 10:19:48 -07:00
|
|
|
static constexpr const char kInitializerAttrName[] = "initializer";
|
|
|
|
static constexpr const char kInterfaceAttrName[] = "interface";
|
2019-09-21 10:18:00 -07:00
|
|
|
static constexpr const char kMemoryScopeAttrName[] = "memory_scope";
|
2019-08-20 13:33:41 -07:00
|
|
|
static constexpr const char kSpecConstAttrName[] = "spec_const";
|
2019-08-17 10:19:48 -07:00
|
|
|
static constexpr const char kTypeAttrName[] = "type";
|
2019-06-17 14:47:22 -07:00
|
|
|
static constexpr const char kValueAttrName[] = "value";
|
2019-07-08 10:56:20 -07:00
|
|
|
static constexpr const char kValuesAttrName[] = "values";
|
2019-08-17 10:19:48 -07:00
|
|
|
static constexpr const char kVariableAttrName[] = "variable";
|
2019-06-17 14:47:22 -07:00
|
|
|
|
2019-06-04 14:03:30 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Common utility functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-25 15:42:41 -07:00
|
|
|
static LogicalResult extractValueFromConstOp(Operation *op,
|
|
|
|
int32_t &indexValue) {
|
2019-08-23 11:07:13 -07:00
|
|
|
auto constOp = dyn_cast<spirv::ConstantOp>(op);
|
2019-07-25 15:42:41 -07:00
|
|
|
if (!constOp) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
auto valueAttr = constOp.value();
|
|
|
|
auto integerValueAttr = valueAttr.dyn_cast<IntegerAttr>();
|
|
|
|
if (!integerValueAttr) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
indexValue = integerValueAttr.getInt();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-03 18:12:52 -07:00
|
|
|
template <typename EnumClass>
|
2019-09-21 10:18:00 -07:00
|
|
|
static ParseResult
|
|
|
|
parseEnumAttribute(EnumClass &value, OpAsmParser &parser,
|
|
|
|
StringRef attrName = spirv::attributeName<EnumClass>()) {
|
2019-07-03 18:12:52 -07:00
|
|
|
Attribute attrVal;
|
|
|
|
SmallVector<NamedAttribute, 1> attr;
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getCurrentLocation();
|
|
|
|
if (parser.parseAttribute(attrVal, parser.getBuilder().getNoneType(),
|
2019-09-21 10:18:00 -07:00
|
|
|
attrName, attr)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-07-03 18:12:52 -07:00
|
|
|
if (!attrVal.isa<StringAttr>()) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "expected ")
|
2019-09-21 10:18:00 -07:00
|
|
|
<< attrName << " attribute specified as string";
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
2019-07-03 18:12:52 -07:00
|
|
|
auto attrOptional =
|
|
|
|
spirv::symbolizeEnum<EnumClass>()(attrVal.cast<StringAttr>().getValue());
|
|
|
|
if (!attrOptional) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "invalid ")
|
2019-09-21 10:18:00 -07:00
|
|
|
<< attrName << " attribute specification: " << attrVal;
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
2019-07-03 18:12:52 -07:00
|
|
|
value = attrOptional.getValue();
|
2019-07-30 14:14:28 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename EnumClass>
|
2019-09-21 10:18:00 -07:00
|
|
|
static ParseResult
|
|
|
|
parseEnumAttribute(EnumClass &value, OpAsmParser &parser, OperationState &state,
|
|
|
|
StringRef attrName = spirv::attributeName<EnumClass>()) {
|
2019-07-30 14:14:28 -07:00
|
|
|
if (parseEnumAttribute(value, parser)) {
|
|
|
|
return failure();
|
|
|
|
}
|
2019-09-21 10:18:00 -07:00
|
|
|
state.addAttribute(attrName, parser.getBuilder().getI32IntegerAttr(
|
2019-09-24 19:24:33 -07:00
|
|
|
llvm::bit_cast<int32_t>(value)));
|
2019-06-24 10:59:05 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseMemoryAccessAttributes(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-06-24 10:59:05 -07:00
|
|
|
// Parse an optional list of attributes staring with '['
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseOptionalLSquare()) {
|
2019-06-24 10:59:05 -07:00
|
|
|
// Nothing to do
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-03 18:12:52 -07:00
|
|
|
spirv::MemoryAccess memoryAccessAttr;
|
|
|
|
if (parseEnumAttribute(memoryAccessAttr, parser, state)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:43 -07:00
|
|
|
if (spirv::bitEnumContains(memoryAccessAttr, spirv::MemoryAccess::Aligned)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
// Parse integer attribute for alignment.
|
|
|
|
Attribute alignmentAttr;
|
2019-09-20 11:36:49 -07:00
|
|
|
Type i32Type = parser.getBuilder().getIntegerType(32);
|
|
|
|
if (parser.parseComma() ||
|
|
|
|
parser.parseAttribute(alignmentAttr, i32Type, kAlignmentAttrName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.parseRSquare();
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LoadStoreOpTy>
|
|
|
|
static void
|
2019-09-20 20:43:02 -07:00
|
|
|
printMemoryAccessAttribute(LoadStoreOpTy loadStoreOp, OpAsmPrinter &printer,
|
2019-06-24 10:59:05 -07:00
|
|
|
SmallVectorImpl<StringRef> &elidedAttrs) {
|
|
|
|
// Print optional memory access attribute.
|
2019-07-02 06:02:20 -07:00
|
|
|
if (auto memAccess = loadStoreOp.memory_access()) {
|
2019-07-03 18:12:52 -07:00
|
|
|
elidedAttrs.push_back(spirv::attributeName<spirv::MemoryAccess>());
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " [\"" << stringifyMemoryAccess(*memAccess) << "\"";
|
2019-06-24 10:59:05 -07:00
|
|
|
|
|
|
|
// Print integer alignment attribute.
|
|
|
|
if (auto alignment = loadStoreOp.alignment()) {
|
2019-07-03 18:12:52 -07:00
|
|
|
elidedAttrs.push_back(kAlignmentAttrName);
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << ", " << alignment;
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << "]";
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
2019-07-03 18:12:52 -07:00
|
|
|
elidedAttrs.push_back(spirv::attributeName<spirv::StorageClass>());
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LoadStoreOpTy>
|
|
|
|
static LogicalResult verifyMemoryAccessAttribute(LoadStoreOpTy loadStoreOp) {
|
|
|
|
// ODS checks for attributes values. Just need to verify that if the
|
|
|
|
// memory-access attribute is Aligned, then the alignment attribute must be
|
|
|
|
// present.
|
|
|
|
auto *op = loadStoreOp.getOperation();
|
2019-07-03 18:12:52 -07:00
|
|
|
auto memAccessAttr = op->getAttr(spirv::attributeName<spirv::MemoryAccess>());
|
2019-07-02 06:02:20 -07:00
|
|
|
if (!memAccessAttr) {
|
|
|
|
// Alignment attribute shouldn't be present if memory access attribute is
|
|
|
|
// not present.
|
2019-07-03 18:12:52 -07:00
|
|
|
if (op->getAttr(kAlignmentAttrName)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return loadStoreOp.emitOpError(
|
|
|
|
"invalid alignment specification without aligned memory access "
|
|
|
|
"specification");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-02 06:02:20 -07:00
|
|
|
auto memAccessVal = memAccessAttr.template cast<IntegerAttr>();
|
|
|
|
auto memAccess = spirv::symbolizeMemoryAccess(memAccessVal.getInt());
|
|
|
|
|
|
|
|
if (!memAccess) {
|
|
|
|
return loadStoreOp.emitOpError("invalid memory access specifier: ")
|
|
|
|
<< memAccessVal;
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:43 -07:00
|
|
|
if (spirv::bitEnumContains(*memAccess, spirv::MemoryAccess::Aligned)) {
|
2019-07-03 18:12:52 -07:00
|
|
|
if (!op->getAttr(kAlignmentAttrName)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return loadStoreOp.emitOpError("missing alignment value");
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-03 18:12:52 -07:00
|
|
|
if (op->getAttr(kAlignmentAttrName)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return loadStoreOp.emitOpError(
|
|
|
|
"invalid alignment specification with non-aligned memory access "
|
|
|
|
"specification");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-21 10:18:00 -07:00
|
|
|
template <typename BarrierOp>
|
|
|
|
static LogicalResult verifyMemorySemantics(BarrierOp op) {
|
|
|
|
// According to the SPIR-V specification:
|
|
|
|
// "Despite being a mask and allowing multiple bits to be combined, it is
|
|
|
|
// invalid for more than one of these four bits to be set: Acquire, Release,
|
|
|
|
// AcquireRelease, or SequentiallyConsistent. Requesting both Acquire and
|
|
|
|
// Release semantics is done by setting the AcquireRelease bit, not by setting
|
|
|
|
// two bits."
|
|
|
|
auto memorySemantics = op.memory_semantics();
|
|
|
|
auto atMostOneInSet = spirv::MemorySemantics::Acquire |
|
|
|
|
spirv::MemorySemantics::Release |
|
|
|
|
spirv::MemorySemantics::AcquireRelease |
|
|
|
|
spirv::MemorySemantics::SequentiallyConsistent;
|
|
|
|
|
|
|
|
auto bitCount = llvm::countPopulation(
|
|
|
|
static_cast<uint32_t>(memorySemantics & atMostOneInSet));
|
|
|
|
if (bitCount > 1) {
|
|
|
|
return op.emitError("expected at most one of these four memory constraints "
|
|
|
|
"to be set: `Acquire`, `Release`,"
|
|
|
|
"`AcquireRelease` or `SequentiallyConsistent`");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-06-24 10:59:05 -07:00
|
|
|
template <typename LoadStoreOpTy>
|
|
|
|
static LogicalResult verifyLoadStorePtrAndValTypes(LoadStoreOpTy op, Value *ptr,
|
|
|
|
Value *val) {
|
|
|
|
// ODS already checks ptr is spirv::PointerType. Just check that the pointee
|
|
|
|
// type of the pointer and the type of the value are the same
|
|
|
|
//
|
|
|
|
// TODO(ravishankarm): Check that the value type satisfies restrictions of
|
|
|
|
// SPIR-V OpLoad/OpStore operations
|
|
|
|
if (val->getType() !=
|
|
|
|
ptr->getType().cast<spirv::PointerType>().getPointeeType()) {
|
|
|
|
return op.emitOpError("mismatch in result type and pointer type");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseVariableDecorations(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-17 10:19:48 -07:00
|
|
|
auto builtInName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::BuiltIn));
|
2019-09-20 11:36:49 -07:00
|
|
|
if (succeeded(parser.parseOptionalKeyword("bind"))) {
|
2019-08-17 10:19:48 -07:00
|
|
|
Attribute set, binding;
|
|
|
|
// Parse optional descriptor binding
|
|
|
|
auto descriptorSetName = convertToSnakeCase(
|
|
|
|
stringifyDecoration(spirv::Decoration::DescriptorSet));
|
|
|
|
auto bindingName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::Binding));
|
2019-09-20 11:36:49 -07:00
|
|
|
Type i32Type = parser.getBuilder().getIntegerType(32);
|
|
|
|
if (parser.parseLParen() ||
|
|
|
|
parser.parseAttribute(set, i32Type, descriptorSetName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseComma() ||
|
|
|
|
parser.parseAttribute(binding, i32Type, bindingName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseRParen()) {
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-09-20 11:36:49 -07:00
|
|
|
} else if (succeeded(parser.parseOptionalKeyword(builtInName))) {
|
2019-08-17 10:19:48 -07:00
|
|
|
StringAttr builtIn;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseLParen() ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.parseAttribute(builtIn, builtInName, state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseRParen()) {
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse other attributes
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.parseOptionalAttributeDict(state.attributes))
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void printVariableDecorations(Operation *op, OpAsmPrinter &printer,
|
2019-08-17 10:19:48 -07:00
|
|
|
SmallVectorImpl<StringRef> &elidedAttrs) {
|
|
|
|
// Print optional descriptor binding
|
|
|
|
auto descriptorSetName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::DescriptorSet));
|
|
|
|
auto bindingName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::Binding));
|
|
|
|
auto descriptorSet = op->getAttrOfType<IntegerAttr>(descriptorSetName);
|
|
|
|
auto binding = op->getAttrOfType<IntegerAttr>(bindingName);
|
|
|
|
if (descriptorSet && binding) {
|
|
|
|
elidedAttrs.push_back(descriptorSetName);
|
|
|
|
elidedAttrs.push_back(bindingName);
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " bind(" << descriptorSet.getInt() << ", " << binding.getInt()
|
|
|
|
<< ")";
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print BuiltIn attribute if present
|
|
|
|
auto builtInName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::BuiltIn));
|
|
|
|
if (auto builtin = op->getAttrOfType<StringAttr>(builtInName)) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " " << builtInName << "(\"" << builtin.getValue() << "\")";
|
2019-08-17 10:19:48 -07:00
|
|
|
elidedAttrs.push_back(builtInName);
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOptionalAttrDict(op->getAttrs(), elidedAttrs);
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
|
2019-09-10 17:47:37 -07:00
|
|
|
// Extracts an element from the given `composite` by following the given
|
|
|
|
// `indices`. Returns a null Attribute if error happens.
|
|
|
|
static Attribute extractCompositeElement(Attribute composite,
|
|
|
|
ArrayRef<unsigned> indices) {
|
|
|
|
// Return composite itself if we reach the end of the index chain.
|
|
|
|
if (indices.empty())
|
|
|
|
return composite;
|
|
|
|
|
|
|
|
if (auto vector = composite.dyn_cast<ElementsAttr>()) {
|
|
|
|
assert(indices.size() == 1 && "must have exactly one index for a vector");
|
|
|
|
return vector.getValue({indices[0]});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto array = composite.dyn_cast<ArrayAttr>()) {
|
|
|
|
assert(!indices.empty() && "must have at least one index for an array");
|
|
|
|
return extractCompositeElement(array.getValue()[indices[0]],
|
|
|
|
indices.drop_front());
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-09-25 19:01:18 -07:00
|
|
|
// Get bit width of types.
|
|
|
|
static unsigned getBitWidth(Type type) {
|
|
|
|
if (type.isa<spirv::PointerType>()) {
|
|
|
|
// Just return 64 bits for pointer types for now.
|
|
|
|
// TODO: Make sure not caller relies on the actual pointer width value.
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
if (type.isIntOrFloat()) {
|
|
|
|
return type.getIntOrFloatBitWidth();
|
|
|
|
}
|
|
|
|
if (auto vectorType = type.dyn_cast<VectorType>()) {
|
|
|
|
assert(vectorType.getElementType().isIntOrFloat());
|
|
|
|
return vectorType.getNumElements() *
|
|
|
|
vectorType.getElementType().getIntOrFloatBitWidth();
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled bit width computation for type");
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:00:50 -07:00
|
|
|
/// Returns true if the given `block` only contains one `spv._merge` op.
|
|
|
|
static inline bool isMergeBlock(Block &block) {
|
|
|
|
return !block.empty() && std::next(block.begin()) == block.end() &&
|
|
|
|
isa<spirv::MergeOp>(block.front());
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:34:37 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Common parsers and printers
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Parses an op that has no inputs and no outputs.
|
|
|
|
static ParseResult parseNoIOOp(OpAsmParser &parser, OperationState &state) {
|
|
|
|
if (parser.parseOptionalAttributeDict(state.attributes))
|
|
|
|
return failure();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints an op that has no inputs and no outputs.
|
|
|
|
static void printNoIOOp(Operation *op, OpAsmPrinter &printer) {
|
|
|
|
printer << op->getName();
|
|
|
|
printer.printOptionalAttrDict(op->getAttrs());
|
|
|
|
}
|
|
|
|
|
|
|
|
static ParseResult parseUnaryOp(OpAsmParser &parser, OperationState &state) {
|
|
|
|
OpAsmParser::OperandType operandInfo;
|
|
|
|
Type type;
|
|
|
|
if (parser.parseOperand(operandInfo) || parser.parseColonType(type) ||
|
|
|
|
parser.resolveOperands(operandInfo, type, state.operands)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
state.addTypes(type);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printUnaryOp(Operation *unaryOp, OpAsmPrinter &printer) {
|
|
|
|
printer << unaryOp->getName() << ' ' << *unaryOp->getOperand(0) << " : "
|
|
|
|
<< unaryOp->getOperand(0)->getType();
|
|
|
|
}
|
|
|
|
|
2019-09-30 10:40:07 -07:00
|
|
|
/// Result of a logical op must be a scalar or vector of boolean type.
|
|
|
|
static Type getUnaryOpResultType(Builder &builder, Type operandType) {
|
|
|
|
Type resultType = builder.getIntegerType(1);
|
|
|
|
if (auto vecType = operandType.dyn_cast<VectorType>()) {
|
|
|
|
return VectorType::get(vecType.getNumElements(), resultType);
|
|
|
|
}
|
|
|
|
return resultType;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ParseResult parseLogicalUnaryOp(OpAsmParser &parser,
|
|
|
|
OperationState &state) {
|
|
|
|
OpAsmParser::OperandType operandInfo;
|
|
|
|
Type type;
|
|
|
|
if (parser.parseOperand(operandInfo) || parser.parseColonType(type) ||
|
|
|
|
parser.resolveOperand(operandInfo, type, state.operands)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
state.addTypes(getUnaryOpResultType(parser.getBuilder(), type));
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ParseResult parseLogicalBinaryOp(OpAsmParser &parser,
|
2019-09-25 16:34:37 -07:00
|
|
|
OperationState &result) {
|
|
|
|
SmallVector<OpAsmParser::OperandType, 2> ops;
|
|
|
|
Type type;
|
|
|
|
if (parser.parseOperandList(ops, 2) || parser.parseColonType(type) ||
|
|
|
|
parser.resolveOperands(ops, type, result.operands)) {
|
|
|
|
return failure();
|
|
|
|
}
|
2019-09-30 10:40:07 -07:00
|
|
|
result.addTypes(getUnaryOpResultType(parser.getBuilder(), type));
|
2019-09-25 16:34:37 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-30 10:40:07 -07:00
|
|
|
static void printLogicalOp(Operation *logicalOp, OpAsmPrinter &printer) {
|
|
|
|
printer << logicalOp->getName() << ' ';
|
|
|
|
printer.printOperands(logicalOp->getOperands());
|
2019-09-25 16:34:37 -07:00
|
|
|
printer << " : " << logicalOp->getOperand(0)->getType();
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:42:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.AccessChainOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static Type getElementPtrType(Type type, ArrayRef<Value *> indices,
|
|
|
|
Location baseLoc) {
|
2019-08-27 10:49:53 -07:00
|
|
|
if (indices.empty()) {
|
2019-07-25 15:42:41 -07:00
|
|
|
emitError(baseLoc, "'spv.AccessChain' op expected at least "
|
|
|
|
"one index ");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptrType = type.dyn_cast<spirv::PointerType>();
|
|
|
|
if (!ptrType) {
|
|
|
|
emitError(baseLoc, "'spv.AccessChain' op expected a pointer "
|
|
|
|
"to composite type, but provided ")
|
|
|
|
<< type;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto resultType = ptrType.getPointeeType();
|
|
|
|
auto resultStorageClass = ptrType.getStorageClass();
|
|
|
|
int32_t index = 0;
|
|
|
|
|
|
|
|
for (auto indexSSA : indices) {
|
|
|
|
auto cType = resultType.dyn_cast<spirv::CompositeType>();
|
|
|
|
if (!cType) {
|
|
|
|
emitError(baseLoc,
|
|
|
|
"'spv.AccessChain' op cannot extract from non-composite type ")
|
|
|
|
<< resultType << " with index " << index;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
index = 0;
|
|
|
|
if (resultType.isa<spirv::StructType>()) {
|
|
|
|
Operation *op = indexSSA->getDefiningOp();
|
|
|
|
if (!op) {
|
|
|
|
emitError(baseLoc, "'spv.AccessChain' op index must be an "
|
|
|
|
"integer spv.constant to access "
|
|
|
|
"element of spv.struct");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(denis0x0D): this should be relaxed to allow
|
|
|
|
// integer literals of other bitwidths.
|
|
|
|
if (failed(extractValueFromConstOp(op, index))) {
|
|
|
|
emitError(baseLoc,
|
|
|
|
"'spv.AccessChain' index must be an integer spv.constant to "
|
|
|
|
"access element of spv.struct, but provided ")
|
|
|
|
<< op->getName();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (index < 0 || static_cast<uint64_t>(index) >= cType.getNumElements()) {
|
|
|
|
emitError(baseLoc, "'spv.AccessChain' op index ")
|
|
|
|
<< index << " out of bounds for " << resultType;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resultType = cType.getElementType(index);
|
|
|
|
}
|
|
|
|
return spirv::PointerType::get(resultType, resultStorageClass);
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
void spirv::AccessChainOp::build(Builder *builder, OperationState &state,
|
2019-08-27 10:49:53 -07:00
|
|
|
Value *basePtr, ArrayRef<Value *> indices) {
|
2019-09-20 19:47:05 -07:00
|
|
|
auto type = getElementPtrType(basePtr->getType(), indices, state.location);
|
2019-08-27 10:49:53 -07:00
|
|
|
assert(type && "Unable to deduce return type based on basePtr and indices");
|
|
|
|
build(builder, state, type, basePtr, indices);
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseAccessChainOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-07-25 15:42:41 -07:00
|
|
|
OpAsmParser::OperandType ptrInfo;
|
|
|
|
SmallVector<OpAsmParser::OperandType, 4> indicesInfo;
|
|
|
|
Type type;
|
|
|
|
// TODO(denis0x0D): regarding to the spec an index must be any integer type,
|
|
|
|
// figure out how to use resolveOperand with a range of types and do not
|
|
|
|
// fail on first attempt.
|
2019-09-20 11:36:49 -07:00
|
|
|
Type indicesType = parser.getBuilder().getIntegerType(32);
|
2019-07-25 15:42:41 -07:00
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseOperand(ptrInfo) ||
|
|
|
|
parser.parseOperandList(indicesInfo, OpAsmParser::Delimiter::Square) ||
|
|
|
|
parser.parseColonType(type) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.resolveOperand(ptrInfo, type, state.operands) ||
|
|
|
|
parser.resolveOperands(indicesInfo, indicesType, state.operands)) {
|
2019-07-25 15:42:41 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto resultType = getElementPtrType(
|
2019-09-20 19:47:05 -07:00
|
|
|
type, llvm::makeArrayRef(state.operands).drop_front(), state.location);
|
2019-07-25 15:42:41 -07:00
|
|
|
if (!resultType) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addTypes(resultType);
|
2019-07-25 15:42:41 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::AccessChainOp op, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::AccessChainOp::getOperationName() << ' ' << *op.base_ptr()
|
|
|
|
<< '[';
|
|
|
|
printer.printOperands(op.indices());
|
|
|
|
printer << "] : " << op.base_ptr()->getType();
|
2019-07-25 15:42:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::AccessChainOp accessChainOp) {
|
|
|
|
SmallVector<Value *, 4> indices(accessChainOp.indices().begin(),
|
|
|
|
accessChainOp.indices().end());
|
|
|
|
auto resultType = getElementPtrType(accessChainOp.base_ptr()->getType(),
|
|
|
|
indices, accessChainOp.getLoc());
|
|
|
|
if (!resultType) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto providedResultType =
|
|
|
|
accessChainOp.getType().dyn_cast<spirv::PointerType>();
|
|
|
|
if (!providedResultType) {
|
|
|
|
return accessChainOp.emitOpError(
|
|
|
|
"result type must be a pointer, but provided")
|
|
|
|
<< providedResultType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resultType != providedResultType) {
|
|
|
|
return accessChainOp.emitOpError("invalid result type: expected ")
|
|
|
|
<< resultType << ", but provided " << providedResultType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv._address_of
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-04 14:02:14 -07:00
|
|
|
void spirv::AddressOfOp::build(Builder *builder, OperationState &state,
|
|
|
|
spirv::GlobalVariableOp var) {
|
|
|
|
build(builder, state, var.type(), builder->getSymbolRefAttr(var));
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseAddressOfOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-17 10:19:48 -07:00
|
|
|
SymbolRefAttr varRefAttr;
|
|
|
|
Type type;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseAttribute(varRefAttr, Type(), kVariableAttrName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseColonType(type)) {
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
auto ptrType = type.dyn_cast<spirv::PointerType>();
|
|
|
|
if (!ptrType) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(parser.getCurrentLocation(),
|
|
|
|
"expected spv.ptr type");
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addTypes(ptrType);
|
2019-08-17 10:19:48 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::AddressOfOp addressOfOp, OpAsmPrinter &printer) {
|
2019-08-17 10:19:48 -07:00
|
|
|
SmallVector<StringRef, 4> elidedAttrs;
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::AddressOfOp::getOperationName();
|
2019-08-17 10:19:48 -07:00
|
|
|
|
|
|
|
// Print symbol name.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " @" << addressOfOp.variable();
|
2019-08-17 10:19:48 -07:00
|
|
|
|
|
|
|
// Print the type.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << addressOfOp.pointer()->getType();
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::AddressOfOp addressOfOp) {
|
|
|
|
auto moduleOp = addressOfOp.getParentOfType<spirv::ModuleOp>();
|
|
|
|
auto varOp =
|
|
|
|
moduleOp.lookupSymbol<spirv::GlobalVariableOp>(addressOfOp.variable());
|
|
|
|
if (!varOp) {
|
2019-08-20 13:33:41 -07:00
|
|
|
return addressOfOp.emitOpError("expected spv.globalVariable symbol");
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
if (addressOfOp.pointer()->getType() != varOp.type()) {
|
2019-08-20 13:33:41 -07:00
|
|
|
return addressOfOp.emitOpError(
|
|
|
|
"result type mismatch with the referenced global variable's type");
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-25 19:01:18 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.BitcastOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseBitcastOp(OpAsmParser &parser, OperationState &state) {
|
|
|
|
OpAsmParser::OperandType operandInfo;
|
|
|
|
Type operandType, resultType;
|
|
|
|
if (parser.parseOperand(operandInfo) || parser.parseKeyword("from") ||
|
|
|
|
parser.parseType(operandType) || parser.parseKeyword("to") ||
|
|
|
|
parser.parseType(resultType)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (parser.resolveOperands(operandInfo, operandType, state.operands)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
state.addTypes(resultType);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::BitcastOp bitcastOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::BitcastOp::getOperationName() << ' ';
|
|
|
|
printer.printOperand(bitcastOp.operand());
|
|
|
|
printer << " from " << bitcastOp.operand()->getType() << " to "
|
|
|
|
<< bitcastOp.result()->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::BitcastOp bitcastOp) {
|
|
|
|
// TODO: The SPIR-V spec validation rules are different for different
|
|
|
|
// versions.
|
|
|
|
auto operandType = bitcastOp.operand()->getType();
|
|
|
|
auto resultType = bitcastOp.result()->getType();
|
|
|
|
if (operandType == resultType) {
|
|
|
|
return bitcastOp.emitError(
|
|
|
|
"result type must be different from operand type");
|
|
|
|
}
|
|
|
|
if (operandType.isa<spirv::PointerType>() &&
|
|
|
|
!resultType.isa<spirv::PointerType>()) {
|
|
|
|
return bitcastOp.emitError(
|
|
|
|
"unhandled bit cast conversion from pointer type to non-pointer type");
|
|
|
|
}
|
|
|
|
if (!operandType.isa<spirv::PointerType>() &&
|
|
|
|
resultType.isa<spirv::PointerType>()) {
|
|
|
|
return bitcastOp.emitError(
|
|
|
|
"unhandled bit cast conversion from non-pointer type to pointer type");
|
|
|
|
}
|
|
|
|
auto operandBitWidth = getBitWidth(operandType);
|
|
|
|
auto resultBitWidth = getBitWidth(resultType);
|
|
|
|
if (operandBitWidth != resultBitWidth) {
|
|
|
|
return bitcastOp.emitOpError("mismatch in result type bitwidth ")
|
|
|
|
<< resultBitWidth << " and operand type bitwidth "
|
|
|
|
<< operandBitWidth;
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-08-30 12:17:21 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.BranchOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseBranchOp(OpAsmParser &parser, OperationState &state) {
|
2019-08-30 12:17:21 -07:00
|
|
|
Block *dest;
|
|
|
|
SmallVector<Value *, 4> destOperands;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseSuccessorAndUseList(dest, destOperands))
|
2019-08-30 12:17:21 -07:00
|
|
|
return failure();
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addSuccessor(dest, destOperands);
|
2019-08-30 12:17:21 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::BranchOp branchOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::BranchOp::getOperationName() << ' ';
|
|
|
|
printer.printSuccessorAndUseList(branchOp.getOperation(), /*index=*/0);
|
2019-08-30 12:17:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::BranchOp branchOp) {
|
|
|
|
auto *op = branchOp.getOperation();
|
|
|
|
if (op->getNumSuccessors() != 1)
|
|
|
|
branchOp.emitOpError("must have exactly one successor");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.BranchConditionalOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseBranchConditionalOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-09-20 11:36:49 -07:00
|
|
|
auto &builder = parser.getBuilder();
|
2019-08-30 12:17:21 -07:00
|
|
|
OpAsmParser::OperandType condInfo;
|
|
|
|
Block *dest;
|
|
|
|
SmallVector<Value *, 4> destOperands;
|
|
|
|
|
|
|
|
// Parse the condition.
|
|
|
|
Type boolTy = builder.getI1Type();
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseOperand(condInfo) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.resolveOperand(condInfo, boolTy, state.operands))
|
2019-08-30 12:17:21 -07:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Parse the optional branch weights.
|
2019-09-20 11:36:49 -07:00
|
|
|
if (succeeded(parser.parseOptionalLSquare())) {
|
2019-08-30 12:17:21 -07:00
|
|
|
IntegerAttr trueWeight, falseWeight;
|
|
|
|
SmallVector<NamedAttribute, 2> weights;
|
|
|
|
|
|
|
|
auto i32Type = builder.getIntegerType(32);
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseAttribute(trueWeight, i32Type, "weight", weights) ||
|
|
|
|
parser.parseComma() ||
|
|
|
|
parser.parseAttribute(falseWeight, i32Type, "weight", weights) ||
|
|
|
|
parser.parseRSquare())
|
2019-08-30 12:17:21 -07:00
|
|
|
return failure();
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute(kBranchWeightAttrName,
|
|
|
|
builder.getArrayAttr({trueWeight, falseWeight}));
|
2019-08-30 12:17:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the true branch.
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseComma() ||
|
|
|
|
parser.parseSuccessorAndUseList(dest, destOperands))
|
2019-08-30 12:17:21 -07:00
|
|
|
return failure();
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addSuccessor(dest, destOperands);
|
2019-08-30 12:17:21 -07:00
|
|
|
|
|
|
|
// Parse the false branch.
|
|
|
|
destOperands.clear();
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseComma() ||
|
|
|
|
parser.parseSuccessorAndUseList(dest, destOperands))
|
2019-08-30 12:17:21 -07:00
|
|
|
return failure();
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addSuccessor(dest, destOperands);
|
2019-08-30 12:17:21 -07:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::BranchConditionalOp branchOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::BranchConditionalOp::getOperationName() << ' ';
|
|
|
|
printer.printOperand(branchOp.condition());
|
2019-08-30 12:17:21 -07:00
|
|
|
|
|
|
|
if (auto weights = branchOp.branch_weights()) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " [";
|
|
|
|
interleaveComma(weights->getValue(), printer, [&](Attribute a) {
|
|
|
|
printer << a.cast<IntegerAttr>().getInt();
|
2019-09-11 14:02:23 -07:00
|
|
|
});
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << "]";
|
2019-08-30 12:17:21 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << ", ";
|
|
|
|
printer.printSuccessorAndUseList(branchOp.getOperation(),
|
|
|
|
spirv::BranchConditionalOp::kTrueIndex);
|
|
|
|
printer << ", ";
|
|
|
|
printer.printSuccessorAndUseList(branchOp.getOperation(),
|
|
|
|
spirv::BranchConditionalOp::kFalseIndex);
|
2019-08-30 12:17:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::BranchConditionalOp branchOp) {
|
|
|
|
auto *op = branchOp.getOperation();
|
|
|
|
if (op->getNumSuccessors() != 2)
|
|
|
|
return branchOp.emitOpError("must have exactly two successors");
|
|
|
|
|
|
|
|
if (auto weights = branchOp.branch_weights()) {
|
|
|
|
if (weights->getValue().size() != 2) {
|
|
|
|
return branchOp.emitOpError("must have exactly two branch weights");
|
|
|
|
}
|
|
|
|
if (llvm::all_of(*weights, [](Attribute attr) {
|
|
|
|
return attr.cast<IntegerAttr>().getValue().isNullValue();
|
|
|
|
}))
|
|
|
|
return branchOp.emitOpError("branch weights cannot both be zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:14:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.CompositeExtractOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseCompositeExtractOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-07-12 06:14:53 -07:00
|
|
|
OpAsmParser::OperandType compositeInfo;
|
|
|
|
Attribute indicesAttr;
|
|
|
|
Type compositeType;
|
|
|
|
llvm::SMLoc attrLocation;
|
|
|
|
int32_t index;
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseOperand(compositeInfo) ||
|
|
|
|
parser.getCurrentLocation(&attrLocation) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.parseAttribute(indicesAttr, kIndicesAttrName, state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseColonType(compositeType) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.resolveOperand(compositeInfo, compositeType, state.operands)) {
|
2019-07-12 06:14:53 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto indicesArrayAttr = indicesAttr.dyn_cast<ArrayAttr>();
|
|
|
|
if (!indicesArrayAttr) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(
|
2019-07-12 06:14:53 -07:00
|
|
|
attrLocation,
|
|
|
|
"expected an 32-bit integer array attribute for 'indices'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!indicesArrayAttr.size()) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(
|
2019-07-12 06:14:53 -07:00
|
|
|
attrLocation, "expected at least one index for spv.CompositeExtract");
|
|
|
|
}
|
|
|
|
|
|
|
|
Type resultType = compositeType;
|
|
|
|
for (auto indexAttr : indicesArrayAttr) {
|
|
|
|
if (auto indexIntAttr = indexAttr.dyn_cast<IntegerAttr>()) {
|
|
|
|
index = indexIntAttr.getInt();
|
|
|
|
} else {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(
|
2019-07-12 06:14:53 -07:00
|
|
|
attrLocation,
|
|
|
|
"expexted an 32-bit integer for index, but found '")
|
|
|
|
<< indexAttr << "'";
|
|
|
|
}
|
2019-07-12 13:53:57 -07:00
|
|
|
|
|
|
|
if (auto cType = resultType.dyn_cast<spirv::CompositeType>()) {
|
|
|
|
if (index < 0 || static_cast<uint64_t>(index) >= cType.getNumElements()) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(attrLocation, "index ")
|
2019-07-12 06:14:53 -07:00
|
|
|
<< index << " out of bounds for " << resultType;
|
|
|
|
}
|
2019-07-12 13:53:57 -07:00
|
|
|
resultType = cType.getElementType(index);
|
2019-07-12 06:14:53 -07:00
|
|
|
} else {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(attrLocation,
|
|
|
|
"cannot extract from non-composite type ")
|
2019-07-12 13:53:57 -07:00
|
|
|
<< resultType << " with index " << index;
|
2019-07-12 06:14:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addTypes(resultType);
|
2019-07-12 06:14:53 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::CompositeExtractOp compositeExtractOp,
|
2019-09-20 20:43:02 -07:00
|
|
|
OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::CompositeExtractOp::getOperationName() << ' '
|
|
|
|
<< *compositeExtractOp.composite() << compositeExtractOp.indices()
|
|
|
|
<< " : " << compositeExtractOp.composite()->getType();
|
2019-07-12 06:14:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::CompositeExtractOp compExOp) {
|
|
|
|
auto resultType = compExOp.composite()->getType();
|
|
|
|
auto indicesArrayAttr = compExOp.indices().dyn_cast<ArrayAttr>();
|
|
|
|
|
|
|
|
if (!indicesArrayAttr.size()) {
|
|
|
|
return compExOp.emitOpError(
|
|
|
|
"expexted at least one index for spv.CompositeExtractOp");
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t index;
|
|
|
|
for (auto indexAttr : indicesArrayAttr) {
|
|
|
|
index = indexAttr.dyn_cast<IntegerAttr>().getInt();
|
2019-07-12 13:53:57 -07:00
|
|
|
if (auto cType = resultType.dyn_cast<spirv::CompositeType>()) {
|
|
|
|
if (index < 0 || static_cast<uint64_t>(index) >= cType.getNumElements()) {
|
2019-07-12 06:14:53 -07:00
|
|
|
return compExOp.emitOpError("index ")
|
|
|
|
<< index << " out of bounds for " << resultType;
|
|
|
|
}
|
2019-07-12 13:53:57 -07:00
|
|
|
resultType = cType.getElementType(index);
|
2019-07-12 06:14:53 -07:00
|
|
|
} else {
|
2019-07-12 13:53:57 -07:00
|
|
|
return compExOp.emitError("cannot extract from non-composite type ")
|
|
|
|
<< resultType << " with index " << index;
|
2019-07-12 06:14:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resultType != compExOp.getType()) {
|
2019-07-12 13:53:57 -07:00
|
|
|
return compExOp.emitOpError("invalid result type: expected ")
|
|
|
|
<< resultType << " but provided " << compExOp.getType();
|
2019-07-12 06:14:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-10 17:47:37 -07:00
|
|
|
OpFoldResult spirv::CompositeExtractOp::fold(ArrayRef<Attribute> operands) {
|
|
|
|
assert(operands.size() == 1 && "spv.CompositeExtract expects one operand");
|
|
|
|
auto indexVector = functional::map(
|
|
|
|
[](Attribute attr) {
|
|
|
|
return static_cast<unsigned>(attr.cast<IntegerAttr>().getInt());
|
|
|
|
},
|
|
|
|
indices());
|
|
|
|
return extractCompositeElement(operands[0], indexVector);
|
|
|
|
}
|
|
|
|
|
2019-06-17 14:47:22 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.constant
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseConstantOp(OpAsmParser &parser, OperationState &state) {
|
2019-06-17 14:47:22 -07:00
|
|
|
Attribute value;
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.parseAttribute(value, kValueAttrName, state.attributes))
|
2019-06-17 14:47:22 -07:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
if (value.getType().isa<NoneType>()) {
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseColonType(type))
|
2019-06-17 14:47:22 -07:00
|
|
|
return failure();
|
|
|
|
} else {
|
|
|
|
type = value.getType();
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
return parser.addTypeToList(type, state.types);
|
2019-06-17 14:47:22 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::ConstantOp constOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::ConstantOp::getOperationName() << ' ' << constOp.value();
|
2019-07-19 10:32:12 -07:00
|
|
|
if (constOp.getType().isa<spirv::ArrayType>()) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << constOp.getType();
|
2019-07-19 10:32:12 -07:00
|
|
|
}
|
2019-06-17 14:47:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-18 11:15:55 -07:00
|
|
|
static LogicalResult verify(spirv::ConstantOp constOp) {
|
2019-06-17 14:47:22 -07:00
|
|
|
auto opType = constOp.getType();
|
|
|
|
auto value = constOp.value();
|
|
|
|
auto valueType = value.getType();
|
|
|
|
|
|
|
|
// ODS already generates checks to make sure the result type is valid. We just
|
|
|
|
// need to additionally check that the value's attribute type is consistent
|
|
|
|
// with the result type.
|
|
|
|
switch (value.getKind()) {
|
|
|
|
case StandardAttributes::Bool:
|
|
|
|
case StandardAttributes::Integer:
|
|
|
|
case StandardAttributes::Float:
|
|
|
|
case StandardAttributes::DenseElements:
|
|
|
|
case StandardAttributes::SparseElements: {
|
|
|
|
if (valueType != opType)
|
|
|
|
return constOp.emitOpError("result type (")
|
|
|
|
<< opType << ") does not match value type (" << valueType << ")";
|
|
|
|
return success();
|
|
|
|
} break;
|
|
|
|
case StandardAttributes::Array: {
|
|
|
|
auto arrayType = opType.dyn_cast<spirv::ArrayType>();
|
|
|
|
if (!arrayType)
|
|
|
|
return constOp.emitOpError(
|
|
|
|
"must have spv.array result type for array value");
|
|
|
|
auto elemType = arrayType.getElementType();
|
|
|
|
for (auto element : value.cast<ArrayAttr>().getValue()) {
|
|
|
|
if (element.getType() != elemType)
|
2019-09-03 12:09:07 -07:00
|
|
|
return constOp.emitOpError("has array element whose type (")
|
|
|
|
<< element.getType()
|
|
|
|
<< ") does not match the result element type (" << elemType
|
|
|
|
<< ')';
|
2019-06-17 14:47:22 -07:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
return constOp.emitOpError("cannot have value of type ") << valueType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:09:07 -07:00
|
|
|
OpFoldResult spirv::ConstantOp::fold(ArrayRef<Attribute> operands) {
|
2019-09-10 17:47:37 -07:00
|
|
|
assert(operands.empty() && "spv.constant has no operands");
|
2019-09-03 12:09:07 -07:00
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool spirv::ConstantOp::isBuildableWith(Type type) {
|
|
|
|
// Must be valid SPIR-V type first.
|
|
|
|
if (!SPIRVDialect::isValidType(type))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (type.getKind() >= Type::FIRST_SPIRV_TYPE &&
|
|
|
|
type.getKind() <= spirv::TypeKind::LAST_SPIRV_TYPE) {
|
|
|
|
// TODO(antiagainst): support contant struct
|
|
|
|
return type.isa<spirv::ArrayType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-21 10:18:00 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.ControlBarrier
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseControlBarrierOp(OpAsmParser &parser,
|
|
|
|
OperationState &state) {
|
|
|
|
spirv::Scope executionScope;
|
|
|
|
spirv::Scope memoryScope;
|
|
|
|
spirv::MemorySemantics memorySemantics;
|
|
|
|
|
|
|
|
return failure(
|
|
|
|
parseEnumAttribute(executionScope, parser, state,
|
|
|
|
kExecutionScopeAttrName) ||
|
|
|
|
parser.parseComma() ||
|
|
|
|
parseEnumAttribute(memoryScope, parser, state, kMemoryScopeAttrName) ||
|
|
|
|
parser.parseComma() ||
|
|
|
|
parseEnumAttribute(memorySemantics, parser, state));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::ControlBarrierOp op, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::ControlBarrierOp::getOperationName() << " \""
|
|
|
|
<< stringifyScope(op.execution_scope()) << "\", \""
|
|
|
|
<< stringifyScope(op.memory_scope()) << "\", \""
|
|
|
|
<< stringifyMemorySemantics(op.memory_semantics()) << "\"";
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:56:20 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.EntryPoint
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-04 14:02:14 -07:00
|
|
|
void spirv::EntryPointOp::build(Builder *builder, OperationState &state,
|
|
|
|
spirv::ExecutionModel executionModel,
|
|
|
|
FuncOp function,
|
|
|
|
ArrayRef<Attribute> interfaceVars) {
|
|
|
|
build(builder, state,
|
|
|
|
builder->getI32IntegerAttr(static_cast<int32_t>(executionModel)),
|
|
|
|
builder->getSymbolRefAttr(function),
|
|
|
|
builder->getArrayAttr(interfaceVars));
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseEntryPointOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-07-08 10:56:20 -07:00
|
|
|
spirv::ExecutionModel execModel;
|
|
|
|
SmallVector<OpAsmParser::OperandType, 0> identifiers;
|
|
|
|
SmallVector<Type, 0> idTypes;
|
2019-10-04 14:02:14 -07:00
|
|
|
SmallVector<Attribute, 4> interfaceVars;
|
2019-07-08 10:56:20 -07:00
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
SymbolRefAttr fn;
|
2019-07-08 10:56:20 -07:00
|
|
|
if (parseEnumAttribute(execModel, parser, state) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.parseAttribute(fn, Type(), kFnNameAttrName, state.attributes)) {
|
2019-07-08 10:56:20 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-08-17 10:19:48 -07:00
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (!parser.parseOptionalComma()) {
|
2019-08-17 10:19:48 -07:00
|
|
|
// Parse the interface variables
|
|
|
|
do {
|
|
|
|
// The name of the interface variable attribute isnt important
|
|
|
|
auto attrName = "var_symbol";
|
|
|
|
SymbolRefAttr var;
|
|
|
|
SmallVector<NamedAttribute, 1> attrs;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseAttribute(var, Type(), attrName, attrs)) {
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
interfaceVars.push_back(var);
|
2019-09-20 11:36:49 -07:00
|
|
|
} while (!parser.parseOptionalComma());
|
2019-07-08 10:56:20 -07:00
|
|
|
}
|
2019-10-04 14:02:14 -07:00
|
|
|
state.addAttribute(kInterfaceAttrName,
|
|
|
|
parser.getBuilder().getArrayAttr(interfaceVars));
|
2019-07-08 10:56:20 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::EntryPointOp entryPointOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::EntryPointOp::getOperationName() << " \""
|
|
|
|
<< stringifyExecutionModel(entryPointOp.execution_model()) << "\" @"
|
|
|
|
<< entryPointOp.fn();
|
2019-10-04 14:02:14 -07:00
|
|
|
auto interfaceVars = entryPointOp.interface().getValue();
|
|
|
|
if (!interfaceVars.empty()) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << ", ";
|
2019-10-04 14:02:14 -07:00
|
|
|
interleaveComma(interfaceVars, printer);
|
2019-07-08 10:56:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::EntryPointOp entryPointOp) {
|
2019-08-17 10:19:48 -07:00
|
|
|
// Checks for fn and interface symbol reference are done in spirv::ModuleOp
|
|
|
|
// verification.
|
2019-07-08 10:56:20 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.ExecutionMode
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-04 14:02:14 -07:00
|
|
|
void spirv::ExecutionModeOp::build(Builder *builder, OperationState &state,
|
|
|
|
FuncOp function,
|
|
|
|
spirv::ExecutionMode executionMode,
|
|
|
|
ArrayRef<int32_t> params) {
|
|
|
|
build(builder, state, builder->getSymbolRefAttr(function),
|
|
|
|
builder->getI32IntegerAttr(static_cast<int32_t>(executionMode)),
|
|
|
|
builder->getI32ArrayAttr(params));
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseExecutionModeOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-07-08 10:56:20 -07:00
|
|
|
spirv::ExecutionMode execMode;
|
2019-07-19 07:30:15 -07:00
|
|
|
Attribute fn;
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.parseAttribute(fn, kFnNameAttrName, state.attributes) ||
|
2019-07-08 10:56:20 -07:00
|
|
|
parseEnumAttribute(execMode, parser, state)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<int32_t, 4> values;
|
2019-09-20 11:36:49 -07:00
|
|
|
Type i32Type = parser.getBuilder().getIntegerType(32);
|
|
|
|
while (!parser.parseOptionalComma()) {
|
2019-07-08 10:56:20 -07:00
|
|
|
SmallVector<NamedAttribute, 1> attr;
|
|
|
|
Attribute value;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseAttribute(value, i32Type, "value", attr)) {
|
2019-07-08 10:56:20 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
values.push_back(value.cast<IntegerAttr>().getInt());
|
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute(kValuesAttrName,
|
|
|
|
parser.getBuilder().getI32ArrayAttr(values));
|
2019-07-08 10:56:20 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::ExecutionModeOp execModeOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::ExecutionModeOp::getOperationName() << " @"
|
|
|
|
<< execModeOp.fn() << " \""
|
|
|
|
<< stringifyExecutionMode(execModeOp.execution_mode()) << "\"";
|
2019-07-08 10:56:20 -07:00
|
|
|
auto values = execModeOp.values();
|
2019-10-04 14:02:14 -07:00
|
|
|
if (!values.size()) {
|
2019-07-08 10:56:20 -07:00
|
|
|
return;
|
|
|
|
}
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << ", ";
|
2019-10-04 14:02:14 -07:00
|
|
|
interleaveComma(values, printer, [&](Attribute a) {
|
|
|
|
printer << a.cast<IntegerAttr>().getInt();
|
|
|
|
});
|
2019-07-08 10:56:20 -07:00
|
|
|
}
|
|
|
|
|
2019-09-16 15:39:16 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-09-16 17:11:50 -07:00
|
|
|
// spv.FunctionCall
|
2019-09-16 15:39:16 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseFunctionCallOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-09-16 15:39:16 -07:00
|
|
|
SymbolRefAttr calleeAttr;
|
|
|
|
FunctionType type;
|
|
|
|
SmallVector<OpAsmParser::OperandType, 4> operands;
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getNameLoc();
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.parseAttribute(calleeAttr, kCallee, state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseOperandList(operands, OpAsmParser::Delimiter::Paren) ||
|
|
|
|
parser.parseColonType(type)) {
|
2019-09-16 15:39:16 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto funcType = type.dyn_cast<FunctionType>();
|
|
|
|
if (!funcType) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "expected function type, but provided ")
|
2019-09-16 15:39:16 -07:00
|
|
|
<< type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (funcType.getNumResults() > 1) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "expected callee function to have 0 or 1 "
|
|
|
|
"result, but provided ")
|
2019-09-16 15:39:16 -07:00
|
|
|
<< funcType.getNumResults();
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
return failure(parser.addTypesToList(funcType.getResults(), state.types) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.resolveOperands(operands, funcType.getInputs(), loc,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.operands));
|
2019-09-16 15:39:16 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::FunctionCallOp functionCallOp, OpAsmPrinter &printer) {
|
2019-09-16 15:39:16 -07:00
|
|
|
SmallVector<Type, 4> argTypes(functionCallOp.getOperandTypes());
|
|
|
|
SmallVector<Type, 1> resultTypes(functionCallOp.getResultTypes());
|
|
|
|
Type functionType =
|
|
|
|
FunctionType::get(argTypes, resultTypes, functionCallOp.getContext());
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::FunctionCallOp::getOperationName() << ' '
|
|
|
|
<< functionCallOp.getAttr(kCallee) << '(';
|
|
|
|
printer.printOperands(functionCallOp.arguments());
|
|
|
|
printer << ") : " << functionType;
|
2019-09-16 15:39:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::FunctionCallOp functionCallOp) {
|
|
|
|
auto fnName = functionCallOp.callee();
|
|
|
|
|
|
|
|
auto moduleOp = functionCallOp.getParentOfType<spirv::ModuleOp>();
|
|
|
|
if (!moduleOp) {
|
|
|
|
return functionCallOp.emitOpError(
|
|
|
|
"must appear in a function inside 'spv.module'");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto funcOp = moduleOp.lookupSymbol<FuncOp>(fnName);
|
|
|
|
if (!funcOp) {
|
|
|
|
return functionCallOp.emitOpError("callee function '")
|
|
|
|
<< fnName << "' not found in 'spv.module'";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto functionType = funcOp.getType();
|
|
|
|
|
|
|
|
if (functionCallOp.getNumResults() > 1) {
|
|
|
|
return functionCallOp.emitOpError(
|
|
|
|
"expected callee function to have 0 or 1 result, but provided ")
|
|
|
|
<< functionCallOp.getNumResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionType.getNumInputs() != functionCallOp.getNumOperands()) {
|
|
|
|
return functionCallOp.emitOpError(
|
|
|
|
"has incorrect number of operands for callee: expected ")
|
|
|
|
<< functionType.getNumInputs() << ", but provided "
|
|
|
|
<< functionCallOp.getNumOperands();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0, e = functionType.getNumInputs(); i != e; ++i) {
|
|
|
|
if (functionCallOp.getOperand(i)->getType() != functionType.getInput(i)) {
|
|
|
|
return functionCallOp.emitOpError(
|
|
|
|
"operand type mismatch: expected operand type ")
|
|
|
|
<< functionType.getInput(i) << ", but provided "
|
|
|
|
<< functionCallOp.getOperand(i)->getType()
|
|
|
|
<< " for operand number " << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionType.getNumResults() != functionCallOp.getNumResults()) {
|
|
|
|
return functionCallOp.emitOpError(
|
|
|
|
"has incorrect number of results has for callee: expected ")
|
|
|
|
<< functionType.getNumResults() << ", but provided "
|
|
|
|
<< functionCallOp.getNumResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionCallOp.getNumResults() &&
|
|
|
|
(functionCallOp.getResult(0)->getType() != functionType.getResult(0))) {
|
|
|
|
return functionCallOp.emitOpError("result type mismatch: expected ")
|
|
|
|
<< functionType.getResult(0) << ", but provided "
|
|
|
|
<< functionCallOp.getResult(0)->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.globalVariable
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseGlobalVariableOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-17 10:19:48 -07:00
|
|
|
// Parse variable name.
|
|
|
|
StringAttr nameAttr;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes)) {
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse optional initializer
|
2019-09-20 11:36:49 -07:00
|
|
|
if (succeeded(parser.parseOptionalKeyword(kInitializerAttrName))) {
|
2019-08-17 10:19:48 -07:00
|
|
|
SymbolRefAttr initSymbol;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseLParen() ||
|
|
|
|
parser.parseAttribute(initSymbol, Type(), kInitializerAttrName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseRParen())
|
2019-08-17 10:19:48 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseVariableDecorations(parser, state)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-08-19 11:38:53 -07:00
|
|
|
Type type;
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getCurrentLocation();
|
|
|
|
if (parser.parseColonType(type)) {
|
2019-08-19 11:38:53 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (!type.isa<spirv::PointerType>()) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "expected spv.ptr type");
|
2019-08-19 11:38:53 -07:00
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute(kTypeAttrName, parser.getBuilder().getTypeAttr(type));
|
2019-08-19 11:38:53 -07:00
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::GlobalVariableOp varOp, OpAsmPrinter &printer) {
|
2019-08-17 10:19:48 -07:00
|
|
|
auto *op = varOp.getOperation();
|
|
|
|
SmallVector<StringRef, 4> elidedAttrs{
|
|
|
|
spirv::attributeName<spirv::StorageClass>()};
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::GlobalVariableOp::getOperationName();
|
2019-08-17 10:19:48 -07:00
|
|
|
|
|
|
|
// Print variable name.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " @" << varOp.sym_name();
|
2019-08-17 10:19:48 -07:00
|
|
|
elidedAttrs.push_back(SymbolTable::getSymbolAttrName());
|
|
|
|
|
|
|
|
// Print optional initializer
|
|
|
|
if (auto initializer = varOp.initializer()) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " " << kInitializerAttrName << "(@" << initializer.getValue()
|
|
|
|
<< ")";
|
2019-08-17 10:19:48 -07:00
|
|
|
elidedAttrs.push_back(kInitializerAttrName);
|
|
|
|
}
|
2019-08-19 11:38:53 -07:00
|
|
|
|
|
|
|
elidedAttrs.push_back(kTypeAttrName);
|
2019-08-17 10:19:48 -07:00
|
|
|
printVariableDecorations(op, printer, elidedAttrs);
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << varOp.type();
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::GlobalVariableOp varOp) {
|
|
|
|
// SPIR-V spec: "Storage Class is the Storage Class of the memory holding the
|
|
|
|
// object. It cannot be Generic. It must be the same as the Storage Class
|
|
|
|
// operand of the Result Type."
|
|
|
|
if (varOp.storageClass() == spirv::StorageClass::Generic)
|
|
|
|
return varOp.emitOpError("storage class cannot be 'Generic'");
|
|
|
|
|
2019-08-20 13:33:41 -07:00
|
|
|
if (auto init = varOp.getAttrOfType<SymbolRefAttr>(kInitializerAttrName)) {
|
2019-08-17 10:19:48 -07:00
|
|
|
auto moduleOp = varOp.getParentOfType<spirv::ModuleOp>();
|
2019-08-20 13:33:41 -07:00
|
|
|
auto *initOp = moduleOp.lookupSymbol(init.getValue());
|
|
|
|
// TODO: Currently only variable initialization with specialization
|
|
|
|
// constants and other variables is supported. They could be normal
|
|
|
|
// constants in the module scope as well.
|
|
|
|
if (!initOp || !(isa<spirv::GlobalVariableOp>(initOp) ||
|
|
|
|
isa<spirv::SpecConstantOp>(initOp))) {
|
|
|
|
return varOp.emitOpError("initializer must be result of a "
|
|
|
|
"spv.specConstant or spv.globalVariable op");
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-06-24 10:59:05 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.LoadOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-04 14:02:14 -07:00
|
|
|
void spirv::LoadOp::build(Builder *builder, OperationState &state,
|
|
|
|
Value *basePtr, IntegerAttr memory_access,
|
|
|
|
IntegerAttr alignment) {
|
|
|
|
auto ptrType = basePtr->getType().cast<spirv::PointerType>();
|
|
|
|
build(builder, state, ptrType.getPointeeType(), basePtr, memory_access,
|
|
|
|
alignment);
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseLoadOp(OpAsmParser &parser, OperationState &state) {
|
2019-06-24 10:59:05 -07:00
|
|
|
// Parse the storage class specification
|
|
|
|
spirv::StorageClass storageClass;
|
|
|
|
OpAsmParser::OperandType ptrInfo;
|
|
|
|
Type elementType;
|
2019-07-30 14:14:28 -07:00
|
|
|
if (parseEnumAttribute(storageClass, parser) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseOperand(ptrInfo) ||
|
2019-07-08 10:56:20 -07:00
|
|
|
parseMemoryAccessAttributes(parser, state) ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.parseOptionalAttributeDict(state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseColon() || parser.parseType(elementType)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptrType = spirv::PointerType::get(elementType, storageClass);
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.resolveOperand(ptrInfo, ptrType, state.operands)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addTypes(elementType);
|
2019-06-24 10:59:05 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::LoadOp loadOp, OpAsmPrinter &printer) {
|
2019-06-24 10:59:05 -07:00
|
|
|
auto *op = loadOp.getOperation();
|
|
|
|
SmallVector<StringRef, 4> elidedAttrs;
|
2019-07-02 06:02:20 -07:00
|
|
|
StringRef sc = stringifyStorageClass(
|
|
|
|
loadOp.ptr()->getType().cast<spirv::PointerType>().getStorageClass());
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::LoadOp::getOperationName() << " \"" << sc << "\" ";
|
2019-06-24 10:59:05 -07:00
|
|
|
// Print the pointer operand.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOperand(loadOp.ptr());
|
2019-06-24 10:59:05 -07:00
|
|
|
|
|
|
|
printMemoryAccessAttribute(loadOp, printer, elidedAttrs);
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOptionalAttrDict(op->getAttrs(), elidedAttrs);
|
|
|
|
printer << " : " << loadOp.getType();
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::LoadOp loadOp) {
|
|
|
|
// SPIR-V spec : "Result Type is the type of the loaded object. It must be a
|
|
|
|
// type with fixed size; i.e., it cannot be, nor include, any
|
|
|
|
// OpTypeRuntimeArray types."
|
|
|
|
if (failed(verifyLoadStorePtrAndValTypes(loadOp, loadOp.ptr(),
|
|
|
|
loadOp.value()))) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
return verifyMemoryAccessAttribute(loadOp);
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:45:08 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.loop
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseLoopOp(OpAsmParser &parser, OperationState &state) {
|
2019-09-05 12:45:08 -07:00
|
|
|
// TODO(antiagainst): support loop control properly
|
2019-09-20 11:36:49 -07:00
|
|
|
Builder builder = parser.getBuilder();
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute("loop_control",
|
|
|
|
builder.getI32IntegerAttr(
|
|
|
|
static_cast<uint32_t>(spirv::LoopControl::None)));
|
2019-09-05 12:45:08 -07:00
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
return parser.parseRegion(*state.addRegion(), /*arguments=*/{},
|
2019-09-20 11:36:49 -07:00
|
|
|
/*argTypes=*/{});
|
2019-09-05 12:45:08 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::LoopOp loopOp, OpAsmPrinter &printer) {
|
2019-09-05 12:45:08 -07:00
|
|
|
auto *op = loopOp.getOperation();
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::LoopOp::getOperationName();
|
|
|
|
printer.printRegion(op->getRegion(0), /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/true);
|
2019-09-05 12:45:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the given `srcBlock` contains only one `spv.Branch` to the
|
|
|
|
/// given `dstBlock`.
|
|
|
|
static inline bool hasOneBranchOpTo(Block &srcBlock, Block &dstBlock) {
|
|
|
|
// Check that there is only one op in the `srcBlock`.
|
2019-09-11 14:02:23 -07:00
|
|
|
if (srcBlock.empty() || std::next(srcBlock.begin()) != srcBlock.end())
|
2019-09-05 12:45:08 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto branchOp = dyn_cast<spirv::BranchOp>(srcBlock.back());
|
|
|
|
return branchOp && branchOp.getSuccessor(0) == &dstBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::LoopOp loopOp) {
|
|
|
|
auto *op = loopOp.getOperation();
|
|
|
|
|
|
|
|
// We need to verify that the blocks follow the following layout:
|
|
|
|
//
|
|
|
|
// +-------------+
|
|
|
|
// | entry block |
|
|
|
|
// +-------------+
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// +-------------+
|
|
|
|
// | loop header | <-----+
|
|
|
|
// +-------------+ |
|
|
|
|
// |
|
|
|
|
// ... |
|
|
|
|
// \ | / |
|
|
|
|
// v |
|
|
|
|
// +---------------+ |
|
|
|
|
// | loop continue | -----+
|
|
|
|
// +---------------+
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
// \ | /
|
|
|
|
// v
|
|
|
|
// +-------------+
|
|
|
|
// | merge block |
|
|
|
|
// +-------------+
|
|
|
|
|
|
|
|
auto ®ion = op->getRegion(0);
|
|
|
|
// Allow empty region as a degenerated case, which can come from
|
|
|
|
// optimizations.
|
|
|
|
if (region.empty())
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// The last block is the merge block.
|
|
|
|
Block &merge = region.back();
|
|
|
|
if (!isMergeBlock(merge))
|
|
|
|
return loopOp.emitOpError(
|
|
|
|
"last block must be the merge block with only one 'spv._merge' op");
|
|
|
|
|
|
|
|
if (std::next(region.begin()) == region.end())
|
|
|
|
return loopOp.emitOpError(
|
|
|
|
"must have an entry block branching to the loop header block");
|
|
|
|
// The first block is the entry block.
|
|
|
|
Block &entry = region.front();
|
|
|
|
|
|
|
|
if (std::next(region.begin(), 2) == region.end())
|
|
|
|
return loopOp.emitOpError(
|
|
|
|
"must have a loop header block branched from the entry block");
|
|
|
|
// The second block is the loop header block.
|
|
|
|
Block &header = *std::next(region.begin(), 1);
|
|
|
|
|
|
|
|
if (!hasOneBranchOpTo(entry, header))
|
|
|
|
return loopOp.emitOpError(
|
|
|
|
"entry block must only have one 'spv.Branch' op to the second block");
|
|
|
|
|
|
|
|
if (std::next(region.begin(), 3) == region.end())
|
|
|
|
return loopOp.emitOpError(
|
|
|
|
"requires a loop continue block branching to the loop header block");
|
|
|
|
// The second to last block is the loop continue block.
|
|
|
|
Block &cont = *std::prev(region.end(), 2);
|
|
|
|
|
|
|
|
// Make sure that we have a branch from the loop continue block to the loop
|
|
|
|
// header block.
|
|
|
|
if (llvm::none_of(
|
|
|
|
llvm::seq<unsigned>(0, cont.getNumSuccessors()),
|
|
|
|
[&](unsigned index) { return cont.getSuccessor(index) == &header; }))
|
|
|
|
return loopOp.emitOpError("second to last block must be the loop continue "
|
|
|
|
"block that branches to the loop header block");
|
|
|
|
|
|
|
|
// Make sure that no other blocks (except the entry and loop continue block)
|
|
|
|
// branches to the loop header block.
|
|
|
|
for (auto &block : llvm::make_range(std::next(region.begin(), 2),
|
|
|
|
std::prev(region.end(), 2))) {
|
|
|
|
for (auto i : llvm::seq<unsigned>(0, block.getNumSuccessors())) {
|
|
|
|
if (block.getSuccessor(i) == &header) {
|
|
|
|
return loopOp.emitOpError("can only have the entry and loop continue "
|
|
|
|
"block branching to the loop header block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-11 14:02:23 -07:00
|
|
|
Block *spirv::LoopOp::getHeaderBlock() {
|
2019-10-02 11:00:50 -07:00
|
|
|
assert(!body().empty() && "op region should not be empty!");
|
2019-09-11 14:02:23 -07:00
|
|
|
// The second block is the loop header block.
|
|
|
|
return &*std::next(body().begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *spirv::LoopOp::getContinueBlock() {
|
2019-10-02 11:00:50 -07:00
|
|
|
assert(!body().empty() && "op region should not be empty!");
|
2019-09-11 14:02:23 -07:00
|
|
|
// The second to last block is the loop continue block.
|
|
|
|
return &*std::prev(body().end(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *spirv::LoopOp::getMergeBlock() {
|
2019-10-02 11:00:50 -07:00
|
|
|
assert(!body().empty() && "op region should not be empty!");
|
2019-09-11 14:02:23 -07:00
|
|
|
// The last block is the loop merge block.
|
|
|
|
return &body().back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void spirv::LoopOp::addEntryAndMergeBlock() {
|
|
|
|
assert(body().empty() && "entry and merge block already exist");
|
|
|
|
body().push_back(new Block());
|
|
|
|
auto *mergeBlock = new Block();
|
|
|
|
body().push_back(mergeBlock);
|
|
|
|
OpBuilder builder(mergeBlock);
|
|
|
|
|
|
|
|
// Add a spv._merge op into the merge block.
|
2019-10-02 11:00:50 -07:00
|
|
|
builder.create<spirv::MergeOp>(getLoc());
|
2019-09-11 14:02:23 -07:00
|
|
|
}
|
|
|
|
|
2019-09-05 12:45:08 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv._merge
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::MergeOp mergeOp) {
|
2019-10-02 11:00:50 -07:00
|
|
|
auto *parentOp = mergeOp.getParentOp();
|
|
|
|
if (!parentOp ||
|
|
|
|
(!isa<spirv::SelectionOp>(parentOp) && !isa<spirv::LoopOp>(parentOp)))
|
|
|
|
return mergeOp.emitOpError(
|
|
|
|
"expected parent op to be 'spv.selection' or 'spv.loop'");
|
|
|
|
|
2019-09-05 12:45:08 -07:00
|
|
|
Block &parentLastBlock = mergeOp.getParentRegion()->back();
|
|
|
|
if (mergeOp.getOperation() != parentLastBlock.getTerminator())
|
|
|
|
return mergeOp.emitOpError(
|
2019-10-02 11:00:50 -07:00
|
|
|
"can only be used in the last block of 'spv.selection' or 'spv.loop'");
|
2019-09-05 12:45:08 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-21 10:18:00 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.MemoryBarrier
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseMemoryBarrierOp(OpAsmParser &parser,
|
|
|
|
OperationState &state) {
|
|
|
|
spirv::Scope memoryScope;
|
|
|
|
spirv::MemorySemantics memorySemantics;
|
|
|
|
|
|
|
|
return failure(
|
|
|
|
parseEnumAttribute(memoryScope, parser, state, kMemoryScopeAttrName) ||
|
|
|
|
parser.parseComma() ||
|
|
|
|
parseEnumAttribute(memorySemantics, parser, state));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::MemoryBarrierOp op, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::MemoryBarrierOp::getOperationName() << " \""
|
|
|
|
<< stringifyScope(op.memory_scope()) << "\", \""
|
|
|
|
<< stringifyMemorySemantics(op.memory_semantics()) << "\"";
|
|
|
|
}
|
|
|
|
|
2019-05-29 10:47:16 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.module
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
void spirv::ModuleOp::build(Builder *builder, OperationState &state) {
|
|
|
|
ensureTerminator(*state.addRegion(), *builder, state.location);
|
2019-06-21 14:51:58 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
void spirv::ModuleOp::build(Builder *builder, OperationState &state,
|
2019-07-30 11:29:48 -07:00
|
|
|
IntegerAttr addressing_model,
|
|
|
|
IntegerAttr memory_model, ArrayAttr capabilities,
|
|
|
|
ArrayAttr extensions,
|
|
|
|
ArrayAttr extended_instruction_sets) {
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute("addressing_model", addressing_model);
|
|
|
|
state.addAttribute("memory_model", memory_model);
|
2019-07-30 11:29:48 -07:00
|
|
|
if (capabilities)
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute("capabilities", capabilities);
|
2019-07-30 11:29:48 -07:00
|
|
|
if (extensions)
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute("extensions", extensions);
|
2019-07-30 11:29:48 -07:00
|
|
|
if (extended_instruction_sets)
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute("extended_instruction_sets", extended_instruction_sets);
|
|
|
|
ensureTerminator(*state.addRegion(), *builder, state.location);
|
2019-07-30 11:29:48 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseModuleOp(OpAsmParser &parser, OperationState &state) {
|
|
|
|
Region *body = state.addRegion();
|
2019-05-29 10:47:16 -07:00
|
|
|
|
2019-07-03 18:12:52 -07:00
|
|
|
// Parse attributes
|
|
|
|
spirv::AddressingModel addrModel;
|
|
|
|
spirv::MemoryModel memoryModel;
|
|
|
|
if (parseEnumAttribute(addrModel, parser, state) ||
|
|
|
|
parseEnumAttribute(memoryModel, parser, state)) {
|
2019-07-02 06:02:20 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{}))
|
2019-07-02 06:02:20 -07:00
|
|
|
return failure();
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (succeeded(parser.parseOptionalKeyword("attributes"))) {
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.parseOptionalAttributeDict(state.attributes))
|
2019-07-02 06:02:20 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-05-29 10:47:16 -07:00
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
spirv::ModuleOp::ensureTerminator(*body, parser.getBuilder(), state.location);
|
2019-05-29 10:47:16 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::ModuleOp moduleOp, OpAsmPrinter &printer) {
|
2019-06-17 13:29:06 -07:00
|
|
|
auto *op = moduleOp.getOperation();
|
2019-07-02 06:02:20 -07:00
|
|
|
|
|
|
|
// Only print out addressing model and memory model in a nicer way if both
|
|
|
|
// presents. Otherwise, print them in the general form. This helps debugging
|
|
|
|
// ill-formed ModuleOp.
|
|
|
|
SmallVector<StringRef, 2> elidedAttrs;
|
2019-07-03 18:12:52 -07:00
|
|
|
auto addressingModelAttrName = spirv::attributeName<spirv::AddressingModel>();
|
|
|
|
auto memoryModelAttrName = spirv::attributeName<spirv::MemoryModel>();
|
|
|
|
if (op->getAttr(addressingModelAttrName) &&
|
|
|
|
op->getAttr(memoryModelAttrName)) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::ModuleOp::getOperationName() << " \""
|
|
|
|
<< spirv::stringifyAddressingModel(moduleOp.addressing_model())
|
|
|
|
<< "\" \"" << spirv::stringifyMemoryModel(moduleOp.memory_model())
|
|
|
|
<< '"';
|
2019-07-03 18:12:52 -07:00
|
|
|
elidedAttrs.assign({addressingModelAttrName, memoryModelAttrName});
|
2019-07-02 06:02:20 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printRegion(op->getRegion(0), /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/false);
|
2019-07-02 06:02:20 -07:00
|
|
|
|
2019-07-03 18:12:52 -07:00
|
|
|
bool printAttrDict =
|
|
|
|
elidedAttrs.size() != 2 ||
|
|
|
|
llvm::any_of(op->getAttrs(), [&addressingModelAttrName,
|
|
|
|
&memoryModelAttrName](NamedAttribute attr) {
|
|
|
|
return attr.first != addressingModelAttrName &&
|
|
|
|
attr.first != memoryModelAttrName;
|
|
|
|
});
|
2019-07-02 06:02:20 -07:00
|
|
|
|
|
|
|
if (printAttrDict) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " attributes";
|
|
|
|
printer.printOptionalAttrDict(op->getAttrs(), elidedAttrs);
|
2019-07-02 06:02:20 -07:00
|
|
|
}
|
2019-05-29 10:47:16 -07:00
|
|
|
}
|
|
|
|
|
2019-06-18 11:15:55 -07:00
|
|
|
static LogicalResult verify(spirv::ModuleOp moduleOp) {
|
2019-06-04 14:03:30 -07:00
|
|
|
auto &op = *moduleOp.getOperation();
|
|
|
|
auto *dialect = op.getDialect();
|
|
|
|
auto &body = op.getRegion(0).front();
|
2019-07-08 10:56:20 -07:00
|
|
|
llvm::DenseMap<std::pair<FuncOp, spirv::ExecutionModel>, spirv::EntryPointOp>
|
|
|
|
entryPoints;
|
2019-08-08 14:40:03 -07:00
|
|
|
SymbolTable table(moduleOp);
|
2019-06-04 14:03:30 -07:00
|
|
|
|
|
|
|
for (auto &op : body) {
|
2019-07-08 10:56:20 -07:00
|
|
|
if (op.getDialect() == dialect) {
|
2019-08-08 14:18:39 -07:00
|
|
|
// For EntryPoint op, check that the function and execution model is not
|
2019-08-17 10:19:48 -07:00
|
|
|
// duplicated in EntryPointOps. Also verify that the interface specified
|
|
|
|
// comes from globalVariables here to make this check cheaper.
|
2019-08-23 11:07:13 -07:00
|
|
|
if (auto entryPointOp = dyn_cast<spirv::EntryPointOp>(op)) {
|
2019-08-08 14:40:03 -07:00
|
|
|
auto funcOp = table.lookup<FuncOp>(entryPointOp.fn());
|
2019-08-08 14:18:39 -07:00
|
|
|
if (!funcOp) {
|
2019-07-08 10:56:20 -07:00
|
|
|
return entryPointOp.emitError("function '")
|
|
|
|
<< entryPointOp.fn() << "' not found in 'spv.module'";
|
|
|
|
}
|
2019-08-17 10:19:48 -07:00
|
|
|
if (auto interface = entryPointOp.interface()) {
|
2019-10-04 14:02:14 -07:00
|
|
|
for (Attribute varRef : interface) {
|
2019-08-17 10:19:48 -07:00
|
|
|
auto varSymRef = varRef.dyn_cast<SymbolRefAttr>();
|
|
|
|
if (!varSymRef) {
|
|
|
|
return entryPointOp.emitError(
|
|
|
|
"expected symbol reference for interface "
|
|
|
|
"specification instead of '")
|
|
|
|
<< varRef;
|
|
|
|
}
|
|
|
|
auto variableOp =
|
|
|
|
table.lookup<spirv::GlobalVariableOp>(varSymRef.getValue());
|
|
|
|
if (!variableOp) {
|
|
|
|
return entryPointOp.emitError("expected spv.globalVariable "
|
|
|
|
"symbol reference instead of'")
|
|
|
|
<< varSymRef << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:56:20 -07:00
|
|
|
auto key = std::pair<FuncOp, spirv::ExecutionModel>(
|
|
|
|
funcOp, entryPointOp.execution_model());
|
|
|
|
auto entryPtIt = entryPoints.find(key);
|
|
|
|
if (entryPtIt != entryPoints.end()) {
|
|
|
|
return entryPointOp.emitError("duplicate of a previous EntryPointOp");
|
|
|
|
}
|
|
|
|
entryPoints[key] = entryPointOp;
|
|
|
|
}
|
2019-06-04 14:03:30 -07:00
|
|
|
continue;
|
2019-07-08 10:56:20 -07:00
|
|
|
}
|
2019-06-04 14:03:30 -07:00
|
|
|
|
2019-08-23 11:07:13 -07:00
|
|
|
auto funcOp = dyn_cast<FuncOp>(op);
|
2019-06-04 14:03:30 -07:00
|
|
|
if (!funcOp)
|
|
|
|
return op.emitError("'spv.module' can only contain func and spv.* ops");
|
|
|
|
|
|
|
|
if (funcOp.isExternal())
|
|
|
|
return op.emitError("'spv.module' cannot contain external functions");
|
|
|
|
|
|
|
|
for (auto &block : funcOp)
|
|
|
|
for (auto &op : block) {
|
|
|
|
if (op.getDialect() == dialect)
|
|
|
|
continue;
|
|
|
|
|
2019-08-23 11:07:13 -07:00
|
|
|
if (isa<FuncOp>(op))
|
2019-06-04 14:03:30 -07:00
|
|
|
return op.emitError("'spv.module' cannot contain nested functions");
|
|
|
|
|
|
|
|
return op.emitError(
|
|
|
|
"functions in 'spv.module' can only contain spv.* ops");
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 11:15:05 -07:00
|
|
|
|
|
|
|
// Verify capabilities. ODS already guarantees that we have an array of
|
|
|
|
// string attributes.
|
|
|
|
if (auto caps = moduleOp.getAttrOfType<ArrayAttr>("capabilities")) {
|
|
|
|
for (auto cap : caps.getValue()) {
|
|
|
|
auto capStr = cap.cast<StringAttr>().getValue();
|
|
|
|
if (!spirv::symbolizeCapability(capStr))
|
|
|
|
return moduleOp.emitOpError("uses unknown capability: ") << capStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:01:13 -07:00
|
|
|
// Verify extensions. ODS already guarantees that we have an array of
|
|
|
|
// string attributes.
|
|
|
|
if (auto exts = moduleOp.getAttrOfType<ArrayAttr>("extensions")) {
|
|
|
|
for (auto ext : exts.getValue()) {
|
|
|
|
auto extStr = ext.cast<StringAttr>().getValue();
|
|
|
|
if (!spirv::symbolizeExtension(extStr))
|
|
|
|
return moduleOp.emitOpError("uses unknown extension: ") << extStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 14:03:30 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:33:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv._reference_of
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseReferenceOfOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-20 13:33:41 -07:00
|
|
|
SymbolRefAttr constRefAttr;
|
|
|
|
Type type;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseAttribute(constRefAttr, Type(), kSpecConstAttrName,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseColonType(type)) {
|
2019-08-20 13:33:41 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
return parser.addTypeToList(type, state.types);
|
2019-08-20 13:33:41 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::ReferenceOfOp referenceOfOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::ReferenceOfOp::getOperationName() << " @"
|
|
|
|
<< referenceOfOp.spec_const() << " : "
|
|
|
|
<< referenceOfOp.reference()->getType();
|
2019-08-20 13:33:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::ReferenceOfOp referenceOfOp) {
|
|
|
|
auto moduleOp = referenceOfOp.getParentOfType<spirv::ModuleOp>();
|
|
|
|
auto specConstOp =
|
|
|
|
moduleOp.lookupSymbol<spirv::SpecConstantOp>(referenceOfOp.spec_const());
|
|
|
|
if (!specConstOp) {
|
|
|
|
return referenceOfOp.emitOpError("expected spv.specConstant symbol");
|
|
|
|
}
|
|
|
|
if (referenceOfOp.reference()->getType() !=
|
|
|
|
specConstOp.default_value().getType()) {
|
|
|
|
return referenceOfOp.emitOpError("result type mismatch with the referenced "
|
|
|
|
"specialization constant's type");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
2019-08-30 12:17:21 -07:00
|
|
|
|
2019-06-04 14:03:30 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.Return
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-23 11:07:13 -07:00
|
|
|
static LogicalResult verify(spirv::ReturnOp returnOp) {
|
|
|
|
auto funcOp = cast<FuncOp>(returnOp.getParentOp());
|
2019-06-04 14:03:30 -07:00
|
|
|
auto numOutputs = funcOp.getType().getNumResults();
|
|
|
|
if (numOutputs != 0)
|
|
|
|
return returnOp.emitOpError("cannot be used in functions returning value")
|
|
|
|
<< (numOutputs > 1 ? "s" : "");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-08-19 10:57:43 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.ReturnValue
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseReturnValueOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-19 10:57:43 -07:00
|
|
|
OpAsmParser::OperandType retValInfo;
|
|
|
|
Type retValType;
|
2019-09-20 19:47:05 -07:00
|
|
|
return failure(parser.parseOperand(retValInfo) ||
|
|
|
|
parser.parseColonType(retValType) ||
|
|
|
|
parser.resolveOperand(retValInfo, retValType, state.operands));
|
2019-08-19 10:57:43 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::ReturnValueOp retValOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::ReturnValueOp::getOperationName() << ' ';
|
|
|
|
printer.printOperand(retValOp.value());
|
|
|
|
printer << " : " << retValOp.value()->getType();
|
2019-08-19 10:57:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::ReturnValueOp retValOp) {
|
2019-08-23 11:07:13 -07:00
|
|
|
auto funcOp = cast<FuncOp>(retValOp.getParentOp());
|
2019-08-19 10:57:43 -07:00
|
|
|
auto numFnResults = funcOp.getType().getNumResults();
|
|
|
|
if (numFnResults != 1)
|
|
|
|
return retValOp.emitOpError(
|
|
|
|
"returns 1 value but enclosing function requires ")
|
|
|
|
<< numFnResults << " results";
|
|
|
|
|
|
|
|
auto operandType = retValOp.value()->getType();
|
|
|
|
auto fnResultType = funcOp.getType().getResult(0);
|
|
|
|
if (operandType != fnResultType)
|
|
|
|
return retValOp.emitOpError(" return value's type (")
|
|
|
|
<< operandType << ") mismatch with function's result type ("
|
|
|
|
<< fnResultType << ")";
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-02 21:06:35 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.Select
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseSelectOp(OpAsmParser &parser, OperationState &state) {
|
2019-09-02 21:06:35 -07:00
|
|
|
OpAsmParser::OperandType condition;
|
|
|
|
SmallVector<OpAsmParser::OperandType, 2> operands;
|
|
|
|
SmallVector<Type, 2> types;
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getCurrentLocation();
|
|
|
|
if (parser.parseOperand(condition) || parser.parseComma() ||
|
|
|
|
parser.parseOperandList(operands, 2) ||
|
|
|
|
parser.parseColonTypeList(types)) {
|
2019-09-02 21:06:35 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (types.size() != 2) {
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(
|
2019-09-02 21:06:35 -07:00
|
|
|
loc, "need exactly two trailing types for select condition and object");
|
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
if (parser.resolveOperand(condition, types[0], state.operands) ||
|
|
|
|
parser.resolveOperands(operands, types[1], state.operands)) {
|
2019-09-02 21:06:35 -07:00
|
|
|
return failure();
|
|
|
|
}
|
2019-09-20 19:47:05 -07:00
|
|
|
return parser.addTypesToList(types[1], state.types);
|
2019-09-02 21:06:35 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::SelectOp op, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::SelectOp::getOperationName() << " ";
|
2019-09-02 21:06:35 -07:00
|
|
|
|
|
|
|
// Print the operands.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOperands(op.getOperands());
|
2019-09-02 21:06:35 -07:00
|
|
|
|
|
|
|
// Print colon and types.
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << op.condition()->getType() << ", "
|
|
|
|
<< op.result()->getType();
|
2019-09-02 21:06:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::SelectOp op) {
|
|
|
|
auto resultTy = op.result()->getType();
|
|
|
|
if (op.true_value()->getType() != resultTy) {
|
|
|
|
return op.emitOpError("result type and true value type must be the same");
|
|
|
|
}
|
|
|
|
if (op.false_value()->getType() != resultTy) {
|
|
|
|
return op.emitOpError("result type and false value type must be the same");
|
|
|
|
}
|
|
|
|
if (auto conditionTy = op.condition()->getType().dyn_cast<VectorType>()) {
|
|
|
|
auto resultVectorTy = resultTy.dyn_cast<VectorType>();
|
|
|
|
if (!resultVectorTy) {
|
|
|
|
return op.emitOpError("result expected to be of vector type when "
|
|
|
|
"condition is of vector type");
|
|
|
|
}
|
|
|
|
if (resultVectorTy.getNumElements() != conditionTy.getNumElements()) {
|
|
|
|
return op.emitOpError("result should have the same number of elements as "
|
|
|
|
"the condition when condition is of vector type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-10-02 11:00:50 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.selection
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseSelectionOp(OpAsmParser &parser,
|
|
|
|
OperationState &state) {
|
|
|
|
// TODO(antiagainst): support selection control properly
|
|
|
|
Builder builder = parser.getBuilder();
|
|
|
|
state.addAttribute("selection_control",
|
|
|
|
builder.getI32IntegerAttr(
|
|
|
|
static_cast<uint32_t>(spirv::SelectionControl::None)));
|
|
|
|
|
|
|
|
return parser.parseRegion(*state.addRegion(), /*arguments=*/{},
|
|
|
|
/*argTypes=*/{});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::SelectionOp selectionOp, OpAsmPrinter &printer) {
|
|
|
|
auto *op = selectionOp.getOperation();
|
|
|
|
|
|
|
|
printer << spirv::SelectionOp::getOperationName();
|
|
|
|
printer.printRegion(op->getRegion(0), /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::SelectionOp selectionOp) {
|
|
|
|
auto *op = selectionOp.getOperation();
|
|
|
|
|
|
|
|
// We need to verify that the blocks follow the following layout:
|
|
|
|
//
|
|
|
|
// +--------------+
|
|
|
|
// | header block |
|
|
|
|
// +--------------+
|
|
|
|
// / | \
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// +---------+ +---------+ +---------+
|
|
|
|
// | case #0 | | case #1 | | case #2 | ...
|
|
|
|
// +---------+ +---------+ +---------+
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
// \ | /
|
|
|
|
// v
|
|
|
|
// +-------------+
|
|
|
|
// | merge block |
|
|
|
|
// +-------------+
|
|
|
|
|
|
|
|
auto ®ion = op->getRegion(0);
|
|
|
|
// Allow empty region as a degenerated case, which can come from
|
|
|
|
// optimizations.
|
|
|
|
if (region.empty())
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// The last block is the merge block.
|
|
|
|
if (!isMergeBlock(region.back()))
|
|
|
|
return selectionOp.emitOpError(
|
|
|
|
"last block must be the merge block with only one 'spv._merge' op");
|
|
|
|
|
|
|
|
if (std::next(region.begin()) == region.end())
|
|
|
|
return selectionOp.emitOpError("must have a selection header block");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *spirv::SelectionOp::getHeaderBlock() {
|
|
|
|
assert(!body().empty() && "op region should not be empty!");
|
|
|
|
// The first block is the loop header block.
|
|
|
|
return &body().front();
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *spirv::SelectionOp::getMergeBlock() {
|
|
|
|
assert(!body().empty() && "op region should not be empty!");
|
|
|
|
// The last block is the loop merge block.
|
|
|
|
return &body().back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void spirv::SelectionOp::addMergeBlock() {
|
|
|
|
assert(body().empty() && "entry and merge block already exist");
|
|
|
|
auto *mergeBlock = new Block();
|
|
|
|
body().push_back(mergeBlock);
|
|
|
|
OpBuilder builder(mergeBlock);
|
|
|
|
|
|
|
|
// Add a spv._merge op into the merge block.
|
|
|
|
builder.create<spirv::MergeOp>(getLoc());
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:33:41 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.specConstant
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
static ParseResult parseSpecConstantOp(OpAsmParser &parser,
|
2019-09-20 19:47:05 -07:00
|
|
|
OperationState &state) {
|
2019-08-20 13:33:41 -07:00
|
|
|
StringAttr nameAttr;
|
|
|
|
Attribute valueAttr;
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
|
2019-09-20 19:47:05 -07:00
|
|
|
state.attributes) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseEqual() ||
|
2019-09-20 19:47:05 -07:00
|
|
|
parser.parseAttribute(valueAttr, kDefaultValueAttrName, state.attributes))
|
2019-08-20 13:33:41 -07:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::SpecConstantOp constOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::SpecConstantOp::getOperationName() << " @"
|
|
|
|
<< constOp.sym_name() << " = ";
|
|
|
|
printer.printAttribute(constOp.default_value());
|
2019-08-20 13:33:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::SpecConstantOp constOp) {
|
|
|
|
auto value = constOp.default_value();
|
|
|
|
|
|
|
|
switch (value.getKind()) {
|
|
|
|
case StandardAttributes::Bool:
|
|
|
|
case StandardAttributes::Integer:
|
|
|
|
case StandardAttributes::Float: {
|
|
|
|
// Make sure bitwidth is allowed.
|
2019-09-03 12:09:07 -07:00
|
|
|
if (!spirv::SPIRVDialect::isValidType(value.getType()))
|
2019-08-20 13:33:41 -07:00
|
|
|
return constOp.emitOpError("default value bitwidth disallowed");
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return constOp.emitOpError(
|
|
|
|
"default value can only be a bool, integer, or float scalar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 10:59:05 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.StoreOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseStoreOp(OpAsmParser &parser, OperationState &state) {
|
2019-06-24 10:59:05 -07:00
|
|
|
// Parse the storage class specification
|
|
|
|
spirv::StorageClass storageClass;
|
|
|
|
SmallVector<OpAsmParser::OperandType, 2> operandInfo;
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getCurrentLocation();
|
2019-06-24 10:59:05 -07:00
|
|
|
Type elementType;
|
2019-07-30 14:14:28 -07:00
|
|
|
if (parseEnumAttribute(storageClass, parser) ||
|
2019-09-20 11:36:49 -07:00
|
|
|
parser.parseOperandList(operandInfo, 2) ||
|
|
|
|
parseMemoryAccessAttributes(parser, state) || parser.parseColon() ||
|
|
|
|
parser.parseType(elementType)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptrType = spirv::PointerType::get(elementType, storageClass);
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.resolveOperands(operandInfo, {ptrType, elementType}, loc,
|
2019-09-20 19:47:05 -07:00
|
|
|
state.operands)) {
|
2019-06-24 10:59:05 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::StoreOp storeOp, OpAsmPrinter &printer) {
|
2019-06-24 10:59:05 -07:00
|
|
|
auto *op = storeOp.getOperation();
|
|
|
|
SmallVector<StringRef, 4> elidedAttrs;
|
2019-07-02 06:02:20 -07:00
|
|
|
StringRef sc = stringifyStorageClass(
|
|
|
|
storeOp.ptr()->getType().cast<spirv::PointerType>().getStorageClass());
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::StoreOp::getOperationName() << " \"" << sc << "\" ";
|
2019-06-24 10:59:05 -07:00
|
|
|
// Print the pointer operand
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOperand(storeOp.ptr());
|
|
|
|
printer << ", ";
|
2019-06-24 10:59:05 -07:00
|
|
|
// Print the value operand
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOperand(storeOp.value());
|
2019-06-24 10:59:05 -07:00
|
|
|
|
|
|
|
printMemoryAccessAttribute(storeOp, printer, elidedAttrs);
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << storeOp.value()->getType();
|
2019-06-24 10:59:05 -07:00
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer.printOptionalAttrDict(op->getAttrs(), elidedAttrs);
|
2019-06-24 10:59:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::StoreOp storeOp) {
|
|
|
|
// SPIR-V spec : "Pointer is the pointer to store through. Its type must be an
|
|
|
|
// OpTypePointer whose Type operand is the same as the type of Object."
|
|
|
|
if (failed(verifyLoadStorePtrAndValTypes(storeOp, storeOp.ptr(),
|
|
|
|
storeOp.value()))) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
return verifyMemoryAccessAttribute(storeOp);
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:59:54 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.Undef
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static ParseResult parseUndefOp(OpAsmParser &parser, OperationState &state) {
|
|
|
|
Type type;
|
|
|
|
if (parser.parseColonType(type)) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
state.addTypes(type);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print(spirv::UndefOp undefOp, OpAsmPrinter &printer) {
|
|
|
|
printer << spirv::UndefOp::getOperationName() << " : " << undefOp.getType();
|
|
|
|
}
|
|
|
|
|
2019-06-18 11:15:55 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// spv.Variable
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-20 19:47:05 -07:00
|
|
|
static ParseResult parseVariableOp(OpAsmParser &parser, OperationState &state) {
|
2019-06-18 11:15:55 -07:00
|
|
|
// Parse optional initializer
|
|
|
|
Optional<OpAsmParser::OperandType> initInfo;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (succeeded(parser.parseOptionalKeyword("init"))) {
|
2019-06-18 11:15:55 -07:00
|
|
|
initInfo = OpAsmParser::OperandType();
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseLParen() || parser.parseOperand(*initInfo) ||
|
|
|
|
parser.parseRParen())
|
2019-06-18 11:15:55 -07:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
if (parseVariableDecorations(parser, state)) {
|
2019-06-18 11:15:55 -07:00
|
|
|
return failure();
|
2019-08-17 10:19:48 -07:00
|
|
|
}
|
2019-06-18 11:15:55 -07:00
|
|
|
|
|
|
|
// Parse result pointer type
|
|
|
|
Type type;
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.parseColon())
|
2019-06-18 11:15:55 -07:00
|
|
|
return failure();
|
2019-09-20 11:36:49 -07:00
|
|
|
auto loc = parser.getCurrentLocation();
|
|
|
|
if (parser.parseType(type))
|
2019-06-18 11:15:55 -07:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
auto ptrType = type.dyn_cast<spirv::PointerType>();
|
|
|
|
if (!ptrType)
|
2019-09-20 11:36:49 -07:00
|
|
|
return parser.emitError(loc, "expected spv.ptr type");
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addTypes(ptrType);
|
2019-06-18 11:15:55 -07:00
|
|
|
|
|
|
|
// Resolve the initializer operand
|
|
|
|
SmallVector<Value *, 1> init;
|
|
|
|
if (initInfo) {
|
2019-09-20 11:36:49 -07:00
|
|
|
if (parser.resolveOperand(*initInfo, ptrType.getPointeeType(), init))
|
2019-06-18 11:15:55 -07:00
|
|
|
return failure();
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addOperands(init);
|
2019-06-18 11:15:55 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 11:36:49 -07:00
|
|
|
auto attr = parser.getBuilder().getI32IntegerAttr(
|
2019-09-24 19:24:33 -07:00
|
|
|
llvm::bit_cast<int32_t>(ptrType.getStorageClass()));
|
2019-09-20 19:47:05 -07:00
|
|
|
state.addAttribute(spirv::attributeName<spirv::StorageClass>(), attr);
|
2019-06-18 11:15:55 -07:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
static void print(spirv::VariableOp varOp, OpAsmPrinter &printer) {
|
2019-06-18 11:15:55 -07:00
|
|
|
auto *op = varOp.getOperation();
|
2019-07-03 18:12:52 -07:00
|
|
|
SmallVector<StringRef, 4> elidedAttrs{
|
|
|
|
spirv::attributeName<spirv::StorageClass>()};
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << spirv::VariableOp::getOperationName();
|
2019-06-18 11:15:55 -07:00
|
|
|
|
|
|
|
// Print optional initializer
|
|
|
|
if (op->getNumOperands() > 0) {
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " init(";
|
|
|
|
printer.printOperands(varOp.initializer());
|
|
|
|
printer << ")";
|
2019-06-18 11:15:55 -07:00
|
|
|
}
|
|
|
|
|
2019-08-17 10:19:48 -07:00
|
|
|
printVariableDecorations(op, printer, elidedAttrs);
|
2019-08-15 10:52:24 -07:00
|
|
|
|
2019-09-20 20:43:02 -07:00
|
|
|
printer << " : " << varOp.getType();
|
2019-06-18 11:15:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(spirv::VariableOp varOp) {
|
|
|
|
// SPIR-V spec: "Storage Class is the Storage Class of the memory holding the
|
|
|
|
// object. It cannot be Generic. It must be the same as the Storage Class
|
|
|
|
// operand of the Result Type."
|
2019-08-17 10:19:48 -07:00
|
|
|
if (varOp.storage_class() != spirv::StorageClass::Function) {
|
|
|
|
return varOp.emitOpError(
|
|
|
|
"can only be used to model function-level variables. Use "
|
|
|
|
"spv.globalVariable for module-level variables.");
|
|
|
|
}
|
2019-06-18 11:15:55 -07:00
|
|
|
|
|
|
|
auto pointerType = varOp.pointer()->getType().cast<spirv::PointerType>();
|
2019-07-02 06:02:20 -07:00
|
|
|
if (varOp.storage_class() != pointerType.getStorageClass())
|
2019-06-18 11:15:55 -07:00
|
|
|
return varOp.emitOpError(
|
|
|
|
"storage class must match result pointer's storage class");
|
|
|
|
|
|
|
|
if (varOp.getNumOperands() != 0) {
|
|
|
|
// SPIR-V spec: "Initializer must be an <id> from a constant instruction or
|
|
|
|
// a global (module scope) OpVariable instruction".
|
2019-08-20 13:33:41 -07:00
|
|
|
auto *initOp = varOp.getOperand(0)->getDefiningOp();
|
|
|
|
if (!initOp || !(isa<spirv::ConstantOp>(initOp) || // for normal constant
|
|
|
|
isa<spirv::ReferenceOfOp>(initOp) || // for spec constant
|
|
|
|
isa<spirv::AddressOfOp>(initOp)))
|
2019-06-18 11:15:55 -07:00
|
|
|
return varOp.emitOpError("initializer must be the result of a "
|
2019-08-20 13:33:41 -07:00
|
|
|
"constant or spv.globalVariable op");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(antiagainst): generate these strings using ODS.
|
|
|
|
auto *op = varOp.getOperation();
|
|
|
|
auto descriptorSetName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::DescriptorSet));
|
|
|
|
auto bindingName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::Binding));
|
|
|
|
auto builtInName =
|
|
|
|
convertToSnakeCase(stringifyDecoration(spirv::Decoration::BuiltIn));
|
|
|
|
|
|
|
|
for (const auto &attr : {descriptorSetName, bindingName, builtInName}) {
|
|
|
|
if (op->getAttr(attr))
|
|
|
|
return varOp.emitOpError("cannot have '")
|
|
|
|
<< attr << "' attribute (only allowed in spv.globalVariable)";
|
2019-06-18 11:15:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-05-26 05:43:20 -07:00
|
|
|
namespace mlir {
|
|
|
|
namespace spirv {
|
|
|
|
|
|
|
|
#define GET_OP_CLASSES
|
2019-07-16 05:06:57 -07:00
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVOps.cpp.inc"
|
2019-05-26 05:43:20 -07:00
|
|
|
|
|
|
|
} // namespace spirv
|
|
|
|
} // namespace mlir
|