[LLVM] Fix Maintainers.md formatting (NFC)
[llvm-project.git] / mlir / lib / IR / BuiltinDialectBytecode.cpp
blob6131b7eae90c8bfb34ddcf4a4b622defe03fa347
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 //===----------------------------------------------------------------------===//
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"
19 using namespace mlir;
21 //===----------------------------------------------------------------------===//
22 // BuiltinDialectBytecodeInterface
23 //===----------------------------------------------------------------------===//
25 namespace {
27 //===----------------------------------------------------------------------===//
28 // Utility functions
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();
37 if (llvm::isa<IndexType>(type)) {
38 return IndexType::kInternalStorageBitWidth;
40 reader.emitError()
41 << "expected integer or index type for IntegerAttr, but got: " << type;
42 return 0;
45 static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
46 Type type, FailureOr<APInt> &val) {
47 unsigned bitWidth = getIntegerBitWidth(reader, type);
48 val = reader.readAPIntWithKnownWidth(bitWidth);
49 return val;
52 static LogicalResult
53 readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
54 FailureOr<APFloat> &val) {
55 auto ftype = dyn_cast<FloatType>(type);
56 if (!ftype)
57 return failure();
58 val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
59 return success();
62 LogicalResult
63 readPotentiallySplatString(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(reader.readString(value)))
69 return failure();
70 return success();
73 void writePotentiallySplatString(DialectBytecodeWriter &writer,
74 DenseStringElementsAttr attr) {
75 bool isSplat = attr.isSplat();
76 if (isSplat)
77 return writer.writeOwnedString(attr.getRawStringData().front());
79 for (StringRef str : attr.getRawStringData())
80 writer.writeOwnedString(str);
83 #include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
85 /// This class implements the bytecode interface for the builtin dialect.
86 struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
87 BuiltinDialectBytecodeInterface(Dialect *dialect)
88 : BytecodeDialectInterface(dialect) {}
90 //===--------------------------------------------------------------------===//
91 // Attributes
93 Attribute readAttribute(DialectBytecodeReader &reader) const override {
94 return ::readAttribute(getContext(), reader);
97 LogicalResult writeAttribute(Attribute attr,
98 DialectBytecodeWriter &writer) const override {
99 return ::writeAttribute(attr, writer);
102 //===--------------------------------------------------------------------===//
103 // Types
105 Type readType(DialectBytecodeReader &reader) const override {
106 return ::readType(getContext(), reader);
109 LogicalResult writeType(Type type,
110 DialectBytecodeWriter &writer) const override {
111 return ::writeType(type, writer);
114 } // namespace
116 void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
117 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();