19#include "llvm/ADT/TypeSwitch.h"
39 if (
auto intType = dyn_cast<IntegerType>(type))
40 return intType.getWidth();
41 if (llvm::isa<IndexType>(type))
42 return IndexType::kInternalStorageBitWidth;
44 <<
"expected integer or index type for IntegerAttr, but got: " << type;
49 Type type, FailureOr<APInt> &val) {
50 std::optional<unsigned> bitWidth = getIntegerBitWidth(reader, type);
62 FailureOr<APFloat> &val) {
63 auto ftype = dyn_cast<FloatType>(type);
74 rawStringData.resize(isSplat ? 1 : type.getNumElements());
75 for (StringRef &value : rawStringData)
82 DenseStringElementsAttr attr) {
83 bool isSplat = attr.isSplat();
87 for (StringRef str : attr.getRawStringData())
94 switch (lineCols.size()) {
96 return FileLineColRange::get(filename);
98 return FileLineColRange::get(filename, lineCols[0]);
100 return FileLineColRange::get(filename, lineCols[0], lineCols[1]);
102 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
105 return FileLineColRange::get(filename, lineCols[0], lineCols[1],
106 lineCols[2], lineCols[3]);
116 lineCols, [&reader](uint64_t &val) {
return reader.
readVarInt(val); });
121 if (range.getStartLine() == 0 && range.getStartColumn() == 0 &&
122 range.getEndLine() == 0 && range.getEndColumn() == 0) {
126 if (range.getStartColumn() == 0 &&
127 range.getStartLine() == range.getEndLine()) {
134 if (range.getEndColumn() == range.getStartColumn() &&
135 range.getStartLine() == range.getEndLine()) {
141 if (range.getStartLine() == range.getEndLine()) {
161 if (!llvm::isa<DenseElementType>(type.getElementType())) {
162 reader.
emitError() <<
"DenseTypedElementsAttr element type must implement "
163 "DenseElementTypeInterface, but got: "
164 << type.getElementType();
173 if (!type.getElementType().isInteger(1)) {
174 rawData.append(blob.begin(), blob.end());
183 size_t numElements = type.getNumElements();
184 size_t packedSize = llvm::divideCeil(numElements, 8);
187 if (blob.size() == 1 && blob[0] ==
static_cast<char>(~0x00)) {
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;
203 rawData.append(blob.begin(), blob.end());
210 if (attr.getElementType().isInteger(1)) {
218 if (attr.isSplat()) {
220 data[0] = rawData[0] ? ~0x00 : 0x00;
225 size_t numElements = attr.getNumElements();
226 data.resize(llvm::divideCeil(numElements, 8));
228 for (
size_t i = 0; i < numElements; ++i)
230 data[i / 8] |= (1 << (i % 8));
238#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
242 BuiltinDialectBytecodeInterface(Dialect *dialect)
243 : BytecodeDialectInterface(dialect) {}
248 Attribute readAttribute(DialectBytecodeReader &reader)
const override {
252 LogicalResult writeAttribute(Attribute attr,
253 DialectBytecodeWriter &writer)
const override {
254 return ::writeAttribute(attr, writer);
260 Type readType(DialectBytecodeReader &reader)
const override {
264 LogicalResult writeType(Type type,
265 DialectBytecodeWriter &writer)
const override {
266 return ::writeType(type, writer);
272 void writeVersion(DialectBytecodeWriter &writer)
const override {
275 if (succeeded(configVersion)) {
277 static_cast<const BuiltinDialectVersion *
>(*configVersion);
278 writer.
writeVarInt(
static_cast<uint64_t
>(version->getVersion()));
282 if (
auto version = cast<BuiltinDialect>(getDialect())->getVersion();
283 version && version->getVersion() > 0) {
284 writer.
writeVarInt(
static_cast<uint64_t
>(version->getVersion()));
288 std::unique_ptr<DialectVersion>
289 readVersion(DialectBytecodeReader &reader)
const override {
294 auto dialectVersion = std::make_unique<BuiltinDialectVersion>(version);
297 <<
"reading newer builtin dialect version than supported";
301 return dialectVersion;
307 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
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.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
void addBytecodeInterface(BuiltinDialect *dialect)
Add the interfaces necessary for encoding the builtin dialect components in bytecode.
Include the generated interface declarations.
static BuiltinDialectVersion getCurrentVersion()