MLIR  17.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 
18 namespace mlir {
19 class Operation;
20 
21 /// This class contains the configuration used for the bytecode writer. It
22 /// controls various aspects of bytecode generation, and contains all of the
23 /// various bytecode writer hooks.
25 public:
26  /// `producer` is an optional string that can be used to identify the producer
27  /// of the bytecode when reading. It has no functional effect on the bytecode
28  /// serialization.
29  BytecodeWriterConfig(StringRef producer = "MLIR" LLVM_VERSION_STRING);
30  /// `map` is a fallback resource map, which when provided will attach resource
31  /// printers for the fallback resources within the map.
33  StringRef producer = "MLIR" LLVM_VERSION_STRING);
35 
36  /// An internal implementation class that contains the state of the
37  /// configuration.
38  struct Impl;
39 
40  /// Return an instance of the internal implementation.
41  const Impl &getImpl() const { return *impl; }
42 
43  //===--------------------------------------------------------------------===//
44  // Resources
45  //===--------------------------------------------------------------------===//
46 
47  /// Attach the given resource printer to the writer configuration.
48  void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
49 
50  /// Attach an resource printer, in the form of a callable, to the
51  /// configuration.
52  template <typename CallableT>
53  std::enable_if_t<std::is_convertible<
54  CallableT, function_ref<void(Operation *, AsmResourceBuilder &)>>::value>
55  attachResourcePrinter(StringRef name, CallableT &&printFn) {
57  name, std::forward<CallableT>(printFn)));
58  }
59 
60  /// Attach resource printers to the AsmState for the fallback resources
61  /// in the given map.
63  for (auto &printer : map.getPrinters())
64  attachResourcePrinter(std::move(printer));
65  }
66 
67 private:
68  /// A pointer to allocated storage for the impl state.
69  std::unique_ptr<Impl> impl;
70 };
71 
72 //===----------------------------------------------------------------------===//
73 // Entry Points
74 //===----------------------------------------------------------------------===//
75 
76 /// Write the bytecode for the given operation to the provided output stream.
77 /// For streams where it matters, the given stream should be in "binary" mode.
78 void writeBytecodeToFile(Operation *op, raw_ostream &os,
79  const BytecodeWriterConfig &config = {});
80 
81 } // namespace mlir
82 
83 #endif // MLIR_BYTECODE_BYTECODEWRITER_H
This class is used to build resource entries for use by the printer.
Definition: AsmState.h:237
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:389
This class contains the configuration used for the bytecode writer.
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.
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.
const Impl & getImpl() const
Return an instance of the internal implementation.
void attachResourcePrinter(std::unique_ptr< AsmResourcePrinter > printer)
Attach the given resource printer to the writer configuration.
A fallback map containing external resources not explicitly handled by another parser/printer.
Definition: AsmState.h:410
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:75
Include the generated interface declarations.
void writeBytecodeToFile(Operation *op, raw_ostream &os, const BytecodeWriterConfig &config={})
Write the bytecode for the given operation to the provided output stream.