xref: /llvm-project/mlir/lib/Dialect/SPIRV/IR/SPIRVOpDefinition.cpp (revision 81b4e7d2b0e1d222c76637f600cfcb74b631dfca)
1*81b4e7d2Svarconst //===- SPIRVOpDefinition.cpp - MLIR SPIR-V Op Definition Implementation ---===//
2*81b4e7d2Svarconst //
3*81b4e7d2Svarconst // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*81b4e7d2Svarconst // See https://llvm.org/LICENSE.txt for license information.
5*81b4e7d2Svarconst // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*81b4e7d2Svarconst //
7*81b4e7d2Svarconst //===----------------------------------------------------------------------===//
8*81b4e7d2Svarconst //
9*81b4e7d2Svarconst // Defines the TableGen'erated SPIR-V op implementation in the SPIR-V dialect.
10*81b4e7d2Svarconst // These are placed in a separate file to reduce the total amount of code in
11*81b4e7d2Svarconst // SPIRVOps.cpp and make that file faster to recompile.
12*81b4e7d2Svarconst //
13*81b4e7d2Svarconst //===----------------------------------------------------------------------===//
14*81b4e7d2Svarconst 
15*81b4e7d2Svarconst #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
16*81b4e7d2Svarconst 
17*81b4e7d2Svarconst #include "SPIRVParsingUtils.h"
18*81b4e7d2Svarconst 
19*81b4e7d2Svarconst #include "mlir/IR/TypeUtilities.h"
20*81b4e7d2Svarconst 
21*81b4e7d2Svarconst namespace mlir::spirv {
22*81b4e7d2Svarconst /// Returns true if the given op is a function-like op or nested in a
23*81b4e7d2Svarconst /// function-like op without a module-like op in the middle.
isNestedInFunctionOpInterface(Operation * op)24*81b4e7d2Svarconst static bool isNestedInFunctionOpInterface(Operation *op) {
25*81b4e7d2Svarconst   if (!op)
26*81b4e7d2Svarconst     return false;
27*81b4e7d2Svarconst   if (op->hasTrait<OpTrait::SymbolTable>())
28*81b4e7d2Svarconst     return false;
29*81b4e7d2Svarconst   if (isa<FunctionOpInterface>(op))
30*81b4e7d2Svarconst     return true;
31*81b4e7d2Svarconst   return isNestedInFunctionOpInterface(op->getParentOp());
32*81b4e7d2Svarconst }
33*81b4e7d2Svarconst 
34*81b4e7d2Svarconst /// Returns true if the given op is an module-like op that maintains a symbol
35*81b4e7d2Svarconst /// table.
isDirectInModuleLikeOp(Operation * op)36*81b4e7d2Svarconst static bool isDirectInModuleLikeOp(Operation *op) {
37*81b4e7d2Svarconst   return op && op->hasTrait<OpTrait::SymbolTable>();
38*81b4e7d2Svarconst }
39*81b4e7d2Svarconst 
40*81b4e7d2Svarconst /// Result of a logical op must be a scalar or vector of boolean type.
getUnaryOpResultType(Type operandType)41*81b4e7d2Svarconst static Type getUnaryOpResultType(Type operandType) {
42*81b4e7d2Svarconst   Builder builder(operandType.getContext());
43*81b4e7d2Svarconst   Type resultType = builder.getIntegerType(1);
44*81b4e7d2Svarconst   if (auto vecType = llvm::dyn_cast<VectorType>(operandType))
45*81b4e7d2Svarconst     return VectorType::get(vecType.getNumElements(), resultType);
46*81b4e7d2Svarconst   return resultType;
47*81b4e7d2Svarconst }
48*81b4e7d2Svarconst 
parseImageOperands(OpAsmParser & parser,spirv::ImageOperandsAttr & attr)49*81b4e7d2Svarconst static ParseResult parseImageOperands(OpAsmParser &parser,
50*81b4e7d2Svarconst                                       spirv::ImageOperandsAttr &attr) {
51*81b4e7d2Svarconst   // Expect image operands
52*81b4e7d2Svarconst   if (parser.parseOptionalLSquare())
53*81b4e7d2Svarconst     return success();
54*81b4e7d2Svarconst 
55*81b4e7d2Svarconst   spirv::ImageOperands imageOperands;
56*81b4e7d2Svarconst   if (parseEnumStrAttr(imageOperands, parser))
57*81b4e7d2Svarconst     return failure();
58*81b4e7d2Svarconst 
59*81b4e7d2Svarconst   attr = spirv::ImageOperandsAttr::get(parser.getContext(), imageOperands);
60*81b4e7d2Svarconst 
61*81b4e7d2Svarconst   return parser.parseRSquare();
62*81b4e7d2Svarconst }
63*81b4e7d2Svarconst 
printImageOperands(OpAsmPrinter & printer,Operation * imageOp,spirv::ImageOperandsAttr attr)64*81b4e7d2Svarconst static void printImageOperands(OpAsmPrinter &printer, Operation *imageOp,
65*81b4e7d2Svarconst                                spirv::ImageOperandsAttr attr) {
66*81b4e7d2Svarconst   if (attr) {
67*81b4e7d2Svarconst     auto strImageOperands = stringifyImageOperands(attr.getValue());
68*81b4e7d2Svarconst     printer << "[\"" << strImageOperands << "\"]";
69*81b4e7d2Svarconst   }
70*81b4e7d2Svarconst }
71*81b4e7d2Svarconst 
72*81b4e7d2Svarconst } // namespace mlir::spirv
73*81b4e7d2Svarconst 
74*81b4e7d2Svarconst // TablenGen'erated operation definitions.
75*81b4e7d2Svarconst #define GET_OP_CLASSES
76*81b4e7d2Svarconst #include "mlir/Dialect/SPIRV/IR/SPIRVOps.cpp.inc"
77