Skip to content

Commit 763406f

Browse files
authored
[Codegen][Tuner] skip linking based on the default entry point attribute (#19603)
This PR generalizes the cases in which the linking pass can be skipped based on the presence of the default entry point attribute. --------- Signed-off-by: Bangtian Liu <liubangtian@gmail.com>
1 parent c992d29 commit 763406f

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

compiler/src/iree/compiler/Codegen/Common/MaterializeTuningSpecsPass.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,30 @@ struct MaterializeTuningSpecsPass final
197197
return;
198198
}
199199

200-
// If only the default tuning spec is available, use it directly and skip
201-
// the linking stage.
202-
if (!hasUserTuningSpec) {
203-
if (failed(dumpFinalTuningSpecToDir(*defaultTuningSpec))) {
200+
// When the user tuning spec and default spec are available, link all
201+
// available libraries into a single module. We insert the default tuning
202+
// spec last, so that any user-specified tuning configurations take
203+
// precedence.
204+
SmallVector<ModuleOp, 2> allSpecs;
205+
if (hasUserTuningSpec) {
206+
allSpecs.push_back(*userTuningSpec);
207+
}
208+
if (hasDefaultTuningSpec) {
209+
allSpecs.push_back(*defaultTuningSpec);
210+
}
211+
212+
// Determine if the linking pass should be skipped.
213+
// Skip if there is only one tuning spec (either user-provided or default)
214+
// with the default attribute.
215+
if (allSpecs.size() == 1 &&
216+
allSpecs[0]->hasAttr(kTuningSpecDefaultEntrypointAttrName)) {
217+
// Use the appropriate tuning spec (user or default).
218+
ModuleOp tuningSpecWithDefaultAttr = allSpecs[0];
219+
if (failed(dumpFinalTuningSpecToDir(tuningSpecWithDefaultAttr))) {
204220
return signalPassFailure();
205221
}
206222
FailureOr<DenseElementsAttr> serializedSpec =
207-
serializeTuningSpecToAttr(*defaultTuningSpec);
223+
serializeTuningSpecToAttr(tuningSpecWithDefaultAttr);
208224
if (failed(serializedSpec)) {
209225
module->emitError("Failed to serialize default tuning specs");
210226
return signalPassFailure();
@@ -213,14 +229,6 @@ struct MaterializeTuningSpecsPass final
213229
return;
214230
}
215231

216-
// When the user tuning spec is available, link all available libraries into
217-
// a single module. We insert the default tuning spec last, so that any
218-
// user-specified tuning configurations take precedence.
219-
SmallVector<ModuleOp, 2> allSpecs = {*userTuningSpec};
220-
if (hasDefaultTuningSpec) {
221-
allSpecs.push_back(*defaultTuningSpec);
222-
}
223-
224232
Location loc =
225233
FusedLoc::get(ctx, llvm::map_to_vector<2>(allSpecs, [](ModuleOp m) {
226234
return m.getLoc();

compiler/src/iree/compiler/Codegen/Common/test/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ iree_lit_test_suite(
107107
"reductions_codegen_spec.mlir",
108108
"reductions_match_spec.mlir",
109109
"tuning_spec.mlir",
110+
"tuning_spec_default.mlir",
110111
],
111112
),
112113
cfg = "//compiler:lit.cfg.py",
@@ -118,6 +119,7 @@ iree_lit_test_suite(
118119
"reductions_codegen_spec.mlir",
119120
"reductions_match_spec.mlir",
120121
"tuning_spec.mlir",
122+
"tuning_spec_default.mlir",
121123
],
122124
tools = [
123125
"//tools:iree-opt",

compiler/src/iree/compiler/Codegen/Common/test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ iree_lit_test_suite(
104104
reductions_codegen_spec.mlir
105105
reductions_match_spec.mlir
106106
tuning_spec.mlir
107+
tuning_spec_default.mlir
107108
)
108109

109110
### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###

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

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// RUN: --iree-codegen-dump-tuning-specs-to=- \
44
// RUN: --mlir-disable-threading --no-implicit-module %s | FileCheck %s
55

6+
// RUN: iree-opt --pass-pipeline='builtin.module(iree-codegen-materialize-tuning-specs)' \
7+
// RUN: --iree-codegen-tuning-spec-path=%p/tuning_spec_default.mlir \
8+
// RUN: --iree-codegen-dump-tuning-specs-to=- \
9+
// RUN: --mlir-disable-threading --no-implicit-module %s | FileCheck %s --check-prefix=SKIPLINK
10+
611
// Check that the final tuning spec is as expected when the user tuning spec is provided.
712

813
// CHECK-LABEL: module @iree_linked_tuning_spec
@@ -19,6 +24,17 @@
1924
// CHECK-SAME: iree_codegen.tuning_spec_mlirbc = dense<{{.+}}> : vector<{{[0-9]+}}xi8>
2025
// CHECK-LABEL: func.func @main_0
2126

27+
28+
// CHECK that the user-provided tuning spec is materized without linking when default tuing spec
29+
// is missing and the user-provided tuning spec is marked the default attribute.
30+
31+
// SKIPLINK-LABEL: module @user_spec
32+
// SKIPLINK-SAME: iree_codegen.tuning_spec_with_default_entrypoint
33+
// SKIPLINK-SAME: transform.with_named_sequence
34+
// SKIPLINK-NOT: module @{{.+}}
35+
// SKIPLINK: module attributes
36+
// SKIPLINK-SAME: iree_codegen.tuning_spec_mlirbc = dense<{{.+}}> : vector<{{[0-9]+}}xi8>
37+
// SKIPLINK-LABEL: func.func @main_0
2238
module {
2339
func.func @main_0() {
2440
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: iree-opt %s
2+
3+
module @user_spec attributes { transform.with_named_sequence, iree_codegen.tuning_spec_with_default_entrypoint } {
4+
transform.named_sequence @__kernel_config(%arg0: !transform.any_op {transform.readonly}) -> !transform.any_op
5+
attributes { iree_codegen.tuning_spec_entrypoint } {
6+
transform.print {name = "Hello Tuning Spec", skip_regions}
7+
transform.yield %arg0 : !transform.any_op
8+
}
9+
}

0 commit comments

Comments
 (0)