10#include "mlir/Bytecode/BytecodeImplementation.h"
11#include "mlir/IR/Diagnostics.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/TypeSwitch.h"
40static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
41 Type type, FailureOr<APInt> &val) {
42 auto intType = mlir::dyn_cast<cir::IntTypeInterface>(type);
44 return reader.emitError()
45 <<
"expected a CIR integer type for cir.int, but got: " << type;
46 val = reader.readAPIntWithKnownWidth(intType.getWidth());
51readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
52 FailureOr<APFloat> &val) {
53 auto fpType = mlir::dyn_cast<cir::FPTypeInterface>(type);
55 return reader.emitError()
56 <<
"expected a CIR floating-point type for cir.fp, but got: "
58 val = reader.readAPFloatWithKnownSemantics(fpType.getFloatSemantics());
64template <
typename EntryTy>
65static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
66 std::optional<EntryTy> &storage) {
67 static_assert(std::is_unsigned_v<EntryTy>,
68 "EntryTy must be unsigned: only unsigned varints are "
69 "supported here, so a negative value cannot be represented");
71 if (failed(reader.readVarInt(present)))
74 storage = std::nullopt;
78 return reader.emitError() <<
"optional integer presence flag " << present
79 <<
" is neither 0 nor 1";
81 if (failed(reader.readVarInt(value)))
84 if (value >
static_cast<uint64_t
>(std::numeric_limits<EntryTy>::max()))
85 return reader.emitError() <<
"optional integer value " << value
86 <<
" does not fit in destination type";
87 storage =
static_cast<EntryTy
>(value);
91template <
typename EntryTy>
92static void writeOptionalInt(DialectBytecodeWriter &writer,
93 std::optional<EntryTy> storage) {
94 static_assert(std::is_unsigned_v<EntryTy>,
95 "EntryTy must be unsigned: only unsigned varints are "
96 "supported here, so a negative value cannot be represented");
97 writer.writeVarInt(storage.has_value() ? 1 : 0);
99 writer.writeVarInt(*storage);
105template <
typename AttrTy>
106static LogicalResult readStdOptionalAttribute(DialectBytecodeReader &reader,
107 std::optional<AttrTy> &storage) {
109 if (failed(reader.readOptionalAttribute(attr)))
114 storage = std::nullopt;
118template <
typename AttrTy>
119static void writeStdOptionalAttribute(DialectBytecodeWriter &writer,
120 std::optional<AttrTy> storage) {
121 assert(!storage || *storage);
122 writer.writeOptionalAttribute(storage.value_or(AttrTy()));
127template <
typename EnumTy>
128static LogicalResult readEnum(DialectBytecodeReader &reader, EnumTy &result,
129 std::optional<EnumTy> (*symbolize)(
uint32_t),
130 llvm::StringRef enumName) {
132 if (failed(reader.readVarInt(value)))
134 if (value >
static_cast<uint64_t
>(std::numeric_limits<uint32_t>::max()))
135 return reader.emitError()
136 << enumName <<
" value " << value <<
" out of range";
137 std::optional<EnumTy>
kind = symbolize(
static_cast<uint32_t>(value));
139 return reader.emitError() <<
"invalid " << enumName <<
" value " << value;
146template <
typename IntTy>
147static LogicalResult readCheckedInt(DialectBytecodeReader &reader,
149 static_assert(std::is_integral_v<IntTy> && !std::is_same_v<IntTy, bool>);
150 if constexpr (std::is_signed_v<IntTy>) {
152 if (failed(reader.readSignedVarInt(value)))
154 if (value >
static_cast<int64_t
>(std::numeric_limits<IntTy>::max()) ||
155 value <
static_cast<int64_t
>(std::numeric_limits<IntTy>::min()))
156 return reader.emitError() <<
"integer value " << value
157 <<
" does not fit in destination type";
158 result =
static_cast<IntTy
>(value);
161 if (failed(reader.readVarInt(value)))
163 if (value >
static_cast<uint64_t
>(std::numeric_limits<IntTy>::max()))
164 return reader.emitError() <<
"integer value " << value
165 <<
" does not fit in destination type";
166 result =
static_cast<IntTy
>(value);
175#include "clang/CIR/Dialect/IR/CIRDialectBytecode.cpp.inc"
182struct CIRDialectBytecodeInterface :
public BytecodeDialectInterface {
183 CIRDialectBytecodeInterface(Dialect *dialect)
184 : BytecodeDialectInterface(dialect) {}
186 Attribute readAttribute(DialectBytecodeReader &reader)
const override {
187 return ::readAttribute(getContext(), reader);
190 LogicalResult writeAttribute(Attribute attr,
191 DialectBytecodeWriter &writer)
const override {
192 return ::writeAttribute(attr, writer);
195 Type readType(DialectBytecodeReader &reader)
const override {
196 return ::readType(getContext(), reader);
199 LogicalResult writeType(Type type,
200 DialectBytecodeWriter &writer)
const override {
201 return ::writeType(type, writer);
207 dialect->addInterfaces<CIRDialectBytecodeInterface>();
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
void addBytecodeInterface(CIRDialect *dialect)
Add the interfaces necessary for encoding the CIR dialect components in bytecode.
const internal::VariadicAllOfMatcher< Attr > attr