-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mlir][linalg][bufferize] Support std.select bufferization
This op is an example for how to deal with ops who's OpResult may aliasing with one of multiple OpOperands. Differential Revision: https://reviews.llvm.org/D116868
- Loading branch information
1 parent
5642ce5
commit 6c654b5
Showing
17 changed files
with
333 additions
and
60 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/AffineInterfaceImpl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
mlir/include/mlir/Dialect/Linalg/ComprehensiveBufferize/StdInterfaceImpl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===- StdInterfaceImpl.h - Standard Impl. of BufferizableOpInterface- ----===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_LINALG_COMPREHENSIVEBUFFERIZE_STD_INTERFACE_IMPL_H | ||
#define MLIR_DIALECT_LINALG_COMPREHENSIVEBUFFERIZE_STD_INTERFACE_IMPL_H | ||
|
||
namespace mlir { | ||
|
||
class DialectRegistry; | ||
|
||
namespace linalg { | ||
namespace comprehensive_bufferize { | ||
namespace std_ext { | ||
|
||
void registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry); | ||
|
||
} // namespace std_ext | ||
} // namespace comprehensive_bufferize | ||
} // namespace linalg | ||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_LINALG_COMPREHENSIVEBUFFERIZE_STD_INTERFACE_IMPL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
mlir/lib/Dialect/Linalg/ComprehensiveBufferize/StdInterfaceImpl.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//===- StdInterfaceImpl.cpp - Standard Impl. of BufferizableOpInterface ---===// | ||
// | ||
// 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/Linalg/ComprehensiveBufferize/StdInterfaceImpl.h" | ||
|
||
#include "mlir/Dialect/Linalg/ComprehensiveBufferize/BufferizableOpInterface.h" | ||
#include "mlir/Dialect/StandardOps/IR/Ops.h" | ||
#include "mlir/IR/Dialect.h" | ||
#include "mlir/IR/Operation.h" | ||
|
||
namespace mlir { | ||
namespace linalg { | ||
namespace comprehensive_bufferize { | ||
namespace std_ext { | ||
|
||
/// Bufferization of std.select. Just replace the operands. | ||
struct SelectOpInterface | ||
: public BufferizableOpInterface::ExternalModel<SelectOpInterface, | ||
SelectOp> { | ||
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, | ||
const BufferizationState &state) const { | ||
return false; | ||
} | ||
|
||
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, | ||
const BufferizationState &state) const { | ||
return false; | ||
} | ||
|
||
OpResult getAliasingOpResult(Operation *op, OpOperand &opOperand, | ||
const BufferizationState &state) const { | ||
return op->getOpResult(0) /*result*/; | ||
} | ||
|
||
SmallVector<OpOperand *> | ||
getAliasingOpOperand(Operation *op, OpResult opResult, | ||
const BufferizationState &state) const { | ||
return {&op->getOpOperand(1) /*true_value*/, | ||
&op->getOpOperand(2) /*false_value*/}; | ||
} | ||
|
||
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, | ||
const BufferizationState &state) const { | ||
auto selectOp = cast<SelectOp>(op); | ||
// `getBuffer` introduces copies if an OpOperand bufferizes out-of-place. | ||
// TODO: It would be more efficient to copy the result of the `select` op | ||
// instead of its OpOperands. In the worst case, 2 copies are inserted at | ||
// the moment (one for each tensor). When copying the op result, only one | ||
// copy would be needed. | ||
Value trueBuffer = | ||
*state.getBuffer(rewriter, selectOp->getOpOperand(1) /*true_value*/); | ||
Value falseBuffer = | ||
*state.getBuffer(rewriter, selectOp->getOpOperand(2) /*false_value*/); | ||
replaceOpWithNewBufferizedOp<SelectOp>( | ||
rewriter, op, selectOp.getCondition(), trueBuffer, falseBuffer); | ||
return success(); | ||
} | ||
|
||
BufferRelation bufferRelation(Operation *op, OpResult opResult, | ||
const BufferizationAliasInfo &aliasInfo, | ||
const BufferizationState &state) const { | ||
return BufferRelation::None; | ||
} | ||
}; | ||
|
||
} // namespace std_ext | ||
} // namespace comprehensive_bufferize | ||
} // namespace linalg | ||
} // namespace mlir | ||
|
||
void mlir::linalg::comprehensive_bufferize::std_ext:: | ||
registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry) { | ||
registry.addOpInterface<SelectOp, std_ext::SelectOpInterface>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.