MLIR 24.0.0git
LLVMDialectBytecode.cpp
Go to the documentation of this file.
1//===- LLVMDialectBytecode.cpp - LLVM Bytecode Implementation -------------===//
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
14#include "mlir/IR/Diagnostics.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/SmallVector.h"
17#include <type_traits>
18
19using namespace mlir;
20using namespace mlir::LLVM;
21
22namespace {
23
24//===--------------------------------------------------------------------===//
25// Optional ArrayRefs
26//
27// Note that both the writer and reader functions consider attributes to be
28// optional. This is because the attribute may be present or empty.
29//===--------------------------------------------------------------------===//
30
31template <class EntryTy>
32static void writeOptionalArrayRef(DialectBytecodeWriter &writer,
33 ArrayRef<EntryTy> storage) {
34 if (storage.empty()) {
35 writer.writeOwnedBool(false);
36 return;
37 }
38
39 writer.writeOwnedBool(true);
40 writer.writeList(storage, [&](EntryTy val) {
41 if constexpr (std::is_base_of_v<Attribute, EntryTy>) {
42 (void)writer.writeOptionalAttribute(val);
43 } else if constexpr (std::is_integral_v<EntryTy>) {
44 (void)writer.writeVarInt(val);
45 } else {
46 static_assert(true, "EntryTy not supported");
47 }
48 });
49}
50
51template <class EntryTy>
52static LogicalResult readOptionalArrayRef(DialectBytecodeReader &reader,
53 SmallVectorImpl<EntryTy> &storage) {
54 bool isPresent = false;
55 if (failed(reader.readBool(isPresent)))
56 return failure();
57 // Nothing to do here, the array is empty.
58 if (!isPresent)
59 return success();
60
61 auto readEntry = [&]() -> FailureOr<EntryTy> {
62 EntryTy temp;
63 if constexpr (std::is_base_of_v<Attribute, EntryTy>) {
64 if (succeeded(reader.readOptionalAttribute(temp)))
65 return temp;
66 } else if constexpr (std::is_integral_v<EntryTy>) {
67 if (succeeded(reader.readVarInt(temp)))
68 return temp;
69 } else {
70 static_assert(true, "EntryTy not supported");
71 }
72 return failure();
73 };
74
75 return reader.readList(storage, readEntry);
76}
77
78//===--------------------------------------------------------------------===//
79// Optional integral types
80//===--------------------------------------------------------------------===//
81
82template <class EntryTy>
83static void writeOptionalInt(DialectBytecodeWriter &writer,
84 std::optional<EntryTy> storage) {
85 static_assert(std::is_integral_v<EntryTy>,
86 "EntryTy must be an integral type");
87 EntryTy val = storage.value_or(0);
88 writer.writeVarIntWithFlag(val, storage.has_value());
89}
90
91template <class EntryTy>
92static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
93 std::optional<EntryTy> &storage) {
94 static_assert(std::is_integral_v<EntryTy>,
95 "EntryTy must be an integral type");
96 uint64_t result = 0;
97 bool flag = false;
98 if (failed(reader.readVarIntWithFlag(result, flag)))
99 return failure();
100 if (flag)
101 storage = static_cast<EntryTy>(result);
102 else
103 storage = std::nullopt;
104 return success();
105}
106} // namespace
107
108//===--------------------------------------------------------------------===//
109// Tablegen generated bytecode functions
110//===--------------------------------------------------------------------===//
111
112#include "mlir/Dialect/LLVMIR/LLVMDialectBytecode.cpp.inc"
113
114namespace {
115//===--------------------------------------------------------------------===//
116// LLVMDialectBytecodeInterface
117//===--------------------------------------------------------------------===//
118
119/// This class implements the bytecode interface for the LLVM dialect.
120struct LLVMDialectBytecodeInterface : public BytecodeDialectInterface {
121 LLVMDialectBytecodeInterface(Dialect *dialect)
122 : BytecodeDialectInterface(dialect) {}
123
124 // Attributes
125 Attribute readAttribute(DialectBytecodeReader &reader) const override {
126 return ::readAttribute(getContext(), reader);
127 }
128
129 LogicalResult writeAttribute(Attribute attr,
130 DialectBytecodeWriter &writer) const override {
131 return ::writeAttribute(attr, writer);
132 }
133
134 // Types
135 Type readType(DialectBytecodeReader &reader) const override {
136 return ::readType(getContext(), reader);
137 }
138
139 LogicalResult writeType(Type type,
140 DialectBytecodeWriter &writer) const override {
141 return ::writeType(type, writer);
142 }
143};
144} // namespace
145
146void LLVM::detail::addBytecodeInterface(LLVMDialect *dialect) {
147 dialect->addInterfaces<LLVMDialectBytecodeInterface>();
148}
return success()
b getContext())
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
virtual LogicalResult readBool(bool &result)=0
Read a bool from the bytecode.
virtual LogicalResult readVarInt(uint64_t &result)=0
Read a variable width integer.
LogicalResult readVarIntWithFlag(uint64_t &result, bool &flag)
Parse a variable length encoded integer whose low bit is used to encode an unrelated flag,...
virtual LogicalResult readOptionalAttribute(Attribute &attr)=0
Read an optional reference to the given attribute.
LogicalResult readList(SmallVectorImpl< T > &result, CallbackFn &&callback)
Read out a list of elements, invoking the provided callback for each element.
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
virtual void writeOptionalAttribute(Attribute attr)=0
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.
virtual void writeOwnedBool(bool value)=0
Write a bool to the output stream.
void addBytecodeInterface(LLVMDialect *dialect)
Add the interfaces necessary for encoding the LLVM dialect components in bytecode.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:853
Include the generated interface declarations.