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;
85 class HeapAsmResourceBlob;
95 llvm::unique_function<void(
void *data,
size_t size,
size_t align)>;
104 : data(data), dataAlignment(dataAlignment), deleter(std::move(deleter)),
105 dataIsMutable(dataIsMutable) {}
107 template <
typename T,
typename DelT>
109 : data((const char *)data.data(), data.size() * sizeof(T)),
110 dataAlignment(alignof(T)),
111 deleter([deleteFn = std::forward<DelT>(deleteFn)](
112 void *data, size_t size, size_t align) {
113 return deleteFn((T *)data, size, align);
115 dataIsMutable(dataIsMutable) {}
120 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
124 dataAlignment = rhs.dataAlignment;
125 deleter = std::move(rhs.deleter);
126 dataIsMutable = rhs.dataIsMutable;
133 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
149 template <
typename T>
158 "cannot access mutable reference to non-mutable data");
174 size_t dataAlignment = 0;
194 bool dataIsMutable =
true) {
196 ArrayRef<char>((
char *)llvm::allocate_buffer(size, align), size), align,
197 llvm::deallocate_buffer, dataIsMutable);
202 bool dataIsMutable =
true) {
206 std::memcpy(blob.
getMutableData().data(), data.data(), data.size());
207 blob.dataIsMutable = dataIsMutable;
210 template <
typename T>
212 bool dataIsMutable =
true) {
214 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
215 alignof(T), dataIsMutable);
229 bool dataIsMutable =
false) {
230 return AsmResourceBlob(data, align, std::move(deleter), dataIsMutable);
232 template <
typename T>
233 static AsmResourceBlob
235 bool dataIsMutable =
false) {
237 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
238 alignof(T), std::move(deleter), dataIsMutable);
259 uint32_t dataAlignment) = 0;
264 template <
typename T>
265 std::enable_if_t<!std::is_same<T, char>::value>
buildBlob(StringRef key,
268 key,
ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
320 virtual FailureOr<AsmResourceBlob>
355 template <
typename CallableT>
357 CallableT &&parseFn) {
359 Processor(StringRef name, CallableT &&parseFn)
362 return parseFn(entry);
365 std::decay_t<CallableT> parseFn;
367 return std::make_unique<Processor>(name, std::forward<CallableT>(parseFn));
397 template <
typename CallableT>
398 static std::unique_ptr<AsmResourcePrinter>
fromCallable(StringRef name,
399 CallableT &&printFn) {
401 Printer(StringRef name, CallableT &&printFn)
405 printFn(op, builder);
408 std::decay_t<CallableT> printFn;
410 return std::make_unique<Printer>(name, std::forward<CallableT>(printFn));
424 std::variant<AsmResourceBlob, bool, std::string>
value)
431 std::variant<AsmResourceBlob, bool, std::string>
value;
439 std::vector<std::unique_ptr<AsmResourcePrinter>>
getPrinters();
456 llvm::MapVector<std::string, std::unique_ptr<ResourceCollection>,
457 llvm::StringMap<unsigned>>
475 : context(context), verifyAfterParse(verifyAfterParse),
476 fallbackResourceMap(fallbackResourceMap) {
477 assert(context &&
"expected valid MLIR context");
494 auto it = resourceParsers.find(name);
495 if (it != resourceParsers.end())
496 return it->second.get();
497 if (fallbackResourceMap)
504 StringRef name = parser->getName();
505 auto it = resourceParsers.try_emplace(name, std::move(parser));
508 "resource parser already registered with the given name");
512 template <
typename CallableT>
513 std::enable_if_t<std::is_convertible<
517 name, std::forward<CallableT>(parserFn)));
522 bool verifyAfterParse;
576 template <
typename CallableT>
577 std::enable_if_t<std::is_convertible<
581 name, std::forward<CallableT>(printFn)));
600 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.
This class represents a processed binary blob of data.
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)