21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Support/Casting.h"
34template <
typename MemoryOpTy>
43 spirv::MemoryAccess memoryAccessAttr;
44 StringAttr memoryAccessAttrName =
45 MemoryOpTy::getMemoryAccessAttrName(state.
name);
47 memoryAccessAttr, parser, state, memoryAccessAttrName))
50 if (spirv::bitEnumContainsAll(memoryAccessAttr,
51 spirv::MemoryAccess::Aligned)) {
54 StringAttr alignmentAttrName = MemoryOpTy::getAlignmentAttrName(state.
name);
69template <
typename MemoryOpTy>
78 spirv::MemoryAccess memoryAccessAttr;
79 StringRef memoryAccessAttrName =
80 MemoryOpTy::getSourceMemoryAccessAttrName(state.
name);
82 memoryAccessAttr, parser, state, memoryAccessAttrName))
85 if (spirv::bitEnumContainsAll(memoryAccessAttr,
86 spirv::MemoryAccess::Aligned)) {
89 StringAttr alignmentAttrName =
90 MemoryOpTy::getSourceAlignmentAttrName(state.
name);
105template <
typename MemoryOpTy>
109 std::optional<spirv::MemoryAccess> memoryAccessAtrrValue = std::nullopt,
110 std::optional<uint32_t> alignmentAttrValue = std::nullopt) {
115 if (
auto memAccess = (memoryAccessAtrrValue ? memoryAccessAtrrValue
116 : memoryOp.getMemoryAccess())) {
117 elidedAttrs.push_back(memoryOp.getSourceMemoryAccessAttrName());
119 printer <<
" [\"" << stringifyMemoryAccess(*memAccess) <<
"\"";
121 if (spirv::bitEnumContainsAll(*memAccess, spirv::MemoryAccess::Aligned)) {
123 if (
auto alignment = (alignmentAttrValue ? alignmentAttrValue
124 : memoryOp.getAlignment())) {
125 elidedAttrs.push_back(memoryOp.getSourceAlignmentAttrName());
126 printer <<
", " << *alignment;
134template <
typename MemoryOpTy>
138 std::optional<spirv::MemoryAccess> memoryAccessAtrrValue = std::nullopt,
139 std::optional<uint32_t> alignmentAttrValue = std::nullopt) {
141 if (
auto memAccess = (memoryAccessAtrrValue ? memoryAccessAtrrValue
142 : memoryOp.getMemoryAccess())) {
143 elidedAttrs.push_back(memoryOp.getMemoryAccessAttrName());
145 printer <<
" [\"" << stringifyMemoryAccess(*memAccess) <<
"\"";
147 if (spirv::bitEnumContainsAll(*memAccess, spirv::MemoryAccess::Aligned)) {
149 if (
auto alignment = (alignmentAttrValue ? alignmentAttrValue
150 : memoryOp.getAlignment())) {
151 elidedAttrs.push_back(memoryOp.getAlignmentAttrName());
152 printer <<
", " << *alignment;
160template <
typename LoadStoreOpTy>
169 llvm::cast<spirv::PointerType>(
ptr.getType()).getPointeeType()) {
170 return op.emitOpError(
"mismatch in result type and pointer type");
175template <
typename MemoryOpTy>
180 auto *op = memoryOp.getOperation();
181 auto memAccessAttr = op->getAttr(memoryOp.getMemoryAccessAttrName());
182 if (!memAccessAttr) {
185 if (op->getAttr(memoryOp.getAlignmentAttrName())) {
186 return memoryOp.emitOpError(
187 "invalid alignment specification without aligned memory access "
193 auto memAccess = llvm::cast<spirv::MemoryAccessAttr>(memAccessAttr);
196 return memoryOp.emitOpError(
"invalid memory access specifier: ")
200 if (spirv::bitEnumContainsAll(memAccess.getValue(),
201 spirv::MemoryAccess::Aligned)) {
202 if (!op->getAttr(memoryOp.getAlignmentAttrName())) {
203 return memoryOp.emitOpError(
"missing alignment value");
206 if (op->getAttr(memoryOp.getAlignmentAttrName())) {
207 return memoryOp.emitOpError(
208 "invalid alignment specification with non-aligned memory access "
219template <
typename MemoryOpTy>
224 auto *op = memoryOp.getOperation();
225 auto memAccessAttr = op->getAttr(memoryOp.getSourceMemoryAccessAttrName());
226 if (!memAccessAttr) {
229 if (op->getAttr(memoryOp.getSourceAlignmentAttrName())) {
230 return memoryOp.emitOpError(
231 "invalid alignment specification without aligned memory access "
237 auto memAccess = llvm::cast<spirv::MemoryAccessAttr>(memAccessAttr);
240 return memoryOp.emitOpError(
"invalid memory access specifier: ")
244 if (spirv::bitEnumContainsAll(memAccess.getValue(),
245 spirv::MemoryAccess::Aligned)) {
246 if (!op->getAttr(memoryOp.getSourceAlignmentAttrName())) {
247 return memoryOp.emitOpError(
"missing alignment value");
250 if (op->getAttr(memoryOp.getSourceAlignmentAttrName())) {
251 return memoryOp.emitOpError(
252 "invalid alignment specification with non-aligned memory access "
264 auto ptrType = llvm::dyn_cast<spirv::PointerType>(type);
266 emitError(baseLoc,
"'spirv.AccessChain' op expected a pointer "
267 "to composite type, but provided ")
272 auto resultType = ptrType.getPointeeType();
273 auto resultStorageClass = ptrType.getStorageClass();
276 for (
auto indexSSA :
indices) {
277 auto cType = llvm::dyn_cast<spirv::CompositeType>(resultType);
281 "'spirv.AccessChain' op cannot extract from non-composite type ")
282 << resultType <<
" with index " <<
index;
286 if (llvm::isa<spirv::StructType>(resultType)) {
287 Operation *op = indexSSA.getDefiningOp();
289 emitError(baseLoc,
"'spirv.AccessChain' op index must be an "
290 "integer spirv.Constant to access "
291 "element of spirv.struct");
300 "'spirv.AccessChain' index must be an integer spirv.Constant to "
301 "access element of spirv.struct, but provided ")
305 if (
index < 0 ||
static_cast<uint64_t
>(
index) >= cType.getNumElements()) {
306 emitError(baseLoc,
"'spirv.AccessChain' op index ")
307 <<
index <<
" out of bounds for " << resultType;
311 resultType = cType.getElementType(
index);
319 assert(type &&
"Unable to deduce return type based on basePtr and indices");
320 build(builder, state, type, basePtr,
indices);
323template <
typename Op>
325 printer <<
' ' << op.getBasePtr() <<
'[' <<
indices
326 <<
"] : " << op.getBasePtr().getType() <<
", " <<
indices.getTypes();
329template <
typename Op>
336 auto providedResultType =
337 llvm::dyn_cast<spirv::PointerType>(accessChainOp.getType());
338 if (!providedResultType)
340 "result type must be a pointer, but provided")
341 << providedResultType;
343 if (resultType != providedResultType)
344 return accessChainOp.
emitOpError(
"invalid result type: expected ")
345 << resultType <<
", but provided " << providedResultType;
350LogicalResult AccessChainOp::verify() {
359 MemoryAccessAttr memoryAccess, IntegerAttr alignment) {
360 auto ptrType = llvm::cast<spirv::PointerType>(basePtr.
getType());
361 build(builder, state, ptrType.getPointeeType(), basePtr, memoryAccess,
365ParseResult LoadOp::parse(OpAsmParser &parser, OperationState &
result) {
367 spirv::StorageClass storageClass;
368 OpAsmParser::UnresolvedOperand ptrInfo;
382 result.addTypes(elementType);
386void LoadOp::print(OpAsmPrinter &printer) {
387 SmallVector<StringRef, 4> elidedAttrs;
388 StringRef sc = stringifyStorageClass(
389 llvm::cast<spirv::PointerType>(getPtr().
getType()).getStorageClass());
390 printer <<
" \"" << sc <<
"\" " << getPtr();
398LogicalResult LoadOp::verify() {
412ParseResult StoreOp::parse(OpAsmParser &parser, OperationState &
result) {
414 spirv::StorageClass storageClass;
415 SmallVector<OpAsmParser::UnresolvedOperand, 2> operandInfo;
433void StoreOp::print(OpAsmPrinter &printer) {
434 SmallVector<StringRef, 4> elidedAttrs;
435 StringRef sc = stringifyStorageClass(
436 llvm::cast<spirv::PointerType>(getPtr().
getType()).getStorageClass());
437 printer <<
" \"" << sc <<
"\" " << getPtr() <<
", " << getValue();
441 printer <<
" : " << getValue().getType();
445LogicalResult StoreOp::verify() {
457void CopyMemoryOp::print(OpAsmPrinter &printer) {
460 StringRef targetStorageClass = stringifyStorageClass(
461 llvm::cast<spirv::PointerType>(getTarget().
getType()).getStorageClass());
462 printer <<
" \"" << targetStorageClass <<
"\" " << getTarget() <<
", ";
464 StringRef sourceStorageClass = stringifyStorageClass(
465 llvm::cast<spirv::PointerType>(getSource().
getType()).getStorageClass());
466 printer <<
" \"" << sourceStorageClass <<
"\" " << getSource();
468 SmallVector<StringRef, 4> elidedAttrs;
471 getSourceMemoryAccess(),
472 getSourceAlignment());
477 llvm::cast<spirv::PointerType>(getTarget().
getType()).getPointeeType();
478 printer <<
" : " << pointeeType;
481ParseResult CopyMemoryOp::parse(OpAsmParser &parser, OperationState &
result) {
482 spirv::StorageClass targetStorageClass;
483 OpAsmParser::UnresolvedOperand targetPtrInfo;
485 spirv::StorageClass sourceStorageClass;
486 OpAsmParser::UnresolvedOperand sourcePtrInfo;
522LogicalResult CopyMemoryOp::verify() {
524 llvm::cast<spirv::PointerType>(getTarget().
getType()).getPointeeType();
527 llvm::cast<spirv::PointerType>(getSource().
getType()).getPointeeType();
529 if (targetType != sourceType)
530 return emitOpError(
"both operands must be pointers to the same type");
550void InBoundsPtrAccessChainOp::build(OpBuilder &builder, OperationState &state,
551 Value basePtr, Value element,
554 assert(type &&
"Unable to deduce return type based on basePtr and indices");
555 build(builder, state, type, basePtr, element,
indices);
558LogicalResult InBoundsPtrAccessChainOp::verify() {
566void PtrAccessChainOp::build(OpBuilder &builder, OperationState &state,
569 assert(type &&
"Unable to deduce return type based on basePtr and indices");
570 build(builder, state, type, basePtr, element,
indices);
573LogicalResult PtrAccessChainOp::verify() {
581ParseResult VariableOp::parse(OpAsmParser &parser, OperationState &
result) {
583 std::optional<OpAsmParser::UnresolvedOperand> initInfo;
585 initInfo = OpAsmParser::UnresolvedOperand();
603 auto ptrType = llvm::dyn_cast<spirv::PointerType>(type);
605 return parser.
emitError(loc,
"expected spirv.ptr type");
616 ptrType.getStorageClass());
622void VariableOp::print(OpAsmPrinter &printer) {
623 SmallVector<StringRef, 4> elidedAttrs{
626 if (getNumOperands() != 0)
627 printer <<
" init(" << getInitializer() <<
")";
633LogicalResult VariableOp::verify() {
637 if (getStorageClass() != spirv::StorageClass::Function) {
639 "can only be used to model function-level variables. Use "
640 "spirv.GlobalVariable for module-level variables.");
643 auto pointerType = llvm::cast<spirv::PointerType>(getPointer().
getType());
644 if (getStorageClass() != pointerType.getStorageClass())
646 "storage class must match result pointer's storage class");
648 if (getNumOperands() != 0) {
651 auto *initOp = getOperand(0).getDefiningOp();
652 if (!initOp || !isa<spirv::ConstantOp,
653 spirv::ReferenceOfOp,
654 spirv::AddressOfOp>(initOp))
655 return emitOpError(
"initializer must be the result of a "
656 "constant or spirv.GlobalVariable op");
659 auto getDecorationAttr = [op = getOperation()](spirv::Decoration decoration) {
661 llvm::convertToSnakeFromCamelCase(stringifyDecoration(decoration)));
665 for (
auto decoration :
666 {spirv::Decoration::DescriptorSet, spirv::Decoration::Binding,
667 spirv::Decoration::BuiltIn}) {
668 if (
auto attr = getDecorationAttr(decoration))
670 << llvm::convertToSnakeFromCamelCase(
671 stringifyDecoration(decoration))
672 <<
"' attribute (only allowed in spirv.GlobalVariable)";
679 auto pointeePtrType = dyn_cast<spirv::PointerType>(getPointeeType());
680 if (!pointeePtrType) {
681 if (
auto pointeeArrayType = dyn_cast<spirv::ArrayType>(getPointeeType())) {
683 dyn_cast<spirv::PointerType>(pointeeArrayType.getElementType());
687 if (pointeePtrType && pointeePtrType.getStorageClass() ==
688 spirv::StorageClass::PhysicalStorageBuffer) {
690 getDecorationAttr(spirv::Decoration::AliasedPointer) !=
nullptr;
691 bool hasRestrictPtr =
692 getDecorationAttr(spirv::Decoration::RestrictPointer) !=
nullptr;
694 if (!hasAliasedPtr && !hasRestrictPtr)
695 return emitOpError() <<
" with physical buffer pointer must be decorated "
696 "either 'AliasedPointer' or 'RestrictPointer'";
698 if (hasAliasedPtr && hasRestrictPtr)
700 <<
" with physical buffer pointer must have exactly one "
701 "aliasing decoration";
p<< " : "<< getMemRefType()<< ", "<< getType();}static LogicalResult verifyVectorMemoryOp(Operation *op, MemRefType memrefType, VectorType vectorType) { if(memrefType.getElementType() !=vectorType.getElementType()) return op-> emitOpError("requires memref and vector types of the same elemental type")
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
virtual ParseResult parseColon()=0
Parse a : token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
Attributes are known-constant values of operations.
IntegerType getIntegerType(unsigned width)
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
This class helps build Operations.
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Location getLoc()
The source location the operation was defined or derived from.
This provides public APIs that all operations should have.
Operation is the basic unit of execution within MLIR.
OperationName getName()
The name of an operation is the key identifier for it.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
static PointerType get(Type pointeeType, StorageClass storageClass)
Operation::operand_range getIndices(Operation *op)
Get the indices that the given load/store operation is operating on.
static ParseResult parseSourceMemoryAccessAttributes(OpAsmParser &parser, OperationState &state)
ParseResult parseEnumStrAttr(EnumClass &value, OpAsmParser &parser, StringRef attrName=spirv::attributeName< EnumClass >())
Parses the next string attribute in parser as an enumerant of the given EnumClass.
static LogicalResult verifySourceMemoryAccessAttribute(MemoryOpTy memoryOp)
static void printSourceMemoryAccessAttribute(MemoryOpTy memoryOp, OpAsmPrinter &printer, SmallVectorImpl< StringRef > &elidedAttrs, std::optional< spirv::MemoryAccess > memoryAccessAtrrValue=std::nullopt, std::optional< uint32_t > alignmentAttrValue=std::nullopt)
ParseResult parseMemoryAccessAttributes(OpAsmParser &parser, OperationState &state)
Parses optional memory access (a.k.a.
static Type getElementPtrType(Type type, ValueRange indices, Location baseLoc)
void printVariableDecorations(Operation *op, OpAsmPrinter &printer, SmallVectorImpl< StringRef > &elidedAttrs)
static LogicalResult verifyLoadStorePtrAndValTypes(LoadStoreOpTy op, Value ptr, Value val)
static LogicalResult verifyMemoryAccessAttribute(MemoryOpTy memoryOp)
constexpr StringRef attributeName()
static void printAccessChain(Op op, ValueRange indices, OpAsmPrinter &printer)
static void printMemoryAccessAttribute(MemoryOpTy memoryOp, OpAsmPrinter &printer, SmallVectorImpl< StringRef > &elidedAttrs, std::optional< spirv::MemoryAccess > memoryAccessAtrrValue=std::nullopt, std::optional< uint32_t > alignmentAttrValue=std::nullopt)
LogicalResult extractValueFromConstOp(Operation *op, int32_t &value)
ParseResult parseVariableDecorations(OpAsmParser &parser, OperationState &state)
static LogicalResult verifyAccessChain(Op accessChainOp, ValueRange indices)
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This represents an operation in an abstracted form, suitable for use with the builder APIs.