//===- BytecodeReaderConfig.h - MLIR Bytecode Reader Config -----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This header config for reading MLIR bytecode files/streams. // //===----------------------------------------------------------------------===// #ifndef MLIR_BYTECODE_BYTECODEREADERCONFIG_H #define MLIR_BYTECODE_BYTECODEREADERCONFIG_H #include "mlir/Support/LLVM.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" namespace mlir { class Attribute; class DialectBytecodeReader; class Type; /// A class to interact with the attributes and types parser when parsing MLIR /// bytecode. template class AttrTypeBytecodeReader { public: AttrTypeBytecodeReader() = default; virtual ~AttrTypeBytecodeReader() = default; virtual LogicalResult read(DialectBytecodeReader &reader, StringRef dialectName, T &entry) = 0; /// Return an Attribute/Type printer implemented via the given callable, whose /// form should match that of the `parse` function above. template >, bool> = true> static std::unique_ptr> fromCallable(CallableT &&readFn) { struct Processor : public AttrTypeBytecodeReader { Processor(CallableT &&readFn) : AttrTypeBytecodeReader(), readFn(std::move(readFn)) {} LogicalResult read(DialectBytecodeReader &reader, StringRef dialectName, T &entry) override { return readFn(reader, dialectName, entry); } std::decay_t readFn; }; return std::make_unique(std::forward(readFn)); } }; //===----------------------------------------------------------------------===// // BytecodeReaderConfig //===----------------------------------------------------------------------===// /// A class containing bytecode-specific configurations of the `ParserConfig`. class BytecodeReaderConfig { public: BytecodeReaderConfig() = default; /// Returns the callbacks available to the parser. ArrayRef>> getAttributeCallbacks() const { return attributeBytecodeParsers; } ArrayRef>> getTypeCallbacks() const { return typeBytecodeParsers; } /// Attach a custom bytecode parser callback to the configuration for parsing /// of custom type/attributes encodings. void attachAttributeCallback( std::unique_ptr> parser) { attributeBytecodeParsers.emplace_back(std::move(parser)); } void attachTypeCallback(std::unique_ptr> parser) { typeBytecodeParsers.emplace_back(std::move(parser)); } /// Attach a custom bytecode parser callback to the configuration for parsing /// of custom type/attributes encodings. template std::enable_if_t>> attachAttributeCallback(CallableT &&parserFn) { attachAttributeCallback(AttrTypeBytecodeReader::fromCallable( std::forward(parserFn))); } template std::enable_if_t>> attachTypeCallback(CallableT &&parserFn) { attachTypeCallback(AttrTypeBytecodeReader::fromCallable( std::forward(parserFn))); } private: llvm::SmallVector>> attributeBytecodeParsers; llvm::SmallVector>> typeBytecodeParsers; }; } // namespace mlir #endif // MLIR_BYTECODE_BYTECODEREADERCONFIG_H