14#ifndef MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
15#define MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/Twine.h"
57 virtual FailureOr<const DialectVersion *>
74 template <
typename T,
typename CallbackFn>
80 std::forward<CallbackFn>(callback));
88 template <
typename T,
typename CallbackFn>
90 CallbackFn &&callback) {
93 for (uint64_t i = 0; i < size; ++i) {
96 if constexpr (llvm::function_traits<std::decay_t<CallbackFn>>::num_args) {
98 if (failed(callback(element)))
100 result.emplace_back(std::move(element));
102 FailureOr<T> element = callback();
105 result.emplace_back(std::move(*element));
121 template <
typename T>
125 template <
typename T>
130 if ((
result = dyn_cast<T>(baseResult)))
132 return emitError() <<
"expected " << llvm::getTypeName<T>()
133 <<
", but got: " << baseResult;
135 template <
typename T>
142 if ((
result = dyn_cast<T>(baseResult)))
144 return emitError() <<
"expected " << llvm::getTypeName<T>()
145 <<
", but got: " << baseResult;
150 template <
typename T>
154 template <
typename T>
159 if ((
result = dyn_cast<T>(baseResult)))
161 return emitError() <<
"expected " << llvm::getTypeName<T>()
162 <<
", but got: " << baseResult;
166 template <
typename ResourceT>
171 if (
auto *
result = dyn_cast<ResourceT>(&*handle))
172 return std::move(*
result);
173 return emitError() <<
"provided resource handle differs from the "
174 "expected resource type";
207 template <
typename T>
209 static_assert(
sizeof(T) <
sizeof(uint64_t),
"expect integer < 64 bits");
210 static_assert(std::is_integral<T>::value,
"expects integer");
211 uint64_t nonZeroesCount;
212 bool useSparseEncoding;
215 if (nonZeroesCount == 0)
217 if (!useSparseEncoding) {
219 if (nonZeroesCount > array.size()) {
221 << nonZeroesCount <<
" but only " << array.size()
222 <<
" storage available.";
225 for (
int64_t index : llvm::seq<int64_t>(0, nonZeroesCount)) {
229 array[
index] = value;
235 uint64_t indexBitSize;
238 constexpr uint64_t maxIndexBitSize = 8;
239 if (indexBitSize > maxIndexBitSize) {
240 emitError(
"reading sparse array with indexing above 8 bits: ")
244 for (uint32_t count : llvm::seq<uint32_t>(0, nonZeroesCount)) {
246 uint64_t indexValuePair;
249 uint64_t
index = indexValuePair & ~(uint64_t(-1) << (indexBitSize));
250 uint64_t value = indexValuePair >> indexBitSize;
251 if (
index >= array.size()) {
252 emitError(
"reading a sparse array found index ")
253 <<
index <<
" but only " << array.size() <<
" storage available.";
256 array[
index] = value;
266 virtual FailureOr<APFloat>
302 template <
typename RangeT,
typename CallbackFn>
305 for (
auto &element : range)
311 template <
typename RangeT,
typename CallbackFn>
313 for (
auto &element : range)
320 template <
typename T>
327 template <
typename T>
362 template <
typename T>
364 static_assert(
sizeof(T) <
sizeof(uint64_t),
"expect integer < 64 bits");
365 static_assert(std::is_integral<T>::value,
"expects integer");
366 uint32_t size = array.size();
367 uint32_t nonZeroesCount = 0, lastIndex = 0;
368 for (uint32_t
index : llvm::seq<uint32_t>(0, size)) {
376 if (lastIndex > 256 || nonZeroesCount > size / 2) {
379 for (
const T &elt : array)
386 if (nonZeroesCount == 0)
389 int indexBitSize = llvm::Log2_32_Ceil(lastIndex + 1);
391 for (uint32_t
index : llvm::seq<uint32_t>(0, lastIndex + 1)) {
392 T value = array[
index];
395 uint64_t indexValuePair = (value << indexBitSize) | (
index);
435 virtual FailureOr<const DialectVersion *>
445template <
typename T,
typename... Ts>
447 FailureOr<T> &value, Ts &&...params) {
451 if (
auto *
result = dyn_cast<T>(&*handle)) {
452 value = std::move(*
result);
460template <
typename T,
typename... Ts>
465 return T::get(std::forward<Ts>(params)...);
468 return T::get(context, std::forward<Ts>(params)...);
471 return T::Base::get(context, std::forward<Ts>(params)...);
476template <
typename T,
typename... Ts>
491template <
typename T,
typename... Ts>
498 return T::getChecked(
emitError, std::forward<Ts>(params)...);
501 return get<T>(context, std::forward<Ts>(params)...);
507#include "mlir/Bytecode/BytecodeDialectInterface.h.inc"
This class represents an opaque handle to a dialect resource entry.
Attributes are known-constant values of operations.
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)
virtual MLIRContext * getContext() const =0
Retrieve the context associated to the reader.
LogicalResult readTypes(SmallVectorImpl< T > &types)
virtual LogicalResult readBool(bool &result)=0
Read a bool from the bytecode.
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.
virtual FailureOr< const DialectVersion * > getDialectVersion(StringRef dialectName) const =0
Retrieve the dialect version by name if available.
virtual uint64_t getBytecodeVersion() const =0
Return the bytecode version being read.
LogicalResult readType(T &result)
LogicalResult readVarIntWithFlag(uint64_t &result, bool &flag)
Parse a variable length encoded integer whose low bit is used to encode an unrelated flag,...
virtual FailureOr< APInt > readAPIntWithKnownWidth(unsigned bitWidth)=0
Read an APInt that is known to have been encoded with the given width.
LogicalResult readSignedVarInts(SmallVectorImpl< int64_t > &result)
LogicalResult readOptionalAttribute(T &result)
virtual InFlightDiagnostic emitWarning(const Twine &msg={}) const =0
Emit a warning to the reader.
virtual LogicalResult readOptionalAttribute(Attribute &attr)=0
Read an optional reference to the given attribute.
LogicalResult readAttribute(T &result)
virtual InFlightDiagnostic emitError(const Twine &msg={}) const =0
Emit an error to the reader.
LogicalResult readSparseArray(MutableArrayRef< T > array)
Read a "small" sparse array of integer <= 32 bits elements, where index/value pairs can be compressed...
FailureOr< ResourceT > readResourceHandle()
Read a handle to a dialect resource.
LogicalResult readListWithKnownSize(SmallVectorImpl< T > &result, uint64_t size, CallbackFn &&callback)
Read out a list of elements with a known size, invoking the provided callback for each element.
virtual LogicalResult readString(StringRef &result)=0
Read a string from the bytecode.
FailureOr< const DialectVersion * > getDialectVersion() const
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.
virtual FailureOr< APFloat > readAPFloatWithKnownSemantics(const llvm::fltSemantics &semantics)=0
Read an APFloat that is known to have been encoded with the given semantics.
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
virtual FailureOr< const DialectVersion * > getDialectVersion(StringRef dialectName) const =0
Retrieve the dialect version by name if available.
virtual void writeOptionalAttribute(Attribute attr)=0
FailureOr< const DialectVersion * > getDialectVersion() const
virtual void writeVarInt(uint64_t value)=0
Write a variable width integer to the output stream.
void writeVarIntWithFlag(uint64_t value, bool flag)
Write a VarInt and a flag packed together.
void writeList(RangeT &&range, CallbackFn &&callback)
Write out a list of elements, invoking the provided callback for each element.
void writeSparseArray(ArrayRef< T > array)
Write out a "small" sparse array of integer <= 32 bits elements, where index/value pairs can be compr...
virtual void writeType(Type type)=0
Write a reference to the given type.
virtual void writeUnownedBlob(ArrayRef< char > blob)=0
Write a blob to the bytecode, which is not owned by the caller.
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 writeOwnedBool(bool value)=0
Write a bool to the output stream.
virtual int64_t getBytecodeVersion() const =0
Return the bytecode version being emitted for.
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 writeListWithKnownSize(RangeT &&range, CallbackFn &&callback)
Write out a list of elements without a length prefix, for cases where the size is known from another ...
void writeTypes(ArrayRef< T > types)
This class is used to represent the version of a dialect, for the purpose of polymorphic destruction.
virtual ~DialectVersion()=default
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.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
decltype(T::get(std::declval< Ts >()...)) has_get_method
decltype(T::getChecked(std::declval< Ts >()...)) has_get_checked_method
Include the generated interface declarations.
static LogicalResult readResourceHandle(DialectBytecodeReader &reader, FailureOr< T > &value, Ts &&...params)
Helper for resource handle reading that returns LogicalResult.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto getChecked(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, Ts &&...params)
Helper method analogous to get, but uses getChecked when available to allow graceful failure on inval...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
llvm::function_ref< Fn > function_ref