MLIR  21.0.0git
BytecodeWriter.h
Go to the documentation of this file.
1 //===- BytecodeWriter.h - MLIR Bytecode Writer ------------------*- 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 write MLIR bytecode files/streams.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_BYTECODE_BYTECODEWRITER_H
14 #define MLIR_BYTECODE_BYTECODEWRITER_H
15 
16 #include "mlir/IR/AsmState.h"
17 #include "llvm/Config/llvm-config.h" // for LLVM_VERSION_STRING
18 
19 namespace mlir {
20 class DialectBytecodeWriter;
21 class DialectVersion;
22 class Operation;
23 
24 /// A class to interact with the attributes and types printer when emitting MLIR
25 /// bytecode.
26 template <class T>
28 public:
30  virtual ~AttrTypeBytecodeWriter() = default;
31 
32  /// Callback writer API used in IRNumbering, where groups are created and
33  /// type/attribute components are numbered. At this stage, writer is expected
34  /// to be a `NumberingDialectWriter`.
35  virtual LogicalResult write(T entry, std::optional<StringRef> &name,
36  DialectBytecodeWriter &writer) = 0;
37 
38  /// Callback writer API used in BytecodeWriter, where groups are created and
39  /// type/attribute components are numbered. Here, DialectBytecodeWriter is
40  /// expected to be an actual writer. The optional stringref specified by
41  /// the user is ignored, since the group was already specified when numbering
42  /// the IR.
43  LogicalResult write(T entry, DialectBytecodeWriter &writer) {
44  std::optional<StringRef> dummy;
45  return write(entry, dummy, writer);
46  }
47 
48  /// Return an Attribute/Type printer implemented via the given callable, whose
49  /// form should match that of the `write` function above.
50  template <typename CallableT,
51  std::enable_if_t<std::is_convertible_v<
52  CallableT, std::function<LogicalResult(
53  T, std::optional<StringRef> &,
55  bool> = true>
56  static std::unique_ptr<AttrTypeBytecodeWriter<T>>
57  fromCallable(CallableT &&writeFn) {
58  struct Processor : public AttrTypeBytecodeWriter<T> {
59  Processor(CallableT &&writeFn)
60  : AttrTypeBytecodeWriter(), writeFn(std::move(writeFn)) {}
61  LogicalResult write(T entry, std::optional<StringRef> &name,
62  DialectBytecodeWriter &writer) override {
63  return writeFn(entry, name, writer);
64  }
65 
66  std::decay_t<CallableT> writeFn;
67  };
68  return std::make_unique<Processor>(std::forward<CallableT>(writeFn));
69  }
70 };
71 
72 /// This class contains the configuration used for the bytecode writer. It
73 /// controls various aspects of bytecode generation, and contains all of the
74 /// various bytecode writer hooks.
76 public:
77  /// `producer` is an optional string that can be used to identify the producer
78  /// of the bytecode when reading. It has no functional effect on the bytecode
79  /// serialization.
80  BytecodeWriterConfig(StringRef producer = "MLIR" LLVM_VERSION_STRING);
81  /// `map` is a fallback resource map, which when provided will attach resource
82  /// printers for the fallback resources within the map.
84  StringRef producer = "MLIR" LLVM_VERSION_STRING);
87 
88  /// An internal implementation class that contains the state of the
89  /// configuration.
90  struct Impl;
91 
92  /// Return an instance of the internal implementation.
93  const Impl &getImpl() const { return *impl; }
94 
95  /// Set the desired bytecode version to emit. This method does not validate
96  /// the desired version. The bytecode writer entry point will return failure
97  /// if it cannot emit the desired version.
98  void setDesiredBytecodeVersion(int64_t bytecodeVersion);
99 
100  /// Get the set desired bytecode version to emit.
101  int64_t getDesiredBytecodeVersion() const;
102 
103  /// A map containing the dialect versions to emit.
104  llvm::StringMap<std::unique_ptr<DialectVersion>> &
105  getDialectVersionMap() const;
106 
107  /// Set a given dialect version to emit on the map.
108  template <class T>
109  void setDialectVersion(std::unique_ptr<DialectVersion> dialectVersion) const {
110  return setDialectVersion(T::getDialectNamespace(),
111  std::move(dialectVersion));
112  }
113  void setDialectVersion(StringRef dialectName,
114  std::unique_ptr<DialectVersion> dialectVersion) const;
115 
116  //===--------------------------------------------------------------------===//
117  // Types and Attributes encoding
118  //===--------------------------------------------------------------------===//
119 
120  /// Retrieve the callbacks.
124  getTypeWriterCallbacks() const;
125 
126  /// Attach a custom bytecode printer callback to the configuration for the
127  /// emission of custom type/attributes encodings.
129  std::unique_ptr<AttrTypeBytecodeWriter<Attribute>> callback);
130  void
131  attachTypeCallback(std::unique_ptr<AttrTypeBytecodeWriter<Type>> callback);
132 
133  /// Attach a custom bytecode printer callback to the configuration for the
134  /// emission of custom type/attributes encodings.
135  template <typename CallableT>
136  std::enable_if_t<std::is_convertible_v<
137  CallableT,
138  std::function<LogicalResult(Attribute, std::optional<StringRef> &,
140  attachAttributeCallback(CallableT &&emitFn) {
142  std::forward<CallableT>(emitFn)));
143  }
144  template <typename CallableT>
145  std::enable_if_t<std::is_convertible_v<
146  CallableT, std::function<LogicalResult(Type, std::optional<StringRef> &,
148  attachTypeCallback(CallableT &&emitFn) {
150  std::forward<CallableT>(emitFn)));
151  }
152 
153  //===--------------------------------------------------------------------===//
154  // Resources
155  //===--------------------------------------------------------------------===//
156 
157  /// Set a boolean flag to skip emission of resources into the bytecode file.
158  void setElideResourceDataFlag(bool shouldElideResourceData = true);
159 
160  /// Attach the given resource printer to the writer configuration.
161  void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
162 
163  /// Attach an resource printer, in the form of a callable, to the
164  /// configuration.
165  template <typename CallableT>
166  std::enable_if_t<std::is_convertible<
167  CallableT, function_ref<void(Operation *, AsmResourceBuilder &)>>::value>
168  attachResourcePrinter(StringRef name, CallableT &&printFn) {
170  name, std::forward<CallableT>(printFn)));
171  }
172 
173  /// Attach resource printers to the AsmState for the fallback resources
174  /// in the given map.
176  for (auto &printer : map.getPrinters())
177  attachResourcePrinter(std::move(printer));
178  }
179 
180 private:
181  /// A pointer to allocated storage for the impl state.
182  std::unique_ptr<Impl> impl;
183 };
184 
185 //===----------------------------------------------------------------------===//
186 // Entry Points
187 //===----------------------------------------------------------------------===//
188 
189 /// Write the bytecode for the given operation to the provided output stream.
190 /// For streams where it matters, the given stream should be in "binary" mode.
191 /// It only ever fails if setDesiredByteCodeVersion can't be honored.
192 LogicalResult writeBytecodeToFile(Operation *op, raw_ostream &os,
193  const BytecodeWriterConfig &config = {});
194 
195 } // namespace mlir
196 
197 #endif // MLIR_BYTECODE_BYTECODEWRITER_H
This class is used to build resource entries for use by the printer.
Definition: AsmState.h:246
static std::unique_ptr< AsmResourcePrinter > fromCallable(StringRef name, CallableT &&printFn)
Return a resource printer implemented via the given callable, whose form should match that of buildRe...
Definition: AsmState.h:398
A class to interact with the attributes and types printer when emitting MLIR bytecode.
virtual ~AttrTypeBytecodeWriter()=default
LogicalResult write(T entry, DialectBytecodeWriter &writer)
Callback writer API used in BytecodeWriter, where groups are created and type/attribute components ar...
static std::unique_ptr< AttrTypeBytecodeWriter< T > > fromCallable(CallableT &&writeFn)
Return an Attribute/Type printer implemented via the given callable, whose form should match that of ...
virtual LogicalResult write(T entry, std::optional< StringRef > &name, DialectBytecodeWriter &writer)=0
Callback writer API used in IRNumbering, where groups are created and type/attribute components are n...
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class contains the configuration used for the bytecode writer.
void attachTypeCallback(std::unique_ptr< AttrTypeBytecodeWriter< Type >> callback)
std::enable_if_t< std::is_convertible_v< CallableT, std::function< LogicalResult(Attribute, std::optional< StringRef > &, DialectBytecodeWriter &)> > > attachAttributeCallback(CallableT &&emitFn)
Attach a custom bytecode printer callback to the configuration for the emission of custom type/attrib...
llvm::StringMap< std::unique_ptr< DialectVersion > > & getDialectVersionMap() const
A map containing the dialect versions to emit.
void setDialectVersion(StringRef dialectName, std::unique_ptr< DialectVersion > dialectVersion) const
void setElideResourceDataFlag(bool shouldElideResourceData=true)
Set a boolean flag to skip emission of resources into the bytecode file.
BytecodeWriterConfig(StringRef producer="MLIR" LLVM_VERSION_STRING)
producer is an optional string that can be used to identify the producer of the bytecode when reading...
void attachFallbackResourcePrinter(FallbackAsmResourceMap &map)
Attach resource printers to the AsmState for the fallback resources in the given map.
int64_t getDesiredBytecodeVersion() const
Get the set desired bytecode version to emit.
void setDialectVersion(std::unique_ptr< DialectVersion > dialectVersion) const
Set a given dialect version to emit on the map.
std::enable_if_t< std::is_convertible< CallableT, function_ref< void(Operation *, AsmResourceBuilder &)> >::value > attachResourcePrinter(StringRef name, CallableT &&printFn)
Attach an resource printer, in the form of a callable, to the configuration.
ArrayRef< std::unique_ptr< AttrTypeBytecodeWriter< Type > > > getTypeWriterCallbacks() const
ArrayRef< std::unique_ptr< AttrTypeBytecodeWriter< Attribute > > > getAttributeWriterCallbacks() const
Retrieve the callbacks.
void setDesiredBytecodeVersion(int64_t bytecodeVersion)
Set the desired bytecode version to emit.
const Impl & getImpl() const
Return an instance of the internal implementation.
std::enable_if_t< std::is_convertible_v< CallableT, std::function< LogicalResult(Type, std::optional< StringRef > &, DialectBytecodeWriter &)> > > attachTypeCallback(CallableT &&emitFn)
void attachResourcePrinter(std::unique_ptr< AsmResourcePrinter > printer)
Attach the given resource printer to the writer configuration.
void attachAttributeCallback(std::unique_ptr< AttrTypeBytecodeWriter< Attribute >> callback)
Attach a custom bytecode printer callback to the configuration for the emission of custom type/attrib...
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
A fallback map containing external resources not explicitly handled by another parser/printer.
Definition: AsmState.h:419
std::vector< std::unique_ptr< AsmResourcePrinter > > getPrinters()
Build a set of resource printers to print the resources within this map.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig config
LogicalResult writeBytecodeToFile(Operation *op, raw_ostream &os, const BytecodeWriterConfig &config={})
Write the bytecode for the given operation to the provided output stream.