MLIR  18.0.0git
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 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 //===----------------------------------------------------------------------===//
17 #include "SPIRVParsingUtils.h"
19 #include "mlir/IR/TypeUtilities.h"
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.
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 }
34 /// Returns true if the given op is an module-like op that maintains a symbol
35 /// table.
37  return op && op->hasTrait<OpTrait::SymbolTable>();
38 }
40 /// Result of a logical op must be a scalar or vector of boolean type.
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 }
50  spirv::ImageOperandsAttr &attr) {
51  // Expect image operands
52  if (parser.parseOptionalLSquare())
53  return success();
55  spirv::ImageOperands imageOperands;
56  if (parseEnumStrAttr(imageOperands, parser))
57  return failure();
59  attr = spirv::ImageOperandsAttr::get(parser.getContext(), imageOperands);
61  return parser.parseRSquare();
62 }
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 }
72 } // namespace mlir::spirv
74 // TablenGen'erated operation definitions.
75 #define GET_OP_CLASSES
76 #include "mlir/Dialect/SPIRV/IR/"
MLIRContext * getContext() const
Definition: AsmPrinter.cpp:72
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:87
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
A trait used to provide symbol table functionalities to a region operation.
Definition: SymbolTable.h:435
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class represents success/failure for parsing-like operations that find it important to chain tog...
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
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 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)
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...