Matthias Springer ab47418df6 [mlir][bufferize] Merge tensor-constant-bufferize into arith-bufferize
The bufferization of arith.constant ops is also switched over to BufferizableOpInterface-based bufferization. The old implementation is deleted. Both implementations utilize GlobalCreator, now renamed to just `getGlobalFor`.

GlobalCreator no longer maintains a set of all created allocations to avoid duplicate allocations of the same constant. Instead, `getGlobalFor` scans the module to see if there is already a global allocation with the same constant value.

For compatibility reasons, it is still possible to create a pass that bufferizes only `arith.constant`. This pass (createConstantBufferizePass) could be deleted once all users were switched over to One-Shot bufferization.

Differential Revision: https://reviews.llvm.org/D118483
2022-01-30 21:37:48 +09:00

65 lines
2.2 KiB
C++

//===- Bufferize.cpp - Bufferization for Arithmetic ops ---------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "PassDetail.h"
#include "mlir/Dialect/Arithmetic/Transforms/BufferizableOpInterfaceImpl.h"
#include "mlir/Dialect/Arithmetic/Transforms/Passes.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
using namespace mlir;
using namespace bufferization;
namespace {
/// Pass to bufferize Arithmetic ops.
struct ArithmeticBufferizePass
: public ArithmeticBufferizeBase<ArithmeticBufferizePass> {
ArithmeticBufferizePass(uint64_t alignment = 0, bool constantOpOnly = false)
: ArithmeticBufferizeBase<ArithmeticBufferizePass>(),
constantOpOnly(constantOpOnly) {
this->alignment = alignment;
}
void runOnOperation() override {
std::unique_ptr<BufferizationOptions> options =
getPartialBufferizationOptions();
if (constantOpOnly) {
options->addToOperationFilter<arith::ConstantOp>();
} else {
options->addToDialectFilter<arith::ArithmeticDialect>();
}
options->bufferAlignment = alignment;
if (failed(bufferizeOp(getOperation(), *options)))
signalPassFailure();
}
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<bufferization::BufferizationDialect, memref::MemRefDialect,
arith::ArithmeticDialect>();
arith::registerBufferizableOpInterfaceExternalModels(registry);
}
private:
bool constantOpOnly;
};
} // namespace
std::unique_ptr<Pass> mlir::arith::createArithmeticBufferizePass() {
return std::make_unique<ArithmeticBufferizePass>();
}
std::unique_ptr<Pass>
mlir::arith::createConstantBufferizePass(uint64_t alignment) {
return std::make_unique<ArithmeticBufferizePass>(alignment,
/*constantOpOnly=*/true);
}