MLIR 23.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#include "llvm/ADT/TypeSwitch.h"
17
18using namespace mlir;
19using namespace mlir::quant;
20
21namespace {
22
23static LogicalResult readDoubleAPFloat(DialectBytecodeReader &reader,
24 double &val) {
25 auto valOr =
26 reader.readAPFloatWithKnownSemantics(llvm::APFloat::IEEEdouble());
27 if (failed(valOr))
28 return failure();
29 val = valOr->convertToDouble();
30 return success();
31}
32
33static LogicalResult readOptionalSignedVarInt(DialectBytecodeReader &reader,
34 std::optional<int64_t> &val) {
35 bool hasValue;
36 if (failed(reader.readBool(hasValue)))
37 return failure();
38 if (hasValue) {
39 int64_t v;
40 if (failed(reader.readSignedVarInt(v)))
41 return failure();
42 val = v;
43 } else {
44 val = std::nullopt;
45 }
46 return success();
47}
48
49static void writeOptionalSignedVarInt(DialectBytecodeWriter &writer,
50 std::optional<int64_t> val) {
51 writer.writeOwnedBool(val.has_value());
52 if (val.has_value())
53 writer.writeSignedVarInt(*val);
54}
55
56#include "mlir/Dialect/Quant/IR/QuantDialectBytecode.cpp.inc"
57
58/// This class implements the bytecode interface for the Quant dialect.
59struct QuantDialectBytecodeInterface : public BytecodeDialectInterface {
60 QuantDialectBytecodeInterface(Dialect *dialect)
61 : BytecodeDialectInterface(dialect) {}
62
63 //===--------------------------------------------------------------------===//
64 // Attributes
65
66 Attribute readAttribute(DialectBytecodeReader &reader) const override {
67 return ::readAttribute(getContext(), reader);
68 }
69
70 LogicalResult writeAttribute(Attribute attr,
71 DialectBytecodeWriter &writer) const override {
72 return ::writeAttribute(attr, writer);
73 }
74
75 //===--------------------------------------------------------------------===//
76 // Types
77
78 Type readType(DialectBytecodeReader &reader) const override {
79 return ::readType(getContext(), reader);
80 }
81
82 LogicalResult writeType(Type type,
83 DialectBytecodeWriter &writer) const override {
84 return ::writeType(type, writer);
85 }
86};
87} // namespace
88
89void quant::detail::addBytecodeInterface(QuantDialect *dialect) {
90 dialect->addInterfaces<QuantDialectBytecodeInterface>();
91}
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:717
Include the generated interface declarations.