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
9#include "BuiltinDialectBytecode.h"
10#include "AttributeDetail.h"
11#include "mlir/Bytecode/BytecodeImplementation.h"
12#include "mlir/IR/BuiltinAttributes.h"
13#include "mlir/IR/BuiltinDialect.h"
14#include "mlir/IR/BuiltinTypes.h"
15#include "mlir/IR/Diagnostics.h"
16#include "mlir/IR/DialectResourceBlobManager.h"
17#include "llvm/ADT/TypeSwitch.h"
18
19using namespace mlir;
20
21//===----------------------------------------------------------------------===//
22// BuiltinDialectBytecodeInterface
23//===----------------------------------------------------------------------===//
24
25namespace {
26
27//===----------------------------------------------------------------------===//
28// Utility functions
29
30// TODO: Move these to separate file.
31
32// Returns the bitwidth if known, else return 0.
33static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
34 if (auto intType = dyn_cast<IntegerType>(type)) {
35 return intType.getWidth();
36 }
37 if (llvm::isa<IndexType>(Val: type)) {
38 return IndexType::kInternalStorageBitWidth;
39 }
40 reader.emitError()
41 << "expected integer or index type for IntegerAttr, but got: " << type;
42 return 0;
43}
44
45static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
46 Type type, FailureOr<APInt> &val) {
47 unsigned bitWidth = getIntegerBitWidth(reader, type);
48 val = reader.readAPIntWithKnownWidth(bitWidth);
49 return val;
50}
51
52static LogicalResult
53readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
54 FailureOr<APFloat> &val) {
55 auto ftype = dyn_cast<FloatType>(Val&: type);
56 if (!ftype)
57 return failure();
58 val = reader.readAPFloatWithKnownSemantics(semantics: ftype.getFloatSemantics());
59 return success();
60}
61
62LogicalResult
63readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
64 bool isSplat,
65 SmallVectorImpl<StringRef> &rawStringData) {
66 rawStringData.resize(isSplat ? 1 : type.getNumElements());
67 for (StringRef &value : rawStringData)
68 if (failed(result: reader.readString(result&: value)))
69 return failure();
70 return success();
71}
72
73void writePotentiallySplatString(DialectBytecodeWriter &writer,
74 DenseStringElementsAttr attr) {
75 bool isSplat = attr.isSplat();
76 if (isSplat)
77 return writer.writeOwnedString(str: attr.getRawStringData().front());
78
79 for (StringRef str : attr.getRawStringData())
80 writer.writeOwnedString(str);
81}
82
83#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
84
85/// This class implements the bytecode interface for the builtin dialect.
86struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
87 BuiltinDialectBytecodeInterface(Dialect *dialect)
88 : BytecodeDialectInterface(dialect) {}
89
90 //===--------------------------------------------------------------------===//
91 // Attributes
92
93 Attribute readAttribute(DialectBytecodeReader &reader) const override {
94 return ::readAttribute(getContext(), reader);
95 }
96
97 LogicalResult writeAttribute(Attribute attr,
98 DialectBytecodeWriter &writer) const override {
99 return ::writeAttribute(attr, writer);
100 }
101
102 //===--------------------------------------------------------------------===//
103 // Types
104
105 Type readType(DialectBytecodeReader &reader) const override {
106 return ::readType(getContext(), reader);
107 }
108
109 LogicalResult writeType(Type type,
110 DialectBytecodeWriter &writer) const override {
111 return ::writeType(type, writer);
112 }
113};
114} // namespace
115
116void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
117 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
118}
119

source code of mlir/lib/IR/BuiltinDialectBytecode.cpp