Skip to content

Commit dacfdca

Browse files
committed
[GR-61687] Fix shared caches should be allowed when used for a single specialization with multiple instances.
PullRequest: graal/19932
2 parents e5234d1 + b07c88f commit dacfdca

File tree

2 files changed

+49
-5
lines changed
  • truffle/src

2 files changed

+49
-5
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
import org.junit.Test;
4747

48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4849
import com.oracle.truffle.api.dsl.Cached;
4950
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5051
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -54,11 +55,14 @@
5455
import com.oracle.truffle.api.dsl.Specialization;
5556
import com.oracle.truffle.api.dsl.UnsupportedSpecializationException;
5657
import com.oracle.truffle.api.dsl.test.GenerateInlineTest.SimpleNode;
58+
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.SharedCachedInMultiInstanceNodeGen;
5759
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.SharedStringInGuardNodeGen;
5860
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UnboundExclusiveObjectNodeGen;
5961
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UnboundSharedObjectNodeGen;
6062
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UseGenerateInlineSharedNodeGen;
6163
import com.oracle.truffle.api.nodes.Node;
64+
import com.oracle.truffle.api.strings.TruffleString;
65+
import com.oracle.truffle.api.strings.TruffleString.Encoding;
6266
import com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest;
6367

6468
@SuppressWarnings({"truffle-inlining", "truffle-neverdefault", "unused"})
@@ -448,4 +452,39 @@ public void testObjectReference() {
448452
});
449453
}
450454

455+
@Test
456+
public void testSharedCachedInMultiInstanceNode() {
457+
SharedCachedInMultiInstanceNode node = SharedCachedInMultiInstanceNodeGen.create();
458+
TruffleString a = TruffleString.fromJavaStringUncached("a", Encoding.UTF_16);
459+
TruffleString b = TruffleString.fromJavaStringUncached("b", Encoding.UTF_16);
460+
TruffleString c = TruffleString.fromJavaStringUncached("c", Encoding.UTF_16);
461+
462+
assertEquals(a, node.execute(a));
463+
assertEquals(b, node.execute(b));
464+
assertEquals(c, node.execute(c));
465+
}
466+
467+
public abstract static class SharedCachedInMultiInstanceNode extends Node {
468+
469+
abstract Object execute(TruffleString name);
470+
471+
@Specialization(guards = {"stringEquals(equalsNode, cachedName, name)"}, limit = "2")
472+
protected TruffleString doCached(TruffleString name,
473+
@Cached("name") TruffleString cachedName,
474+
@Cached @Shared TruffleString.EqualNode equalsNode,
475+
@Cached("doGeneric(name)") TruffleString cachedResult) {
476+
return cachedResult;
477+
}
478+
479+
static boolean stringEquals(TruffleString.EqualNode equalNode, TruffleString s1, TruffleString s2) {
480+
return equalNode.execute(s1, s2, Encoding.UTF_16);
481+
}
482+
483+
@TruffleBoundary
484+
@Specialization(replaces = "doCached")
485+
protected TruffleString doGeneric(TruffleString name) {
486+
return name;
487+
}
488+
}
489+
451490
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,11 +1263,16 @@ public static Map<CacheExpression, String> computeSharing(Element templateType,
12631263
}
12641264

12651265
if (declaredSharing.size() <= 1 && (expressions == null || expressions.size() <= 1)) {
1266-
cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(),
1267-
"Could not find any other cached parameter that this parameter could be shared. " +
1268-
"Cached parameters are only sharable if they declare the same type and initializer expressions and if the specialization only has a single instance. " +
1269-
"Remove the @%s annotation or make the parameter sharable to resolve this.",
1270-
types.Cached_Shared.asElement().getSimpleName().toString());
1266+
if (declaredSharing.size() == 1 && expressions != null && expressions.size() == 1 && declaredSharing.get(0).specialization.hasMultipleInstances()) {
1267+
// allow single shared in multiple instance specialization
1268+
sharedExpressions.put(sharable.expression, group);
1269+
} else {
1270+
cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(),
1271+
"Could not find any other cached parameter that this parameter could be shared. " +
1272+
"Cached parameters are only sharable if they declare the same type and initializer expressions and if the specialization only has a single instance. " +
1273+
"Remove the @%s annotation or make the parameter sharable to resolve this.",
1274+
types.Cached_Shared.asElement().getSimpleName().toString());
1275+
}
12711276
} else {
12721277
if (declaredSharing.size() <= 1) {
12731278

0 commit comments

Comments
 (0)