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 
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.
25  if (!op)
26  return false;
27  if (op->hasTrait<OpTrait::SymbolTable>())
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;
39  if (op->hasTrait<OpTrait::SymbolTable>())
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.
53 static 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 
61 static 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 
76 static 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 } // namespace mlir::spirv
85 
86 // TablenGen'erated operation definitions.
87 #define GET_OP_CLASSES
88 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.cpp.inc"
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:51
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:67
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:452
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
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
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 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)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...