MLIR 24.0.0git
BytecodeImplementation.h
Go to the documentation of this file.
1//===- BytecodeImplementation.h - MLIR Bytecode Implementation --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This header defines various interfaces and utilities necessary for dialects
10// to hook into bytecode serialization.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
15#define MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
16
17#include "mlir/IR/Attributes.h"
18#include "mlir/IR/Diagnostics.h"
19#include "mlir/IR/Dialect.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Twine.h"
23
24namespace mlir {
25//===--------------------------------------------------------------------===//
26// Dialect Version Interface.
27//===--------------------------------------------------------------------===//
28
29/// This class is used to represent the version of a dialect, for the purpose
30/// of polymorphic destruction.
32public:
33 virtual ~DialectVersion() = default;
34};
35
36//===----------------------------------------------------------------------===//
37// DialectBytecodeReader
38//===----------------------------------------------------------------------===//
39
40/// This class defines a virtual interface for reading a bytecode stream,
41/// providing hooks into the bytecode reader. As such, this class should only be
42/// derived and defined by the main bytecode reader, users (i.e. dialects)
43/// should generally only interact with this class via the
44/// BytecodeDialectInterface below.
46public:
47 virtual ~DialectBytecodeReader() = default;
48
49 /// Emit an error to the reader.
50 virtual InFlightDiagnostic emitError(const Twine &msg = {}) const = 0;
51
52 /// Emit a warning to the reader.
53 virtual InFlightDiagnostic emitWarning(const Twine &msg = {}) const = 0;
54
55 /// Retrieve the dialect version by name if available.
56 virtual FailureOr<const DialectVersion *>
57 getDialectVersion(StringRef dialectName) const = 0;
58 template <class T>
59 FailureOr<const DialectVersion *> getDialectVersion() const {
60 return getDialectVersion(T::getDialectNamespace());
61 }
62
63 /// Retrieve the context associated to the reader.
64 virtual MLIRContext *getContext() const = 0;
65
66 /// Return the bytecode version being read.
67 virtual uint64_t getBytecodeVersion() const = 0;
68
69 /// Read out a list of elements, invoking the provided callback for each
70 /// element. The callback function may be in any of the following forms:
71 /// * LogicalResult(T &)
72 /// * FailureOr<T>()
73 template <typename T, typename CallbackFn>
74 LogicalResult readList(SmallVectorImpl<T> &result, CallbackFn &&callback) {
75 uint64_t size;
76 if (failed(readVarInt(size)))
77 return failure();
78 return readListWithKnownSize(result, size,
79 std::forward<CallbackFn>(callback));
80 }
81
82 /// Read out a list of elements with a known size, invoking the provided
83 /// callback for each element. Unlike readList, this does not read a length
84 /// prefix. The callback function may be in any of the following forms:
85 /// * LogicalResult(T &)
86 /// * FailureOr<T>()
87 template <typename T, typename CallbackFn>
88 LogicalResult readListWithKnownSize(SmallVectorImpl<T> &result, uint64_t size,
89 CallbackFn &&callback) {
90 result.reserve(size);
91
92 for (uint64_t i = 0; i < size; ++i) {
93 // Check if the callback uses FailureOr, or populates the result by
94 // reference.
95 if constexpr (llvm::function_traits<std::decay_t<CallbackFn>>::num_args) {
96 T element = {};
97 if (failed(callback(element)))
98 return failure();
99 result.emplace_back(std::move(element));
100 } else {
101 FailureOr<T> element = callback();
102 if (failed(element))
103 return failure();
104 result.emplace_back(std::move(*element));
105 }
106 }
107 return success();
108 }
109
110 //===--------------------------------------------------------------------===//
111 // IR
112 //===--------------------------------------------------------------------===//
113
114 /// Read a reference to the given attribute.
115 virtual LogicalResult readAttribute(Attribute &result) = 0;
116 /// Read an optional reference to the given attribute. Returns success even if
117 /// the Attribute isn't present.
118 virtual LogicalResult readOptionalAttribute(Attribute &attr) = 0;
119
120 template <typename T>
121 LogicalResult readAttributes(SmallVectorImpl<T> &attrs) {
122 return readList(attrs, [this](T &attr) { return readAttribute(attr); });
123 }
124 template <typename T>
125 LogicalResult readAttribute(T &result) {
126 Attribute baseResult;
127 if (failed(readAttribute(baseResult)))
128 return failure();
129 if ((result = dyn_cast<T>(baseResult)))
130 return success();
131 return emitError() << "expected " << llvm::getTypeName<T>()
132 << ", but got: " << baseResult;
133 }
134 template <typename T>
135 LogicalResult readOptionalAttribute(T &result) {
136 Attribute baseResult;
137 if (failed(readOptionalAttribute(baseResult)))
138 return failure();
139 if (!baseResult)
140 return success();
141 if ((result = dyn_cast<T>(baseResult)))
142 return success();
143 return emitError() << "expected " << llvm::getTypeName<T>()
144 << ", but got: " << baseResult;
145 }
146
147 /// Publish `type` as the result of the entry currently being read, before
148 /// that entry has finished reading its own body.
149 ///
150 /// This is the bytecode analogue of AsmParser::tryStartCyclicParse, and it
151 /// exists for the same reason: a mutable type may refer to itself, directly
152 /// or through a pointer, so the body cannot be read until the type it
153 /// belongs to already exists. A dialect creates the incomplete type, calls
154 /// this, reads the body (a nested reference back to this same entry then
155 /// resolves to the incomplete type rather than re-entering it), and then
156 /// completes it. Unlike tryStartCyclicParse, which returns a guard and
157 /// fails a nested parse of the same type, this publishes the type so
158 /// nested reads succeed with the incomplete instance.
159 ///
160 /// Contract for the dialect: call this before reading any nested entry
161 /// that may refer back, and return the published instance as the read
162 /// result. The read may run more than once (deferred entries are retried),
163 /// so the type must be uniqued by key and completing it idempotent;
164 /// publishing a different instance for the same entry fails. There is no
165 /// attribute analogue yet; mutable attributes keep the fallback encoding.
166 ///
167 /// Emits an error and returns failure if the reader is not inside a custom
168 /// type entry, `type` is null or immutable, or a different instance was
169 /// already published for the entry.
170 virtual LogicalResult tryStartCyclicRead(Type type) = 0;
171
172 /// Read a reference to the given type.
173 virtual LogicalResult readType(Type &result) = 0;
174 template <typename T>
175 LogicalResult readTypes(SmallVectorImpl<T> &types) {
176 return readList(types, [this](T &type) { return readType(type); });
177 }
178 template <typename T>
179 LogicalResult readType(T &result) {
180 Type baseResult;
181 if (failed(readType(baseResult)))
182 return failure();
183 if ((result = dyn_cast<T>(baseResult)))
184 return success();
185 return emitError() << "expected " << llvm::getTypeName<T>()
186 << ", but got: " << baseResult;
187 }
188
189 /// Read a handle to a dialect resource.
190 template <typename ResourceT>
191 FailureOr<ResourceT> readResourceHandle() {
192 FailureOr<AsmDialectResourceHandle> handle = readResourceHandle();
193 if (failed(handle))
194 return failure();
195 if (auto *result = dyn_cast<ResourceT>(&*handle))
196 return std::move(*result);
197 return emitError() << "provided resource handle differs from the "
198 "expected resource type";
199 }
200
201 //===--------------------------------------------------------------------===//
202 // Primitives
203 //===--------------------------------------------------------------------===//
204
205 /// Read a variable width integer.
206 virtual LogicalResult readVarInt(uint64_t &result) = 0;
207
208 /// Read a signed variable width integer.
209 virtual LogicalResult readSignedVarInt(int64_t &result) = 0;
211 return readList(result,
212 [this](int64_t &value) { return readSignedVarInt(value); });
213 }
214
215 /// Parse a variable length encoded integer whose low bit is used to encode an
216 /// unrelated flag, i.e: `(integerValue << 1) | (flag ? 1 : 0)`.
217 LogicalResult readVarIntWithFlag(uint64_t &result, bool &flag) {
218 if (failed(readVarInt(result)))
219 return failure();
220 flag = result & 1;
221 result >>= 1;
222 return success();
223 }
224
225 /// Read a "small" sparse array of integer <= 32 bits elements, where
226 /// index/value pairs can be compressed when the array is small.
227 /// Note that only some position of the array will be read and the ones
228 /// not stored in the bytecode are gonne be left untouched.
229 /// If the provided array is too small for the stored indices, an error
230 /// will be returned.
231 template <typename T>
232 LogicalResult readSparseArray(MutableArrayRef<T> array) {
233 static_assert(sizeof(T) < sizeof(uint64_t), "expect integer < 64 bits");
234 static_assert(std::is_integral<T>::value, "expects integer");
235 uint64_t nonZeroesCount;
236 bool useSparseEncoding;
237 if (failed(readVarIntWithFlag(nonZeroesCount, useSparseEncoding)))
238 return failure();
239 if (nonZeroesCount == 0)
240 return success();
241 if (!useSparseEncoding) {
242 // This is a simple dense array.
243 if (nonZeroesCount > array.size()) {
244 emitError("trying to read an array of ")
245 << nonZeroesCount << " but only " << array.size()
246 << " storage available.";
247 return failure();
248 }
249 for (int64_t index : llvm::seq<int64_t>(0, nonZeroesCount)) {
250 uint64_t value;
251 if (failed(readVarInt(value)))
252 return failure();
253 array[index] = value;
254 }
255 return success();
256 }
257 // Read sparse encoding
258 // This is the number of bits used for packing the index with the value.
259 uint64_t indexBitSize;
260 if (failed(readVarInt(indexBitSize)))
261 return failure();
262 constexpr uint64_t maxIndexBitSize = 8;
263 if (indexBitSize > maxIndexBitSize) {
264 emitError("reading sparse array with indexing above 8 bits: ")
265 << indexBitSize;
266 return failure();
267 }
268 for (uint32_t count : llvm::seq<uint32_t>(0, nonZeroesCount)) {
269 (void)count;
270 uint64_t indexValuePair;
271 if (failed(readVarInt(indexValuePair)))
272 return failure();
273 uint64_t index = indexValuePair & ~(uint64_t(-1) << (indexBitSize));
274 uint64_t value = indexValuePair >> indexBitSize;
275 if (index >= array.size()) {
276 emitError("reading a sparse array found index ")
277 << index << " but only " << array.size() << " storage available.";
278 return failure();
279 }
280 array[index] = value;
281 }
282 return success();
283 }
284
285 /// Read an APInt that is known to have been encoded with the given width.
286 virtual FailureOr<APInt> readAPIntWithKnownWidth(unsigned bitWidth) = 0;
287
288 /// Read an APFloat that is known to have been encoded with the given
289 /// semantics.
290 virtual FailureOr<APFloat>
291 readAPFloatWithKnownSemantics(const llvm::fltSemantics &semantics) = 0;
292
293 /// Read a string from the bytecode.
294 virtual LogicalResult readString(StringRef &result) = 0;
295
296 /// Read a blob from the bytecode.
297 virtual LogicalResult readBlob(ArrayRef<char> &result) = 0;
298
299 /// Read a bool from the bytecode.
300 virtual LogicalResult readBool(bool &result) = 0;
301
302private:
303 /// Read a handle to a dialect resource.
304 virtual FailureOr<AsmDialectResourceHandle> readResourceHandle() = 0;
305};
306
307//===----------------------------------------------------------------------===//
308// DialectBytecodeWriter
309//===----------------------------------------------------------------------===//
310
311/// This class defines a virtual interface for writing to a bytecode stream,
312/// providing hooks into the bytecode writer. As such, this class should only be
313/// derived and defined by the main bytecode writer, users (i.e. dialects)
314/// should generally only interact with this class via the
315/// BytecodeDialectInterface below.
317public:
318 virtual ~DialectBytecodeWriter() = default;
319
320 //===--------------------------------------------------------------------===//
321 // IR
322 //===--------------------------------------------------------------------===//
323
324 /// Write out a list of elements, invoking the provided callback for each
325 /// element.
326 template <typename RangeT, typename CallbackFn>
327 void writeList(RangeT &&range, CallbackFn &&callback) {
328 writeVarInt(llvm::size(range));
329 for (auto &element : range)
330 callback(element);
331 }
332
333 /// Write out a list of elements without a length prefix, for cases where the
334 /// size is known from another field.
335 template <typename RangeT, typename CallbackFn>
336 void writeListWithKnownSize(RangeT &&range, CallbackFn &&callback) {
337 for (auto &element : range)
338 callback(element);
339 }
340
341 /// Write a reference to the given attribute.
342 virtual void writeAttribute(Attribute attr) = 0;
343 virtual void writeOptionalAttribute(Attribute attr) = 0;
344 template <typename T>
346 writeList(attrs, [this](T attr) { writeAttribute(attr); });
347 }
348
349 /// Write a reference to the given type.
350 virtual void writeType(Type type) = 0;
351 template <typename T>
353 writeList(types, [this](T type) { writeType(type); });
354 }
355
356 /// Write the given handle to a dialect resource.
357 virtual void
359
360 //===--------------------------------------------------------------------===//
361 // Primitives
362 //===--------------------------------------------------------------------===//
363
364 /// Write a variable width integer to the output stream. This should be the
365 /// preferred method for emitting integers whenever possible.
366 virtual void writeVarInt(uint64_t value) = 0;
367
368 /// Write a signed variable width integer to the output stream. This should be
369 /// the preferred method for emitting signed integers whenever possible.
370 virtual void writeSignedVarInt(int64_t value) = 0;
372 writeList(value, [this](int64_t value) { writeSignedVarInt(value); });
373 }
374
375 /// Write a VarInt and a flag packed together.
376 void writeVarIntWithFlag(uint64_t value, bool flag) {
377 writeVarInt((value << 1) | (flag ? 1 : 0));
378 }
379
380 /// Write out a "small" sparse array of integer <= 32 bits elements, where
381 /// index/value pairs can be compressed when the array is small. This method
382 /// will scan the array multiple times and should not be used for large
383 /// arrays. The optional provided "zero" can be used to adjust for the
384 /// expected repeated value. We assume here that the array size fits in a 32
385 /// bits integer.
386 template <typename T>
388 static_assert(sizeof(T) < sizeof(uint64_t), "expect integer < 64 bits");
389 static_assert(std::is_integral<T>::value, "expects integer");
390 uint32_t size = array.size();
391 uint32_t nonZeroesCount = 0, lastIndex = 0;
392 for (uint32_t index : llvm::seq<uint32_t>(0, size)) {
393 if (!array[index])
394 continue;
395 nonZeroesCount++;
396 lastIndex = index;
397 }
398 // If the last position is too large, or the array isn't at least 50%
399 // sparse, emit it with a dense encoding.
400 if (lastIndex > 256 || nonZeroesCount > size / 2) {
401 // Emit the array size and a flag which indicates whether it is sparse.
402 writeVarIntWithFlag(size, false);
403 for (const T &elt : array)
404 writeVarInt(elt);
405 return;
406 }
407 // Emit sparse: first the number of elements we'll write and a flag
408 // indicating it is a sparse encoding.
409 writeVarIntWithFlag(nonZeroesCount, true);
410 if (nonZeroesCount == 0)
411 return;
412 // This is the number of bits used for packing the index with the value.
413 int indexBitSize = llvm::Log2_32_Ceil(lastIndex + 1);
414 writeVarInt(indexBitSize);
415 for (uint32_t index : llvm::seq<uint32_t>(0, lastIndex + 1)) {
416 T value = array[index];
417 if (!value)
418 continue;
419 uint64_t indexValuePair = (value << indexBitSize) | (index);
420 writeVarInt(indexValuePair);
421 }
422 }
423
424 /// Write an APInt to the bytecode stream whose bitwidth will be known
425 /// externally at read time. This method is useful for encoding APInt values
426 /// when the width is known via external means, such as via a type. This
427 /// method should generally only be invoked if you need an APInt, otherwise
428 /// use the varint methods above. APInt values are generally encoded using
429 /// zigzag encoding, to enable more efficient encodings for negative values.
430 virtual void writeAPIntWithKnownWidth(const APInt &value) = 0;
431
432 /// Write an APFloat to the bytecode stream whose semantics will be known
433 /// externally at read time. This method is useful for encoding APFloat values
434 /// when the semantics are known via external means, such as via a type.
435 virtual void writeAPFloatWithKnownSemantics(const APFloat &value) = 0;
436
437 /// Write a string to the bytecode, which is owned by the caller and is
438 /// guaranteed to not die before the end of the bytecode process. This should
439 /// only be called if such a guarantee can be made, such as when the string is
440 /// owned by an attribute or type.
441 virtual void writeOwnedString(StringRef str) = 0;
442
443 /// Write a blob to the bytecode, which is owned by the caller and is
444 /// guaranteed to not die before the end of the bytecode process. The blob is
445 /// written as-is, with no additional compression or compaction.
446 virtual void writeOwnedBlob(ArrayRef<char> blob) = 0;
447
448 /// Write a blob to the bytecode, which is not owned by the caller. The blob
449 /// is copied into the bytecode, and need not strictly outlive the call.
450 virtual void writeUnownedBlob(ArrayRef<char> blob) = 0;
451
452 /// Write a bool to the output stream.
453 virtual void writeOwnedBool(bool value) = 0;
454
455 /// Return the bytecode version being emitted for.
456 virtual int64_t getBytecodeVersion() const = 0;
457
458 /// Retrieve the dialect version by name if available.
459 virtual FailureOr<const DialectVersion *>
460 getDialectVersion(StringRef dialectName) const = 0;
461
462 template <class T>
463 FailureOr<const DialectVersion *> getDialectVersion() const {
464 return getDialectVersion(T::getDialectNamespace());
465 }
466};
467
468/// Helper for resource handle reading that returns LogicalResult.
469template <typename T, typename... Ts>
471 FailureOr<T> &value, Ts &&...params) {
472 FailureOr<T> handle = reader.readResourceHandle<T>();
473 if (failed(handle))
474 return failure();
475 if (auto *result = dyn_cast<T>(&*handle)) {
476 value = std::move(*result);
477 return success();
478 }
479 return failure();
480}
481
482/// Helper method that injects context only if needed, this helps unify some of
483/// the attribute construction methods.
484template <typename T, typename... Ts>
485auto get(MLIRContext *context, Ts &&...params) {
486 // Prefer a direct `get` method if one exists.
487 if constexpr (llvm::is_detected<detail::has_get_method, T, Ts...>::value) {
488 (void)context;
489 return T::get(std::forward<Ts>(params)...);
490 } else if constexpr (llvm::is_detected<detail::has_get_method, T,
491 MLIRContext *, Ts...>::value) {
492 return T::get(context, std::forward<Ts>(params)...);
493 } else {
494 // Otherwise, pass to the base get.
495 return T::Base::get(context, std::forward<Ts>(params)...);
496 }
497}
498
499namespace detail {
500template <typename T, typename... Ts>
501using has_get_checked_method = decltype(T::getChecked(std::declval<Ts>()...));
502} // namespace detail
503
504/// Helper method analogous to `get`, but uses `getChecked` when available to
505/// allow graceful failure on invalid parameters instead of asserting.
506///
507/// Only the no-context form of `getChecked` is tried here. Types that expose
508/// `getChecked(emitError, params...)` without a leading `MLIRContext*` (e.g.
509/// MemRefType, VectorType, RankedTensorType) will use it for graceful failure.
510/// Everything else falls back to `get<T>()`. We intentionally do NOT try
511/// `T::getChecked(emitError, context, params...)`: for types that only inherit
512/// the base `StorageUserBase::getChecked` template (e.g. ArrayAttr), that
513/// template instantiation requires a complete storage type which may not be
514/// available in the bytecode reading TU.
515template <typename T, typename... Ts>
517 MLIRContext *context, Ts &&...params) {
518 if constexpr (llvm::is_detected<detail::has_get_checked_method, T,
520 Ts...>::value) {
521 (void)context;
522 return T::getChecked(emitError, std::forward<Ts>(params)...);
523 } else {
524 // Fall back to get() for types that don't define a no-context getChecked.
525 return get<T>(context, std::forward<Ts>(params)...);
526 }
527}
528
529} // namespace mlir
530
531#include "mlir/Bytecode/BytecodeDialectInterface.h.inc"
532
533#endif // MLIR_BYTECODE_BYTECODEIMPLEMENTATION_H
return success()
This class represents an opaque handle to a dialect resource entry.
Attributes are known-constant values of operations.
Definition Attributes.h:25
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 LogicalResult tryStartCyclicRead(Type type)=0
Publish type as the result of the entry currently being read, before that entry has finished reading ...
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.
Definition MLIRContext.h:71
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
decltype(T::get(std::declval< Ts >()...)) has_get_method
decltype(T::getChecked(std::declval< Ts >()...)) has_get_checked_method
Include the generated interface declarations.
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
Definition LLVM.h:155
LogicalResult readResourceHandle(DialectBytecodeReader &reader, FailureOr< T > &value, Ts &&...params)
Helper for resource handle reading that returns LogicalResult.