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

Eliminate references to Truffle nodes & co. in the compiler #8172

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -14,16 +14,12 @@
import org.enso.compiler.PackageRepository;
import org.enso.compiler.Passes;
import org.enso.compiler.SerializationManager;
import org.enso.compiler.core.ir.Expression;
import org.enso.compiler.data.BindingsMap;
import org.enso.compiler.data.CompilerConfig;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
Copy link
Member Author

Choose a reason for hiding this comment

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

No more scopes and ExpressionNode in the CompilerContext.

import org.enso.pkg.Package;
import org.enso.pkg.QualifiedName;
import org.enso.polyglot.CompilationStage;
import org.enso.polyglot.data.TypeGraph;

/**
* Interface that encapsulate all services {@link Compiler} needs from Truffle or other environment.
Expand Down Expand Up @@ -63,12 +59,6 @@ public interface CompilerContext {

void truffleRunCodegen(Module module, CompilerConfig config) throws IOException;

void truffleRunCodegen(
Source source, ModuleScope scope, CompilerConfig config, org.enso.compiler.core.ir.Module ir);

ExpressionNode truffleRunInline(
Source source, LocalScope localScope, Module module, CompilerConfig config, Expression ir);

// module related

void runStubsGenerator(Module module);
Expand Down Expand Up @@ -103,6 +93,8 @@ void initializeBuiltinsIr(

<T> Optional<TruffleFile> saveCache(Cache<T, ?> cache, T entry, boolean useGlobalCacheLocations);

TypeGraph getTypeHierarchy();

public static interface Updater {
void bindingsMap(BindingsMap map);

Expand All @@ -128,12 +120,8 @@ public abstract static class Module {

public abstract boolean isSameAs(org.enso.interpreter.runtime.Module m);

public abstract org.enso.interpreter.runtime.scope.ModuleScope getScope();

public abstract QualifiedName getName();

public abstract Type findType(String name);

public abstract BindingsMap getBindingsMap();

public abstract TruffleFile getSourceFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import org.enso.compiler.core.ConstantsNames;

/** Language-level constants for use throughout the program. */
public class Constants {
public final class Constants {
private Constants() {}

public static final String SCOPE_SEPARATOR = ".";

/** Names for different language elements. */
public static class Names {
public static final String SELF_ARGUMENT = ConstantsNames.SELF_ARGUMENT;
public static final String SELF_TYPE_ARGUMENT = ConstantsNames.SELF_TYPE_ARGUMENT;
public static final String THAT_ARGUMENT = ConstantsNames.THAT_ARGUMENT;
public static final String FROM_MEMBER = ConstantsNames.FROM_MEMBER;
}
public static interface Names extends ConstantsNames {}

/** Cache sizes for different AST nodes. */
public static class CacheSizes {
private CacheSizes() {}

public static final String ARGUMENT_SORTER_NODE = "10";
public static final String FUNCTION_INTEROP_LIBRARY = "10";
public static final String THUNK_EXECUTOR_NODE = "10";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.node.ProgramRootNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.IrToTruffle;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.state.ExecutionEnvironment;
import org.enso.interpreter.runtime.tag.AvoidIdInstrumentationTag;
import org.enso.interpreter.runtime.tag.IdentifiedTag;
Expand Down Expand Up @@ -260,13 +262,23 @@ protected ExecutableNode parse(InlineParsingRequest request) throws InlineParsin
scala.Option.empty()
);
Compiler silentCompiler = context.getCompiler().duplicateWithConfig(redirectConfigWithStrictErrors);
scala.Option<ExpressionNode> exprNode;
ExpressionNode exprNode;
try {
exprNode = silentCompiler
.runInline(
request.getSource().getCharacters().toString(),
inlineContext
);
var optionTupple = silentCompiler.runInline(
request.getSource().getCharacters().toString(),
inlineContext
);
if (optionTupple.nonEmpty()) {
var newInlineContext = optionTupple.get()._1();
var ir = optionTupple.get()._2();
var sco = newInlineContext.localScope().getOrElse(LocalScope::root);
var mod = newInlineContext.module$access$0().module$access$0();
var m = org.enso.interpreter.runtime.Module.fromCompilerModule(mod);
var toTruffle = new IrToTruffle(context, request.getSource(), m.getScope(), redirectConfigWithStrictErrors);
exprNode = toTruffle.runInline(ir, sco, "<inline_source>");
} else {
exprNode = null;
}
} catch (UnhandledEntity e) {
throw new InlineParsingException("Unhandled entity: " + e.entity(), e);
} catch (CompilationAbortedException e) {
Expand All @@ -277,16 +289,16 @@ protected ExecutableNode parse(InlineParsingRequest request) throws InlineParsin
silentCompiler.shutdown(false);
}

if (exprNode.isDefined()) {
var language = EnsoLanguage.get(exprNode.get());
if (exprNode != null) {
var language = EnsoLanguage.get(exprNode);
return new ExecutableNode(language) {
@Child
private ExpressionNode expr;

@Override
public Object execute(VirtualFrame frame) {
if (expr == null) {
expr = insert(exprNode.get());
expr = insert(exprNode);
}
return expr.executeGeneric(frame);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.enso.interpreter.Constants;
import org.enso.interpreter.node.BaseNode;
import org.enso.interpreter.node.ClosureRootNode;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.node.callable.thunk.ThunkExecutorNode;
import org.enso.interpreter.node.expression.builtin.text.util.ToJavaStringNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.IrToTruffle;
import org.enso.interpreter.runtime.callable.CallerInfo;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.text.Text;
Expand Down Expand Up @@ -66,12 +66,26 @@ RootCallTarget parseExpression(LocalScope scope, ModuleScope moduleScope, String
LocalScope localScope = scope.createChild();
InlineContext inlineContext =
InlineContext.fromJava(
localScope, moduleScope, getTailStatus(), context.getCompilerConfig());
ExpressionNode expr =
context.getCompiler().runInline(expression, inlineContext).getOrElse(() -> null);
if (expr == null) {
localScope,
moduleScope.getModule().asCompilerModule(),
scala.Option.apply(getTailStatus() != TailStatus.NOT_TAIL),
context.getCompilerConfig());
var compiler = context.getCompiler();
var tuppleOption = compiler.runInline(expression, inlineContext);
if (tuppleOption.isEmpty()) {
throw new RuntimeException("Invalid code passed to `eval`: " + expression);
}
var newInlineContext = tuppleOption.get()._1();
var ir = tuppleOption.get()._2();
var src = tuppleOption.get()._3();

var sco = newInlineContext.localScope().getOrElse(LocalScope::root);
var mod = newInlineContext.module$access$0().module$access$0();

var m = org.enso.interpreter.runtime.Module.fromCompilerModule(mod);
var toTruffle =
new IrToTruffle(context, src, m.getScope(), compiler.org$enso$compiler$Compiler$$config);
var expr = toTruffle.runInline(ir, sco, "<inline_source>");

if (shouldCaptureResultScope) {
expr = CaptureResultScopeNode.build(expr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.enso.interpreter.runtime;

import org.enso.compiler.pass.analyse.BindingAnalysis$;
import org.enso.compiler.codegen.IrToTruffle;
import org.enso.compiler.codegen.RuntimeStubsGenerator;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.FreshNameSupply;

Expand Down Expand Up @@ -30,10 +28,11 @@
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.type.Types;
import org.enso.pkg.Package;
import org.enso.pkg.QualifiedName;
import org.enso.polyglot.CompilationStage;
import org.enso.polyglot.data.TypeGraph;

import scala.Option;

Expand Down Expand Up @@ -113,18 +112,8 @@ public Thread createSystemThread(Runnable r) {

@Override
public void truffleRunCodegen(CompilerContext.Module module, CompilerConfig config) throws IOException {
truffleRunCodegen(module.getSource(), module.getScope(), config, module.getIr());
}

@Override
public void truffleRunCodegen(Source source, ModuleScope scope, CompilerConfig config, org.enso.compiler.core.ir.Module ir) {
new IrToTruffle(context, source, scope, config).run(ir);
}

@Override
public ExpressionNode truffleRunInline(Source source, LocalScope localScope, CompilerContext.Module module, CompilerConfig config, Expression ir) {
return new IrToTruffle(context, source, module.getScope(), config)
.runInline(ir, localScope, "<inline_source>");
var m = org.enso.interpreter.runtime.Module.fromCompilerModule(module);
new IrToTruffle(context, module.getSource(), m.getScope(), config).run(module.getIr());
}

// module related
Expand Down Expand Up @@ -168,6 +157,11 @@ public CompilationStage getCompilationStage(CompilerContext.Module module) {
return module.getCompilationStage();
}

@Override
public TypeGraph getTypeHierarchy() {
return Types.getTypeHierarchy();
}

@Override
public void updateModule(CompilerContext.Module module, Consumer<Updater> callback) {
try (var u = new ModuleUpdater((Module)module)) {
Expand Down Expand Up @@ -369,21 +363,11 @@ public boolean isSameAs(org.enso.interpreter.runtime.Module m) {
return module == m;
}

// XXX
public org.enso.interpreter.runtime.scope.ModuleScope getScope() {
return module.getScope();
}

@Override
public QualifiedName getName() {
return module.getName();
}

@Override
public Type findType(String name) {
return module.getScope().getTypes().get(name);
}

@Override
public BindingsMap getBindingsMap() {
if (module.getIr() != null) {
Expand Down
23 changes: 3 additions & 20 deletions engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import org.enso.compiler.phase.{
ImportResolver
}
import org.enso.editions.LibraryName
import org.enso.interpreter.node.{ExpressionNode => RuntimeExpression}
import org.enso.interpreter.runtime.scope.ModuleScope
Copy link
Member Author

Choose a reason for hiding this comment

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

No more Node and ModuleScope referenced in the compiler.

import org.enso.pkg.QualifiedName
import org.enso.polyglot.LanguageInfo
import org.enso.polyglot.CompilationStage
Expand Down Expand Up @@ -691,7 +689,7 @@ class Compiler(
def runInline(
srcString: String,
inlineContext: InlineContext
): Option[RuntimeExpression] = {
): Option[(InlineContext, Expression, Source)] = {
val newContext = inlineContext.copy(freshNameSupply = Some(freshNameSupply))
val source = Source
.newBuilder(
Expand All @@ -705,7 +703,7 @@ class Compiler(
ensoCompiler.generateIRInline(tree).flatMap { ir =>
val compilerOutput = runCompilerPhasesInline(ir, newContext)
runErrorHandlingInline(compilerOutput, source, newContext)
Some(newContext.truffleRunInline(context, source, config, compilerOutput))
Some((newContext, compilerOutput, source))
}
}

Expand All @@ -722,7 +720,7 @@ class Compiler(
qualifiedName: String,
loc: Option[IdentifiedLocation],
source: Source
): ModuleScope = {
): Unit = {
val module = Option(context.findTopScopeModule(qualifiedName))
.getOrElse {
val locStr = fileLocationFromSection(loc, source)
Expand All @@ -740,7 +738,6 @@ class Compiler(
"Trying to use a module in codegen without generating runtime stubs"
)
}
module.getScope
}

/** Parses the given source with the new Rust parser.
Expand Down Expand Up @@ -1231,20 +1228,6 @@ class Compiler(
source.getPath + ":" + srcLocation
}

/** Generates code for the truffle interpreter.
*
* @param ir the program to translate
* @param source the source code of the program represented by `ir`
* @param scope the module scope in which the code is to be generated
*/
def truffleCodegen(
ir: IRModule,
source: Source,
scope: ModuleScope
): Unit = {
context.truffleRunCodegen(source, scope, config, ir)
}

/** Performs shutdown actions for the compiler.
*
* @param waitForPendingJobCompletion whether or not shutdown should wait for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.enso.compiler.context

import org.enso.compiler.core.ir.Expression
import org.enso.compiler.PackageRepository
import org.enso.compiler.data.CompilerConfig
import org.enso.compiler.pass.PassConfiguration
import org.enso.interpreter.node.BaseNode.TailStatus
import org.enso.interpreter.runtime.scope.{LocalScope, ModuleScope}
import org.enso.interpreter.node.ExpressionNode
import com.oracle.truffle.api.source.Source
import org.enso.interpreter.runtime.scope.LocalScope

/** A type containing the information about the execution context for an inline
* expression.
Expand All @@ -31,41 +27,28 @@ case class InlineContext(
pkgRepo: Option[PackageRepository] = None
) {
def bindingsAnalysis() = module.bindingsAnalysis()

def truffleRunInline(
context: CompilerContext,
source: Source,
config: CompilerConfig,
ir: Expression
): ExpressionNode = {
val s = localScope.getOrElse(LocalScope.root)
return module.truffleRunInline(context, source, s, config, ir)
}
}
object InlineContext {

/** Implements a null-safe conversion from nullable objects to Scala's option
* internally.
*
* @param localScope the local scope instance
* @param moduleScope the module scope instance
* @param module the module defining the context
* @param isInTailPosition whether or not the inline expression occurs in a
* tail position
* @return the [[InlineContext]] instance corresponding to the arguments
*/
def fromJava(
localScope: LocalScope,
moduleScope: ModuleScope,
isInTailPosition: TailStatus,
module: CompilerContext.Module,
isInTailPosition: Option[Boolean],
compilerConfig: CompilerConfig
): InlineContext = {
InlineContext(
localScope = Option(localScope),
module = ModuleContext(
moduleScope.getModule().asCompilerModule(),
compilerConfig
),
isInTailPosition = Option(isInTailPosition != TailStatus.NOT_TAIL),
localScope = Option(localScope),
module = ModuleContext(module, compilerConfig),
isInTailPosition = isInTailPosition,
compilerConfig = compilerConfig
)
}
Expand Down
Loading