MLIR 24.0.0git
QuantDialectBytecode.cpp
Go to the documentation of this file.
1//===- QuantDialectBytecode.cpp - Quant Bytecode Implementation
2//------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
14#include "mlir/IR/Diagnostics.h"
15#include "llvm/ADT/APFloat.h"
16
17using namespace mlir;
18using namespace mlir::quant;
19
20namespace {
21
22static LogicalResult readDoubleAPFloat(DialectBytecodeReader &reader,
23 double &val) {
24 auto valOr =
25 reader.readAPFloatWithKnownSemantics(llvm::APFloat::IEEEdouble());
26 if (failed(valOr))
27 return failure();
28 val = valOr->convertToDouble();
29 return success();
30}
31
32static LogicalResult readOptionalSignedVarInt(DialectBytecodeReader &reader,
33 std::optional<int64_t> &val) {
34 bool hasValue;
35 if (failed(reader.readBool(hasValue)))
36 return failure();
37 if (hasValue) {
38 int64_t v;
39 if (failed(reader.readSignedVarInt(v)))
40 return failure();
41 val = v;
42 } else {
43 val = std::nullopt;
44 }
45 return success();
46}
47
48static void writeOptionalSignedVarInt(DialectBytecodeWriter &writer,
49 std::optional<int64_t> val) {
50 writer.writeOwnedBool(val.has_value());
51 if (val.has_value())
52 writer.writeSignedVarInt(*val);
53}
54} // namespace
55
56#include "mlir/Dialect/Quant/IR/QuantDialectBytecode.cpp.inc"
57
58namespace {
59/// This class implements the bytecode interface for the Quant dialect.
60struct QuantDialectBytecodeInterface : public BytecodeDialectInterface {
61 QuantDialectBytecodeInterface(Dialect *dialect)
62 : BytecodeDialectInterface(dialect) {}
63
64 //===--------------------------------------------------------------------===//
65 // Attributes
66
67 Attribute readAttribute(DialectBytecodeReader &reader) const override {
68 return ::readAttribute(getContext(), reader);
69 }
70
71 LogicalResult writeAttribute(Attribute attr,
72 DialectBytecodeWriter &writer) const override {
73 return ::writeAttribute(attr, writer);
74 }
75
76 //===--------------------------------------------------------------------===//
77 // Types
78
79 Type readType(DialectBytecodeReader &reader) const override {
80 return ::readType(getContext(), reader);
81 }
82
83 LogicalResult writeType(Type type,
84 DialectBytecodeWriter &writer) const override {
85 return ::writeType(type, writer);
86 }
87};
88} // namespace
89
90void quant::detail::addBytecodeInterface(QuantDialect *dialect) {
91 dialect->addInterfaces<QuantDialectBytecodeInterface>();
92}
return success()
b getContext())
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
virtual LogicalResult readBool(bool &result)=0
Read a bool from the bytecode.
virtual LogicalResult readSignedVarInt(int64_t &result)=0
Read a signed variable width integer.
virtual FailureOr< APFloat > readAPFloatWithKnownSemantics(const llvm::fltSemantics &semantics)=0
Read an APFloat that is known to have been encoded with the given semantics.
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
virtual void writeSignedVarInt(int64_t value)=0
Write a signed variable width integer to the output stream.
virtual void writeOwnedBool(bool value)=0
Write a bool to the output stream.
void addBytecodeInterface(QuantDialect *dialect)
Add the interfaces necessary for encoding the quantization dialect components in bytecode.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.