MLIR  18.0.0git
SPIRVParsingUtils.cpp
Go to the documentation of this file.
1 //===- SPIRVParsingUtilities.cpp - MLIR SPIR-V Dialect Parsing Utils-------===//
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 // Implements common SPIR-V dialect parsing functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "SPIRVParsingUtils.h"
14 
15 #include "llvm/ADT/StringExtras.h"
16 
17 using namespace mlir::spirv::AttrNames;
18 
19 namespace mlir::spirv {
20 
22  OperationState &state,
23  StringRef attrName) {
24  // Parse an optional list of attributes staring with '['
25  if (parser.parseOptionalLSquare()) {
26  // Nothing to do
27  return success();
28  }
29 
30  spirv::MemoryAccess memoryAccessAttr;
31  if (spirv::parseEnumStrAttr<spirv::MemoryAccessAttr>(memoryAccessAttr, parser,
32  state, attrName))
33  return failure();
34 
35  if (spirv::bitEnumContainsAll(memoryAccessAttr,
36  spirv::MemoryAccess::Aligned)) {
37  // Parse integer attribute for alignment.
38  Attribute alignmentAttr;
39  Type i32Type = parser.getBuilder().getIntegerType(32);
40  if (parser.parseComma() ||
41  parser.parseAttribute(alignmentAttr, i32Type,
43  state.attributes)) {
44  return failure();
45  }
46  }
47  return parser.parseRSquare();
48 }
49 
51  OperationState &state) {
52  auto builtInName = llvm::convertToSnakeFromCamelCase(
53  stringifyDecoration(spirv::Decoration::BuiltIn));
54  if (succeeded(parser.parseOptionalKeyword("bind"))) {
55  Attribute set, binding;
56  // Parse optional descriptor binding
57  auto descriptorSetName = llvm::convertToSnakeFromCamelCase(
58  stringifyDecoration(spirv::Decoration::DescriptorSet));
59  auto bindingName = llvm::convertToSnakeFromCamelCase(
60  stringifyDecoration(spirv::Decoration::Binding));
61  Type i32Type = parser.getBuilder().getIntegerType(32);
62  if (parser.parseLParen() ||
63  parser.parseAttribute(set, i32Type, descriptorSetName,
64  state.attributes) ||
65  parser.parseComma() ||
66  parser.parseAttribute(binding, i32Type, bindingName,
67  state.attributes) ||
68  parser.parseRParen()) {
69  return failure();
70  }
71  } else if (succeeded(parser.parseOptionalKeyword(builtInName))) {
72  StringAttr builtIn;
73  if (parser.parseLParen() ||
74  parser.parseAttribute(builtIn, builtInName, state.attributes) ||
75  parser.parseRParen()) {
76  return failure();
77  }
78  }
79 
80  // Parse other attributes
81  if (parser.parseOptionalAttrDict(state.attributes))
82  return failure();
83 
84  return success();
85 }
86 
87 } // namespace mlir::spirv
static std::string bindingName()
Returns the string name of the Binding decoration.
static std::string descriptorSetName()
Returns the string name of the DescriptorSet decoration.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:87
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
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
constexpr char kAlignmentAttrName[]
ParseResult parseMemoryAccessAttributes(OpAsmParser &parser, OperationState &state, StringRef attrName)
Parses optional memory access (a.k.a.
ParseResult parseVariableDecorations(OpAsmParser &parser, OperationState &state)
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:68
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This represents an operation in an abstracted form, suitable for use with the builder APIs.