1 //===- ConvertConst.cpp - Quantizes constant ops --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PassDetail.h"
10 #include "mlir/Dialect/Quant/Passes.h"
11 #include "mlir/Dialect/Quant/QuantOps.h"
12 #include "mlir/Dialect/Quant/QuantizeUtils.h"
13 #include "mlir/Dialect/Quant/UniformSupport.h"
14 #include "mlir/Dialect/StandardOps/IR/Ops.h"
15 #include "mlir/IR/Attributes.h"
16 #include "mlir/IR/Matchers.h"
17 #include "mlir/IR/PatternMatch.h"
18 #include "mlir/IR/StandardTypes.h"
19 
20 using namespace mlir;
21 using namespace mlir::quant;
22 
23 namespace {
24 struct ConvertConstPass : public QuantConvertConstBase<ConvertConstPass> {
25   void runOnFunction() override;
26 };
27 
28 struct QuantizedConstRewrite : public OpRewritePattern<QuantizeCastOp> {
29   using OpRewritePattern<QuantizeCastOp>::OpRewritePattern;
30 
31   LogicalResult matchAndRewrite(QuantizeCastOp qbarrier,
32                                 PatternRewriter &rewriter) const override;
33 };
34 
35 } // end anonymous namespace
36 
37 /// Matches a [constant] -> [qbarrier] where the qbarrier results type is
38 /// quantized and the operand type is quantizable.
39 
40 LogicalResult
41 QuantizedConstRewrite::matchAndRewrite(QuantizeCastOp qbarrier,
42                                        PatternRewriter &rewriter) const {
43   Attribute value;
44 
45   // Is the operand a constant?
46   if (!matchPattern(qbarrier.arg(), m_Constant(&value))) {
47     return failure();
48   }
49 
50   // Does the qbarrier convert to a quantized type. This will not be true
51   // if a quantized type has not yet been chosen or if the cast to an equivalent
52   // storage type is not supported.
53   Type qbarrierResultType = qbarrier.getResult().getType();
54   QuantizedType quantizedElementType =
55       QuantizedType::getQuantizedElementType(qbarrierResultType);
56   if (!quantizedElementType) {
57     return failure();
58   }
59   if (!QuantizedType::castToStorageType(qbarrierResultType)) {
60     return failure();
61   }
62 
63   // Is the operand type compatible with the expressed type of the quantized
64   // type? This will not be true if the qbarrier is superfluous (converts
65   // from and to a quantized type).
66   if (!quantizedElementType.isCompatibleExpressedType(
67           qbarrier.arg().getType())) {
68     return failure();
69   }
70 
71   // Is the constant value a type expressed in a way that we support?
72   if (!value.isa<FloatAttr>() && !value.isa<DenseElementsAttr>() &&
73       !value.isa<SparseElementsAttr>()) {
74     return failure();
75   }
76 
77   Type newConstValueType;
78   auto newConstValue =
79       quantizeAttr(value, quantizedElementType, newConstValueType);
80   if (!newConstValue) {
81     return failure();
82   }
83 
84   // When creating the new const op, use a fused location that combines the
85   // original const and the qbarrier that led to the quantization.
86   auto fusedLoc = FusedLoc::get(
87       {qbarrier.arg().getDefiningOp()->getLoc(), qbarrier.getLoc()},
88       rewriter.getContext());
89   auto newConstOp =
90       rewriter.create<ConstantOp>(fusedLoc, newConstValueType, newConstValue);
91   rewriter.replaceOpWithNewOp<StorageCastOp>(qbarrier, qbarrier.getType(),
92                                              newConstOp);
93   return success();
94 }
95 
96 void ConvertConstPass::runOnFunction() {
97   OwningRewritePatternList patterns;
98   auto func = getFunction();
99   auto *context = &getContext();
100   patterns.insert<QuantizedConstRewrite>(context);
101   applyPatternsAndFoldGreedily(func, patterns);
102 }
103 
104 std::unique_ptr<OperationPass<FuncOp>> mlir::quant::createConvertConstPass() {
105   return std::make_unique<ConvertConstPass>();
106 }
107