Skip to content

Commit

Permalink
Detect and use MiniPassFactory in PassFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Oct 8, 2024
1 parent e2fc538 commit a58ecc8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
* only inline compilation, its {@link #createForModuleCompilation(ModuleContext)} method should
* return null.
*/
public interface MiniPassFactory<T extends MiniIRPass> {
public interface MiniPassFactory {
/**
* Creates an instance of mini pass that is capable of transforming IR elements in the context of
* a module.
*
* @param moduleContext A mini pass can optionally save reference to this module context.
* @return May return null if module compilation is not supported.
*/
T createForModuleCompilation(ModuleContext moduleContext);
MiniIRPass createForModuleCompilation(ModuleContext moduleContext);

/**
* Creates an instance of mini pass that is capable of transforming IR elements in the context of
Expand All @@ -25,5 +25,5 @@ public interface MiniPassFactory<T extends MiniIRPass> {
* @param inlineContext A mini pass can optionally save reference to this inline context.
* @return Must not return null. Inline compilation should always be supported.
*/
T createForInlineCompilation(InlineContext inlineContext);
MiniIRPass createForInlineCompilation(InlineContext inlineContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static IR compileRecursively(IR ir, MiniIRPass miniPass) {
newIr = ir;
}
}
var transformedIr = preparedMiniPass.transformIr((Expression) newIr);
var transformedIr = preparedMiniPass.transformIr(newIr);
logTransform(newIr, preparedMiniPass, transformedIr);
if (!preparedMiniPass.checkPostCondition(transformedIr)) {
throw new CompilerError("Post condition failed after applying mini pass " + preparedMiniPass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ class PassManager(
c.shouldWriteToContext = isLastRunOf(index, pass, passGroup)
)

pass.runModule(intermediateIR, newContext)
if (pass.isInstanceOf[MiniPassFactory]) {
val miniFactory = pass.asInstanceOf[MiniPassFactory]
val miniPass = miniFactory.createForModuleCompilation(newContext)
MiniPassTraverser.compileModuleWithMiniPass(intermediateIR, miniPass)
} else {
pass.runModule(intermediateIR, newContext)
}
}
}
}
Expand Down Expand Up @@ -148,7 +154,14 @@ class PassManager(
c.shouldWriteToContext = isLastRunOf(index, pass, passGroup)
)

pass.runExpression(intermediateIR, newContext)
if (pass.isInstanceOf[MiniPassFactory]) {
val miniFactory = pass.asInstanceOf[MiniPassFactory]
val miniPass = miniFactory.createForInlineCompilation(newContext)
MiniPassTraverser.compileInlineWithMiniPass(intermediateIR, miniPass)
} else {
pass.runExpression(intermediateIR, newContext)
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.enso.compiler.pass.resolve.{ExpressionAnnotations, GlobalNames}
*
* - The tail position of its expression, where relevant.
*/
case object TailCall extends IRPass with MiniPassFactory[TailCallMini] {
case object TailCall extends IRPass with MiniPassFactory {

/** The annotation metadata type associated with IR nodes by this pass. */
override type Metadata = TailPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import org.enso.compiler.pass.resolve.{
*
* - A [[FreshNameSupply]]
*/
case object LambdaShorthandToLambda
extends IRPass
with MiniPassFactory[LambdaShorthandToLambdaMini] {
case object LambdaShorthandToLambda extends IRPass with MiniPassFactory {
override type Metadata = IRPass.Metadata.Empty
override type Config = IRPass.Configuration.Default

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import org.enso.compiler.pass.analyse.{
*
* - Nothing
*/
case object OperatorToFunction
extends IRPass
with MiniPassFactory[OperatorToFunctionMini] {
case object OperatorToFunction extends IRPass with MiniPassFactory {

/** A purely desugaring pass has no analysis output. */
override type Metadata = IRPass.Metadata.Empty
Expand Down Expand Up @@ -46,6 +44,11 @@ case object OperatorToFunction
ir: Module,
moduleContext: ModuleContext
): Module = {
if (!java.lang.Boolean.getBoolean("testing.mini.passes")) {
throw new IllegalStateException(
"OperatorToFunction.runModule should no longer be used. This is a mini pass!"
)
}
val new_bindings = ir.bindings.map { a =>
a.mapExpressions(
runExpression(
Expand All @@ -71,7 +74,12 @@ case object OperatorToFunction
override def runExpression(
ir: Expression,
inlineContext: InlineContext
): Expression =
): Expression = {
if (!java.lang.Boolean.getBoolean("testing.mini.passes")) {
throw new IllegalStateException(
"OperatorToFunction.runExpression should no longer be used. This is a mini pass!"
)
}
ir.transformExpressions { case operatorBinary: Operator.Binary =>
new Application.Prefix(
operatorBinary.operator,
Expand All @@ -85,6 +93,7 @@ case object OperatorToFunction
operatorBinary.diagnostics
)
}
}

override def createForModuleCompilation(
moduleContext: ModuleContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
public abstract class MiniPassTester {
protected void compareModuleCompilation(
Module ir,
ModuleContext moduleContext,
IRPass megaPass,
MiniPassFactory<? extends MiniIRPass> miniPassFactory) {
Module ir, ModuleContext moduleContext, IRPass megaPass, MiniPassFactory miniPassFactory) {
var miniPass = miniPassFactory.createForModuleCompilation(moduleContext);
if (miniPass == null) {
throw new IllegalArgumentException("Mini pass does not support module compilation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class OperatorToFunctionTest extends CompilerTest {

val ctx = buildInlineContext()
val modCtx = buildModuleContext()
System.setProperty("testing.mini.passes", "true")

/** Generates an operator and its corresponding function.
*
Expand Down

0 comments on commit a58ecc8

Please sign in to comment.