14 #ifndef MLIR_IR_ASMSTATE_H_
15 #define MLIR_IR_ASMSTATE_H_
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/StringMap.h"
27 class AsmResourcePrinter;
28 class AsmDialectResourceHandle;
93 llvm::unique_function<void(
void *data,
size_t size,
size_t align)>;
102 : data(data), dataAlignment(dataAlignment), deleter(std::move(deleter)),
103 dataIsMutable(dataIsMutable) {}
105 template <
typename T,
typename DelT>
107 : data((const char *)data.data(), data.size() * sizeof(T)),
108 dataAlignment(alignof(T)),
109 deleter([deleteFn = std::forward<DelT>(deleteFn)](
110 void *data, size_t size, size_t align) {
111 return deleteFn((T *)data, size, align);
113 dataIsMutable(dataIsMutable) {}
118 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
122 dataAlignment = rhs.dataAlignment;
123 deleter = std::move(rhs.deleter);
124 dataIsMutable = rhs.dataIsMutable;
131 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
147 template <
typename T>
156 "cannot access mutable reference to non-mutable data");
172 size_t dataAlignment = 0;
190 bool dataIsMutable =
true) {
192 ArrayRef<char>((
char *)llvm::allocate_buffer(size, align), size), align,
193 llvm::deallocate_buffer, dataIsMutable);
198 bool dataIsMutable =
true) {
200 std::memcpy(blob.
getMutableData().data(), data.data(), data.size());
203 template <
typename T>
205 bool dataIsMutable =
true) {
207 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
208 alignof(T), dataIsMutable);
222 bool dataIsMutable =
false) {
223 return AsmResourceBlob(data, align, std::move(deleter), dataIsMutable);
225 template <
typename T>
226 static AsmResourceBlob
228 bool dataIsMutable =
false) {
230 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
231 alignof(T), std::move(deleter), dataIsMutable);
252 uint32_t dataAlignment) = 0;
257 template <
typename T>
258 std::enable_if_t<!std::is_same<T, char>::value>
buildBlob(StringRef key,
261 key,
ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
313 virtual FailureOr<AsmResourceBlob>
348 template <
typename CallableT>
350 CallableT &&parseFn) {
352 Processor(StringRef name, CallableT &&parseFn)
355 return parseFn(entry);
358 std::decay_t<CallableT> parseFn;
360 return std::make_unique<Processor>(name, std::forward<CallableT>(parseFn));
390 template <
typename CallableT>
391 static std::unique_ptr<AsmResourcePrinter>
fromCallable(StringRef name,
392 CallableT &&printFn) {
394 Printer(StringRef name, CallableT &&printFn)
398 printFn(op, builder);
401 std::decay_t<CallableT> printFn;
403 return std::make_unique<Printer>(name, std::forward<CallableT>(printFn));
417 std::variant<AsmResourceBlob, bool, std::string>
value)
424 std::variant<AsmResourceBlob, bool, std::string>
value;
432 std::vector<std::unique_ptr<AsmResourcePrinter>>
getPrinters();
449 llvm::MapVector<std::string, std::unique_ptr<ResourceCollection>,
450 llvm::StringMap<unsigned>>
468 : context(context), verifyAfterParse(verifyAfterParse),
469 fallbackResourceMap(fallbackResourceMap) {
470 assert(context &&
"expected valid MLIR context");
487 auto it = resourceParsers.find(name);
488 if (it != resourceParsers.end())
489 return it->second.get();
490 if (fallbackResourceMap)
497 StringRef name = parser->getName();
498 auto it = resourceParsers.try_emplace(name, std::move(parser));
501 "resource parser already registered with the given name");
505 template <
typename CallableT>
506 std::enable_if_t<std::is_convertible<
510 name, std::forward<CallableT>(parserFn)));
515 bool verifyAfterParse;
569 template <
typename CallableT>
570 std::enable_if_t<std::is_convertible<
574 name, std::forward<CallableT>(printFn)));
593 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.
A class containing bytecode-specific configurations of the ParserConfig.
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.
BytecodeReaderConfig & getBytecodeReaderConfig() const
Returns the parsing configurations associated to the bytecode read.
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)