MLIR  19.0.0git
BytecodeReaderConfig.h
Go to the documentation of this file.
1 //===- BytecodeReader.h - MLIR Bytecode Reader ------------------*- C++ -*-===//
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 // This header defines interfaces to read MLIR bytecode files/streams.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_BYTECODE_BYTECODEREADERCONFIG_H
14 #define MLIR_BYTECODE_BYTECODEREADERCONFIG_H
15 
16 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 
22 namespace mlir {
23 class Attribute;
24 class DialectBytecodeReader;
25 class Type;
26 
27 /// A class to interact with the attributes and types parser when parsing MLIR
28 /// bytecode.
29 template <class T>
31 public:
33  virtual ~AttrTypeBytecodeReader() = default;
34 
36  StringRef dialectName, T &entry) = 0;
37 
38  /// Return an Attribute/Type printer implemented via the given callable, whose
39  /// form should match that of the `parse` function above.
40  template <typename CallableT,
41  std::enable_if_t<
42  std::is_convertible_v<
43  CallableT, std::function<LogicalResult(
44  DialectBytecodeReader &, StringRef, T &)>>,
45  bool> = true>
46  static std::unique_ptr<AttrTypeBytecodeReader<T>>
47  fromCallable(CallableT &&readFn) {
48  struct Processor : public AttrTypeBytecodeReader<T> {
49  Processor(CallableT &&readFn)
50  : AttrTypeBytecodeReader(), readFn(std::move(readFn)) {}
51  LogicalResult read(DialectBytecodeReader &reader, StringRef dialectName,
52  T &entry) override {
53  return readFn(reader, dialectName, entry);
54  }
55 
56  std::decay_t<CallableT> readFn;
57  };
58  return std::make_unique<Processor>(std::forward<CallableT>(readFn));
59  }
60 };
61 
62 //===----------------------------------------------------------------------===//
63 // BytecodeReaderConfig
64 //===----------------------------------------------------------------------===//
65 
66 /// A class containing bytecode-specific configurations of the `ParserConfig`.
68 public:
69  BytecodeReaderConfig() = default;
70 
71  /// Returns the callbacks available to the parser.
74  return attributeBytecodeParsers;
75  }
77  getTypeCallbacks() const {
78  return typeBytecodeParsers;
79  }
80 
81  /// Attach a custom bytecode parser callback to the configuration for parsing
82  /// of custom type/attributes encodings.
84  std::unique_ptr<AttrTypeBytecodeReader<Attribute>> parser) {
85  attributeBytecodeParsers.emplace_back(std::move(parser));
86  }
87  void
89  typeBytecodeParsers.emplace_back(std::move(parser));
90  }
91 
92  /// Attach a custom bytecode parser callback to the configuration for parsing
93  /// of custom type/attributes encodings.
94  template <typename CallableT>
95  std::enable_if_t<std::is_convertible_v<
96  CallableT, std::function<LogicalResult(DialectBytecodeReader &, StringRef,
97  Attribute &)>>>
98  attachAttributeCallback(CallableT &&parserFn) {
100  std::forward<CallableT>(parserFn)));
101  }
102  template <typename CallableT>
103  std::enable_if_t<std::is_convertible_v<
104  CallableT,
105  std::function<LogicalResult(DialectBytecodeReader &, StringRef, Type &)>>>
106  attachTypeCallback(CallableT &&parserFn) {
108  std::forward<CallableT>(parserFn)));
109  }
110 
111 private:
113  attributeBytecodeParsers;
115  typeBytecodeParsers;
116 };
117 
118 } // namespace mlir
119 
120 #endif // MLIR_BYTECODE_BYTECODEREADERCONFIG_H
A class to interact with the attributes and types parser when parsing MLIR bytecode.
virtual LogicalResult read(DialectBytecodeReader &reader, StringRef dialectName, T &entry)=0
static std::unique_ptr< AttrTypeBytecodeReader< T > > fromCallable(CallableT &&readFn)
Return an Attribute/Type printer implemented via the given callable, whose form should match that of ...
virtual ~AttrTypeBytecodeReader()=default
Attributes are known-constant values of operations.
Definition: Attributes.h:25
A class containing bytecode-specific configurations of the ParserConfig.
void attachAttributeCallback(std::unique_ptr< AttrTypeBytecodeReader< Attribute >> parser)
Attach a custom bytecode parser callback to the configuration for parsing of custom type/attributes e...
std::enable_if_t< std::is_convertible_v< CallableT, std::function< LogicalResult(DialectBytecodeReader &, StringRef, Type &)> > > attachTypeCallback(CallableT &&parserFn)
void attachTypeCallback(std::unique_ptr< AttrTypeBytecodeReader< Type >> parser)
std::enable_if_t< std::is_convertible_v< CallableT, std::function< LogicalResult(DialectBytecodeReader &, StringRef, Attribute &)> > > attachAttributeCallback(CallableT &&parserFn)
Attach a custom bytecode parser callback to the configuration for parsing of custom type/attributes e...
ArrayRef< std::unique_ptr< AttrTypeBytecodeReader< Attribute > > > getAttributeCallbacks() const
Returns the callbacks available to the parser.
ArrayRef< std::unique_ptr< AttrTypeBytecodeReader< Type > > > getTypeCallbacks() const
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
@ Type
An inlay hint that for a type annotation.
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26