1 //===- Bufferize.cpp - Bufferization for std 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 // This file implements bufferization of tensor-valued std.constant ops.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PassDetail.h"
14 #include "mlir/Dialect/StandardOps/IR/Ops.h"
15 #include "mlir/Dialect/StandardOps/Transforms/Passes.h"
16 #include "mlir/IR/BlockAndValueMapping.h"
17 #include "mlir/Transforms/Bufferize.h"
18 #include "mlir/Transforms/DialectConversion.h"
19 
20 using namespace mlir;
21 
22 namespace {
23 // This class creates global ops for all tensor-valued constants in the program.
24 // It creates them with pretty names and makes sure that duplicate globals
25 // aren't created.
26 class GlobalCreator {
27 public:
28   explicit GlobalCreator(ModuleOp module);
getGlobalFor(Attribute attr)29   GlobalMemrefOp getGlobalFor(Attribute attr) {
30     assert(globals.find(attr) != globals.end() && "unknown constant attr");
31     return globals[attr];
32   }
33 
34 private:
35   DenseMap<Attribute, GlobalMemrefOp> globals;
36 };
37 
GlobalCreator(ModuleOp module)38 GlobalCreator::GlobalCreator(ModuleOp module) {
39   BufferizeTypeConverter typeConverter;
40   // Create a builder without an insertion point. We will insert using the
41   // symbol table to guarantee unique names.
42   OpBuilder globalBuilder(module.getContext());
43   SymbolTable symbolTable(module);
44   module.walk([&](ConstantOp op) {
45     // We only want tensor constants for now.
46     auto type = op.getType().dyn_cast<RankedTensorType>();
47     if (!type)
48       return;
49     // If we already have a global for this constant value, no need to do
50     // anything else.
51     auto it = globals.find(op.getValue());
52     if (it != globals.end())
53       return;
54 
55     // Create a pretty name.
56     SmallString<64> buf;
57     llvm::raw_svector_ostream os(buf);
58     interleave(type.getShape(), os, "x");
59     os << "x" << type.getElementType();
60 
61     auto global = globalBuilder.create<GlobalMemrefOp>(
62         op.getLoc(), (Twine("__constant_") + os.str()).str(),
63         /*sym_visibility=*/globalBuilder.getStringAttr("private"),
64         /*type=*/
65         TypeAttr::get(typeConverter.convertType(type)), /*initial_value=*/
66         op.getValue().cast<ElementsAttr>(), /*constant=*/true);
67     symbolTable.insert(global);
68     // The symbol table inserts at the end of the module, but globals are a bit
69     // nicer if they are at the beginning.
70     global->moveBefore(&module.front());
71     globals[op.getValue()] = global;
72   });
73 }
74 } // namespace
75 
76 namespace {
77 class BufferizeTensorConstantOp : public OpConversionPattern<ConstantOp> {
78 public:
BufferizeTensorConstantOp(GlobalCreator & globals,TypeConverter & typeConverter,MLIRContext * context)79   BufferizeTensorConstantOp(GlobalCreator &globals,
80                             TypeConverter &typeConverter, MLIRContext *context)
81       : OpConversionPattern<ConstantOp>(typeConverter, context, /*benefit=*/1),
82         globals(globals) {}
83 
84   LogicalResult
matchAndRewrite(ConstantOp op,ArrayRef<Value> operands,ConversionPatternRewriter & rewriter) const85   matchAndRewrite(ConstantOp op, ArrayRef<Value> operands,
86                   ConversionPatternRewriter &rewriter) const override {
87     auto type = op.getType().dyn_cast<RankedTensorType>();
88     if (!type)
89       return failure();
90 
91     auto globalMemref = globals.getGlobalFor(op.value());
92     rewriter.replaceOpWithNewOp<GetGlobalMemrefOp>(op, globalMemref.type(),
93                                                    globalMemref.getName());
94     return success();
95   }
96   GlobalCreator &globals;
97 };
98 } // namespace
99 
100 namespace {
101 struct TensorConstantBufferizePass
102     : public TensorConstantBufferizeBase<TensorConstantBufferizePass> {
runOnOperation__anonfd1905150411::TensorConstantBufferizePass103   void runOnOperation() override {
104     auto module = getOperation();
105     GlobalCreator globals(module);
106 
107     auto *context = &getContext();
108     BufferizeTypeConverter typeConverter;
109     OwningRewritePatternList patterns;
110     ConversionTarget target(*context);
111 
112     target.addLegalDialect<StandardOpsDialect>();
113     patterns.insert<BufferizeTensorConstantOp>(globals, typeConverter, context);
114     target.addDynamicallyLegalOp<ConstantOp>(
115         [&](ConstantOp op) { return typeConverter.isLegal(op.getType()); });
116     if (failed(applyPartialConversion(module, target, std::move(patterns))))
117       signalPassFailure();
118   }
119 };
120 } // namespace
121 
createTensorConstantBufferizePass()122 std::unique_ptr<Pass> mlir::createTensorConstantBufferizePass() {
123   return std::make_unique<TensorConstantBufferizePass>();
124 }
125