Skip to content

Commit e46f5e4

Browse files
committed
[Codegen][Tuner] improve verifier for the default tuning spec
Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
1 parent 3d40e00 commit e46f5e4

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

compiler/src/iree/compiler/Codegen/Common/test/verify_tuning_specs.mlir

+37
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,40 @@ module @iree_default_tuning_spec attributes { iree_codegen.tuning_spec_with_defa
6666
return
6767
}
6868
}
69+
70+
// -----
71+
72+
module @iree_default_tuning_spec attributes { iree_codegen.tuning_spec_with_default_entrypoint } {
73+
// expected-error @+1{{The named sequence '__kernel_config' must have the attribute 'iree_codegen.tuning_spec_entrypoint'}}
74+
transform.named_sequence @__kernel_config(%arg0: !transform.any_op {transform.consumed})
75+
-> (!transform.any_op) {
76+
transform.yield %arg0 : !transform.any_op
77+
}
78+
}
79+
80+
// -----
81+
82+
module @iree_default_tuning_spec attributes { iree_codegen.tuning_spec_with_default_entrypoint } {
83+
transform.named_sequence @match_a(%arg: !transform.any_op {transform.readonly}) -> (!transform.any_op) {
84+
transform.yield %arg : !transform.any_op
85+
}
86+
87+
transform.named_sequence @match_b(%arg: !transform.any_op {transform.readonly}) -> (!transform.any_op) {
88+
transform.yield %arg : !transform.any_op
89+
}
90+
91+
transform.named_sequence @apply_op_config(%op: !transform.any_op {transform.readonly}) {
92+
transform.yield
93+
}
94+
95+
// expected-error @+1{{The named sequence '__kernel_config' must contain exactly one 'ForeachMatchOp', but found 2}}
96+
transform.named_sequence @__kernel_config(%arg0: !transform.any_op {transform.consumed})
97+
-> (!transform.any_op) attributes { iree_codegen.tuning_spec_entrypoint } {
98+
99+
%res_a = transform.foreach_match in %arg0 @match_a -> @apply_op_config
100+
: (!transform.any_op) -> (!transform.any_op)
101+
%res_b = transform.foreach_match in %res_a @match_b -> @apply_op_config
102+
: (!transform.any_op) -> (!transform.any_op)
103+
transform.yield %res_b : !transform.any_op
104+
}
105+
}

compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenDialect.cpp

+29-4
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,40 @@ IREECodegenDialect::verifyOperationAttribute(Operation *op,
7272

7373
if (symbol == kTuningSpecDefaultEntrypointAttrName) {
7474
if (auto moduleOp = dyn_cast<ModuleOp>(op)) {
75-
if (!llvm::any_of(moduleOp.getOps<transform::NamedSequenceOp>(),
75+
auto kernelConfigOpIt =
76+
llvm::find_if(moduleOp.getOps<transform::NamedSequenceOp>(),
7677
[](transform::NamedSequenceOp op) {
7778
return op.getName() == kKernelConfigSpecName;
78-
})) {
79+
});
80+
81+
if (kernelConfigOpIt ==
82+
moduleOp.getOps<transform::NamedSequenceOp>().end()) {
7983
return moduleOp.emitError()
80-
<< "The tuning specification must include a named "
81-
"sequence with the symbol name '"
84+
<< "The tuning specification must include a named sequence with "
85+
"the symbol name '"
8286
<< kKernelConfigSpecName << "'.";
8387
}
88+
89+
transform::NamedSequenceOp kernelConfigOp = *kernelConfigOpIt;
90+
91+
// Verify that the NamedSequenceOp has the attribute
92+
// `kTuningSpecEntrypointAttrName`.
93+
if (!kernelConfigOp->hasAttr(kTuningSpecEntrypointAttrName)) {
94+
return kernelConfigOp.emitError()
95+
<< "The named sequence '" << kKernelConfigSpecName
96+
<< "' must have the attribute '" << kTuningSpecEntrypointAttrName
97+
<< "'.";
98+
}
99+
100+
// Ensure there is exactly one ForeachMatchOp inside the NamedSequenceOp.
101+
auto foreachMatchOps =
102+
llvm::to_vector(kernelConfigOp.getOps<transform::ForeachMatchOp>());
103+
if (foreachMatchOps.size() != 1) {
104+
return kernelConfigOp.emitError()
105+
<< "The named sequence '" << kKernelConfigSpecName
106+
<< "' must contain exactly one 'ForeachMatchOp', but found "
107+
<< foreachMatchOps.size() << ".";
108+
}
84109
}
85110
}
86111

0 commit comments

Comments
 (0)