MLIR  19.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"
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 }
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:41
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
void addBytecodeInterface(QuantizationDialect *dialect)
Add the interfaces necessary for encoding the quantization dialect components in bytecode.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26