14 #ifndef MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
15 #define MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
23 #include "llvm/ADT/Twine.h"
46 template <
typename T,
typename CallbackFn>
53 for (uint64_t i = 0; i < size; ++i) {
56 if constexpr (llvm::function_traits<std::decay_t<CallbackFn>>::num_args) {
58 if (
failed(callback(element)))
60 result.emplace_back(std::move(element));
65 result.emplace_back(std::move(*element));
86 if ((result = baseResult.
dyn_cast<T>()))
88 return emitError() <<
"expected " << llvm::getTypeName<T>()
89 <<
", but got: " << baseResult;
103 if ((result = baseResult.
dyn_cast<T>()))
105 return emitError() <<
"expected " << llvm::getTypeName<T>()
106 <<
", but got: " << baseResult;
110 template <
typename ResourceT>
115 if (
auto *result = dyn_cast<ResourceT>(&*handle))
116 return std::move(*result);
117 return emitError() <<
"provided resource handle differs from the "
118 "expected resource type";
173 template <
typename RangeT,
typename CallbackFn>
176 for (
auto &element : range)
182 template <
typename T>
189 template <
typename T>
254 reader.
emitError() <<
"dialect " << getDialect()->getNamespace()
255 <<
" does not support reading attributes from bytecode";
262 reader.
emitError() <<
"dialect " << getDialect()->getNamespace()
263 <<
" does not support reading types from bytecode";
This class represents an opaque handle to a dialect resource entry.
Attributes are known-constant values of operations.
virtual Type readType(DialectBytecodeReader &reader) const
Read a type belonging to this dialect from the given reader.
virtual Attribute readAttribute(DialectBytecodeReader &reader) const
Read an attribute belonging to this dialect from the given reader.
virtual LogicalResult writeAttribute(Attribute attr, DialectBytecodeWriter &writer) const
Write the given attribute, which belongs to this dialect, to the given writer.
virtual LogicalResult writeType(Type type, DialectBytecodeWriter &writer) const
Write the given type, which belongs to this dialect, to the given writer.
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
virtual ~DialectBytecodeReader()=default
virtual LogicalResult readBlob(ArrayRef< char > &result)=0
Read a blob from the bytecode.
LogicalResult readAttributes(SmallVectorImpl< T > &attrs)
FailureOr< ResourceT > readResourceHandle()
Read a handle to a dialect resource.
virtual FailureOr< APInt > readAPIntWithKnownWidth(unsigned bitWidth)=0
Read an APInt that is known to have been encoded with the given width.
LogicalResult readTypes(SmallVectorImpl< T > &types)
virtual LogicalResult readVarInt(uint64_t &result)=0
Read a variable width integer.
virtual LogicalResult readType(Type &result)=0
Read a reference to the given type.
LogicalResult readType(T &result)
LogicalResult readSignedVarInts(SmallVectorImpl< int64_t > &result)
virtual InFlightDiagnostic emitError(const Twine &msg={})=0
Emit an error to the reader.
LogicalResult readAttribute(T &result)
virtual FailureOr< APFloat > readAPFloatWithKnownSemantics(const llvm::fltSemantics &semantics)=0
Read an APFloat that is known to have been encoded with the given semantics.
virtual LogicalResult readString(StringRef &result)=0
Read a string from the bytecode.
virtual LogicalResult readSignedVarInt(int64_t &result)=0
Read a signed variable width integer.
LogicalResult readList(SmallVectorImpl< T > &result, CallbackFn &&callback)
Read out a list of elements, invoking the provided callback for each element.
virtual LogicalResult readAttribute(Attribute &result)=0
Read a reference to the given attribute.
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
virtual void writeVarInt(uint64_t value)=0
Write a variable width integer to the output stream.
void writeList(RangeT &&range, CallbackFn &&callback)
Write out a list of elements, invoking the provided callback for each element.
virtual void writeType(Type type)=0
Write a reference to the given type.
virtual void writeAPIntWithKnownWidth(const APInt &value)=0
Write an APInt to the bytecode stream whose bitwidth will be known externally at read time.
virtual void writeOwnedBlob(ArrayRef< char > blob)=0
Write a blob to the bytecode, which is owned by the caller and is guaranteed to not die before the en...
virtual void writeAttribute(Attribute attr)=0
Write a reference to the given attribute.
virtual ~DialectBytecodeWriter()=default
void writeAttributes(ArrayRef< T > attrs)
virtual void writeSignedVarInt(int64_t value)=0
Write a signed variable width integer to the output stream.
virtual void writeResourceHandle(const AsmDialectResourceHandle &resource)=0
Write the given handle to a dialect resource.
virtual void writeAPFloatWithKnownSemantics(const APFloat &value)=0
Write an APFloat to the bytecode stream whose semantics will be known externally at read time.
void writeSignedVarInts(ArrayRef< int64_t > value)
virtual void writeOwnedString(StringRef str)=0
Write a string to the bytecode, which is owned by the caller and is guaranteed to not die before the ...
void writeTypes(ArrayRef< T > types)
This class provides support for representing a failure result, or a valid value of type T.
This class represents a diagnostic that is inflight and set to be reported.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
The base class used for all derived interface types.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.