@@ -270,11 +270,48 @@ Status DotOpEmitter::EmitLinalgMatmul() {
270270 return EmitMlirFuncAndCall (
271271 mlir_context_, b_, dot_info_.result_shape , operand_shapes, target_ptr,
272272 operand_ptrs, name, [&](mlir::OpBuilder* builder, mlir::FuncOp function) {
273+ CHECK_EQ (dot_info_.dim_nums .lhs_contracting_dimensions_size (), 1 );
274+ CHECK_EQ (dot_info_.dim_nums .rhs_contracting_dimensions_size (), 1 );
275+ mlir::MLIRContext* context = builder->getContext ();
273276 mlir::edsc::ScopedContext scope (*builder, function.getLoc ());
274277 mlir::Value a = function.getArgument (0 ), b = function.getArgument (1 ),
275278 c = function.getArgument (2 );
276- mlir::edsc::intrinsics::linalg_matmul (mlir::TypeRange{},
277- mlir::ValueRange{b, c, a});
279+
280+ llvm::SmallVector<mlir::AffineExpr, 2 > b_exprs (
281+ dot_info_.lhs_shape .rank ());
282+ llvm::SmallVector<mlir::AffineExpr, 2 > c_exprs (
283+ dot_info_.rhs_shape .rank ());
284+
285+ llvm::SmallVector<mlir::AffineExpr, 2 > parallel_exprs;
286+ mlir::AffineExpr reduce_expr;
287+ for (int i = 0 ; i != dot_info_.result_shape .rank (); ++i) {
288+ parallel_exprs.push_back (mlir::getAffineDimExpr (i, context));
289+ }
290+ reduce_expr =
291+ mlir::getAffineDimExpr (dot_info_.result_shape .rank (), context);
292+
293+ // The reduction expr is shared for both inputs.
294+ b_exprs[dot_info_.dim_nums .lhs_contracting_dimensions (0 )] = reduce_expr;
295+ c_exprs[dot_info_.dim_nums .rhs_contracting_dimensions (0 )] = reduce_expr;
296+
297+ // Fill in the remaining parallel exprs.
298+ int par_expr_num = 0 ;
299+ for (auto * v : {&b_exprs, &c_exprs}) {
300+ for (auto & e : *v) {
301+ if (!e) {
302+ e = parallel_exprs[par_expr_num++];
303+ }
304+ }
305+ }
306+
307+ llvm::SmallVector<mlir::IteratorType, 4 > types (
308+ parallel_exprs.size (), mlir::IteratorType::Parallel);
309+ types.push_back (mlir::IteratorType::Reduction);
310+
311+ mlir::edsc::StructuredIndexed s_a (a), s_b (b), s_c (c);
312+ mlir::edsc::makeGenericLinalgOp (types, {s_b (b_exprs), s_c (c_exprs)},
313+ {s_a (parallel_exprs)},
314+ mlir::edsc::ops::macRegionBuilder);
278315 mlir::edsc::intrinsics::std_ret ();
279316
280317 mlir::linalg::LinalgTilingOptions tilingOptions;
@@ -283,13 +320,13 @@ Status DotOpEmitter::EmitLinalgMatmul() {
283320 target_machine_features_.minimum_alignment_for_allocation (
284321 ShapeUtil::ByteSizeOf (dot_info_.result_shape ));
285322 mlir_strategy::MatmulCodegenStrategy strategy;
286- strategy.tile <mlir::linalg::MatmulOp >(tilingOptions)
287- .promote <mlir::linalg::MatmulOp >(
323+ strategy.tile <mlir::linalg::GenericOp >(tilingOptions)
324+ .promote <mlir::linalg::GenericOp >(
288325 mlir::linalg::LinalgPromotionOptions ()
289326 .setAlignment (alignment)
290327 .setUseFullTileBuffersByDefault (true )
291328 .setUseAlloca (true ))
292- .vectorize <mlir::linalg::MatmulOp >()
329+ .vectorize <mlir::linalg::GenericOp >()
293330 .setVectorTransformsOptions (
294331 mlir::vector::VectorTransformsOptions ()
295332 .setVectorTransformsOptions (
0 commit comments