Skip to content

Commit

Permalink
[mlir][tensor] Fix tensor.pad to remove newly static values (llvm#7…
Browse files Browse the repository at this point in the history
…9938)

The canonicalization incrementally converts foldable dynamic hi/lo
padding to static hi/lo values. During this canonicalization the
static-fied valued should be removed from the dynamic values.
  • Loading branch information
rsuderman authored Jan 30, 2024
1 parent 198652a commit 70eb0e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3158,19 +3158,23 @@ struct FoldStaticPadding : public OpRewritePattern<PadOp> {

// Extract the static info from the high and low operands.
SmallVector<int64_t> constOperandsLow;
SmallVector<Value> newLows;
for (auto operand : padTensorOp.getLow()) {
APSInt intOp;
if (!matchPattern(operand, m_ConstantInt(&intOp))) {
constOperandsLow.push_back(ShapedType::kDynamic);
newLows.push_back(operand);
continue;
}
constOperandsLow.push_back(intOp.getExtValue());
}
SmallVector<int64_t> constOperandsHigh;
SmallVector<Value> newHighs;
for (auto operand : padTensorOp.getHigh()) {
APSInt intOp;
if (!matchPattern(operand, m_ConstantInt(&intOp))) {
constOperandsHigh.push_back(ShapedType::kDynamic);
newHighs.push_back(operand);
continue;
}
constOperandsHigh.push_back(intOp.getExtValue());
Expand Down Expand Up @@ -3222,7 +3226,7 @@ struct FoldStaticPadding : public OpRewritePattern<PadOp> {
newOutDims, padTensorOp.getType().getElementType());
auto newOp = rewriter.create<PadOp>(
padTensorOp->getLoc(), newResultType, input, staticLow, staticHigh,
padTensorOp.getLow(), padTensorOp.getHigh(), padTensorOp.getNofold(),
newLows, newHighs, padTensorOp.getNofold(),
getPrunedAttributeList(padTensorOp, PadOp::getAttributeNames()));

IRMapping mapper;
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Tensor/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ func.func @pad_same_static_shape(%arg0: tensor<5x6xf32>, %a: index)
// CHECK-LABEL: func @pad_fold_static(
// CHECK-SAME: %[[INPUT:.*]]: tensor<?x64x?x?xf32>) -> tensor<?x?x?x?xf32> {
// CHECK: %[[CST:.*]] = arith.constant 0.000000e+00 : f32
// CHECK: %[[PADDING:.*]] = arith.constant 4 : index
// CHECK-NOT: arith.constant 4 : index
// CHECK: %[[PADDED:.*]] = tensor.pad %[[INPUT]]
// CHECK-SAME: low[0, 4, 1, 1] high[0, 4, 1, 1] {
// CHECK: ^bb0(%[[ARG1:.*]]: index, %[[ARG2:.*]]: index, %[[ARG3:.*]]: index, %[[ARG4:.*]]: index):
Expand Down

0 comments on commit 70eb0e3

Please sign in to comment.