[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / mlir / lib / IR / BuiltinDialectBytecode.cpp
blob9487e6c50ded904dcc0a10c8d7c889ba2c8c9d9e
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();
36 } else if (llvm::isa<IndexType>(type)) {
37 return IndexType::kInternalStorageBitWidth;
39 reader.emitError()
40 << "expected integer or index type for IntegerAttr, but got: " << type;
41 return 0;
44 static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
45 Type type, FailureOr<APInt> &val) {
46 unsigned bitWidth = getIntegerBitWidth(reader, type);
47 val = reader.readAPIntWithKnownWidth(bitWidth);
48 return val;
51 static LogicalResult
52 readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
53 FailureOr<APFloat> &val) {
54 auto ftype = dyn_cast<FloatType>(type);
55 if (!ftype)
56 return failure();
57 val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
58 return success();
61 LogicalResult
62 readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
63 bool isSplat,
64 SmallVectorImpl<StringRef> &rawStringData) {
65 rawStringData.resize(isSplat ? 1 : type.getNumElements());
66 for (StringRef &value : rawStringData)
67 if (failed(reader.readString(value)))
68 return failure();
69 return success();
72 void writePotentiallySplatString(DialectBytecodeWriter &writer,
73 DenseStringElementsAttr attr) {
74 bool isSplat = attr.isSplat();
75 if (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 //===--------------------------------------------------------------------===//
90 // Attributes
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 //===--------------------------------------------------------------------===//
102 // Types
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);
113 } // namespace
115 void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
116 dialect->addInterfaces<BuiltinDialectBytecodeInterface>();