Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup DataflowError.withDefaultTrace #11153

Merged
merged 12 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ Object execute(Atom self, Object environmentName) {
.makeUnimplemented("execution environment mismatch");
throw new PanicException(error, this);
}
return currentEnv.hasContextEnabled(self.getConstructor().getName());
return currentEnv.hasContextEnabled(self.getConstructor(), EnsoContext.get(this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.enso.interpreter.runtime.error.PanicException.handleExceptionMessage;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.TruffleStackTraceElement;
Expand Down Expand Up @@ -53,11 +54,14 @@ public final class DataflowError extends AbstractTruffleException implements Ens
*/
public static DataflowError withDefaultTrace(State state, Object payload, Node location) {
assert payload != null;
var ensoCtx = EnsoContext.get(location);
var dataflowStacktraceCtx = ensoCtx.getBuiltins().context().getDataflowStackTrace();
// dataflowStacktraceCtx is a compiler constant, because it is a constructor of a builtin
// type.
CompilerAsserts.partialEvaluationConstant(dataflowStacktraceCtx);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps with the performance. The compiler is most probably not able to determine that dataflowStacktraceCtx is actually a constant (reference to a constructor of a builtin type).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps with the performance.

That would be surprising. It is a CompilerAsserts - e.g. not a directive, but only an assert that something is true. E.g. either dataflowStacktraceCtx is constant or the compilation is going to fail. The fact whether it is constant or not isn't changed by the assert. The fact whether compilation goes or not is influenced by the assert.

boolean attachFullStackTrace =
state == null
|| EnsoContext.get(location)
.getExecutionEnvironment()
.hasContextEnabled("Dataflow_Stack_Trace");
|| ensoCtx.getExecutionEnvironment().hasContextEnabled(dataflowStacktraceCtx, ensoCtx);
if (attachFullStackTrace) {
var result = new DataflowError(payload, UNLIMITED_STACK_TRACE, location);
TruffleStackTrace.fillIn(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.enso.interpreter.runtime.state;

import org.enso.interpreter.node.expression.builtin.runtime.Context;
import com.oracle.truffle.api.CompilerDirectives;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.atom.Atom;
import org.enso.interpreter.runtime.data.atom.AtomConstructor;

public class ExecutionEnvironment {

private final String name;

// Ideally we would "just" use a map here. But that leads
// to native image build problems. This in turn leads to
// TruffleBoundary annotations which in turn leads to slow path.
private final String[] keys;
private final Boolean[] permissions;
private final Permissions permissions;

public static final String LIVE_ENVIRONMENT_NAME = "live";

Expand All @@ -22,21 +19,18 @@ public class ExecutionEnvironment {
public static final ExecutionEnvironment DESIGN =
new ExecutionEnvironment(DESIGN_ENVIRONMENT_NAME);

private static final ExecutionEnvironment initLive(String name) {
String[] keys = new String[] {Context.INPUT_NAME, Context.OUTPUT_NAME};
Boolean[] permissions = new Boolean[] {true, true};
return new ExecutionEnvironment(name, keys, permissions);
private static ExecutionEnvironment initLive(String name) {
var permissions = new Permissions(true, true, false);
return new ExecutionEnvironment(name, permissions);
}

public ExecutionEnvironment(String name) {
this.name = name;
this.keys = new String[0];
this.permissions = new Boolean[0];
this.permissions = new Permissions(false, false, false);
}

private ExecutionEnvironment(String name, String[] keys, Boolean[] permissions) {
private ExecutionEnvironment(String name, Permissions permissions) {
this.name = name;
this.keys = keys;
this.permissions = permissions;
}

Expand All @@ -53,49 +47,39 @@ public ExecutionEnvironment withContextDisabled(Atom context) {
}

private ExecutionEnvironment update(Atom context, boolean value) {
assert context.getConstructor().getType()
== EnsoContext.get(null).getBuiltins().context().getType();
int keyFound = -1;
for (int i = 0; i < keys.length; i++) {
if (keys[i].equals(context.getConstructor().getName())) {
keyFound = i;
}
}
String[] keys1;
Boolean[] permissions1;
if (keyFound != -1) {
keys1 = cloneArray(keys, new String[keys.length]);
permissions1 = cloneArray(permissions, new Boolean[keys.length]);
permissions1[keyFound] = value;
var contextBuiltin = EnsoContext.get(null).getBuiltins().context();
assert context.getConstructor().getType() == contextBuiltin.getType();
var ctor = context.getConstructor();
Permissions newPermissions;
if (ctor == contextBuiltin.getInput()) {
newPermissions = new Permissions(value, permissions.output, permissions.dataflowStacktrace);
} else if (ctor == contextBuiltin.getOutput()) {
newPermissions = new Permissions(permissions.input, value, permissions.dataflowStacktrace);
} else if (ctor == contextBuiltin.getDataflowStackTrace()) {
newPermissions = new Permissions(permissions.input, permissions.output, value);
} else {
keys1 = cloneArray(keys, new String[keys.length + 1]);
permissions1 = cloneArray(permissions, new Boolean[keys.length + 1]);
keyFound = keys.length;
keys1[keyFound] = context.getConstructor().getName();
permissions1[keyFound] = value;
throw CompilerDirectives.shouldNotReachHere("Unknown context `" + ctor.getName() + "`");
}
return new ExecutionEnvironment(name, keys1, permissions1);
return new ExecutionEnvironment(name, newPermissions);
}

private <T> T[] cloneArray(T[] fromArray, T[] toArray) {
for (int i = 0; i < fromArray.length; i++) {
toArray[i] = fromArray[i];
}
return toArray;
}

public Boolean hasContextEnabled(String context) {
int keyFound = -1;
for (int i = 0; i < keys.length; i++) {
if (keys[i].equals(context)) {
keyFound = i;
}
}
if (keyFound != -1) {
return permissions[keyFound];
} else {
return false;
/**
* Checks if the context is enabled in this execution environment.
*
* @param runtimeCtx Constructor of {@code Standard.Base.Runtime.Context} builtin type.
* @return {@code true} if the context is enabled in this execution environment.
*/
public boolean hasContextEnabled(AtomConstructor runtimeCtx, EnsoContext ensoCtx) {
var contextBuiltin = ensoCtx.getBuiltins().context();
if (runtimeCtx == contextBuiltin.getInput()) {
return permissions.input;
} else if (runtimeCtx == contextBuiltin.getOutput()) {
return permissions.output;
} else if (runtimeCtx == contextBuiltin.getDataflowStackTrace()) {
return permissions.dataflowStacktrace;
}
throw CompilerDirectives.shouldNotReachHere(
"Unknown runtimeCtx `" + runtimeCtx.getName() + "`");
}

public static ExecutionEnvironment forName(String name) {
Expand All @@ -108,4 +92,9 @@ public static ExecutionEnvironment forName(String name) {
throw new IllegalArgumentException("Unsupported Execution Environment `" + name + "`");
}
}

/**
* Fields correspond to the constructors of {@code Standard.Base.Runtime.Context} builtin type.
*/
private record Permissions(boolean input, boolean output, boolean dataflowStacktrace) {}
}
Loading