MLIR 23.0.0git
BuiltinDialectBytecode.cpp
Go to the documentation of this file.
1//===- BuiltinDialectBytecode.cpp - Builtin 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
10#include "AttributeDetail.h"
15#include "mlir/IR/Diagnostics.h"
17#include "mlir/IR/Location.h"
18#include "mlir/Support/LLVM.h"
19#include "llvm/ADT/TypeSwitch.h"
20#include <cstdint>
21
22using namespace mlir;
23
24//===----------------------------------------------------------------------===//
25// BuiltinDialectBytecodeInterface
26//===----------------------------------------------------------------------===//
27
28namespace {
29
30//===----------------------------------------------------------------------===//
31// Utility functions
32//===----------------------------------------------------------------------===//
33
34// TODO: Move these to separate file.
35
36// Returns the bitwidth if known, else return std::nullopt.
37static std::optional<unsigned> getIntegerBitWidth(DialectBytecodeReader &reader,
38 Type type) {
39 if (auto intType = dyn_cast<IntegerType>(type))
40 return intType.getWidth();
41 if (llvm::isa<IndexType>(type))
42 return IndexType::kInternalStorageBitWidth;
43 reader.emitError()
44 << "expected integer or index type for IntegerAttr, but got: " << type;
45 return std::nullopt;
46}
47
48static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
49 Type type, FailureOr<APInt> &val) {
50 std::optional<unsigned> bitWidth = getIntegerBitWidth(reader, type);
51 // getIntegerBitWidth returns std::nullopt and emits an error for unsupported
52 // types. Bail out early to avoid creating a zero-width APInt with a non-zero
53 // value.
54 if (!bitWidth)
55 return failure();
56 val = reader.readAPIntWithKnownWidth(*bitWidth);
57 return val;
58}
59
60static LogicalResult
61readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
62 FailureOr<APFloat> &val) {
63 auto ftype = dyn_cast<FloatType>(type);
64 if (!ftype)
65 return failure();
66 val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
67 return success();
68}
69
70LogicalResult
71readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
72 bool isSplat,
73 SmallVectorImpl<StringRef> &rawStringData) {
74 rawStringData.resize(isSplat ? 1 : type.getNumElements());
75 for (StringRef &value : rawStringData)
76 if (failed(reader.readString(value)))
77 return failure();
78 return success();
79}
80
81static void writePotentiallySplatString(DialectBytecodeWriter &writer,
82 DenseStringElementsAttr attr) {
83 bool isSplat = attr.isSplat();
84 if (isSplat)
85 return writer.writeOwnedString(attr.getRawStringData().front());
86
87 for (StringRef str : attr.getRawStringData())
88 writer.writeOwnedString(str);
89}
90
91static FileLineColRange getFileLineColRange(MLIRContext *context,
92 StringAttr filename,
93 ArrayRef<uint64_t> lineCols) {
94 switch (lineCols.size()) {
95 case 0:
96 return FileLineColRange::get(filename);
97 case 1:
98 return FileLineColRange::get(filename, lineCols[0]);
99 case 2:
100 return FileLineColRange::get(filename, lineCols[0], lineCols[1]);
101 case 3:
102 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
103 lineCols[2]);
104 case 4:
105 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
106 lineCols[2], lineCols[3]);
107 default:
108 return {};
109 }
110}
111
112static LogicalResult
113readFileLineColRangeLocs(DialectBytecodeReader &reader,
114 SmallVectorImpl<uint64_t> &lineCols) {
115 return reader.readList(
116 lineCols, [&reader](uint64_t &val) { return reader.readVarInt(val); });
117}
118
119static void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
120 FileLineColRange range) {
121 if (range.getStartLine() == 0 && range.getStartColumn() == 0 &&
122 range.getEndLine() == 0 && range.getEndColumn() == 0) {
123 writer.writeVarInt(0);
124 return;
125 }
126 if (range.getStartColumn() == 0 &&
127 range.getStartLine() == range.getEndLine()) {
128 writer.writeVarInt(1);
129 writer.writeVarInt(range.getStartLine());
130 return;
131 }
132 // The single file:line:col is handled by other writer, but checked here for
133 // completeness.
134 if (range.getEndColumn() == range.getStartColumn() &&
135 range.getStartLine() == range.getEndLine()) {
136 writer.writeVarInt(2);
137 writer.writeVarInt(range.getStartLine());
138 writer.writeVarInt(range.getStartColumn());
139 return;
140 }
141 if (range.getStartLine() == range.getEndLine()) {
142 writer.writeVarInt(3);
143 writer.writeVarInt(range.getStartLine());
144 writer.writeVarInt(range.getStartColumn());
145 writer.writeVarInt(range.getEndColumn());
146 return;
147 }
148 writer.writeVarInt(4);
149 writer.writeVarInt(range.getStartLine());
150 writer.writeVarInt(range.getStartColumn());
151 writer.writeVarInt(range.getEndLine());
152 writer.writeVarInt(range.getEndColumn());
153}
154
155static LogicalResult
156readDenseTypedElementsAttr(DialectBytecodeReader &reader, ShapedType type,
157 SmallVectorImpl<char> &rawData) {
158 // Validate that the element type implements DenseElementTypeInterface.
159 // Without this check, downstream code unconditionally calls
160 // getDenseElementBitWidth() which asserts on unsupported types.
161 if (!llvm::isa<DenseElementType>(type.getElementType())) {
162 reader.emitError() << "DenseTypedElementsAttr element type must implement "
163 "DenseElementTypeInterface, but got: "
164 << type.getElementType();
165 return failure();
166 }
167
168 ArrayRef<char> blob;
169 if (failed(reader.readBlob(blob)))
170 return failure();
171
172 // If the type is not i1, just copy the blob.
173 if (!type.getElementType().isInteger(1)) {
174 rawData.append(blob.begin(), blob.end());
175 return success();
176 }
177
178 // Check to see if this is using the packed format.
179 // Note: this could be asserted instead as this should be the case. But we
180 // did have period where the unpacked was being serialized, this enables
181 // consuming those still and the check for which case we are in is pretty
182 // cheap.
183 size_t numElements = type.getNumElements();
184 size_t packedSize = llvm::divideCeil(numElements, 8);
185
186 // Unpack splats to single element 0x01 to match unpacked splat format.
187 if (blob.size() == 1 && blob[0] == static_cast<char>(~0x00)) {
188 rawData.resize(1);
189 rawData[0] = 0x01;
190 return success();
191 }
192
193 // Unpack the blob if it's packed.
194 // Splat and blob.size() == packedSize for all N<=8 elements are ambiguous,
195 // non 0xFF means not splat so must be unpacked.
196 if (blob.size() == packedSize && blob.size() != numElements) {
197 rawData.resize(numElements);
198 for (size_t i = 0; i < numElements; ++i)
199 rawData[i] = (blob[i / 8] & (1 << (i % 8))) ? 1 : 0;
200 return success();
201 }
202 // Otherwise, fallback to the default behavior.
203 rawData.append(blob.begin(), blob.end());
204 return success();
205}
206
207static void writeDenseTypedElementsAttr(DialectBytecodeWriter &writer,
209 // Check to see if this is an i1 dense attribute.
210 if (attr.getElementType().isInteger(1)) {
211 // Pack the data.
213 ArrayRef<char> rawData = attr.getRawData();
214
215 // If the attribute is a splat, we can just splat the value directly.
216 // Use 0xFF to avoid ambiguity with packed format of <=8 elements,
217 // written ~0x00 to ensure proper compilation with signed chars.
218 if (attr.isSplat()) {
219 data.resize(1);
220 data[0] = rawData[0] ? ~0x00 : 0x00;
221 writer.writeUnownedBlob(data);
222 return;
223 }
224
225 size_t numElements = attr.getNumElements();
226 data.resize(llvm::divideCeil(numElements, 8));
227 // Otherwise, pack the data manually.
228 for (size_t i = 0; i < numElements; ++i)
229 if (rawData[i])
230 data[i / 8] |= (1 << (i % 8));
231 writer.writeUnownedBlob(data);
232 return;
233 }
234
235 writer.writeOwnedBlob(attr.getRawData());
236}
237
238#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
239
240/// This class implements the bytecode interface for the builtin dialect.
241struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
242 BuiltinDialectBytecodeInterface(Dialect *dialect)
243 : BytecodeDialectInterface(dialect) {}
244
245 //===--------------------------------------------------------------------===//
246 // Attributes
247
248 Attribute readAttribute(DialectBytecodeReader &reader) const override {
249 return ::readAttribute(getContext(), reader);
250 }
251
252 LogicalResult writeAttribute(Attribute attr,
253 DialectBytecodeWriter &writer) const override {
254 return ::writeAttribute(attr, writer);
255 }
256
257 //===--------------------------------------------------------------------===//
258 // Types
259
260 Type readType(DialectBytecodeReader &reader) const override {
261 return ::readType(getContext(), reader);
262 }
263
264 LogicalResult writeType(Type type,
265 DialectBytecodeWriter &writer) const override {
266 return ::writeType(type, writer);
267 }
268
269 //===--------------------------------------------------------------------===//
270 // Version
271
272 void writeVersion(DialectBytecodeWriter &writer) const override {
273 auto configVersion = writer.getDialectVersion(getDialect()->getNamespace());
274 // Write version set in config.
275 if (succeeded(configVersion)) {
276 auto *version =
277 static_cast<const BuiltinDialectVersion *>(*configVersion);
278 writer.writeVarInt(static_cast<uint64_t>(version->getVersion()));
279 return;
280 }
281 // Else, write current set version version if not 0.
282 if (auto version = cast<BuiltinDialect>(getDialect())->getVersion();
283 version && version->getVersion() > 0) {
284 writer.writeVarInt(static_cast<uint64_t>(version->getVersion()));
285 }
286 }
287
288 std::unique_ptr<DialectVersion>
289 readVersion(DialectBytecodeReader &reader) const override {
290 uint64_t version;
291 if (failed(reader.readVarInt(version)))
292 return nullptr;
293
294 auto dialectVersion = std::make_unique<BuiltinDialectVersion>(version);
295 if (BuiltinDialectVersion::getCurrentVersion() < *dialectVersion) {
296 reader.emitError()
297 << "reading newer builtin dialect version than supported";
298 return nullptr;
299 }
300
301 return dialectVersion;
302 }
303};
304} // namespace
305
306void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
307 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
308}
return success()
b getContext())
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
virtual LogicalResult readBlob(ArrayRef< char > &result)=0
Read a blob from the bytecode.
virtual LogicalResult readVarInt(uint64_t &result)=0
Read a variable width integer.
virtual FailureOr< APInt > readAPIntWithKnownWidth(unsigned bitWidth)=0
Read an APInt that is known to have been encoded with the given width.
virtual InFlightDiagnostic emitError(const Twine &msg={}) const =0
Emit an error to the reader.
virtual LogicalResult readString(StringRef &result)=0
Read a string from the bytecode.
LogicalResult readList(SmallVectorImpl< T > &result, CallbackFn &&callback)
Read out a list of elements, invoking the provided callback for each element.
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 writeVarInt(uint64_t value)=0
Write a variable width integer to the output stream.
virtual void writeUnownedBlob(ArrayRef< char > blob)=0
Write a blob to the bytecode, which is not owned by the caller.
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 writeOwnedString(StringRef str)=0
Write a string to the bytecode, which is owned by the caller and is guaranteed to not die before the ...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
void addBytecodeInterface(BuiltinDialect *dialect)
Add the interfaces necessary for encoding the builtin 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.
static BuiltinDialectVersion getCurrentVersion()