Skip to content

Commit ef15dae

Browse files
committed
Add support of param type for transform.structured.tile_using_forall
1 parent 4b7e861 commit ef15dae

File tree

3 files changed

+169
-15
lines changed

3 files changed

+169
-15
lines changed

mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ include "mlir/IR/RegionKindInterface.td"
2323
// value in the payload IR.
2424
def TransformParamTypeOrAnyHandle : Type<
2525
Or<[TransformHandleTypeInterface.predicate,
26-
Transform_ParamType.predicate]>,
26+
TransformParamTypeInterface.predicate]>,
2727
"transform 'param' type or any handle type">;
2828

2929
//===----------------------------------------------------------------------===//
@@ -1935,10 +1935,10 @@ def TileUsingForallOp :
19351935
}];
19361936

19371937
let arguments = (ins TransformHandleTypeInterface:$target,
1938-
Variadic<TransformHandleTypeInterface>:$num_threads,
1939-
Variadic<TransformHandleTypeInterface>:$tile_sizes,
1940-
Optional<TransformHandleTypeInterface>:$packed_num_threads,
1941-
Optional<TransformHandleTypeInterface>:$packed_tile_sizes,
1938+
Variadic<TransformParamTypeOrAnyHandle>:$num_threads,
1939+
Variadic<TransformParamTypeOrAnyHandle>:$tile_sizes,
1940+
Optional<TransformParamTypeOrAnyHandle>:$packed_num_threads,
1941+
Optional<TransformParamTypeOrAnyHandle>:$packed_tile_sizes,
19421942
DefaultValuedOptionalAttr<DenseI64ArrayAttr, "{}">:$static_num_threads,
19431943
DefaultValuedOptionalAttr<DenseI64ArrayAttr, "{}">:$static_tile_sizes,
19441944
OptionalAttr<DeviceMappingArrayAttr>:$mapping);

mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ static FailureOr<LinalgOp> tryApply(Operation *operation, Args &&...args) {
8686
return cast<LinalgOp>(result->getOperation());
8787
}
8888

89-
/// Assuming that `ofr` is an index attr or a transform dialect handle mapped
90-
/// to exactly one op with one index result, return that value.
89+
/// Assuming that `ofr` is an index attr or a param of index type
90+
/// or a transform dialect handle mapped to exactly one op
91+
/// with one index result, return that value.
9192
static DiagnosedSilenceableFailure unpackSingleIndexResultPayloadOperations(
9293
transform::TransformState &state, TransformOpInterface transformOp,
9394
SmallVector<OpFoldResult> &result, ArrayRef<OpFoldResult> ofrs) {
@@ -98,12 +99,23 @@ static DiagnosedSilenceableFailure unpackSingleIndexResultPayloadOperations(
9899
result.push_back(ofr);
99100
continue;
100101
}
101-
auto payloadOps = state.getPayloadOps(ofr.get<Value>());
102+
103+
Value transformValue = ofr.get<Value>();
104+
if (isa<ParamType>(transformValue.getType())) {
105+
ArrayRef<Attribute> params = state.getParams(transformValue);
106+
if (params.size() != 1)
107+
return transformOp.emitDefiniteFailure()
108+
<< "requires exactly one parameter associated";
109+
result.push_back(params[0]);
110+
continue;
111+
}
112+
113+
auto payloadOps = state.getPayloadOps(transformValue);
102114
if (!llvm::hasSingleElement(payloadOps)) {
103115
DiagnosedSilenceableFailure diag =
104116
transformOp.emitSilenceableError()
105117
<< "handle must be mapped to exactly one payload op";
106-
diag.attachNote(ofr.get<Value>().getLoc())
118+
diag.attachNote(transformValue.getLoc())
107119
<< "mapped to " << llvm::range_size(payloadOps) << " payload ops";
108120
return diag;
109121
}
@@ -123,14 +135,31 @@ static DiagnosedSilenceableFailure unpackSingleIndexResultPayloadOperations(
123135
return DiagnosedSilenceableFailure::success();
124136
}
125137

126-
// Given a list of OpFoldResults that are either index attrs or op
127-
// handles, return a list of OpFoldResults where all op handles are
128-
// replaced with the first (and only) OpResult of that payload op. (There
129-
// must be exactly one mapped payload op and it must have exactly one
130-
// index result.)
138+
// Given a list of params that are index attrs or a list of OpFoldResults
139+
// that are either index attrs or op handles, return a list of OpFoldResults
140+
// of index attrs or a list of OpFoldResults where all op handles are
141+
// replaced with the first (and only) OpResult of that payload op.
142+
// (There must be exactly one parameter associated with the AnyParamType or
143+
// one mapped payload op which must have exactly one index result.)
131144
static DiagnosedSilenceableFailure unpackSingleIndexResultPayloadOperations(
132145
transform::TransformState &state, TransformOpInterface transformOp,
133146
SmallVector<OpFoldResult> &result, Value packedHandle) {
147+
if (isa<AnyParamType>(packedHandle.getType())) {
148+
ArrayRef<Attribute> params = state.getParams(packedHandle);
149+
if (params.size() != 1)
150+
return transformOp.emitDefiniteFailure()
151+
<< "requires exactly one parameter associated";
152+
ArrayAttr paramsArray = dyn_cast<ArrayAttr>(params[0]);
153+
if (!paramsArray)
154+
return transformOp.emitDefiniteFailure() << "expected ArrayAttr";
155+
for (Attribute param : paramsArray.getValue()) {
156+
if (!isa<IntegerAttr>(param))
157+
return transformOp.emitDefiniteFailure() << "expected IntegerAttr";
158+
result.push_back(param);
159+
}
160+
return DiagnosedSilenceableFailure::success();
161+
}
162+
134163
for (Operation *op : state.getPayloadOps(packedHandle)) {
135164
if (op->getNumResults() != 1 || !op->getResult(0).getType().isIndex()) {
136165
DiagnosedSilenceableFailure diag =

mlir/test/Dialect/Linalg/tile-to-forall.mlir

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s --transform-interpreter -canonicalize -cse -split-input-file | FileCheck %s
1+
// RUN: mlir-opt %s --transform-interpreter -canonicalize -cse -split-input-file -verify-diagnostics | FileCheck %s
22

33
// Offset per thread:
44
// CHECK-DAG: affine_map<(d0)[s0] -> (d0 * (s0 ceildiv 10))>
@@ -451,3 +451,128 @@ module attributes {transform.with_named_sequence} {
451451
}
452452
}
453453

454+
// -----
455+
456+
// CHECK-DAG: #[[$map0:.+]] = affine_map<()[s0] -> (s0 ceildiv 10)>
457+
// CHECK-DAG: #[[$map1:.+]] = affine_map<()[s0] -> (s0 ceildiv 20)>
458+
// CHECK-DAG: #[[$map2:.+]] = affine_map<(d0)[s0] -> (d0 * -10 + s0, 10)>
459+
// CHECK-DAG: #[[$map4:.+]] = affine_map<(d0)[s0] -> (d0 * -20 + s0, 20)>
460+
// CHECK-DAG: #[[$map5:.+]] = affine_map<(d0) -> (d0 * 10)>
461+
// CHECK-DAG: #[[$map6:.+]] = affine_map<(d0) -> (d0 * 20)>
462+
463+
// CHECK-LABEL: matmul_tile_size_dynamic_param(
464+
// CHECK-SAME: %[[A:[0-9a-z]+]]: tensor<?x?xf32>
465+
// CHECK-SAME: %[[B:[0-9a-z]+]]: tensor<?x?xf32>
466+
// CHECK-SAME: %[[C:[0-9a-z]+]]: tensor<?x?xf32>
467+
func.func @matmul_tile_size_dynamic_param(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?x?xf32>) -> tensor<?x?xf32> {
468+
// CHECK: %[[M:.+]] = tensor.dim %[[A]], %c0 :
469+
// CHECK: %[[N:.+]] = tensor.dim %[[B]], %c1 :
470+
// CHECK: %[[NT0:.+]] = affine.apply #map()[%[[M]]]
471+
// CHECK: %[[NT1:.+]] = affine.apply #map1()[%[[N]]]
472+
// CHECK: scf.forall (%[[IV0:.+]], %[[IV1:.+]]) in (%[[NT0]], %[[NT1]]) shared_outs(%[[C_BLK:.*]] = %[[C]])
473+
// CHECK: %[[TS0:.+]] = affine.min #[[$map2]](%[[IV0]])[%[[M]]]
474+
// CHECK: %[[TS1:.+]] = affine.min #[[$map4]](%[[IV1]])[%[[N]]]
475+
// CHECK: %[[LB0:.+]] = affine.apply #[[$map5]](%[[IV0]])
476+
// CHECK: %[[LB1:.+]] = affine.apply #[[$map6]](%[[IV1]])
477+
// CHECK: tensor.extract_slice %[[A]]
478+
// CHECK: tensor.extract_slice %[[B]]
479+
// CHECK: tensor.extract_slice %[[C_BLK]]
480+
// CHECK: linalg.matmul
481+
// CHECK: scf.forall.in_parallel
482+
// CHECK-NEXT: tensor.parallel_insert_slice
483+
%0 = linalg.matmul ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
484+
outs(%C : tensor<?x?xf32>) -> (tensor<?x?xf32>)
485+
return %0 : tensor<?x?xf32>
486+
}
487+
488+
module attributes {transform.with_named_sequence} {
489+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
490+
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
491+
%sz = transform.param.constant 10 : i64 -> !transform.param<i64>
492+
%1:2 = transform.structured.tile_using_forall %0 tile_sizes [%sz : !transform.param<i64>, 20]
493+
: (!transform.any_op) -> (!transform.any_op, !transform.any_op)
494+
transform.yield
495+
}
496+
}
497+
498+
// -----
499+
500+
// CHECK-DAG: #[[$map0:.+]] = affine_map<()[s0] -> (s0 ceildiv 10)>
501+
// CHECK-DAG: #[[$map1:.+]] = affine_map<()[s0] -> (s0 ceildiv 20)>
502+
// CHECK-DAG: #[[$map2:.+]] = affine_map<(d0)[s0] -> (d0 * -10 + s0, 10)>
503+
// CHECK-DAG: #[[$map4:.+]] = affine_map<(d0)[s0] -> (d0 * -20 + s0, 20)>
504+
// CHECK-DAG: #[[$map5:.+]] = affine_map<(d0) -> (d0 * 10)>
505+
// CHECK-DAG: #[[$map6:.+]] = affine_map<(d0) -> (d0 * 20)>
506+
507+
// CHECK-LABEL: matmul_tile_size_dynamic_param(
508+
// CHECK-SAME: %[[A:[0-9a-z]+]]: tensor<?x?xf32>
509+
// CHECK-SAME: %[[B:[0-9a-z]+]]: tensor<?x?xf32>
510+
// CHECK-SAME: %[[C:[0-9a-z]+]]: tensor<?x?xf32>
511+
func.func @matmul_tile_size_dynamic_param(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?x?xf32>) -> tensor<?x?xf32> {
512+
// CHECK: %[[M:.+]] = tensor.dim %[[A]], %c0 :
513+
// CHECK: %[[N:.+]] = tensor.dim %[[B]], %c1 :
514+
// CHECK: %[[NT0:.+]] = affine.apply #map()[%[[M]]]
515+
// CHECK: %[[NT1:.+]] = affine.apply #map1()[%[[N]]]
516+
// CHECK: scf.forall (%[[IV0:.+]], %[[IV1:.+]]) in (%[[NT0]], %[[NT1]]) shared_outs(%[[C_BLK:.*]] = %[[C]])
517+
// CHECK: %[[TS0:.+]] = affine.min #[[$map2]](%[[IV0]])[%[[M]]]
518+
// CHECK: %[[TS1:.+]] = affine.min #[[$map4]](%[[IV1]])[%[[N]]]
519+
// CHECK: %[[LB0:.+]] = affine.apply #[[$map5]](%[[IV0]])
520+
// CHECK: %[[LB1:.+]] = affine.apply #[[$map6]](%[[IV1]])
521+
// CHECK: tensor.extract_slice %[[A]]
522+
// CHECK: tensor.extract_slice %[[B]]
523+
// CHECK: tensor.extract_slice %[[C_BLK]]
524+
// CHECK: linalg.matmul
525+
// CHECK: scf.forall.in_parallel
526+
// CHECK-NEXT: tensor.parallel_insert_slice
527+
%0 = linalg.matmul ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
528+
outs(%C : tensor<?x?xf32>) -> (tensor<?x?xf32>)
529+
return %0 : tensor<?x?xf32>
530+
}
531+
532+
module attributes {transform.with_named_sequence} {
533+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
534+
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
535+
%sz = transform.param.constant [10 : i64, 20 : i64] -> !transform.any_param
536+
%1:2 = transform.structured.tile_using_forall %0 tile_sizes *(%sz : !transform.any_param)
537+
: (!transform.any_op) -> (!transform.any_op, !transform.any_op)
538+
transform.yield
539+
}
540+
}
541+
542+
// -----
543+
544+
func.func @matmul_tile_size_param_not_array(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?x?xf32>) -> tensor<?x?xf32> {
545+
%0 = linalg.matmul ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
546+
outs(%C : tensor<?x?xf32>) -> (tensor<?x?xf32>)
547+
return %0 : tensor<?x?xf32>
548+
}
549+
550+
module attributes {transform.with_named_sequence} {
551+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
552+
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
553+
%sz = transform.param.constant "[10 : i64, 20 : i64]" -> !transform.any_param
554+
// expected-error @below {{expected ArrayAttr}}
555+
%1:2 = transform.structured.tile_using_forall %0 tile_sizes *(%sz : !transform.any_param)
556+
: (!transform.any_op) -> (!transform.any_op, !transform.any_op)
557+
transform.yield
558+
}
559+
}
560+
561+
// -----
562+
563+
func.func @matmul_tile_size_param_not_array(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?x?xf32>) -> tensor<?x?xf32> {
564+
%0 = linalg.matmul ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
565+
outs(%C : tensor<?x?xf32>) -> (tensor<?x?xf32>)
566+
return %0 : tensor<?x?xf32>
567+
}
568+
569+
module attributes {transform.with_named_sequence} {
570+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
571+
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
572+
%sz = transform.param.constant ["10", "20"] -> !transform.any_param
573+
// expected-error @below {{expected IntegerAttr}}
574+
%1:2 = transform.structured.tile_using_forall %0 tile_sizes *(%sz : !transform.any_param)
575+
: (!transform.any_op) -> (!transform.any_op, !transform.any_op)
576+
transform.yield
577+
}
578+
}

0 commit comments

Comments
 (0)