Skip to content

Commit 25c4b2b

Browse files
authored
iss: Add strategy analysis infrastructure (#957)
1 parent 8ea1c42 commit 25c4b2b

24 files changed

Lines changed: 2134 additions & 82 deletions

vadl/main/vadl/dump/infoEnrichers/ViamEnricherCollection.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import vadl.dump.InfoEnricher;
3030
import vadl.dump.InfoUtils;
3131
import vadl.dump.entities.DefinitionEntity;
32+
import vadl.iss.passes.extensions.InstrExecPlan;
33+
import vadl.iss.passes.extensions.InstrInfo;
3234
import vadl.utils.SourceLocation;
3335
import vadl.viam.DefProp;
3436
import vadl.viam.Encoding;
@@ -295,6 +297,36 @@ private static String sourceHeader(SourceLocation location) {
295297
entity.addInfo(info);
296298
});
297299

300+
public static InfoEnricher VECTOR_TCG_PLAN_SUPPLIER_TAGS =
301+
forType(DefinitionEntity.class, (entity, passResult) -> {
302+
if (!(entity.origin() instanceof Instruction instruction)) {
303+
return;
304+
}
305+
306+
var info = instruction.extension(InstrInfo.class);
307+
if (info == null || info.executionPlan() == null) {
308+
return;
309+
}
310+
311+
var executionPlan = info.executionPlan();
312+
entity.addInfo(Info.Tag.of("SelectedExecutionStrategy",
313+
executionPlan.selectedStrategy().name()));
314+
315+
var directGvec = executionPlan.evaluation(
316+
InstrExecPlan.StrategyKind.DIRECT_GVEC);
317+
if (directGvec == null) {
318+
return;
319+
}
320+
321+
entity.addInfo(Info.Tag.of("DirectGvecStatus", directGvec.status().name()));
322+
entity.addInfo(Info.Tag.of("DirectGvecIssues",
323+
directGvec.issues().isEmpty()
324+
? "-"
325+
: directGvec.issues().stream()
326+
.map(issue -> issue.code())
327+
.collect(Collectors.joining(", "))));
328+
});
329+
298330

299331
public static InfoEnricher BEHAVIOR_NO_LOCATION_EXPANDABLE =
300332
forType(DefinitionEntity.class, (entity, passResult) -> {
@@ -405,6 +437,7 @@ private static String sourceHeader(SourceLocation location) {
405437
VERIFY_SUPPLIER_EXPANDABLE,
406438
SOURCE_CODE_SUPPLIER_EXPANDABLE,
407439
RESOURCE_ACCESS_SUPPLIER_EXPANDABLE,
440+
VECTOR_TCG_PLAN_SUPPLIER_TAGS,
408441
BEHAVIOR_NO_LOCATION_EXPANDABLE,
409442
STAGE_ORDER_SUPPLIER,
410443
OPERATION_INSTRUCTIONS_SUPPLIER_EXPANDABLE,

vadl/main/vadl/iss/passes/common/IssExecStrategyPass.java

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-FileCopyrightText : © 2026 TU Wien <vadl@tuwien.ac.at>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.iss.passes.common.planning;
18+
19+
import java.util.Comparator;
20+
import java.util.List;
21+
import vadl.iss.passes.extensions.InstrExecPlan;
22+
import vadl.iss.passes.extensions.InstrExecPlan.StrategyEvaluation;
23+
import vadl.iss.passes.extensions.InstrExecPlan.StrategyKind;
24+
import vadl.viam.Instruction;
25+
26+
/**
27+
* Evaluates all configured execution strategies and selects the best viable one.
28+
*/
29+
final class InstructionExecutionPlanner {
30+
31+
private final List<StrategyEvaluator> evaluators;
32+
33+
InstructionExecutionPlanner(List<StrategyEvaluator> evaluators) {
34+
this.evaluators = List.copyOf(evaluators);
35+
}
36+
37+
/**
38+
* Plans instruction execution by evaluating all strategies and selecting the best viable one.
39+
*/
40+
InstrExecPlan plan(Instruction instruction) {
41+
var evaluations = evaluators.stream()
42+
.map(evaluator -> evaluator.evaluate(instruction))
43+
.toList();
44+
var selected = evaluations.stream()
45+
.filter(StrategyEvaluation::isViable)
46+
.min(Comparator
47+
.comparingInt(StrategyEvaluation::estimatedCost)
48+
.thenComparingInt(evaluation -> strategyPriority(evaluation.strategy())))
49+
.orElseThrow(() -> new IllegalStateException(
50+
"Expected at least one viable execution strategy for " + instruction.simpleName()));
51+
return new InstrExecPlan(evaluations, selected);
52+
}
53+
54+
private int strategyPriority(StrategyKind strategy) {
55+
return switch (strategy) {
56+
case DIRECT_GVEC -> 0;
57+
case TCG_SCALAR -> 1;
58+
case HELPER_CALL -> 2;
59+
};
60+
}
61+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText : © 2026 TU Wien <vadl@tuwien.ac.at>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.iss.passes.common.planning;
18+
19+
import static vadl.iss.passes.TcgPassUtils.instrInfo;
20+
21+
import java.io.IOException;
22+
import java.util.List;
23+
import javax.annotation.Nullable;
24+
import vadl.configuration.IssConfiguration;
25+
import vadl.iss.passes.AbstractIssPass;
26+
import vadl.iss.passes.common.planning.evaluators.DirectGvecStrategyEvaluator;
27+
import vadl.iss.passes.common.planning.evaluators.HelperCallStrategyEvaluator;
28+
import vadl.iss.passes.common.planning.evaluators.TcgScalarStrategyEvaluator;
29+
import vadl.pass.PassName;
30+
import vadl.pass.PassResults;
31+
import vadl.viam.Specification;
32+
33+
/**
34+
* Computes and stores instruction execution plans plus the currently supported backend strategy.
35+
*
36+
* <p>The selected execution plan is the backend-independent source of truth. The legacy
37+
* {@link vadl.iss.passes.extensions.InstrInfo.ExecStrategy} is derived from the selected plan so
38+
* existing code generation can keep treating unsupported non-scalar strategies as helper calls.</p>
39+
*/
40+
public class IssExecStrategyPass extends AbstractIssPass {
41+
42+
private final InstructionExecutionPlanner executionPlanner;
43+
44+
public IssExecStrategyPass(IssConfiguration configuration) {
45+
super(configuration);
46+
this.executionPlanner = new InstructionExecutionPlanner(defaultEvaluators());
47+
}
48+
49+
@Override
50+
public PassName getName() {
51+
return PassName.of("ISS Exec Strategy Classification");
52+
}
53+
54+
@Override
55+
public @Nullable Object execute(PassResults passResults, Specification viam) throws IOException {
56+
if (viam.isa().isEmpty()) {
57+
return null;
58+
}
59+
60+
var isa = viam.isa().get();
61+
isa.ownInstructions().forEach(
62+
instr -> instrInfo(instr).setExecutionPlan(executionPlanner.plan(instr))
63+
);
64+
return null;
65+
}
66+
67+
private List<StrategyEvaluator> defaultEvaluators() {
68+
return List.of(
69+
new DirectGvecStrategyEvaluator(),
70+
new TcgScalarStrategyEvaluator(),
71+
new HelperCallStrategyEvaluator()
72+
);
73+
}
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText : © 2026 TU Wien <vadl@tuwien.ac.at>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package vadl.iss.passes.common.planning;
18+
19+
import vadl.iss.passes.extensions.InstrExecPlan.StrategyEvaluation;
20+
import vadl.viam.Instruction;
21+
22+
/**
23+
* Evaluates whether one execution strategy can implement a given instruction.
24+
*/
25+
public interface StrategyEvaluator {
26+
27+
/**
28+
* Evaluates the strategy for the given instruction.
29+
*/
30+
StrategyEvaluation evaluate(Instruction instruction);
31+
}

0 commit comments

Comments
 (0)