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