From e8f7d019fc21a300cee0dc9281706ee6d2e4d793 Mon Sep 17 00:00:00 2001 From: Alexander Belyaev Date: Wed, 4 May 2022 17:46:17 +0200 Subject: [PATCH] [mlir] Add a flag to allow equivalent results. Differential Revision: https://reviews.llvm.org/D124931 --- .../Bufferization/Transforms/OneShotAnalysis.h | 4 ++ .../Dialect/Bufferization/Transforms/Passes.td | 3 ++ .../Dialect/Bufferization/Transforms/Bufferize.cpp | 1 + .../Transforms/FuncBufferizableOpInterfaceImpl.cpp | 58 ++++++++++++---------- ...-shot-module-bufferize-allow-return-allocs.mlir | 14 ++++++ 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h b/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h index 859b138..2a60201 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h @@ -47,6 +47,10 @@ struct OneShotBufferizationOptions : public BufferizationOptions { /// Specifies whether returning newly allocated memrefs should be allowed. /// Otherwise, a pass failure is triggered. bool allowReturnAllocs = false; + + /// Specifies whether buffer return values that are equivalent to a FuncOp + /// bbArg should be dropped. + bool dropEquivalentFuncResults = true; }; /// The BufferizationAliasInfo class maintains a list of buffer aliases and diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td index 37d4a3e..1820df8 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td @@ -230,6 +230,9 @@ def OneShotBufferize : Pass<"one-shot-bufferize", "ModuleOp"> { `test-analysis-only`. }]; let options = [ + Option<"dropEquivalentFuncResults", "drop-equivalent-func-results", "bool", + /*default=*/"true", + "Drop buffer return values that are equivalent to a FuncOp arg.">, Option<"allowReturnAllocs", "allow-return-allocs", "bool", /*default=*/"false", "Allows returning/yielding new allocations from a block.">, diff --git a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp index a16b191..cee7dfc 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp @@ -169,6 +169,7 @@ struct OneShotBufferizePass if (!options) { // Make new bufferization options if none were provided when creating the // pass. + opt.dropEquivalentFuncResults = dropEquivalentFuncResults; opt.allowReturnAllocs = allowReturnAllocs; opt.allowUnknownOps = allowUnknownOps; opt.alwaysAliasingWithDest = alwaysAliasingWithDest; diff --git a/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp b/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp index df29e7a..25d3df2 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp @@ -269,17 +269,19 @@ struct CallOpInterface continue; } - if (Optional bbArgIdx = - getEquivalentFuncArgIdx(funcOp, funcState, returnValIdx)) { - // Return operands that are equivalent to some bbArg, are not - // returned. - FailureOr bufferOrFailure = - state.getBuffer(rewriter, callOp->getOpOperand(*bbArgIdx)); - if (failed(bufferOrFailure)) - return failure(); - replacementValues[returnValIdx] = *bufferOrFailure; - newOperands[*bbArgIdx] = *bufferOrFailure; - continue; + if (options.dropEquivalentFuncResults) { + if (Optional bbArgIdx = + getEquivalentFuncArgIdx(funcOp, funcState, returnValIdx)) { + // Return operands that are equivalent to some bbArg, are not + // returned. + FailureOr bufferOrFailure = + state.getBuffer(rewriter, callOp->getOpOperand(*bbArgIdx)); + if (failed(bufferOrFailure)) + return failure(); + replacementValues[returnValIdx] = *bufferOrFailure; + newOperands[*bbArgIdx] = *bufferOrFailure; + continue; + } } if (!options.allowReturnAllocs) @@ -404,7 +406,8 @@ struct FuncOpInterface FunctionType funcType = funcOp.getFunctionType(); const FuncAnalysisState &funcState = getFuncAnalysisState(state.getAnalysisState()); - const BufferizationOptions &options = state.getOptions(); + const OneShotBufferizationOptions &options = + static_cast(state.getOptions()); // Construct the bufferized function type. SmallVector argTypes; @@ -479,20 +482,23 @@ struct FuncOpInterface } // If return operand is equivalent to some bbArg, no need to return it. - if (Optional equivBbArgIdx = getEquivalentFuncArgIdx( - funcOp, funcState, returnOperand.getOperandNumber())) { - rewriter.setInsertionPoint(returnOp); - Location loc = returnOp.getLoc(); - Value toMemrefOp = rewriter.create( - loc, getMemRefType(returnVal.getType().cast(), options), - returnVal); - BlockArgument equivBbArg = funcOp.getArgument(*equivBbArgIdx); - // Note: This copy will fold away. It must be inserted here to ensure - // that `returnVal` still has at least one use and does not fold away. - if (failed( - createMemCpy(rewriter, loc, toMemrefOp, equivBbArg, options))) - return funcOp->emitError("could not generate copy for bbArg"); - continue; + if (options.dropEquivalentFuncResults) { + if (Optional equivBbArgIdx = getEquivalentFuncArgIdx( + funcOp, funcState, returnOperand.getOperandNumber())) { + rewriter.setInsertionPoint(returnOp); + Location loc = returnOp.getLoc(); + Value toMemrefOp = rewriter.create( + loc, + getMemRefType(returnVal.getType().cast(), options), + returnVal); + BlockArgument equivBbArg = funcOp.getArgument(*equivBbArgIdx); + // Note: This copy will fold away. It must be inserted here to ensure + // that `returnVal` still has at least one use and does not fold away. + if (failed( + createMemCpy(rewriter, loc, toMemrefOp, equivBbArg, options))) + return funcOp->emitError("could not generate copy for bbArg"); + continue; + } } returnValues.push_back(*state.getBuffer(rewriter, returnOperand)); diff --git a/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-allow-return-allocs.mlir b/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-allow-return-allocs.mlir index 509c96f..4ef9995 100644 --- a/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-allow-return-allocs.mlir +++ b/mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-allow-return-allocs.mlir @@ -1,4 +1,5 @@ // RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries=1 allow-return-allocs" -split-input-file | FileCheck %s +// RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries=1 allow-return-allocs drop-equivalent-func-results=false" -split-input-file | FileCheck %s --check-prefix=EQUIV // Run fuzzer with different seeds. // RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries=1 allow-return-allocs test-analysis-only analysis-fuzzer-seed=23" -split-input-file -o /dev/null @@ -62,3 +63,16 @@ func.func @main(%t: tensor, %sz: index, %idx: index) -> (f32, f32) { %r2 = tensor.extract %filled[%idx] : tensor return %r1, %r2 : f32, f32 } + +// ----- + +func.func @return_arg(%A: tensor) -> tensor { + func.return %A : tensor +} +// CHECK-LABEL: func @return_arg +// CHECK-SAME: %[[A:.*]]: memref