14 #ifndef MLIR_IR_ASMSTATE_H_
15 #define MLIR_IR_ASMSTATE_H_
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/StringMap.h"
25 class AsmResourcePrinter;
26 class AsmDialectResourceHandle;
91 llvm::unique_function<void(
void *data,
size_t size,
size_t align)>;
100 : data(data), dataAlignment(dataAlignment), deleter(std::move(deleter)),
101 dataIsMutable(dataIsMutable) {}
103 template <
typename T,
typename DelT>
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);
111 dataIsMutable(dataIsMutable) {}
116 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
120 dataAlignment = rhs.dataAlignment;
121 deleter = std::move(rhs.deleter);
122 dataIsMutable = rhs.dataIsMutable;
129 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
145 template <
typename T>
154 "cannot access mutable reference to non-mutable data");
170 size_t dataAlignment = 0;
188 bool dataIsMutable =
true) {
190 ArrayRef<char>((
char *)llvm::allocate_buffer(size, align), size), align,
191 llvm::deallocate_buffer, dataIsMutable);
196 bool dataIsMutable =
true) {
198 std::memcpy(blob.
getMutableData().data(), data.data(), data.size());
201 template <
typename T>
203 bool dataIsMutable =
true) {
205 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
206 alignof(T), dataIsMutable);
220 bool dataIsMutable =
false) {
221 return AsmResourceBlob(data, align, std::move(deleter), dataIsMutable);
223 template <
typename T>
224 static AsmResourceBlob
226 bool dataIsMutable =
false) {
228 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
229 alignof(T), std::move(deleter), dataIsMutable);
250 uint32_t dataAlignment) = 0;
255 template <
typename T>
256 std::enable_if_t<!std::is_same<T, char>::value>
buildBlob(StringRef key,
259 key,
ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
346 template <
typename CallableT>
348 CallableT &&parseFn) {
350 Processor(StringRef name, CallableT &&parseFn)
353 return parseFn(entry);
356 std::decay_t<CallableT> parseFn;
358 return std::make_unique<Processor>(name, std::forward<CallableT>(parseFn));
388 template <
typename CallableT>
389 static std::unique_ptr<AsmResourcePrinter>
fromCallable(StringRef name,
390 CallableT &&printFn) {
392 Printer(StringRef name, CallableT &&printFn)
396 printFn(op, builder);
399 std::decay_t<CallableT> printFn;
401 return std::make_unique<Printer>(name, std::forward<CallableT>(printFn));
415 std::variant<AsmResourceBlob, bool, std::string>
value)
422 std::variant<AsmResourceBlob, bool, std::string>
value;
430 std::vector<std::unique_ptr<AsmResourcePrinter>>
getPrinters();
447 llvm::MapVector<std::string, std::unique_ptr<ResourceCollection>,
448 llvm::StringMap<unsigned>>
466 : context(context), verifyAfterParse(verifyAfterParse),
467 fallbackResourceMap(fallbackResourceMap) {
468 assert(context &&
"expected valid MLIR context");
480 auto it = resourceParsers.find(name);
481 if (it != resourceParsers.end())
482 return it->second.get();
483 if (fallbackResourceMap)
490 StringRef name = parser->getName();
491 auto it = resourceParsers.try_emplace(name, std::move(parser));
494 "resource parser already registered with the given name");
498 template <
typename CallableT>
499 std::enable_if_t<std::is_convertible<
503 name, std::forward<CallableT>(parserFn)));
508 bool verifyAfterParse;
561 template <
typename CallableT>
562 std::enable_if_t<std::is_convertible<
566 name, std::forward<CallableT>(printFn)));
585 std::unique_ptr<detail::AsmStateImpl>
impl;
This class represents a single parsed resource entry.
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 ~AsmParsedResourceEntry()
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.
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.
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.
AsmResourceBlob(AsmResourceBlob &&)=default
AsmResourceBlob(ArrayRef< T > data, DelT &&deleteFn, bool dataIsMutable)
Utility constructor that initializes a blob with a non-char type T.
const DeleterFn & getDeleter() const
AsmResourceBlob & operator=(const AsmResourceBlob &)=delete
size_t getDataAlignment() const
Return the alignment of the underlying data.
ArrayRef< T > getDataAs() const
Return the underlying data as an array of the given type.
DeleterFn & getDeleter()
Return the deleter function of this blob.
AsmResourceBlob()=default
AsmResourceBlob & operator=(AsmResourceBlob &&rhs)
MutableArrayRef< char > getMutableData()
Return a mutable reference to the raw underlying data of this blob.
ArrayRef< char > getData() const
Return the raw underlying data of this blob.
AsmResourceBlob(ArrayRef< char > data, size_t dataAlignment, DeleterFn deleter, bool dataIsMutable)
AsmResourceBlob(const AsmResourceBlob &)=delete
bool isMutable() const
Return if the data of this blob is mutable.
This class is used to build resource entries for use by the printer.
void buildBlob(StringRef key, const AsmResourceBlob &blob)
Build an resource entry represented by the given resource blob.
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 ~AsmResourceBuilder()
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.
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.
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...
AsmResourceParser(StringRef name)
Create a new parser with the given identifying name.
StringRef getName() const
Return the name of this parser.
virtual ~AsmResourceParser()
This class represents an instance of a resource printer.
AsmResourcePrinter(StringRef name)
Create a new printer with the given identifying name.
StringRef getName() const
Return the name of this printer.
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...
virtual ~AsmResourcePrinter()
This class provides management for the lifetime of the state used when printing the IR.
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.
detail::AsmStateImpl & getImpl()
Return an instance of the internal implementation.
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.
const OpPrintingFlags & getPrinterFlags() const
Get the printer flags.
This class provides support for representing a failure result, or a valid value of type T.
A fallback map containing external resources not explicitly handled by another parser/printer.
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.
static AsmResourceBlob allocateAndCopyInferAlign(ArrayRef< T > data, bool dataIsMutable=true)
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.
static AsmResourceBlob allocate(size_t size, size_t align, bool dataIsMutable=true)
Create a new heap allocated blob with the given size and alignment.
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.
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.
This class represents a configuration for the MLIR assembly parser.
bool shouldVerifyAfterParse() const
Returns if the parser should verify the IR after parsing.
void attachResourceParser(std::unique_ptr< AsmResourceParser > parser)
Attach the given resource parser.
ParserConfig(MLIRContext *context, bool verifyAfterParse=true, FallbackAsmResourceMap *fallbackResourceMap=nullptr)
Construct a parser configuration with the given context.
MLIRContext * getContext() const
Return the MLIRContext to be used when parsing.
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.
AsmResourceParser * getResourceParser(StringRef name) const
Return the resource parser registered to the given name, or nullptr if no parser with name is registe...
This class provides a simple utility wrapper for creating "unmanaged" AsmResourceBlobs.
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.
static AsmResourceBlob allocateInferAlign(ArrayRef< T > data, AsmResourceBlob::DeleterFn deleter={}, bool dataIsMutable=false)
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 ...
AsmResourceEntryKind
This enum represents the different kinds of resource values.
@ Blob
A blob of data with an accompanying alignment.
This class represents an opaque resource.
std::string key
The key identifying the resource.
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...
OpaqueAsmResource(StringRef key, std::variant< AsmResourceBlob, bool, std::string > value)
This class represents an efficient way to signal success or failure.