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
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.
24static bool isNestedInFunctionOpInterface(Operation *op) {
25 if (!op)
26 return false;
27 if (op->hasTrait<OpTrait::SymbolTable>())
28 return false;
29 if (isa<FunctionOpInterface>(Val: op))
30 return true;
31 return isNestedInFunctionOpInterface(op: op->getParentOp());
32}
33
34/// Returns true if the given op is an module-like op that maintains a symbol
35/// table.
36static 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.
41static 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
49static 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
64static 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

source code of mlir/lib/Dialect/SPIRV/IR/SPIRVOpDefinition.cpp