MLIR 23.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 "llvm/ADT/TypeSwitch.h"
18#include <type_traits>
19
20using namespace mlir;
21using namespace mlir::LLVM;
22
23namespace {
24
25//===--------------------------------------------------------------------===//
26// Optional ArrayRefs
27//
28// Note that both the writer and reader functions consider attributes to be
29// optional. This is because the attribute may be present or empty.
30//===--------------------------------------------------------------------===//
31
32template <class EntryTy>
33static void writeOptionalArrayRef(DialectBytecodeWriter &writer,
34 ArrayRef<EntryTy> storage) {
35 if (storage.empty()) {
36 writer.writeOwnedBool(false);
37 return;
38 }
39
40 writer.writeOwnedBool(true);
41 writer.writeList(storage, [&](EntryTy val) {
42 if constexpr (std::is_base_of_v<Attribute, EntryTy>) {
43 (void)writer.writeOptionalAttribute(val);
44 } else if constexpr (std::is_integral_v<EntryTy>) {
45 (void)writer.writeVarInt(val);
46 } else {
47 static_assert(true, "EntryTy not supported");
48 }
49 });
50}
51
52template <class EntryTy>
53static LogicalResult readOptionalArrayRef(DialectBytecodeReader &reader,
54 SmallVectorImpl<EntryTy> &storage) {
55 bool isPresent = false;
56 if (failed(reader.readBool(isPresent)))
57 return failure();
58 // Nothing to do here, the array is empty.
59 if (!isPresent)
60 return success();
61
62 auto readEntry = [&]() -> FailureOr<EntryTy> {
63 EntryTy temp;
64 if constexpr (std::is_base_of_v<Attribute, EntryTy>) {
65 if (succeeded(reader.readOptionalAttribute(temp)))
66 return temp;
67 } else if constexpr (std::is_integral_v<EntryTy>) {
68 if (succeeded(reader.readVarInt(temp)))
69 return temp;
70 } else {
71 static_assert(true, "EntryTy not supported");
72 }
73 return failure();
74 };
75
76 return reader.readList(storage, readEntry);
77}
78
79//===--------------------------------------------------------------------===//
80// Optional integral types
81//===--------------------------------------------------------------------===//
82
83template <class EntryTy>
84static void writeOptionalInt(DialectBytecodeWriter &writer,
85 std::optional<EntryTy> storage) {
86 static_assert(std::is_integral_v<EntryTy>,
87 "EntryTy must be an integral type");
88 EntryTy val = storage.value_or(0);
89 writer.writeVarIntWithFlag(val, storage.has_value());
90}
91
92template <class EntryTy>
93static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
94 std::optional<EntryTy> &storage) {
95 static_assert(std::is_integral_v<EntryTy>,
96 "EntryTy must be an integral type");
97 uint64_t result = 0;
98 bool flag = false;
99 if (failed(reader.readVarIntWithFlag(result, flag)))
100 return failure();
101 if (flag)
102 storage = static_cast<EntryTy>(result);
103 else
104 storage = std::nullopt;
105 return success();
106}
107
108//===--------------------------------------------------------------------===//
109// Tablegen generated bytecode functions
110//===--------------------------------------------------------------------===//
111
112#include "mlir/Dialect/LLVMIR/LLVMDialectBytecode.cpp.inc"
113
114//===--------------------------------------------------------------------===//
115// LLVMDialectBytecodeInterface
116//===--------------------------------------------------------------------===//
117
118/// This class implements the bytecode interface for the LLVM dialect.
119struct LLVMDialectBytecodeInterface : public BytecodeDialectInterface {
120 LLVMDialectBytecodeInterface(Dialect *dialect)
121 : BytecodeDialectInterface(dialect) {}
122
123 // Attributes
124 Attribute readAttribute(DialectBytecodeReader &reader) const override {
125 return ::readAttribute(getContext(), reader);
126 }
127
128 LogicalResult writeAttribute(Attribute attr,
129 DialectBytecodeWriter &writer) const override {
130 return ::writeAttribute(attr, writer);
131 }
132
133 // Types
134 Type readType(DialectBytecodeReader &reader) const override {
135 return ::readType(getContext(), reader);
136 }
137
138 LogicalResult writeType(Type type,
139 DialectBytecodeWriter &writer) const override {
140 return ::writeType(type, writer);
141 }
142};
143} // namespace
144
145void LLVM::detail::addBytecodeInterface(LLVMDialect *dialect) {
146 dialect->addInterfaces<LLVMDialectBytecodeInterface>();
147}
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:717
Include the generated interface declarations.