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 0.
37static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
38 if (auto intType = dyn_cast<IntegerType>(type)) {
39 return intType.getWidth();
40 }
41 if (llvm::isa<IndexType>(type)) {
42 return IndexType::kInternalStorageBitWidth;
43 }
44 reader.emitError()
45 << "expected integer or index type for IntegerAttr, but got: " << type;
46 return 0;
47}
48
49static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
50 Type type, FailureOr<APInt> &val) {
51 unsigned bitWidth = getIntegerBitWidth(reader, type);
52 val = reader.readAPIntWithKnownWidth(bitWidth);
53 return val;
54}
55
56static LogicalResult
57readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
58 FailureOr<APFloat> &val) {
59 auto ftype = dyn_cast<FloatType>(type);
60 if (!ftype)
61 return failure();
62 val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
63 return success();
64}
65
66LogicalResult
67readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
68 bool isSplat,
69 SmallVectorImpl<StringRef> &rawStringData) {
70 rawStringData.resize(isSplat ? 1 : type.getNumElements());
71 for (StringRef &value : rawStringData)
72 if (failed(reader.readString(value)))
73 return failure();
74 return success();
75}
76
77static void writePotentiallySplatString(DialectBytecodeWriter &writer,
78 DenseStringElementsAttr attr) {
79 bool isSplat = attr.isSplat();
80 if (isSplat)
81 return writer.writeOwnedString(attr.getRawStringData().front());
82
83 for (StringRef str : attr.getRawStringData())
84 writer.writeOwnedString(str);
85}
86
87static FileLineColRange getFileLineColRange(MLIRContext *context,
88 StringAttr filename,
89 ArrayRef<uint64_t> lineCols) {
90 switch (lineCols.size()) {
91 case 0:
92 return FileLineColRange::get(filename);
93 case 1:
94 return FileLineColRange::get(filename, lineCols[0]);
95 case 2:
96 return FileLineColRange::get(filename, lineCols[0], lineCols[1]);
97 case 3:
98 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
99 lineCols[2]);
100 case 4:
101 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
102 lineCols[2], lineCols[3]);
103 default:
104 return {};
105 }
106}
107
108static LogicalResult
109readFileLineColRangeLocs(DialectBytecodeReader &reader,
110 SmallVectorImpl<uint64_t> &lineCols) {
111 return reader.readList(
112 lineCols, [&reader](uint64_t &val) { return reader.readVarInt(val); });
113}
114
115static void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
116 FileLineColRange range) {
117 if (range.getStartLine() == 0 && range.getStartColumn() == 0 &&
118 range.getEndLine() == 0 && range.getEndColumn() == 0) {
119 writer.writeVarInt(0);
120 return;
121 }
122 if (range.getStartColumn() == 0 &&
123 range.getStartLine() == range.getEndLine()) {
124 writer.writeVarInt(1);
125 writer.writeVarInt(range.getStartLine());
126 return;
127 }
128 // The single file:line:col is handled by other writer, but checked here for
129 // completeness.
130 if (range.getEndColumn() == range.getStartColumn() &&
131 range.getStartLine() == range.getEndLine()) {
132 writer.writeVarInt(2);
133 writer.writeVarInt(range.getStartLine());
134 writer.writeVarInt(range.getStartColumn());
135 return;
136 }
137 if (range.getStartLine() == range.getEndLine()) {
138 writer.writeVarInt(3);
139 writer.writeVarInt(range.getStartLine());
140 writer.writeVarInt(range.getStartColumn());
141 writer.writeVarInt(range.getEndColumn());
142 return;
143 }
144 writer.writeVarInt(4);
145 writer.writeVarInt(range.getStartLine());
146 writer.writeVarInt(range.getStartColumn());
147 writer.writeVarInt(range.getEndLine());
148 writer.writeVarInt(range.getEndColumn());
149}
150
151static LogicalResult
152readDenseIntOrFPElementsAttr(DialectBytecodeReader &reader, ShapedType type,
153 SmallVectorImpl<char> &rawData) {
154 ArrayRef<char> blob;
155 if (failed(reader.readBlob(blob)))
156 return failure();
157
158 // If the type is not i1, just copy the blob.
159 if (!type.getElementType().isInteger(1)) {
160 rawData.append(blob.begin(), blob.end());
161 return success();
162 }
163
164 // Check to see if this is using the packed format.
165 // Note: this could be asserted instead as this should be the case. But we
166 // did have period where the unpacked was being serialized, this enables
167 // consuming those still and the check for which case we are in is pretty
168 // cheap.
169 size_t numElements = type.getNumElements();
170 size_t packedSize = llvm::divideCeil(numElements, 8);
171 if (blob.size() == packedSize && blob.size() != numElements) {
172 // Unpack the blob.
173 rawData.resize(numElements);
174 for (size_t i = 0; i < numElements; ++i)
175 rawData[i] = (blob[i / 8] & (1 << (i % 8))) ? 1 : 0;
176 return success();
177 }
178 // Otherwise, fallback to the default behavior.
179 rawData.append(blob.begin(), blob.end());
180 return success();
181}
182
183static void writeDenseIntOrFPElementsAttr(DialectBytecodeWriter &writer,
185 // Check to see if this is an i1 dense attribute.
186 if (attr.getElementType().isInteger(1)) {
187 // Pack the data.
189 ArrayRef<char> rawData = attr.getRawData();
190
191 // If the attribute is a splat, we can just splat the value directly.
192 if (attr.isSplat()) {
193 data.resize(1);
194 data[0] = rawData[0] ? 0xFF : 0x00;
195 writer.writeUnownedBlob(data);
196 return;
197 }
198
199 size_t numElements = attr.getNumElements();
200 data.resize(llvm::divideCeil(numElements, 8));
201 // Otherwise, pack the data manually.
202 for (size_t i = 0; i < numElements; ++i)
203 if (rawData[i])
204 data[i / 8] |= (1 << (i % 8));
205 writer.writeUnownedBlob(data);
206 return;
207 }
208
209 writer.writeOwnedBlob(attr.getRawData());
210}
211
212#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
213
214/// This class implements the bytecode interface for the builtin dialect.
215struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
216 BuiltinDialectBytecodeInterface(Dialect *dialect)
217 : BytecodeDialectInterface(dialect) {}
218
219 //===--------------------------------------------------------------------===//
220 // Attributes
221
222 Attribute readAttribute(DialectBytecodeReader &reader) const override {
223 return ::readAttribute(getContext(), reader);
224 }
225
226 LogicalResult writeAttribute(Attribute attr,
227 DialectBytecodeWriter &writer) const override {
228 return ::writeAttribute(attr, writer);
229 }
230
231 //===--------------------------------------------------------------------===//
232 // Types
233
234 Type readType(DialectBytecodeReader &reader) const override {
235 return ::readType(getContext(), reader);
236 }
237
238 LogicalResult writeType(Type type,
239 DialectBytecodeWriter &writer) const override {
240 return ::writeType(type, writer);
241 }
242};
243} // namespace
244
245void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
246 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
247}
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 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.