1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
//===- BuiltinDialectBytecode.cpp - Builtin Bytecode Implementation -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "BuiltinDialectBytecode.h"
#include "AttributeDetail.h"
#include "mlir/Bytecode/BytecodeImplementation.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/DialectResourceBlobManager.h"
#include "mlir/IR/Location.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/TypeSwitch.h"
#include <cstdint>
using namespace mlir;
//===----------------------------------------------------------------------===//
// BuiltinDialectBytecodeInterface
//===----------------------------------------------------------------------===//
namespace {
//===----------------------------------------------------------------------===//
// Utility functions
//===----------------------------------------------------------------------===//
// TODO: Move these to separate file.
// Returns the bitwidth if known, else return 0.
static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
if (auto intType = dyn_cast<IntegerType>(type)) {
return intType.getWidth();
}
if (llvm::isa<IndexType>(type)) {
return IndexType::kInternalStorageBitWidth;
}
reader.emitError()
<< "expected integer or index type for IntegerAttr, but got: " << type;
return 0;
}
static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
Type type, FailureOr<APInt> &val) {
unsigned bitWidth = getIntegerBitWidth(reader, type);
val = reader.readAPIntWithKnownWidth(bitWidth);
return val;
}
static LogicalResult
readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
FailureOr<APFloat> &val) {
auto ftype = dyn_cast<FloatType>(type);
if (!ftype)
return failure();
val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
return success();
}
LogicalResult
readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
bool isSplat,
SmallVectorImpl<StringRef> &rawStringData) {
rawStringData.resize(isSplat ? 1 : type.getNumElements());
for (StringRef &value : rawStringData)
if (failed(reader.readString(value)))
return failure();
return success();
}
static void writePotentiallySplatString(DialectBytecodeWriter &writer,
DenseStringElementsAttr attr) {
bool isSplat = attr.isSplat();
if (isSplat)
return writer.writeOwnedString(attr.getRawStringData().front());
for (StringRef str : attr.getRawStringData())
writer.writeOwnedString(str);
}
static FileLineColRange getFileLineColRange(MLIRContext *context,
StringAttr filename,
ArrayRef<uint64_t> lineCols) {
switch (lineCols.size()) {
case 0:
return FileLineColRange::get(filename);
case 1:
return FileLineColRange::get(filename, lineCols[0]);
case 2:
return FileLineColRange::get(filename, lineCols[0], lineCols[1]);
case 3:
return FileLineColRange::get(filename, lineCols[0], lineCols[1],
lineCols[2]);
case 4:
return FileLineColRange::get(filename, lineCols[0], lineCols[1],
lineCols[2], lineCols[3]);
default:
return {};
}
}
static LogicalResult
readFileLineColRangeLocs(DialectBytecodeReader &reader,
SmallVectorImpl<uint64_t> &lineCols) {
return reader.readList(
lineCols, [&reader](uint64_t &val) { return reader.readVarInt(val); });
}
static void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
FileLineColRange range) {
if (range.getStartLine() == 0 && range.getStartColumn() == 0 &&
range.getEndLine() == 0 && range.getEndColumn() == 0) {
writer.writeVarInt(0);
return;
}
if (range.getStartColumn() == 0 &&
range.getStartLine() == range.getEndLine()) {
writer.writeVarInt(1);
writer.writeVarInt(range.getStartLine());
return;
}
// The single file:line:col is handled by other writer, but checked here for
// completeness.
if (range.getEndColumn() == range.getStartColumn() &&
range.getStartLine() == range.getEndLine()) {
writer.writeVarInt(2);
writer.writeVarInt(range.getStartLine());
writer.writeVarInt(range.getStartColumn());
return;
}
if (range.getStartLine() == range.getEndLine()) {
writer.writeVarInt(3);
writer.writeVarInt(range.getStartLine());
writer.writeVarInt(range.getStartColumn());
writer.writeVarInt(range.getEndColumn());
return;
}
writer.writeVarInt(4);
writer.writeVarInt(range.getStartLine());
writer.writeVarInt(range.getStartColumn());
writer.writeVarInt(range.getEndLine());
writer.writeVarInt(range.getEndColumn());
}
#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
/// This class implements the bytecode interface for the builtin dialect.
struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
BuiltinDialectBytecodeInterface(Dialect *dialect)
: BytecodeDialectInterface(dialect) {}
//===--------------------------------------------------------------------===//
// Attributes
Attribute readAttribute(DialectBytecodeReader &reader) const override {
return ::readAttribute(getContext(), reader);
}
LogicalResult writeAttribute(Attribute attr,
DialectBytecodeWriter &writer) const override {
return ::writeAttribute(attr, writer);
}
//===--------------------------------------------------------------------===//
// Types
Type readType(DialectBytecodeReader &reader) const override {
return ::readType(getContext(), reader);
}
LogicalResult writeType(Type type,
DialectBytecodeWriter &writer) const override {
return ::writeType(type, writer);
}
};
} // namespace
void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
}
|