Skip to content

Commit

Permalink
Exclusive context policy ensures EnsoContext.get returns compilation …
Browse files Browse the repository at this point in the history
…constant (#7493)

Fixes #6809 by giving up on any attempt to share `Node` among multiple `Context` via sharing a single `Engine`.
  • Loading branch information
JaroslavTulach authored Aug 8, 2023
1 parent 52b1189 commit 05e8aad
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 60 deletions.
34 changes: 21 additions & 13 deletions engine/runtime/src/main/java/org/enso/interpreter/EnsoLanguage.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package org.enso.interpreter;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.Option;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.debug.DebuggerTags;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.ProvidedTags;
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.nodes.ExecutableNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.enso.compiler.Compiler;
import org.enso.compiler.context.InlineContext;
import org.enso.compiler.data.CompilerConfig;
Expand Down Expand Up @@ -50,6 +40,18 @@
import org.graalvm.options.OptionKey;
import org.graalvm.options.OptionType;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.Option;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.debug.DebuggerTags;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.ProvidedTags;
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.nodes.ExecutableNode;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;

/**
* The root of the Enso implementation.
*
Expand All @@ -66,7 +68,7 @@
version = LanguageInfo.VERSION,
defaultMimeType = LanguageInfo.MIME_TYPE,
characterMimeTypes = {LanguageInfo.MIME_TYPE},
contextPolicy = TruffleLanguage.ContextPolicy.SHARED,
contextPolicy = TruffleLanguage.ContextPolicy.EXCLUSIVE,
dependentLanguages = {EpbLanguage.ID},
fileTypeDetectors = FileDetector.class,
services= { Timer.class, NotificationHandler.Forwarder.class, LockManager.class }
Expand Down Expand Up @@ -278,9 +280,15 @@ protected ExecutableNode parse(InlineParsingRequest request) throws InlineParsin
if (exprNode.isDefined()) {
var language = EnsoLanguage.get(exprNode.get());
return new ExecutableNode(language) {
@Child
private ExpressionNode expr;

@Override
public Object execute(VirtualFrame frame) {
return exprNode.get().executeGeneric(frame);
if (expr == null) {
expr = insert(exprNode.get());
}
return expr.executeGeneric(frame);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package org.enso.interpreter.runtime;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.Shape;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicLong;

import org.enso.compiler.Compiler;
import org.enso.compiler.PackageRepository;
import org.enso.compiler.PackageRepositoryUtils;
Expand All @@ -32,6 +24,7 @@
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.OptionsHelper;
import org.enso.interpreter.instrument.NotificationHandler;
import org.enso.interpreter.runtime.Module;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.scope.TopLevelScope;
Expand All @@ -45,6 +38,22 @@
import org.enso.polyglot.LanguageInfo;
import org.enso.polyglot.RuntimeOptions;
import org.graalvm.options.OptionKey;

import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.Shape;

import scala.jdk.javaapi.OptionConverters;

/**
Expand Down Expand Up @@ -179,7 +188,35 @@ public void initialize() {
* com.oracle.truffle.api.TruffleContext}.
*/
public static EnsoContext get(Node node) {
return REFERENCE.get(node);
var ctx = REFERENCE.get(node);
if (checkNodes.isValid() && !CompilerDirectives.isPartialEvaluationConstant(ctx)) {
reportSlowContextAccess(node);
}
return ctx;
}

private static final Assumption checkNodes = Truffle.getRuntime().createAssumption("context check");
private static final Set<Node> reportedNulllRootNodes = new HashSet<>();
private static long checkUntil = Long.MAX_VALUE;

@TruffleBoundary
private static void reportSlowContextAccess(Node n) {
if (System.currentTimeMillis() > checkUntil) {
checkNodes.invalidate();
}
if (reportedNulllRootNodes.add(n)) {
var ex = new Exception("""
no root node for {n}
with section: {s}
with root nodes: {r}
"""
.replace("{n}", "" + n)
.replace("{s}", "" + n.getEncapsulatingSourceSection())
.replace("{r}", "" + n.getRootNode())
);
ex.printStackTrace();
checkUntil = System.currentTimeMillis() + 10000;
}
}

public static TruffleLanguage.ContextReference<EnsoContext> getReference() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
Expand Down Expand Up @@ -347,7 +348,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().function();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.interop.*;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
Expand Down Expand Up @@ -380,7 +384,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached(value = "1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().function();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ boolean isLimitReached(@Shared("warnsLib") @CachedLibrary(limit = "3") WarningsL
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().array();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
Expand Down Expand Up @@ -94,7 +95,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().date();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
Expand Down Expand Up @@ -220,7 +221,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().dateTime();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
Expand Down Expand Up @@ -35,7 +36,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().duration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;

import java.util.function.IntFunction;

import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.PanicException;
Expand All @@ -29,6 +31,8 @@
import java.time.ZonedDateTime;
import java.util.Set;

import com.oracle.truffle.api.dsl.Cached;

/**
* A wrapper for {@link TruffleFile} objects exposed to the language. For methods documentation
* please refer to {@link TruffleFile}.
Expand Down Expand Up @@ -340,7 +344,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().file();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
Expand Down Expand Up @@ -159,7 +160,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().timeOfDay();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
Expand Down Expand Up @@ -99,7 +100,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().timeZone();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
Expand Down Expand Up @@ -84,7 +85,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().managedResource();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
Expand Down Expand Up @@ -60,7 +61,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().ref();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ boolean hasType() {
}

@ExportMessage
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().vector();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ boolean hasType() {
}

@ExportMessage(library = TypesLibrary.class)
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
Type getType(@CachedLibrary("this") TypesLibrary thisLib, @Cached("1") int ignore) {
return EnsoContext.get(thisLib).getBuiltins().map();
}

Expand Down
Loading

0 comments on commit 05e8aad

Please sign in to comment.