xref: /llvm-project/mlir/lib/Dialect/SPIRV/IR/SPIRVOpDefinition.cpp (revision 81b4e7d2b0e1d222c76637f600cfcb74b631dfca)
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 
15 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
16 
17 #include "SPIRVParsingUtils.h"
18 
19 #include "mlir/IR/TypeUtilities.h"
20 
21 namespace 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.
isNestedInFunctionOpInterface(Operation * op)24 static bool isNestedInFunctionOpInterface(Operation *op) {
25   if (!op)
26     return false;
27   if (op->hasTrait<OpTrait::SymbolTable>())
28     return false;
29   if (isa<FunctionOpInterface>(op))
30     return true;
31   return isNestedInFunctionOpInterface(op->getParentOp());
32 }
33 
34 /// Returns true if the given op is an module-like op that maintains a symbol
35 /// table.
isDirectInModuleLikeOp(Operation * op)36 static bool isDirectInModuleLikeOp(Operation *op) {
37   return op && op->hasTrait<OpTrait::SymbolTable>();
38 }
39 
40 /// Result of a logical op must be a scalar or vector of boolean type.
getUnaryOpResultType(Type operandType)41 static Type getUnaryOpResultType(Type operandType) {
42   Builder builder(operandType.getContext());
43   Type resultType = builder.getIntegerType(1);
44   if (auto vecType = llvm::dyn_cast<VectorType>(operandType))
45     return VectorType::get(vecType.getNumElements(), resultType);
46   return resultType;
47 }
48 
parseImageOperands(OpAsmParser & parser,spirv::ImageOperandsAttr & attr)49 static ParseResult parseImageOperands(OpAsmParser &parser,
50                                       spirv::ImageOperandsAttr &attr) {
51   // Expect image operands
52   if (parser.parseOptionalLSquare())
53     return success();
54 
55   spirv::ImageOperands imageOperands;
56   if (parseEnumStrAttr(imageOperands, parser))
57     return failure();
58 
59   attr = spirv::ImageOperandsAttr::get(parser.getContext(), imageOperands);
60 
61   return parser.parseRSquare();
62 }
63 
printImageOperands(OpAsmPrinter & printer,Operation * imageOp,spirv::ImageOperandsAttr attr)64 static void printImageOperands(OpAsmPrinter &printer, Operation *imageOp,
65                                spirv::ImageOperandsAttr attr) {
66   if (attr) {
67     auto strImageOperands = stringifyImageOperands(attr.getValue());
68     printer << "[\"" << strImageOperands << "\"]";
69   }
70 }
71 
72 } // namespace mlir::spirv
73 
74 // TablenGen'erated operation definitions.
75 #define GET_OP_CLASSES
76 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.cpp.inc"
77