Skip to content

Commit d0969e5

Browse files
committed
Fix assumption warning should only be triggered for usages that reach a @fallback.
1 parent 91fd633 commit d0969e5

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

truffle/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Guards that only bind idempotent methods and no dynamic values can always be ass
4949
The generated code leverages this information and asserts instead of executes the guard on the fast-path.
5050
The DSL now emits warnings with for all guards where specifying the annotations may be beneficial.
5151
Note that all guards that do not bind dynamic values are assumed idempotent by default for compatibility reasons.
52+
* GR-43903 Usages of `@Specialization(assumptions=...)` that reach a `@Fallback` specialization now produce a suppressable warning. In most situations, such specializations should be migrated to use a regular guard instead. For example, instead of using `@Specialization(assumptions = "assumption")` you might need to be using `@Specialization(guards = "assumption.isValid()")`.
53+
* GR-43903 Added `@Idempotent` and `@NonIdempotent` DSL annotations useful for DSL guard optimizations. Guards that only bind idempotent methods and no dynamic values can always be assumed `true` after they were `true` once on the slow-path. The generated code leverages this information and asserts instead of executes the guard on the fast-path. The DSL now emits warnings with for all guards where specifying the annotations may be beneficial. Note that all guards that do not bind dynamic values are assumed idempotent by default for compatibility reasons.
5254

5355

5456
* GR-43903 DSL method guards that do not bind dynamic values are no longer just asserted on the fast-path.

truffle/docs/DSLWarnings.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The following warning keys are supported:
2626
* `truffle-static-method` warnings when the DSL recommends to use the `static` modifier.
2727
* `truffle-unused` warnings if a DSL attribute or annotation has no effect and is recommended to be removed.
2828
* `truffle-abstract-export` warnings if an abstract message of a Truffle library is not exported.
29-
* `truffle-assumption` if the assumptions feature is used with a specialization that may never have multiple instances.
29+
* `truffle-assumption` if the assumptions feature is used with a specialization that reaches a `@Fallback` specialization.
3030
* `truffle-guard` if a guard uses methods where a `@Idempotent` or `@NonIdempotent` method may be beneficial for the generated code.
3131

3232
Specific warnings can also be suppressed globally using the `-Atruffle.dsl.SuppressWarnings=truffle-inlining,truffle-neverdefault` Java compiler processor option.

truffle/src/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/AssumptionsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ abstract static class AssumptionFallbackNode extends SlowPathListenerNode {
784784

785785
abstract boolean execute(Object arg);
786786

787+
@ExpectError("It is discouraged to use assumptions with a specialization that reaches a @Fallback specialization. %")
787788
@Specialization(guards = {"arg > 1", "arg == cachedArg"}, assumptions = "assumption", limit = "1")
788789
public boolean s0(int arg,
789790
@Cached("arg") int cachedArg,
@@ -816,7 +817,6 @@ abstract static class AssumptionNoGuardNode extends SlowPathListenerNode {
816817

817818
abstract boolean execute(Object arg);
818819

819-
@ExpectError("It is discouraged to use the assumptions property with a specialization that cannot have multiple specialization instances%")
820820
@Specialization(assumptions = "assumption")
821821
public boolean s0(Object arg,
822822
@Cached("NEVER_VALID") Assumption assumption) {

truffle/src/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedReachableFallbackTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ abstract static class GuardKindsNode extends Node {
304304

305305
abstract Object execute(Object other);
306306

307+
@SuppressWarnings("truffle-assumption")
307308
@Specialization(guards = {"guardNode1.execute(obj)", "notTwo(obj)"}, rewriteOn = RuntimeException.class, assumptions = "createAssumption()", limit = "1")
308309
protected Object s1(int obj,
309310
@Cached(value = "create(1)") NotGuardNode guardNode1) {

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/parser/NodeParser.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,22 @@ private static void initializeFallbackReachability(NodeData node) {
15321532
}
15331533

15341534
}
1535+
1536+
for (SpecializationData specialization : specializations) {
1537+
if (!specialization.getAssumptionExpressions().isEmpty() && specialization.isReachesFallback()) {
1538+
specialization.addSuppressableWarning(TruffleSuppressedWarnings.ASSUMPTION,
1539+
"""
1540+
It is discouraged to use assumptions with a specialization that reaches a @%s specialization.\s\
1541+
Specialization instances get removed if assumptions are no longer valid, which may lead to unexpected @%s invocations.\s\
1542+
This may be fixed by translating the assumption usage to a regular method guard instead.\s\
1543+
Instead of assumptions="a" you may use guards="a.isValid()".\s\
1544+
This problem may also be fixed by adding a new more generic specialization that replaces this specialization.\s\
1545+
""",
1546+
getSimpleName(types.Fallback),
1547+
getSimpleName(types.Fallback));
1548+
}
1549+
}
1550+
15351551
}
15361552

15371553
private static void initializeExecutableTypeHierarchy(NodeData node) {
@@ -3025,21 +3041,6 @@ private void initializeAssumptions(SpecializationData specialization, DSLExpress
30253041
assumptionId++;
30263042
}
30273043
specialization.setAssumptionExpressions(assumptionExpressions);
3028-
3029-
if (!assumptionExpressions.isEmpty() && !specialization.isGuardBindsCache()) {
3030-
specialization.addSuppressableWarning(TruffleSuppressedWarnings.ASSUMPTION, """
3031-
It is discouraged to use the assumptions property with a specialization that cannot have multiple specialization instances.\s\
3032-
Note that assumption specializations get removed if the assumption is no longer true.\s\
3033-
This rarely makes sense for specializations without multiple instances.\s\
3034-
Most often this can be fixed by translating the assumption usage to a regular method guard instead.\s\
3035-
So instead of assumptions="a" you may use guards="a.isValid()".\s\
3036-
If your specialization should have multiple instances, make sure you bind a cached value in the guard.\s\
3037-
""",
3038-
getSimpleName(types.Fallback),
3039-
getSimpleName(types.Specialization),
3040-
getSimpleName(types.Fallback));
3041-
}
3042-
30433044
}
30443045

30453046
private void initializeLimit(SpecializationData specialization, DSLExpressionResolver resolver, boolean uncached) {

0 commit comments

Comments
 (0)