10#include "mlir/Bytecode/BytecodeImplementation.h"
11#include "mlir/IR/Diagnostics.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
34static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
35 Type type, FailureOr<APInt> &val) {
36 auto intType = mlir::dyn_cast<cir::IntTypeInterface>(type);
38 return reader.emitError()
39 <<
"expected a CIR integer type for cir.int, but got: " << type;
40 val = reader.readAPIntWithKnownWidth(intType.getWidth());
45readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
46 FailureOr<APFloat> &val) {
47 auto fpType = mlir::dyn_cast<cir::FPTypeInterface>(type);
49 return reader.emitError()
50 <<
"expected a CIR floating-point type for cir.fp, but got: "
52 val = reader.readAPFloatWithKnownSemantics(fpType.getFloatSemantics());
58template <
typename EntryTy>
59static LogicalResult readOptionalInt(DialectBytecodeReader &reader,
60 std::optional<EntryTy> &storage) {
61 static_assert(std::is_unsigned_v<EntryTy>,
62 "EntryTy must be unsigned: only unsigned varints are "
63 "supported here, so a negative value cannot be represented");
65 if (failed(reader.readVarInt(present)))
68 storage = std::nullopt;
72 return reader.emitError() <<
"optional integer presence flag " << present
73 <<
" is neither 0 nor 1";
75 if (failed(reader.readVarInt(value)))
78 if (value >
static_cast<uint64_t>(std::numeric_limits<EntryTy>::max()))
79 return reader.emitError() <<
"optional integer value " << value
80 <<
" does not fit in destination type";
81 storage =
static_cast<EntryTy
>(value);
85template <
typename EntryTy>
86static void writeOptionalInt(DialectBytecodeWriter &writer,
87 std::optional<EntryTy> storage) {
88 static_assert(std::is_unsigned_v<EntryTy>,
89 "EntryTy must be unsigned: only unsigned varints are "
90 "supported here, so a negative value cannot be represented");
91 writer.writeVarInt(storage.has_value() ? 1 : 0);
93 writer.writeVarInt(*storage);
99template <
typename AttrTy>
100static LogicalResult readStdOptionalAttribute(DialectBytecodeReader &reader,
101 std::optional<AttrTy> &storage) {
103 if (failed(reader.readOptionalAttribute(attr)))
108 storage = std::nullopt;
112template <
typename AttrTy>
113static void writeStdOptionalAttribute(DialectBytecodeWriter &writer,
114 std::optional<AttrTy> storage) {
115 assert(!storage || *storage);
116 writer.writeOptionalAttribute(storage.value_or(AttrTy()));
119static LogicalResult readOptionalType(DialectBytecodeReader &reader,
121 bool present =
false;
122 if (failed(reader.readBool(present)))
128 return reader.readType(result);
131static void writeOptionalType(DialectBytecodeWriter &writer, Type type) {
132 writer.writeOwnedBool(
static_cast<bool>(type));
134 writer.writeType(type);
139template <
typename EnumTy>
140static LogicalResult readEnum(DialectBytecodeReader &reader, EnumTy &result,
141 std::optional<EnumTy> (*symbolize)(
uint32_t),
142 llvm::StringRef enumName) {
144 if (failed(reader.readVarInt(value)))
146 if (value >
static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()))
147 return reader.emitError()
148 << enumName <<
" value " << value <<
" out of range";
149 std::optional<EnumTy>
kind = symbolize(
static_cast<uint32_t>(value));
151 return reader.emitError() <<
"invalid " << enumName <<
" value " << value;
158template <
typename IntTy>
159static LogicalResult readCheckedInt(DialectBytecodeReader &reader,
161 static_assert(std::is_integral_v<IntTy> && !std::is_same_v<IntTy, bool>);
162 if constexpr (std::is_signed_v<IntTy>) {
164 if (failed(reader.readSignedVarInt(value)))
166 if (value >
static_cast<int64_t>(std::numeric_limits<IntTy>::max()) ||
167 value <
static_cast<int64_t>(std::numeric_limits<IntTy>::min()))
168 return reader.emitError() <<
"integer value " << value
169 <<
" does not fit in destination type";
170 result =
static_cast<IntTy
>(value);
173 if (failed(reader.readVarInt(value)))
175 if (value >
static_cast<uint64_t>(std::numeric_limits<IntTy>::max()))
176 return reader.emitError() <<
"integer value " << value
177 <<
" does not fit in destination type";
178 result =
static_cast<IntTy
>(value);
189#include "clang/CIR/Dialect/IR/CIRDialectBytecode.cpp.inc"
198struct CIRDialectBytecodeInterface :
public BytecodeDialectInterface {
199 CIRDialectBytecodeInterface(Dialect *dialect)
200 : BytecodeDialectInterface(dialect) {}
202 Attribute readAttribute(DialectBytecodeReader &reader)
const override {
203 return ::readAttribute(getContext(), reader);
206 LogicalResult writeAttribute(Attribute attr,
207 DialectBytecodeWriter &writer)
const override {
208 return ::writeAttribute(attr, writer);
211 Type readType(DialectBytecodeReader &reader)
const override {
212 return ::readType(getContext(), reader);
215 LogicalResult writeType(Type type,
216 DialectBytecodeWriter &writer)
const override {
217 return ::writeType(type, writer);
223 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
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t