1 //===- BuiltinDialectBytecode.cpp - Builtin Bytecode Implementation -------===//
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
7 //===----------------------------------------------------------------------===//
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"
21 //===----------------------------------------------------------------------===//
22 // BuiltinDialectBytecodeInterface
23 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
30 // TODO: Move these to separate file.
32 // Returns the bitwidth if known, else return 0.
33 static unsigned getIntegerBitWidth(DialectBytecodeReader
&reader
, Type type
) {
34 if (auto intType
= dyn_cast
<IntegerType
>(type
)) {
35 return intType
.getWidth();
36 } else if (llvm::isa
<IndexType
>(type
)) {
37 return IndexType::kInternalStorageBitWidth
;
40 << "expected integer or index type for IntegerAttr, but got: " << type
;
44 static LogicalResult
readAPIntWithKnownWidth(DialectBytecodeReader
&reader
,
45 Type type
, FailureOr
<APInt
> &val
) {
46 unsigned bitWidth
= getIntegerBitWidth(reader
, type
);
47 val
= reader
.readAPIntWithKnownWidth(bitWidth
);
52 readAPFloatWithKnownSemantics(DialectBytecodeReader
&reader
, Type type
,
53 FailureOr
<APFloat
> &val
) {
54 auto ftype
= dyn_cast
<FloatType
>(type
);
57 val
= reader
.readAPFloatWithKnownSemantics(ftype
.getFloatSemantics());
62 readPotentiallySplatString(DialectBytecodeReader
&reader
, ShapedType type
,
64 SmallVectorImpl
<StringRef
> &rawStringData
) {
65 rawStringData
.resize(isSplat
? 1 : type
.getNumElements());
66 for (StringRef
&value
: rawStringData
)
67 if (failed(reader
.readString(value
)))
72 void writePotentiallySplatString(DialectBytecodeWriter
&writer
,
73 DenseStringElementsAttr attr
) {
74 bool isSplat
= attr
.isSplat();
76 return writer
.writeOwnedString(attr
.getRawStringData().front());
78 for (StringRef str
: attr
.getRawStringData())
79 writer
.writeOwnedString(str
);
82 #include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
84 /// This class implements the bytecode interface for the builtin dialect.
85 struct BuiltinDialectBytecodeInterface
: public BytecodeDialectInterface
{
86 BuiltinDialectBytecodeInterface(Dialect
*dialect
)
87 : BytecodeDialectInterface(dialect
) {}
89 //===--------------------------------------------------------------------===//
92 Attribute
readAttribute(DialectBytecodeReader
&reader
) const override
{
93 return ::readAttribute(getContext(), reader
);
96 LogicalResult
writeAttribute(Attribute attr
,
97 DialectBytecodeWriter
&writer
) const override
{
98 return ::writeAttribute(attr
, writer
);
101 //===--------------------------------------------------------------------===//
104 Type
readType(DialectBytecodeReader
&reader
) const override
{
105 return ::readType(getContext(), reader
);
108 LogicalResult
writeType(Type type
,
109 DialectBytecodeWriter
&writer
) const override
{
110 return ::writeType(type
, writer
);
115 void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect
*dialect
) {
116 dialect
->addInterfaces
<BuiltinDialectBytecodeInterface
>();