xref: /llvm-project/mlir/lib/Dialect/Quant/IR/QuantDialectBytecode.cpp (revision 852b6486246141e44cc9f126f542a2ae0d73b3d6)
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 
10 #include "QuantDialectBytecode.h"
11 #include "mlir/Bytecode/BytecodeImplementation.h"
12 #include "mlir/Dialect/Quant/IR/Quant.h"
13 #include "mlir/Dialect/Quant/IR/QuantTypes.h"
14 #include "mlir/IR/Diagnostics.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/TypeSwitch.h"
18 
19 using namespace mlir;
20 using namespace mlir::quant;
21 
22 namespace {
23 
24 static LogicalResult readDoubleAPFloat(DialectBytecodeReader &reader,
25                                        double &val) {
26   auto valOr =
27       reader.readAPFloatWithKnownSemantics(llvm::APFloat::IEEEdouble());
28   if (failed(valOr))
29     return failure();
30   val = valOr->convertToDouble();
31   return success();
32 }
33 
34 #include "mlir/Dialect/Quant/IR/QuantDialectBytecode.cpp.inc"
35 
36 /// This class implements the bytecode interface for the Quant dialect.
37 struct QuantDialectBytecodeInterface : public BytecodeDialectInterface {
38   QuantDialectBytecodeInterface(Dialect *dialect)
39       : BytecodeDialectInterface(dialect) {}
40 
41   //===--------------------------------------------------------------------===//
42   // Attributes
43 
44   Attribute readAttribute(DialectBytecodeReader &reader) const override {
45     return ::readAttribute(getContext(), reader);
46   }
47 
48   LogicalResult writeAttribute(Attribute attr,
49                                DialectBytecodeWriter &writer) const override {
50     return ::writeAttribute(attr, writer);
51   }
52 
53   //===--------------------------------------------------------------------===//
54   // Types
55 
56   Type readType(DialectBytecodeReader &reader) const override {
57     return ::readType(getContext(), reader);
58   }
59 
60   LogicalResult writeType(Type type,
61                           DialectBytecodeWriter &writer) const override {
62     return ::writeType(type, writer);
63   }
64 };
65 } // namespace
66 
67 void quant::detail::addBytecodeInterface(QuantDialect *dialect) {
68   dialect->addInterfaces<QuantDialectBytecodeInterface>();
69 }
70