The MLIR classes Type/Attribute/Operation/Op/Value support cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast functionality in addition to defining methods with the same name. This change begins the migration of uses of the method to the corresponding function call as has been decided as more consistent. Note that there still exist classes that only define methods directly, such as AffineExpr, and this does not include work currently to support a functional cast/isa call. Caveats include: - This clang-tidy script probably has more problems. - This only touches C++ code, so nothing that is being generated. Context: - https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…" - Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443 Implementation: This first patch was created with the following steps. The intention is to only do automated changes at first, so I waste less time if it's reverted, and so the first mass change is more clear as an example to other teams that will need to follow similar steps. Steps are described per line, as comments are removed by git: 0. Retrieve the change from the following to build clang-tidy with an additional check: https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check 1. Build clang-tidy 2. Run clang-tidy over your entire codebase while disabling all checks and enabling the one relevant one. Run on all header files also. 3. Delete .inc files that were also modified, so the next build rebuilds them to a pure state. 4. Some changes have been deleted for the following reasons: - Some files had a variable also named cast - Some files had not included a header file that defines the cast functions - Some files are definitions of the classes that have the casting methods, so the code still refers to the method instead of the function without adding a prefix or removing the method declaration at the same time. ``` ninja -C $BUILD_DIR clang-tidy run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\ -header-filter=mlir/ mlir/* -fix rm -rf $BUILD_DIR/tools/mlir/**/*.inc git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\ mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\ mlir/lib/**/IR/\ mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\ mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\ mlir/test/lib/Dialect/Test/TestTypes.cpp\ mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\ mlir/test/lib/Dialect/Test/TestAttributes.cpp\ mlir/unittests/TableGen/EnumsGenTest.cpp\ mlir/test/python/lib/PythonTestCAPI.cpp\ mlir/include/mlir/IR/ ``` Differential Revision: https://reviews.llvm.org/D150123
225 lines
8.3 KiB
C++
225 lines
8.3 KiB
C++
//===- BufferResultsToOutParams.cpp - Calling convention conversion -------===//
|
|
//
|
|
// 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 "mlir/Dialect/Bufferization/Transforms/Passes.h"
|
|
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
|
#include "mlir/IR/Operation.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
namespace mlir {
|
|
namespace bufferization {
|
|
#define GEN_PASS_DEF_BUFFERRESULTSTOOUTPARAMS
|
|
#include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
|
|
} // namespace bufferization
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
|
|
/// Return `true` if the given MemRef type has a fully dynamic layout.
|
|
static bool hasFullyDynamicLayoutMap(MemRefType type) {
|
|
int64_t offset;
|
|
SmallVector<int64_t, 4> strides;
|
|
if (failed(getStridesAndOffset(type, strides, offset)))
|
|
return false;
|
|
if (!llvm::all_of(strides, ShapedType::isDynamic))
|
|
return false;
|
|
if (!ShapedType::isDynamic(offset))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/// Return `true` if the given MemRef type has a static identity layout (i.e.,
|
|
/// no layout).
|
|
static bool hasStaticIdentityLayout(MemRefType type) {
|
|
return type.getLayout().isIdentity();
|
|
}
|
|
|
|
// Updates the func op and entry block.
|
|
//
|
|
// Any args appended to the entry block are added to `appendedEntryArgs`.
|
|
static LogicalResult
|
|
updateFuncOp(func::FuncOp func,
|
|
SmallVectorImpl<BlockArgument> &appendedEntryArgs) {
|
|
auto functionType = func.getFunctionType();
|
|
|
|
// Collect information about the results will become appended arguments.
|
|
SmallVector<Type, 6> erasedResultTypes;
|
|
BitVector erasedResultIndices(functionType.getNumResults());
|
|
for (const auto &resultType : llvm::enumerate(functionType.getResults())) {
|
|
if (auto memrefType = dyn_cast<MemRefType>(resultType.value())) {
|
|
if (!hasStaticIdentityLayout(memrefType) &&
|
|
!hasFullyDynamicLayoutMap(memrefType)) {
|
|
// Only buffers with static identity layout can be allocated. These can
|
|
// be casted to memrefs with fully dynamic layout map. Other layout maps
|
|
// are not supported.
|
|
return func->emitError()
|
|
<< "cannot create out param for result with unsupported layout";
|
|
}
|
|
erasedResultIndices.set(resultType.index());
|
|
erasedResultTypes.push_back(memrefType);
|
|
}
|
|
}
|
|
|
|
// Add the new arguments to the function type.
|
|
auto newArgTypes = llvm::to_vector<6>(
|
|
llvm::concat<const Type>(functionType.getInputs(), erasedResultTypes));
|
|
auto newFunctionType = FunctionType::get(func.getContext(), newArgTypes,
|
|
functionType.getResults());
|
|
func.setType(newFunctionType);
|
|
|
|
// Transfer the result attributes to arg attributes.
|
|
auto erasedIndicesIt = erasedResultIndices.set_bits_begin();
|
|
for (int i = 0, e = erasedResultTypes.size(); i < e; ++i, ++erasedIndicesIt) {
|
|
func.setArgAttrs(functionType.getNumInputs() + i,
|
|
func.getResultAttrs(*erasedIndicesIt));
|
|
}
|
|
|
|
// Erase the results.
|
|
func.eraseResults(erasedResultIndices);
|
|
|
|
// Add the new arguments to the entry block if the function is not external.
|
|
if (func.isExternal())
|
|
return success();
|
|
Location loc = func.getLoc();
|
|
for (Type type : erasedResultTypes)
|
|
appendedEntryArgs.push_back(func.front().addArgument(type, loc));
|
|
|
|
return success();
|
|
}
|
|
|
|
// Updates all ReturnOps in the scope of the given func::FuncOp by either
|
|
// keeping them as return values or copying the associated buffer contents into
|
|
// the given out-params.
|
|
static void updateReturnOps(func::FuncOp func,
|
|
ArrayRef<BlockArgument> appendedEntryArgs) {
|
|
func.walk([&](func::ReturnOp op) {
|
|
SmallVector<Value, 6> copyIntoOutParams;
|
|
SmallVector<Value, 6> keepAsReturnOperands;
|
|
for (Value operand : op.getOperands()) {
|
|
if (isa<MemRefType>(operand.getType()))
|
|
copyIntoOutParams.push_back(operand);
|
|
else
|
|
keepAsReturnOperands.push_back(operand);
|
|
}
|
|
OpBuilder builder(op);
|
|
for (auto t : llvm::zip(copyIntoOutParams, appendedEntryArgs))
|
|
builder.create<memref::CopyOp>(op.getLoc(), std::get<0>(t),
|
|
std::get<1>(t));
|
|
builder.create<func::ReturnOp>(op.getLoc(), keepAsReturnOperands);
|
|
op.erase();
|
|
});
|
|
}
|
|
|
|
// Updates all CallOps in the scope of the given ModuleOp by allocating
|
|
// temporary buffers for newly introduced out params.
|
|
static LogicalResult
|
|
updateCalls(ModuleOp module,
|
|
const bufferization::BufferResultsToOutParamsOptions &options) {
|
|
bool didFail = false;
|
|
SymbolTable symtab(module);
|
|
module.walk([&](func::CallOp op) {
|
|
auto callee = symtab.lookup<func::FuncOp>(op.getCallee());
|
|
if (!callee) {
|
|
op.emitError() << "cannot find callee '" << op.getCallee() << "' in "
|
|
<< "symbol table";
|
|
didFail = true;
|
|
return;
|
|
}
|
|
if (!options.filterFn(&callee))
|
|
return;
|
|
SmallVector<Value, 6> replaceWithNewCallResults;
|
|
SmallVector<Value, 6> replaceWithOutParams;
|
|
for (OpResult result : op.getResults()) {
|
|
if (isa<MemRefType>(result.getType()))
|
|
replaceWithOutParams.push_back(result);
|
|
else
|
|
replaceWithNewCallResults.push_back(result);
|
|
}
|
|
SmallVector<Value, 6> outParams;
|
|
OpBuilder builder(op);
|
|
for (Value memref : replaceWithOutParams) {
|
|
if (!cast<MemRefType>(memref.getType()).hasStaticShape()) {
|
|
op.emitError()
|
|
<< "cannot create out param for dynamically shaped result";
|
|
didFail = true;
|
|
return;
|
|
}
|
|
auto memrefType = cast<MemRefType>(memref.getType());
|
|
auto allocType =
|
|
MemRefType::get(memrefType.getShape(), memrefType.getElementType(),
|
|
AffineMap(), memrefType.getMemorySpace());
|
|
Value outParam = builder.create<memref::AllocOp>(op.getLoc(), allocType);
|
|
if (!hasStaticIdentityLayout(memrefType)) {
|
|
// Layout maps are already checked in `updateFuncOp`.
|
|
assert(hasFullyDynamicLayoutMap(memrefType) &&
|
|
"layout map not supported");
|
|
outParam =
|
|
builder.create<memref::CastOp>(op.getLoc(), memrefType, outParam);
|
|
}
|
|
memref.replaceAllUsesWith(outParam);
|
|
outParams.push_back(outParam);
|
|
}
|
|
|
|
auto newOperands = llvm::to_vector<6>(op.getOperands());
|
|
newOperands.append(outParams.begin(), outParams.end());
|
|
auto newResultTypes = llvm::to_vector<6>(llvm::map_range(
|
|
replaceWithNewCallResults, [](Value v) { return v.getType(); }));
|
|
auto newCall = builder.create<func::CallOp>(op.getLoc(), op.getCalleeAttr(),
|
|
newResultTypes, newOperands);
|
|
for (auto t : llvm::zip(replaceWithNewCallResults, newCall.getResults()))
|
|
std::get<0>(t).replaceAllUsesWith(std::get<1>(t));
|
|
op.erase();
|
|
});
|
|
|
|
return failure(didFail);
|
|
}
|
|
|
|
LogicalResult mlir::bufferization::promoteBufferResultsToOutParams(
|
|
ModuleOp module,
|
|
const bufferization::BufferResultsToOutParamsOptions &options) {
|
|
for (auto func : module.getOps<func::FuncOp>()) {
|
|
if (!options.filterFn(&func))
|
|
continue;
|
|
SmallVector<BlockArgument, 6> appendedEntryArgs;
|
|
if (failed(updateFuncOp(func, appendedEntryArgs)))
|
|
return failure();
|
|
if (func.isExternal())
|
|
continue;
|
|
updateReturnOps(func, appendedEntryArgs);
|
|
}
|
|
if (failed(updateCalls(module, options)))
|
|
return failure();
|
|
return success();
|
|
}
|
|
|
|
namespace {
|
|
struct BufferResultsToOutParamsPass
|
|
: bufferization::impl::BufferResultsToOutParamsBase<
|
|
BufferResultsToOutParamsPass> {
|
|
explicit BufferResultsToOutParamsPass(
|
|
const bufferization::BufferResultsToOutParamsOptions &options)
|
|
: options(options) {}
|
|
|
|
void runOnOperation() override {
|
|
if (failed(bufferization::promoteBufferResultsToOutParams(getOperation(),
|
|
options)))
|
|
return signalPassFailure();
|
|
}
|
|
|
|
private:
|
|
bufferization::BufferResultsToOutParamsOptions options;
|
|
};
|
|
} // namespace
|
|
|
|
std::unique_ptr<Pass> mlir::bufferization::createBufferResultsToOutParamsPass(
|
|
const bufferization::BufferResultsToOutParamsOptions &options) {
|
|
return std::make_unique<BufferResultsToOutParamsPass>(options);
|
|
}
|