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;
86 class HeapAsmResourceBlob;
96 llvm::unique_function<void(
void *data,
size_t size,
size_t align)>;
105 : data(data), dataAlignment(dataAlignment), deleter(std::move(deleter)),
106 dataIsMutable(dataIsMutable) {}
108 template <
typename T,
typename DelT>
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);
116 dataIsMutable(dataIsMutable) {}
121 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
125 dataAlignment = rhs.dataAlignment;
126 deleter = std::move(rhs.deleter);
127 dataIsMutable = rhs.dataIsMutable;
134 deleter(
const_cast<char *
>(data.data()), data.size(), dataAlignment);
150 template <
typename T>
159 "cannot access mutable reference to non-mutable data");
175 size_t dataAlignment = 0;
195 bool dataIsMutable =
true) {
197 ArrayRef<char>((
char *)llvm::allocate_buffer(size, align), size), align,
198 llvm::deallocate_buffer, dataIsMutable);
203 bool dataIsMutable =
true) {
207 std::memcpy(blob.
getMutableData().data(), data.data(), data.size());
208 blob.dataIsMutable = dataIsMutable;
211 template <
typename T>
213 bool dataIsMutable =
true) {
215 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
216 alignof(T), dataIsMutable);
230 bool dataIsMutable =
false) {
231 return AsmResourceBlob(data, align, std::move(deleter), dataIsMutable);
233 template <
typename T>
234 static AsmResourceBlob
236 bool dataIsMutable =
false) {
238 ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
239 alignof(T), std::move(deleter), dataIsMutable);
260 uint32_t dataAlignment) = 0;
265 template <
typename T>
266 std::enable_if_t<!std::is_same<T, char>::value>
buildBlob(StringRef key,
269 key,
ArrayRef<char>((
const char *)data.data(), data.size() *
sizeof(T)),
321 virtual FailureOr<AsmResourceBlob>
357 template <
typename CallableT>
359 CallableT &&parseFn) {
361 Processor(StringRef name, CallableT &&parseFn)
364 return parseFn(entry);
367 std::decay_t<CallableT> parseFn;
369 return std::make_unique<Processor>(name, std::forward<CallableT>(parseFn));
399 template <
typename CallableT>
400 static std::unique_ptr<AsmResourcePrinter>
fromCallable(StringRef name,
401 CallableT &&printFn) {
403 Printer(StringRef name, CallableT &&printFn)
407 printFn(op, builder);
410 std::decay_t<CallableT> printFn;
412 return std::make_unique<Printer>(name, std::forward<CallableT>(printFn));
426 std::variant<AsmResourceBlob, bool, std::string>
value)
433 std::variant<AsmResourceBlob, bool, std::string>
value;
441 std::vector<std::unique_ptr<AsmResourcePrinter>>
getPrinters();
458 llvm::MapVector<std::string, std::unique_ptr<ResourceCollection>,
459 llvm::StringMap<unsigned>>
477 : context(context), verifyAfterParse(verifyAfterParse),
478 fallbackResourceMap(fallbackResourceMap) {
479 assert(context &&
"expected valid MLIR context");
496 auto it = resourceParsers.find(name);
497 if (it != resourceParsers.end())
498 return it->second.get();
499 if (fallbackResourceMap)
506 StringRef name = parser->getName();
507 auto it = resourceParsers.try_emplace(name, std::move(parser));
510 "resource parser already registered with the given name");
514 template <
typename CallableT>
515 std::enable_if_t<std::is_convertible<
519 name, std::forward<CallableT>(parserFn)));
524 bool verifyAfterParse;
578 template <
typename CallableT>
579 std::enable_if_t<std::is_convertible<
583 name, std::forward<CallableT>(printFn)));
602 std::unique_ptr<detail::AsmStateImpl>
impl;
union mlir::linalg::@1192::ArityGroupAndKind::Kind kind
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)