Skip to content

Commit

Permalink
[GR-61687] Fix shared caches should be allowed when used for a single…
Browse files Browse the repository at this point in the history
… specialization with multiple instances.

PullRequest: graal/19932
  • Loading branch information
chumer committed Jan 28, 2025
2 parents e5234d1 + b07c88f commit dacfdca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import org.junit.Test;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Exclusive;
import com.oracle.truffle.api.dsl.Cached.Shared;
Expand All @@ -54,11 +55,14 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.dsl.UnsupportedSpecializationException;
import com.oracle.truffle.api.dsl.test.GenerateInlineTest.SimpleNode;
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.SharedCachedInMultiInstanceNodeGen;
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.SharedStringInGuardNodeGen;
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UnboundExclusiveObjectNodeGen;
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UnboundSharedObjectNodeGen;
import com.oracle.truffle.api.dsl.test.SharedCachedTestFactory.UseGenerateInlineSharedNodeGen;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.api.strings.TruffleString.Encoding;
import com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest;

@SuppressWarnings({"truffle-inlining", "truffle-neverdefault", "unused"})
Expand Down Expand Up @@ -448,4 +452,39 @@ public void testObjectReference() {
});
}

@Test
public void testSharedCachedInMultiInstanceNode() {
SharedCachedInMultiInstanceNode node = SharedCachedInMultiInstanceNodeGen.create();
TruffleString a = TruffleString.fromJavaStringUncached("a", Encoding.UTF_16);
TruffleString b = TruffleString.fromJavaStringUncached("b", Encoding.UTF_16);
TruffleString c = TruffleString.fromJavaStringUncached("c", Encoding.UTF_16);

assertEquals(a, node.execute(a));
assertEquals(b, node.execute(b));
assertEquals(c, node.execute(c));
}

public abstract static class SharedCachedInMultiInstanceNode extends Node {

abstract Object execute(TruffleString name);

@Specialization(guards = {"stringEquals(equalsNode, cachedName, name)"}, limit = "2")
protected TruffleString doCached(TruffleString name,
@Cached("name") TruffleString cachedName,
@Cached @Shared TruffleString.EqualNode equalsNode,
@Cached("doGeneric(name)") TruffleString cachedResult) {
return cachedResult;
}

static boolean stringEquals(TruffleString.EqualNode equalNode, TruffleString s1, TruffleString s2) {
return equalNode.execute(s1, s2, Encoding.UTF_16);
}

@TruffleBoundary
@Specialization(replaces = "doCached")
protected TruffleString doGeneric(TruffleString name) {
return name;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1263,11 +1263,16 @@ public static Map<CacheExpression, String> computeSharing(Element templateType,
}

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

Expand Down

0 comments on commit dacfdca

Please sign in to comment.