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