MLIR 22.0.0git
SPIRVOpDefinition.cpp
Go to the documentation of this file.
1//===- SPIRVOpDefinition.cpp - MLIR SPIR-V Op Definition Implementation ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines the TableGen'erated SPIR-V op implementation in the SPIR-V dialect.
10// These are placed in a separate file to reduce the total amount of code in
11// SPIRVOps.cpp and make that file faster to recompile.
12//
13//===----------------------------------------------------------------------===//
14
16
17#include "SPIRVParsingUtils.h"
18
20
21namespace mlir::spirv {
22/// Returns true if the given op is a function-like op or nested in a
23/// function-like op without a module-like op in the middle.
25 if (!op)
26 return false;
28 return false;
29 if (isa<FunctionOpInterface>(op))
30 return true;
32}
33
34/// Returns true if the given op is a GraphARM op or nested in a
35/// GraphARM op without a module-like op in the middle.
37 if (!op)
38 return false;
40 return false;
41 if (isa<spirv::GraphARMOp>(op))
42 return true;
44}
45
46/// Returns true if the given op is an module-like op that maintains a symbol
47/// table.
49 return op && op->hasTrait<OpTrait::SymbolTable>();
50}
51
52/// Result of a logical op must be a scalar or vector of boolean type.
53static Type getUnaryOpResultType(Type operandType) {
54 Builder builder(operandType.getContext());
55 Type resultType = builder.getIntegerType(1);
56 if (auto vecType = llvm::dyn_cast<VectorType>(operandType))
57 return VectorType::get(vecType.getNumElements(), resultType);
58 return resultType;
59}
60
61static ParseResult parseImageOperands(OpAsmParser &parser,
62 spirv::ImageOperandsAttr &attr) {
63 // Expect image operands
64 if (parser.parseOptionalLSquare())
65 return success();
66
67 spirv::ImageOperands imageOperands;
68 if (parseEnumStrAttr(imageOperands, parser))
69 return failure();
70
71 attr = spirv::ImageOperandsAttr::get(parser.getContext(), imageOperands);
72
73 return parser.parseRSquare();
74}
75
76static void printImageOperands(OpAsmPrinter &printer, Operation *imageOp,
77 spirv::ImageOperandsAttr attr) {
78 if (attr) {
79 auto strImageOperands = stringifyImageOperands(attr.getValue());
80 printer << "[\"" << strImageOperands << "\"]";
81 }
82}
83
84/// Adapted from the cf.switch implementation.
85/// <cases> ::= `default` `:` bb-id (`(` ssa-use-and-type-list `)`)?
86/// ( `,` integer `:` bb-id (`(` ssa-use-and-type-list `)`)? )*
87static ParseResult parseSwitchOpCases(
88 OpAsmParser &parser, Type &selectorType, Block *&defaultTarget,
90 SmallVectorImpl<Type> &defaultOperandTypes, DenseIntElementsAttr &literals,
93 &targetOperands,
94 SmallVectorImpl<SmallVector<Type>> &targetOperandTypes) {
95 if (parser.parseKeyword("default") || parser.parseColon() ||
96 parser.parseSuccessor(defaultTarget))
97 return failure();
98 if (succeeded(parser.parseOptionalLParen())) {
99 if (parser.parseOperandList(defaultOperands, OpAsmParser::Delimiter::None,
100 /*allowResultNumber=*/false) ||
101 parser.parseColonTypeList(defaultOperandTypes) || parser.parseRParen())
102 return failure();
103 }
104
105 SmallVector<APInt> values;
106 unsigned bitWidth = selectorType.getIntOrFloatBitWidth();
107 while (succeeded(parser.parseOptionalComma())) {
108 int64_t value = 0;
109 if (failed(parser.parseInteger(value)))
110 return failure();
111 values.push_back(APInt(bitWidth, value, /*isSigned=*/true));
112
113 Block *target;
115 SmallVector<Type> operandTypes;
116 if (failed(parser.parseColon()) || failed(parser.parseSuccessor(target)))
117 return failure();
118 if (succeeded(parser.parseOptionalLParen())) {
119 if (failed(parser.parseOperandList(operands,
121 failed(parser.parseColonTypeList(operandTypes)) ||
122 failed(parser.parseRParen()))
123 return failure();
124 }
125 targets.push_back(target);
126 targetOperands.emplace_back(operands);
127 targetOperandTypes.emplace_back(operandTypes);
128 }
129
130 if (!values.empty()) {
131 ShapedType literalType =
132 VectorType::get(static_cast<int64_t>(values.size()), selectorType);
133 literals = DenseIntElementsAttr::get(literalType, values);
134 }
135 return success();
136}
137
138static void
139printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type selectorType,
140 Block *defaultTarget, OperandRange defaultOperands,
141 TypeRange defaultOperandTypes, DenseIntElementsAttr literals,
142 SuccessorRange targets, OperandRangeRange targetOperands,
143 const TypeRangeRange &targetOperandTypes) {
144 p << " default: ";
145 p.printSuccessorAndUseList(defaultTarget, defaultOperands);
146
147 if (!literals)
148 return;
149
150 for (auto [index, literal] : llvm::enumerate(literals.getValues<APInt>())) {
151 p << ',';
152 p.printNewline();
153 p << " ";
154 p << literal.getLimitedValue();
155 p << ": ";
156 p.printSuccessorAndUseList(targets[index], targetOperands[index]);
157 }
158 p.printNewline();
159}
160
161} // namespace mlir::spirv
162
163// TablenGen'erated operation definitions.
164#define GET_OP_CLASSES
165#include "mlir/Dialect/SPIRV/IR/SPIRVOps.cpp.inc"
return success()
@ None
Zero or more operands with no delimiters.
virtual ParseResult parseColonTypeList(SmallVectorImpl< Type > &result)=0
Parse a colon followed by a type list, which must have at least one type.
MLIRContext * getContext() const
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
virtual ParseResult parseColon()=0
Parse a : token.
virtual ParseResult parseOptionalLParen()=0
Parse a ( token if present.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
Block represents an ordered list of Operations.
Definition Block.h:33
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:67
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult parseSuccessor(Block *&dest)=0
Parse a single operation successor.
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 printNewline()=0
Print a newline and indent the printer to the start of the current operation.
virtual void printSuccessorAndUseList(Block *successor, ValueRange succOperands)=0
Print the successor and its operands.
A trait used to provide symbol table functionalities to a region operation.
This class represents a contiguous range of operand ranges, e.g.
Definition ValueRange.h:84
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:43
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:749
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:234
This class implements the successor iterators for Block.
This class provides an abstraction for a range of TypeRange.
Definition TypeRange.h:95
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition Types.cpp:35
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:122
static bool isNestedInGraphARMOpInterface(Operation *op)
Returns true if the given op is a GraphARM op or nested in a GraphARM op without a module-like op in ...
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 bool isDirectInModuleLikeOp(Operation *op)
Returns true if the given op is an module-like op that maintains a symbol table.
static Type getUnaryOpResultType(Type operandType)
Result of a logical op must be a scalar or vector of boolean type.
static ParseResult parseSwitchOpCases(OpAsmParser &parser, Type &selectorType, Block *&defaultTarget, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &defaultOperands, SmallVectorImpl< Type > &defaultOperandTypes, DenseIntElementsAttr &literals, SmallVectorImpl< Block * > &targets, SmallVectorImpl< SmallVector< OpAsmParser::UnresolvedOperand > > &targetOperands, SmallVectorImpl< SmallVector< Type > > &targetOperandTypes)
Adapted from the cf.switch implementation.
static void printImageOperands(OpAsmPrinter &printer, Operation *imageOp, spirv::ImageOperandsAttr attr)
static bool isNestedInFunctionOpInterface(Operation *op)
Returns true if the given op is a function-like op or nested in a function-like op without a module-l...
static ParseResult parseImageOperands(OpAsmParser &parser, spirv::ImageOperandsAttr &attr)
static void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type selectorType, Block *defaultTarget, OperandRange defaultOperands, TypeRange defaultOperandTypes, DenseIntElementsAttr literals, SuccessorRange targets, OperandRangeRange targetOperands, const TypeRangeRange &targetOperandTypes)