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  /// Set the desired bytecode version to emit. This method does not validate
44  /// the desired version. The bytecode writer entry point will return failure
45  /// if it cannot emit the desired version.
46  void setDesiredBytecodeVersion(int64_t bytecodeVersion);
47 
48  /// Get the set desired bytecode version to emit.
49  int64_t getDesiredBytecodeVersion() const;
50 
51  //===--------------------------------------------------------------------===//
52  // Resources
53  //===--------------------------------------------------------------------===//
54 
55  /// Attach the given resource printer to the writer configuration.
56  void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
57 
58  /// Attach an resource printer, in the form of a callable, to the
59  /// configuration.
60  template <typename CallableT>
61  std::enable_if_t<std::is_convertible<
62  CallableT, function_ref<void(Operation *, AsmResourceBuilder &)>>::value>
63  attachResourcePrinter(StringRef name, CallableT &&printFn) {
65  name, std::forward<CallableT>(printFn)));
66  }
67 
68  /// Attach resource printers to the AsmState for the fallback resources
69  /// in the given map.
71  for (auto &printer : map.getPrinters())
72  attachResourcePrinter(std::move(printer));
73  }
74 
75 private:
76  /// A pointer to allocated storage for the impl state.
77  std::unique_ptr<Impl> impl;
78 };
79 
80 //===----------------------------------------------------------------------===//
81 // Entry Points
82 //===----------------------------------------------------------------------===//
83 
84 /// Write the bytecode for the given operation to the provided output stream.
85 /// For streams where it matters, the given stream should be in "binary" mode.
86 /// It only ever fails if setDesiredByteCodeVersion can't be honored.
87 LogicalResult writeBytecodeToFile(Operation *op, raw_ostream &os,
88  const BytecodeWriterConfig &config = {});
89 
90 } // namespace mlir
91 
92 #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.
int64_t getDesiredBytecodeVersion() const
Get the set desired bytecode version to emit.
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.
void setDesiredBytecodeVersion(int64_t bytecodeVersion)
Set the desired bytecode version to emit.
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:88
This header declares functions that assit transformations in the MemRef dialect.
LogicalResult writeBytecodeToFile(Operation *op, raw_ostream &os, const BytecodeWriterConfig &config={})
Write the bytecode for the given operation to the provided output stream.