MLIR  20.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 an module-like op that maintains a symbol
35 /// table.
37  return op && op->hasTrait<OpTrait::SymbolTable>();
38 }
39 
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 }
48 
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 
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"
MLIRContext * getContext() const
Definition: AsmPrinter.cpp:73
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:111
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
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:750
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
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...