MLIR  17.0.0git
AsmState.h
Go to the documentation of this file.
1 //===- AsmState.h - Assembly State Utilities --------------------*- 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 file defines various classes and utilites for interacting with the MLIR
10 // assembly formats.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_IR_ASMSTATE_H_
15 #define MLIR_IR_ASMSTATE_H_
16 
18 #include "mlir/Support/LLVM.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/StringMap.h"
21 
22 #include <memory>
23 
24 namespace mlir {
25 class AsmResourcePrinter;
26 class AsmDialectResourceHandle;
27 class Operation;
28 
29 namespace detail {
30 class AsmStateImpl;
31 } // namespace detail
32 
33 //===----------------------------------------------------------------------===//
34 // Resources
35 //===----------------------------------------------------------------------===//
36 
37 /// The following classes enable support for parsing and printing resources
38 /// within MLIR assembly formats. Resources are a mechanism by which dialects,
39 /// and external clients, may attach additional information when parsing or
40 /// printing IR without that information being encoded in the IR itself.
41 /// Resources are not uniqued within the MLIR context, are not attached directly
42 /// to any operation, and are solely intended to live and be processed outside
43 /// of the immediate IR.
44 ///
45 /// Resources are encoded using a key-value pair nested within dictionaries
46 /// anchored either on a dialect, or an externally registered entity.
47 /// Dictionaries anchored on dialects use the dialect namespace directly, and
48 /// dictionaries anchored on external entities use a provided unique identifier.
49 /// The resource key is an identifier used to disambiguate the data. The
50 /// resource value may be stored in various limited forms, but general encodings
51 /// use a string (human readable) or blob format (binary). Within the textual
52 /// format, an example may be of the form:
53 ///
54 /// {-#
55 /// // The `dialect_resources` section within the file-level metadata
56 /// // dictionary is used to contain any dialect resource entries.
57 /// dialect_resources: {
58 /// // Here is a dictionary anchored on "foo_dialect", which is a dialect
59 /// // namespace.
60 /// foo_dialect: {
61 /// // `some_dialect_resource` is a key to be interpreted by the dialect,
62 /// // and used to initialize/configure/etc.
63 /// some_dialect_resource: "Some important resource value"
64 /// }
65 /// },
66 /// // The `external_resources` section within the file-level metadata
67 /// // dictionary is used to contain any non-dialect resource entries.
68 /// external_resources: {
69 /// // Here is a dictionary anchored on "mlir_reproducer", which is an
70 /// // external entity representing MLIR's crash reproducer functionality.
71 /// mlir_reproducer: {
72 /// // `pipeline` is an entry that holds a crash reproducer pipeline
73 /// // resource.
74 /// pipeline: "func.func(canonicalize,cse)"
75 /// }
76 /// }
77 /// #-}
78 ///
79 
80 //===----------------------------------------------------------------------===//
81 // Resource Entry
82 
83 /// This class represents a processed binary blob of data. A resource blob is
84 /// essentially a collection of data, potentially mutable, with an associated
85 /// deleter function (used if the data needs to be destroyed).
87 public:
88  /// A deleter function that frees a blob given the data, allocation size, and
89  /// allocation aligment.
90  using DeleterFn =
91  llvm::unique_function<void(void *data, size_t size, size_t align)>;
92 
93  //===--------------------------------------------------------------------===//
94  // Construction
95  //===--------------------------------------------------------------------===//
96 
97  AsmResourceBlob() = default;
98  AsmResourceBlob(ArrayRef<char> data, size_t dataAlignment, DeleterFn deleter,
99  bool dataIsMutable)
100  : data(data), dataAlignment(dataAlignment), deleter(std::move(deleter)),
101  dataIsMutable(dataIsMutable) {}
102  /// Utility constructor that initializes a blob with a non-char type T.
103  template <typename T, typename DelT>
104  AsmResourceBlob(ArrayRef<T> data, DelT &&deleteFn, bool dataIsMutable)
105  : data((const char *)data.data(), data.size() * sizeof(T)),
106  dataAlignment(alignof(T)),
107  deleter([deleteFn = std::forward<DelT>(deleteFn)](
108  void *data, size_t size, size_t align) {
109  return deleteFn((T *)data, size, align);
110  }),
111  dataIsMutable(dataIsMutable) {}
114  // Delete the current blob if necessary.
115  if (deleter)
116  deleter(const_cast<char *>(data.data()), data.size(), dataAlignment);
117 
118  // Take the data entries from rhs.
119  data = rhs.data;
120  dataAlignment = rhs.dataAlignment;
121  deleter = std::move(rhs.deleter);
122  dataIsMutable = rhs.dataIsMutable;
123  return *this;
124  }
125  AsmResourceBlob(const AsmResourceBlob &) = delete;
128  if (deleter)
129  deleter(const_cast<char *>(data.data()), data.size(), dataAlignment);
130  }
131 
132  //===--------------------------------------------------------------------===//
133  // Data Access
134  //===--------------------------------------------------------------------===//
135 
136  /// Return the alignment of the underlying data.
137  size_t getDataAlignment() const { return dataAlignment; }
138 
139  /// Return the raw underlying data of this blob.
140  ArrayRef<char> getData() const { return data; }
141 
142  /// Return the underlying data as an array of the given type. This is an
143  /// inherrently unsafe operation, and should only be used when the data is
144  /// known to be of the correct type.
145  template <typename T>
147  return llvm::ArrayRef<T>((const T *)data.data(), data.size() / sizeof(T));
148  }
149 
150  /// Return a mutable reference to the raw underlying data of this blob.
151  /// Asserts that the blob `isMutable`.
153  assert(isMutable() &&
154  "cannot access mutable reference to non-mutable data");
155  return MutableArrayRef<char>(const_cast<char *>(data.data()), data.size());
156  }
157 
158  /// Return if the data of this blob is mutable.
159  bool isMutable() const { return dataIsMutable; }
160 
161  /// Return the deleter function of this blob.
162  DeleterFn &getDeleter() { return deleter; }
163  const DeleterFn &getDeleter() const { return deleter; }
164 
165 private:
166  /// The raw, properly aligned, blob data.
167  ArrayRef<char> data;
168 
169  /// The alignment of the data.
170  size_t dataAlignment = 0;
171 
172  /// An optional deleter function used to deallocate the underlying data when
173  /// necessary.
174  DeleterFn deleter;
175 
176  /// Whether the data is mutable.
177  bool dataIsMutable;
178 };
179 
180 /// This class provides a simple utility wrapper for creating heap allocated
181 /// AsmResourceBlobs.
183 public:
184  /// Create a new heap allocated blob with the given size and alignment.
185  /// `dataIsMutable` indicates if the allocated data can be mutated. By
186  /// default, we treat heap allocated blobs as mutable.
187  static AsmResourceBlob allocate(size_t size, size_t align,
188  bool dataIsMutable = true) {
189  return AsmResourceBlob(
190  ArrayRef<char>((char *)llvm::allocate_buffer(size, align), size), align,
191  llvm::deallocate_buffer, dataIsMutable);
192  }
193  /// Create a new heap allocated blob and copy the provided data into it.
195  size_t align,
196  bool dataIsMutable = true) {
197  AsmResourceBlob blob = allocate(data.size(), align, dataIsMutable);
198  std::memcpy(blob.getMutableData().data(), data.data(), data.size());
199  return blob;
200  }
201  template <typename T>
203  bool dataIsMutable = true) {
205  ArrayRef<char>((const char *)data.data(), data.size() * sizeof(T)),
206  alignof(T), dataIsMutable);
207  }
208 };
209 /// This class provides a simple utility wrapper for creating "unmanaged"
210 /// AsmResourceBlobs. The lifetime of the data provided to these blobs is
211 /// guaranteed to persist beyond the lifetime of this reference.
213 public:
214  /// Create a new unmanaged resource directly referencing the provided data.
215  /// `dataIsMutable` indicates if the allocated data can be mutated. By
216  /// default, we treat unmanaged blobs as immutable.
217  static AsmResourceBlob
218  allocateWithAlign(ArrayRef<char> data, size_t align,
219  AsmResourceBlob::DeleterFn deleter = {},
220  bool dataIsMutable = false) {
221  return AsmResourceBlob(data, align, std::move(deleter), dataIsMutable);
222  }
223  template <typename T>
224  static AsmResourceBlob
226  bool dataIsMutable = false) {
227  return allocateWithAlign(
228  ArrayRef<char>((const char *)data.data(), data.size() * sizeof(T)),
229  alignof(T), std::move(deleter), dataIsMutable);
230  }
231 };
232 
233 /// This class is used to build resource entries for use by the printer. Each
234 /// resource entry is represented using a key/value pair. The provided key must
235 /// be unique within the current context, which allows for a client to provide
236 /// resource entries without worrying about overlap with other clients.
238 public:
240 
241  /// Build a resource entry represented by the given bool.
242  virtual void buildBool(StringRef key, bool data) = 0;
243 
244  /// Build a resource entry represented by the given human-readable string
245  /// value.
246  virtual void buildString(StringRef key, StringRef data) = 0;
247 
248  /// Build an resource entry represented by the given binary blob data.
249  virtual void buildBlob(StringRef key, ArrayRef<char> data,
250  uint32_t dataAlignment) = 0;
251  /// Build an resource entry represented by the given binary blob data. This is
252  /// a useful overload if the data type is known. Note that this does not
253  /// support `char` element types to avoid accidentally not providing the
254  /// expected alignment of data in situations that treat blobs generically.
255  template <typename T>
256  std::enable_if_t<!std::is_same<T, char>::value> buildBlob(StringRef key,
257  ArrayRef<T> data) {
258  buildBlob(
259  key, ArrayRef<char>((const char *)data.data(), data.size() * sizeof(T)),
260  alignof(T));
261  }
262  /// Build an resource entry represented by the given resource blob. This is
263  /// a useful overload if a blob already exists in-memory.
264  void buildBlob(StringRef key, const AsmResourceBlob &blob) {
265  buildBlob(key, blob.getData(), blob.getDataAlignment());
266  }
267 };
268 
269 /// This enum represents the different kinds of resource values.
271  /// A blob of data with an accompanying alignment.
272  Blob,
273  /// A boolean value.
274  Bool,
275  /// A string value.
276  String,
277 };
278 StringRef toString(AsmResourceEntryKind kind);
279 
280 /// This class represents a single parsed resource entry.
282 public:
284 
285  /// Return the key of the resource entry.
286  virtual StringRef getKey() const = 0;
287 
288  /// Emit an error at the location of this entry.
289  virtual InFlightDiagnostic emitError() const = 0;
290 
291  /// Return the kind of this value.
292  virtual AsmResourceEntryKind getKind() const = 0;
293 
294  /// Parse the resource entry represented by a boolean. Returns failure if the
295  /// entry does not correspond to a bool.
296  virtual FailureOr<bool> parseAsBool() const = 0;
297 
298  /// Parse the resource entry represented by a human-readable string. Returns
299  /// failure if the entry does not correspond to a string.
301 
302  /// An allocator function used to allocate memory for a blob when required.
303  /// The function is provided a size and alignment, and should return an
304  /// aligned allocation buffer.
306  function_ref<AsmResourceBlob(size_t size, size_t align)>;
307 
308  /// Parse the resource entry represented by a binary blob. Returns failure if
309  /// the entry does not correspond to a blob. If the blob needed to be
310  /// allocated, the given allocator function is invoked.
312  parseAsBlob(BlobAllocatorFn allocator) const = 0;
313  /// Parse the resource entry represented by a binary blob using heap
314  /// allocation.
316  return parseAsBlob([](size_t size, size_t align) {
317  return HeapAsmResourceBlob::allocate(size, align);
318  });
319  }
320 };
321 
322 //===----------------------------------------------------------------------===//
323 // Resource Parser/Printer
324 
325 /// This class represents an instance of a resource parser. This class should be
326 /// implemented by non-dialect clients that want to inject additional resources
327 /// into MLIR assembly formats.
329 public:
330  /// Create a new parser with the given identifying name. This name uniquely
331  /// identifies the entries of this parser, and differentiates them from other
332  /// contexts.
333  AsmResourceParser(StringRef name) : name(name.str()) {}
335 
336  /// Return the name of this parser.
337  StringRef getName() const { return name; }
338 
339  /// Parse the given resource entry. Returns failure if the key/data were not
340  /// valid, or could otherwise not be processed correctly. Any necessary errors
341  /// should be emitted with the provided entry.
343 
344  /// Return a resource parser implemented via the given callable, whose form
345  /// should match that of `parseResource` above.
346  template <typename CallableT>
347  static std::unique_ptr<AsmResourceParser> fromCallable(StringRef name,
348  CallableT &&parseFn) {
349  struct Processor : public AsmResourceParser {
350  Processor(StringRef name, CallableT &&parseFn)
351  : AsmResourceParser(name), parseFn(std::move(parseFn)) {}
353  return parseFn(entry);
354  }
355 
356  std::decay_t<CallableT> parseFn;
357  };
358  return std::make_unique<Processor>(name, std::forward<CallableT>(parseFn));
359  }
360 
361 private:
362  std::string name;
363 };
364 
365 /// This class represents an instance of a resource printer. This class should
366 /// be implemented by non-dialect clients that want to inject additional
367 /// resources into MLIR assembly formats.
369 public:
370  /// Create a new printer with the given identifying name. This name uniquely
371  /// identifies the entries of this printer, and differentiates them from
372  /// other contexts.
373  AsmResourcePrinter(StringRef name) : name(name.str()) {}
375 
376  /// Return the name of this printer.
377  StringRef getName() const { return name; }
378 
379  /// Build any resources to include during printing, utilizing the given
380  /// top-level root operation to help determine what information to include.
381  /// Provided data should be registered in the form of a key/data pair, to the
382  /// given builder.
383  virtual void buildResources(Operation *op,
384  AsmResourceBuilder &builder) const = 0;
385 
386  /// Return a resource printer implemented via the given callable, whose form
387  /// should match that of `buildResources` above.
388  template <typename CallableT>
389  static std::unique_ptr<AsmResourcePrinter> fromCallable(StringRef name,
390  CallableT &&printFn) {
391  struct Printer : public AsmResourcePrinter {
392  Printer(StringRef name, CallableT &&printFn)
393  : AsmResourcePrinter(name), printFn(std::move(printFn)) {}
394  void buildResources(Operation *op,
395  AsmResourceBuilder &builder) const override {
396  printFn(op, builder);
397  }
398 
399  std::decay_t<CallableT> printFn;
400  };
401  return std::make_unique<Printer>(name, std::forward<CallableT>(printFn));
402  }
403 
404 private:
405  std::string name;
406 };
407 
408 /// A fallback map containing external resources not explicitly handled by
409 /// another parser/printer.
411 public:
412  /// This class represents an opaque resource.
415  std::variant<AsmResourceBlob, bool, std::string> value)
416  : key(key.str()), value(std::move(value)) {}
417 
418  /// The key identifying the resource.
419  std::string key;
420  /// An opaque value for the resource, whose variant values align 1-1 with
421  /// the kinds defined in AsmResourceEntryKind.
422  std::variant<AsmResourceBlob, bool, std::string> value;
423  };
424 
425  /// Return a parser than can be used for parsing entries for the given
426  /// identifier key.
427  AsmResourceParser &getParserFor(StringRef key);
428 
429  /// Build a set of resource printers to print the resources within this map.
430  std::vector<std::unique_ptr<AsmResourcePrinter>> getPrinters();
431 
432 private:
433  struct ResourceCollection : public AsmResourceParser {
434  ResourceCollection(StringRef name) : AsmResourceParser(name) {}
435 
436  /// Parse a resource into this collection.
437  LogicalResult parseResource(AsmParsedResourceEntry &entry) final;
438 
439  /// Build the resources held by this collection.
440  void buildResources(Operation *op, AsmResourceBuilder &builder) const;
441 
442  /// The set of resources parsed into this collection.
444  };
445 
446  /// The set of opaque resources.
447  llvm::MapVector<std::string, std::unique_ptr<ResourceCollection>,
448  llvm::StringMap<unsigned>>
449  keyToResources;
450 };
451 
452 //===----------------------------------------------------------------------===//
453 // ParserConfig
454 //===----------------------------------------------------------------------===//
455 
456 /// This class represents a configuration for the MLIR assembly parser. It
457 /// contains all of the necessary state to parse a MLIR source file.
459 public:
460  /// Construct a parser configuration with the given context.
461  /// `verifyAfterParse` indicates if the IR should be verified after parsing.
462  /// `fallbackResourceMap` is an optional fallback handler that can be used to
463  /// parse external resources not explicitly handled by another parser.
464  ParserConfig(MLIRContext *context, bool verifyAfterParse = true,
465  FallbackAsmResourceMap *fallbackResourceMap = nullptr)
466  : context(context), verifyAfterParse(verifyAfterParse),
467  fallbackResourceMap(fallbackResourceMap) {
468  assert(context && "expected valid MLIR context");
469  }
470 
471  /// Return the MLIRContext to be used when parsing.
472  MLIRContext *getContext() const { return context; }
473 
474  /// Returns if the parser should verify the IR after parsing.
475  bool shouldVerifyAfterParse() const { return verifyAfterParse; }
476 
477  /// Return the resource parser registered to the given name, or nullptr if no
478  /// parser with `name` is registered.
479  AsmResourceParser *getResourceParser(StringRef name) const {
480  auto it = resourceParsers.find(name);
481  if (it != resourceParsers.end())
482  return it->second.get();
483  if (fallbackResourceMap)
484  return &fallbackResourceMap->getParserFor(name);
485  return nullptr;
486  }
487 
488  /// Attach the given resource parser.
489  void attachResourceParser(std::unique_ptr<AsmResourceParser> parser) {
490  StringRef name = parser->getName();
491  auto it = resourceParsers.try_emplace(name, std::move(parser));
492  (void)it;
493  assert(it.second &&
494  "resource parser already registered with the given name");
495  }
496 
497  /// Attach the given callable resource parser with the given name.
498  template <typename CallableT>
499  std::enable_if_t<std::is_convertible<
500  CallableT, function_ref<LogicalResult(AsmParsedResourceEntry &)>>::value>
501  attachResourceParser(StringRef name, CallableT &&parserFn) {
503  name, std::forward<CallableT>(parserFn)));
504  }
505 
506 private:
507  MLIRContext *context;
508  bool verifyAfterParse;
510  FallbackAsmResourceMap *fallbackResourceMap;
511 };
512 
513 //===----------------------------------------------------------------------===//
514 // AsmState
515 //===----------------------------------------------------------------------===//
516 
517 /// This class provides management for the lifetime of the state used when
518 /// printing the IR. It allows for alleviating the cost of recomputing the
519 /// internal state of the asm printer.
520 ///
521 /// The IR should not be mutated in-between invocations using this state, and
522 /// the IR being printed must not be an parent of the IR originally used to
523 /// initialize this state. This means that if a child operation is provided, a
524 /// parent operation cannot reuse this state.
525 class AsmState {
526 public:
527  /// This map represents the raw locations of operations within the output
528  /// stream. This maps the original pointer to the operation, to a pair of line
529  /// and column in the output stream.
531 
532  /// Initialize the asm state at the level of the given operation. A location
533  /// map may optionally be provided to be populated when printing. `map` is an
534  /// optional fallback resource map, which when provided will attach resource
535  /// printers for the fallback resources within the map.
536  AsmState(Operation *op,
537  const OpPrintingFlags &printerFlags = OpPrintingFlags(),
538  LocationMap *locationMap = nullptr,
539  FallbackAsmResourceMap *map = nullptr);
540  AsmState(MLIRContext *ctx,
541  const OpPrintingFlags &printerFlags = OpPrintingFlags(),
542  LocationMap *locationMap = nullptr,
543  FallbackAsmResourceMap *map = nullptr);
545 
546  /// Get the printer flags.
547  const OpPrintingFlags &getPrinterFlags() const;
548 
549  /// Return an instance of the internal implementation. Returns nullptr if the
550  /// state has not been initialized.
552 
553  //===--------------------------------------------------------------------===//
554  // Resources
555  //===--------------------------------------------------------------------===//
556 
557  /// Attach the given resource printer to the AsmState.
558  void attachResourcePrinter(std::unique_ptr<AsmResourcePrinter> printer);
559 
560  /// Attach an resource printer, in the form of a callable, to the AsmState.
561  template <typename CallableT>
562  std::enable_if_t<std::is_convertible<
563  CallableT, function_ref<void(Operation *, AsmResourceBuilder &)>>::value>
564  attachResourcePrinter(StringRef name, CallableT &&printFn) {
566  name, std::forward<CallableT>(printFn)));
567  }
568 
569  /// Attach resource printers to the AsmState for the fallback resources
570  /// in the given map.
572  for (auto &printer : map.getPrinters())
573  attachResourcePrinter(std::move(printer));
574  }
575 
576  /// Returns a map of dialect resources that were referenced when using this
577  /// state to print IR.
579  getDialectResources() const;
580 
581 private:
582  AsmState() = delete;
583 
584  /// A pointer to allocated storage for the impl state.
585  std::unique_ptr<detail::AsmStateImpl> impl;
586 };
587 
588 //===----------------------------------------------------------------------===//
589 // AsmPrinter CommandLine Options
590 //===----------------------------------------------------------------------===//
591 
592 /// Register a set of useful command-line options that can be used to configure
593 /// various flags within the AsmPrinter.
595 
596 } // namespace mlir
597 
598 #endif // MLIR_IR_ASMSTATE_H_
This class represents a single parsed resource entry.
Definition: AsmState.h:281
virtual FailureOr< AsmResourceBlob > parseAsBlob(BlobAllocatorFn allocator) const =0
Parse the resource entry represented by a binary blob.
virtual InFlightDiagnostic emitError() const =0
Emit an error at the location of this entry.
virtual AsmResourceEntryKind getKind() const =0
Return the kind of this value.
virtual FailureOr< std::string > parseAsString() const =0
Parse the resource entry represented by a human-readable string.
FailureOr< AsmResourceBlob > parseAsBlob() const
Parse the resource entry represented by a binary blob using heap allocation.
Definition: AsmState.h:315
virtual FailureOr< bool > parseAsBool() const =0
Parse the resource entry represented by a boolean.
virtual StringRef getKey() const =0
Return the key of the resource entry.
The following classes enable support for parsing and printing resources within MLIR assembly formats.
Definition: AsmState.h:86
llvm::unique_function< void(void *data, size_t size, size_t align)> DeleterFn
A deleter function that frees a blob given the data, allocation size, and allocation aligment.
Definition: AsmState.h:91
AsmResourceBlob(AsmResourceBlob &&)=default
AsmResourceBlob(ArrayRef< T > data, DelT &&deleteFn, bool dataIsMutable)
Utility constructor that initializes a blob with a non-char type T.
Definition: AsmState.h:104
const DeleterFn & getDeleter() const
Definition: AsmState.h:163
AsmResourceBlob & operator=(const AsmResourceBlob &)=delete
size_t getDataAlignment() const
Return the alignment of the underlying data.
Definition: AsmState.h:137
ArrayRef< T > getDataAs() const
Return the underlying data as an array of the given type.
Definition: AsmState.h:146
DeleterFn & getDeleter()
Return the deleter function of this blob.
Definition: AsmState.h:162
AsmResourceBlob & operator=(AsmResourceBlob &&rhs)
Definition: AsmState.h:113
MutableArrayRef< char > getMutableData()
Return a mutable reference to the raw underlying data of this blob.
Definition: AsmState.h:152
ArrayRef< char > getData() const
Return the raw underlying data of this blob.
Definition: AsmState.h:140
AsmResourceBlob(ArrayRef< char > data, size_t dataAlignment, DeleterFn deleter, bool dataIsMutable)
Definition: AsmState.h:98
AsmResourceBlob(const AsmResourceBlob &)=delete
bool isMutable() const
Return if the data of this blob is mutable.
Definition: AsmState.h:159
This class is used to build resource entries for use by the printer.
Definition: AsmState.h:237
void buildBlob(StringRef key, const AsmResourceBlob &blob)
Build an resource entry represented by the given resource blob.
Definition: AsmState.h:264
virtual void buildString(StringRef key, StringRef data)=0
Build a resource entry represented by the given human-readable string value.
virtual void buildBool(StringRef key, bool data)=0
Build a resource entry represented by the given bool.
std::enable_if_t<!std::is_same< T, char >::value > buildBlob(StringRef key, ArrayRef< T > data)
Build an resource entry represented by the given binary blob data.
Definition: AsmState.h:256
virtual void buildBlob(StringRef key, ArrayRef< char > data, uint32_t dataAlignment)=0
Build an resource entry represented by the given binary blob data.
This class represents an instance of a resource parser.
Definition: AsmState.h:328
virtual LogicalResult parseResource(AsmParsedResourceEntry &entry)=0
Parse the given resource entry.
static std::unique_ptr< AsmResourceParser > fromCallable(StringRef name, CallableT &&parseFn)
Return a resource parser implemented via the given callable, whose form should match that of parseRes...
Definition: AsmState.h:347
AsmResourceParser(StringRef name)
Create a new parser with the given identifying name.
Definition: AsmState.h:333
StringRef getName() const
Return the name of this parser.
Definition: AsmState.h:337
This class represents an instance of a resource printer.
Definition: AsmState.h:368
AsmResourcePrinter(StringRef name)
Create a new printer with the given identifying name.
Definition: AsmState.h:373
StringRef getName() const
Return the name of this printer.
Definition: AsmState.h:377
virtual void buildResources(Operation *op, AsmResourceBuilder &builder) const =0
Build any resources to include during printing, utilizing the given top-level root operation to help ...
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 provides management for the lifetime of the state used when printing the IR.
Definition: AsmState.h:525
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 AsmState.
Definition: AsmState.h:564
detail::AsmStateImpl & getImpl()
Return an instance of the internal implementation.
Definition: AsmState.h:551
void attachResourcePrinter(std::unique_ptr< AsmResourcePrinter > printer)
Attach the given resource printer to the AsmState.
DenseMap< Dialect *, SetVector< AsmDialectResourceHandle > > & getDialectResources() const
Returns a map of dialect resources that were referenced when using this state to print IR.
void attachFallbackResourcePrinter(FallbackAsmResourceMap &map)
Attach resource printers to the AsmState for the fallback resources in the given map.
Definition: AsmState.h:571
const OpPrintingFlags & getPrinterFlags() const
Get the printer flags.
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
A fallback map containing external resources not explicitly handled by another parser/printer.
Definition: AsmState.h:410
AsmResourceParser & getParserFor(StringRef key)
Return a parser than can be used for parsing entries for the given identifier key.
std::vector< std::unique_ptr< AsmResourcePrinter > > getPrinters()
Build a set of resource printers to print the resources within this map.
This class provides a simple utility wrapper for creating heap allocated AsmResourceBlobs.
Definition: AsmState.h:182
static AsmResourceBlob allocateAndCopyInferAlign(ArrayRef< T > data, bool dataIsMutable=true)
Definition: AsmState.h:202
static AsmResourceBlob allocateAndCopyWithAlign(ArrayRef< char > data, size_t align, bool dataIsMutable=true)
Create a new heap allocated blob and copy the provided data into it.
Definition: AsmState.h:194
static AsmResourceBlob allocate(size_t size, size_t align, bool dataIsMutable=true)
Create a new heap allocated blob with the given size and alignment.
Definition: AsmState.h:187
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Set of flags used to control the behavior of the various IR print methods (e.g.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
This class represents a configuration for the MLIR assembly parser.
Definition: AsmState.h:458
bool shouldVerifyAfterParse() const
Returns if the parser should verify the IR after parsing.
Definition: AsmState.h:475
void attachResourceParser(std::unique_ptr< AsmResourceParser > parser)
Attach the given resource parser.
Definition: AsmState.h:489
ParserConfig(MLIRContext *context, bool verifyAfterParse=true, FallbackAsmResourceMap *fallbackResourceMap=nullptr)
Construct a parser configuration with the given context.
Definition: AsmState.h:464
MLIRContext * getContext() const
Return the MLIRContext to be used when parsing.
Definition: AsmState.h:472
std::enable_if_t< std::is_convertible< CallableT, function_ref< LogicalResult(AsmParsedResourceEntry &)> >::value > attachResourceParser(StringRef name, CallableT &&parserFn)
Attach the given callable resource parser with the given name.
Definition: AsmState.h:501
AsmResourceParser * getResourceParser(StringRef name) const
Return the resource parser registered to the given name, or nullptr if no parser with name is registe...
Definition: AsmState.h:479
This class provides a simple utility wrapper for creating "unmanaged" AsmResourceBlobs.
Definition: AsmState.h:212
static AsmResourceBlob allocateWithAlign(ArrayRef< char > data, size_t align, AsmResourceBlob::DeleterFn deleter={}, bool dataIsMutable=false)
Create a new unmanaged resource directly referencing the provided data.
Definition: AsmState.h:218
static AsmResourceBlob allocateInferAlign(ArrayRef< T > data, AsmResourceBlob::DeleterFn deleter={}, bool dataIsMutable=false)
Definition: AsmState.h:225
Include the generated interface declarations.
StringRef toString(AsmResourceEntryKind kind)
void registerAsmPrinterCLOptions()
Register a set of useful command-line options that can be used to configure various flags within the ...
Definition: AsmPrinter.cpp:178
AsmResourceEntryKind
This enum represents the different kinds of resource values.
Definition: AsmState.h:270
@ String
A string value.
@ Bool
A boolean value.
@ Blob
A blob of data with an accompanying alignment.
This class represents an opaque resource.
Definition: AsmState.h:413
std::string key
The key identifying the resource.
Definition: AsmState.h:419
std::variant< AsmResourceBlob, bool, std::string > value
An opaque value for the resource, whose variant values align 1-1 with the kinds defined in AsmResourc...
Definition: AsmState.h:422
OpaqueAsmResource(StringRef key, std::variant< AsmResourceBlob, bool, std::string > value)
Definition: AsmState.h:414
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26