Skip to content

Commit

Permalink
[Flow] Raise special linalg.generic ops to linalg.fill ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Max191 committed Aug 22, 2023
1 parent 2a914c1 commit 2281e94
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ std::optional<Value> matchATransposeBMatmul(linalg::LinalgOp matmulOp) {
return std::nullopt;
}

// Method to match a linalg.generic op representing a linalg.fill op. Returns
// the fill value (input operand to linalg.fill) on success.
std::optional<Value> matchGenericFill(linalg::LinalgOp linalgOp) {
if (isa<linalg::GenericOp>(linalgOp.getOperation()) &&
linalgOp.getNumDpsInputs() == 0 && linalgOp.getNumDpsInits() == 1 &&
linalgOp.getNumParallelLoops() == linalgOp.getNumLoops() &&
linalgOp.getIndexingMapsArray()[0].isIdentity()) {
// Check that the op body is only a linalg.yield op.
Value yieldOperand;
for (Operation &bodyOp : linalgOp.getBlock()->getOperations()) {
if (isa<linalg::YieldOp>(bodyOp))
yieldOperand = bodyOp.getOperand(0);
else
return std::nullopt;
}
// Check that the operand of the linalg.yield op is not an argument of the
// linalg.generic basic block
for (Value blockArg : linalgOp.getBlock()->getArguments())
if (yieldOperand == blockArg)
return std::nullopt;
return yieldOperand;
}
return std::nullopt;
}

struct RaiseSpecialOpsPass : public RaiseSpecialOpsBase<RaiseSpecialOpsPass> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<IREE::LinalgExt::IREELinalgExtDialect>();
Expand All @@ -84,6 +109,7 @@ struct RaiseSpecialOpsPass : public RaiseSpecialOpsBase<RaiseSpecialOpsPass> {
void runOnOperation() override {
SmallVector<std::pair<linalg::LinalgOp, Value>> softmaxRoots;
SmallVector<std::pair<linalg::MatmulOp, Value>> transposeMatmulRoots;
SmallVector<std::pair<linalg::GenericOp, Value>> genericFills;
getOperation()->walk([&](linalg::LinalgOp op) {
{
transform_ext::MatcherContext matcherContext;
Expand All @@ -98,6 +124,10 @@ struct RaiseSpecialOpsPass : public RaiseSpecialOpsBase<RaiseSpecialOpsPass> {
transposeMatmulRoots.push_back(std::make_pair(
cast<linalg::MatmulOp>(op.getOperation()), newRhs.value()));
}
if (std::optional<Value> fillInput = matchGenericFill(op)) {
genericFills.push_back(
std::make_pair(cast<linalg::GenericOp>(op), fillInput.value()));
}
}
});
IRRewriter rewriter(&getContext());
Expand All @@ -119,6 +149,15 @@ struct RaiseSpecialOpsPass : public RaiseSpecialOpsBase<RaiseSpecialOpsPass> {
rewriter.replaceOpWithNewOp<linalg::MatmulTransposeBOp>(
matmulOp, ValueRange{lhs, newRhs}, ValueRange{init}, attrs);
}
for (std::pair<linalg::GenericOp, Value> genericFill : genericFills) {
auto genericOp = genericFill.first;
Value fillInput = genericFill.second;
Value init = genericOp.getDpsInitOperand(0)->get();
rewriter.setInsertionPoint(genericOp);
SmallVector<NamedAttribute> attrs = getPrunedAttributeList(genericOp);
rewriter.replaceOpWithNewOp<linalg::FillOp>(
genericOp, ValueRange{fillInput}, ValueRange{init}, attrs);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,29 @@ func.func @aTransposeBMatmul(%arg0 : tensor<10x20xf32>,
// CHECK: %[[RESULT:.+]] = linalg.matmul_transpose_b
// CHECK-SAME: ins(%[[ARG0]], %[[ARG1]] :
// CHECK: return %[[RESULT]]

func.func @generic_fill(%arg0: tensor<?x?xf32>) -> tensor<1x1x?x?xf32> {
%cst = arith.constant 0.000000e+00 : f32
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%dim = tensor.dim %arg0, %c0 : tensor<?x?xf32>
%dim_0 = tensor.dim %arg0, %c1 : tensor<?x?xf32>
%0 = tensor.empty(%dim, %dim_0) : tensor<1x1x?x?xf32>
%1 = linalg.generic {
indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
iterator_types = ["parallel", "parallel", "parallel", "parallel"]}
outs(%0 : tensor<1x1x?x?xf32>) {
^bb0(%out: f32):
linalg.yield %cst : f32
} -> tensor<1x1x?x?xf32>
return %1 : tensor<1x1x?x?xf32>
}
// CHECK-LABEL: func @generic_fill
// CHECK-SAME: %[[ARG0:.+]]: tensor<?x?xf32>
// CHECK: %[[CST:.+]] = arith.constant 0.000000e+00 : f32
// CHECK: %[[EMPTY:.+]] = tensor.empty
// CHECK-SAME: : tensor<1x1x?x?xf32>
// CHECK: %[[RESULT:.+]] = linalg.fill
// CHECK-SAME: ins(%[[CST]] : f32)
// CHECK-SAME: outs(%[[EMPTY]] : tensor<1x1x?x?xf32>)
// CHECK: return %[[RESULT]]

0 comments on commit 2281e94

Please sign in to comment.