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 1 commit
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,13 +14,9 @@
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;
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 @@ -128,8 +118,6 @@ 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);
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 @@ -67,11 +67,22 @@ RootCallTarget parseExpression(LocalScope scope, ModuleScope moduleScope, String
InlineContext inlineContext =
InlineContext.fromJava(
localScope, moduleScope, getTailStatus(), context.getCompilerConfig());
ExpressionNode expr =
context.getCompiler().runInline(expression, inlineContext).getOrElse(() -> null);
if (expr == null) {
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 @@ -113,18 +111,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 @@ -369,11 +357,6 @@ 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();
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,10 @@
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

/** A type containing the information about the execution context for an inline
* expression.
Expand All @@ -31,16 +28,6 @@ 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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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.pkg.Package;
import org.enso.pkg.QualifiedName;
import org.enso.interpreter.runtime.scope.LocalScope
import org.enso.interpreter.node.ExpressionNode
import com.oracle.truffle.api.source.Source
import org.enso.compiler.data.BindingsMap.ModuleReference

Expand All @@ -28,17 +25,8 @@ case class ModuleContext(
isGeneratingDocs: Boolean = false,
pkgRepo: Option[PackageRepository] = None
) {
def isSynthetic() = module.isSynthetic()
def bindingsAnalysis() = module.getBindingsMap()
def truffleRunInline(
context: CompilerContext,
source: Source,
s: LocalScope,
config: CompilerConfig,
ir: Expression
): ExpressionNode = {
return context.truffleRunInline(source, s, module, config, ir)
}
def isSynthetic() = module.isSynthetic()
def bindingsAnalysis() = module.getBindingsMap()
def getName(): QualifiedName = module.getName()
def getPackage(): Package[_] = module.getPackage()
def getSource(): Source = module.getSource()
Expand Down
Loading