MLIR  20.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 
10 #include "QuantDialectBytecode.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 }
static MLIRContext * getContext(OpFoldResult val)
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
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...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
void addBytecodeInterface(QuantDialect *dialect)
Add the interfaces necessary for encoding the quantization dialect components in bytecode.
Include the generated interface declarations.