From 9c747d2462c82f3cd2c89be6001f30693f6d7295 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 28 Oct 2024 20:39:59 +0100 Subject: [PATCH] Towards single Scope for all if-then-else branches --- .../pass/analyse/PassPersistance.java | 4 +- .../compiler/pass/analyse/AliasAnalysis.scala | 13 ++--- .../pass/analyse/FramePointerAnalysis.scala | 33 ++---------- .../pass/analyse/alias/graph/Graph.scala | 52 +++---------------- .../core/ir/expression/errors/Redefined.scala | 3 ++ 5 files changed, 21 insertions(+), 84 deletions(-) diff --git a/engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/PassPersistance.java b/engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/PassPersistance.java index 829c0f6a530c..210cb6b49586 100644 --- a/engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/PassPersistance.java +++ b/engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/PassPersistance.java @@ -102,12 +102,11 @@ public PersistAliasAnalysisGraphScope() { @Override @SuppressWarnings("unchecked") protected Graph.Scope readObject(Input in) throws IOException { - var flatten = in.readBoolean(); var childScopes = in.readInline(scala.collection.immutable.List.class); var occurrencesValues = (scala.collection.immutable.Set) in.readObject(); var occurrences = occurrencesValues.map(v -> Tuple2$.MODULE$.apply(v.id(), v)).toMap(null); var allDefinitions = in.readInline(scala.collection.immutable.List.class); - var parent = new Graph.Scope(flatten, childScopes, occurrences, allDefinitions); + var parent = new Graph.Scope(childScopes, occurrences, allDefinitions); var optionParent = Option.apply(parent); childScopes.forall( (object) -> { @@ -121,7 +120,6 @@ protected Graph.Scope readObject(Input in) throws IOException { @Override @SuppressWarnings("unchecked") protected void writeObject(Graph.Scope obj, Output out) throws IOException { - out.writeBoolean(obj.flattenToParent()); out.writeInline(scala.collection.immutable.List.class, obj.childScopes()); out.writeObject(obj.occurrences().values().toSet()); out.writeInline(scala.collection.immutable.List.class, obj.allDefinitions()); diff --git a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/AliasAnalysis.scala b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/AliasAnalysis.scala index efc848971365..bcfb27dc053e 100644 --- a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/AliasAnalysis.scala +++ b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/AliasAnalysis.scala @@ -416,7 +416,7 @@ case object AliasAnalysis extends IRPass { ) case binding @ Expression.Binding(name, expression, _, _) => if ( - !parentScope.hasSymbolOccurrenceAs[GraphOccurrence.Def](name.name) + true // !parentScope.hasSymbolOccurrenceAs[GraphOccurrence.Def](name.name) ) { val isSuspended = expression match { case Expression.Block(_, _, _, isSuspended, _) => isSuspended @@ -450,7 +450,8 @@ case object AliasAnalysis extends IRPass { ) ) } else { - errors.Redefined.Binding(binding) + // errors.Redefined.Binding(binding) + binding } case app: Application => analyseApplication(app, graph, parentScope) @@ -787,7 +788,7 @@ case object AliasAnalysis extends IRPass { if (!isConstructorNameInPatternContext && !name.isMethod) { graph.resolveLocalUsage(occurrence) } else { - graph.resolveGlobalUsage(occurrence) + // graph.resolveGlobalUsage(occurrence) } } name.updateMetadata( @@ -810,9 +811,9 @@ case object AliasAnalysis extends IRPass { graph: Graph, parentScope: Scope ): IfThenElse = { - val condScope = parentScope.addChild(flattenToParent = true) - val trueScope = parentScope.addChild(flattenToParent = true) - val falseScope = parentScope.addChild(flattenToParent = true) + val condScope = parentScope; // .addChild() + val trueScope = parentScope; // .addChild() + val falseScope = parentScope; // .addChild() ir.copy( cond = analyseExpression(ir.cond, graph, condScope), trueBranch = analyseExpression(ir.trueBranch, graph, trueScope), diff --git a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/FramePointerAnalysis.scala b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/FramePointerAnalysis.scala index df3579f680e6..6b56881ccef9 100644 --- a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/FramePointerAnalysis.scala +++ b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/FramePointerAnalysis.scala @@ -95,12 +95,7 @@ case object FramePointerAnalysis extends IRPass { } private def updateSymbolNames(e: IR, s: Graph.Scope): Unit = { - var symbols = s.allDefinitions.map(_.symbol) - s.childScopes.foreach(ch => - if (ch.flattenToParent) { - symbols ++= ch.allDefinitions.map(_.symbol) - } - ) + val symbols = s.allDefinitions.map(_.symbol) updateMeta(e, FrameVariableNames.create(symbols)) } @@ -365,27 +360,7 @@ case object FramePointerAnalysis extends IRPass { "Def occurrence must be in the given scope" ) ) - val parentOffset = if (scope.flattenToParent && scope.parent.nonEmpty) { - val p = scope.parent.get - var off = p.allDefinitions.size - val found = p.childScopes.find(ch => { - if (ch == scope) { - true - } else { - if (ch.flattenToParent) { - off += ch.allDefinitions.size - } - false - } - }) - org.enso.common.Asserts.assertInJvm( - found != null, - "Child must be found: " + scope + " among " + p.childScopes - ) - off - } else { - 0 - } + val parentOffset = 0 parentOffset + idxInScope + LocalScope.internalSlotsSize } @@ -402,9 +377,7 @@ case object FramePointerAnalysis extends IRPass { var currScope: Option[Graph.Scope] = Some(childScope) var scopeDistance = 0 while (currScope.isDefined && currScope.get != parentScope) { - if (!currScope.get.flattenToParent) { - scopeDistance += 1 - } + scopeDistance += 1 currScope = currScope.get.parent } scopeDistance diff --git a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/alias/graph/Graph.scala b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/alias/graph/Graph.scala index 28d9615ce23a..167cc0dda57e 100644 --- a/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/alias/graph/Graph.scala +++ b/engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/analyse/alias/graph/Graph.scala @@ -16,18 +16,14 @@ sealed class Graph( ) extends Serializable { private var sourceLinks: Map[Graph.Id, Set[Graph.Link]] = new HashMap() private var targetLinks: Map[Graph.Id, Set[Graph.Link]] = new HashMap() - private var frozen: Boolean = false { links.foreach(addSourceTargetLink) } - private var globalSymbols: Map[Graph.Symbol, GraphOccurrence.Global] = - Map() - /** @return the next counter value */ - def nextIdCounter: Int = _nextIdCounter + private[compiler] def nextIdCounter: Int = _nextIdCounter /** @return a deep structural copy of `this` */ def deepCopy( @@ -37,29 +33,15 @@ sealed class Graph( this.rootScope.deepCopy(scope_mapping), this.nextIdCounter ) - copy.links = this.links - copy.sourceLinks = this.sourceLinks - copy.targetLinks = this.targetLinks - copy.globalSymbols = this.globalSymbols + copy.links = this.links + copy.sourceLinks = this.sourceLinks + copy.targetLinks = this.targetLinks copy } def getLinks(): Set[Graph.Link] = links - def freeze(): Unit = { - frozen = true - } - - /** Registers a requested global symbol in the aliasing scope. - * - * @param sym the symbol occurrence - */ - def addGlobalSymbol(sym: GraphOccurrence.Global): Unit = { - org.enso.common.Asserts.assertInJvm(!frozen) - if (!globalSymbols.contains(sym.symbol)) { - globalSymbols = globalSymbols + (sym.symbol -> sym) - } - } + def freeze(): Unit = {} /** Creates a deep copy of the aliasing graph structure. * @@ -126,24 +108,6 @@ sealed class Graph( ) } - /** Resolves any links for the given usage of a symbol, assuming the symbol - * is global (i.e. method, constructor etc.) - * - * @param occurrence the symbol usage - * @return the link, if it exists - */ - def resolveGlobalUsage( - occurrence: GraphOccurrence.Use - ): Option[Graph.Link] = { - scopeFor(occurrence.id) match { - case Some(scope) => - globalSymbols - .get(occurrence.symbol) - .map(g => Graph.Link(occurrence.id, scope.scopesToRoot + 1, g.id)) - case None => None - } - } - /** Returns a string representation of the graph. * * @return a string representation of `this` @@ -359,7 +323,6 @@ object Graph { * Note that there may not be a link for all these definitions. */ sealed class Scope( - val flattenToParent: Boolean = false, var childScopes: List[Scope] = List(), var occurrences: Map[Id, GraphOccurrence] = HashMap(), var allDefinitions: List[GraphOccurrence.Def] = List() @@ -407,7 +370,6 @@ object Graph { ) val newScope = new Scope( - this.flattenToParent, childScopeCopies.toList, occurrences, allDefinitions @@ -442,8 +404,8 @@ object Graph { * @param is this scope "just virtual" and will be flatten to parent at the end? * @return a scope that is a child of `this` */ - def addChild(flattenToParent: Boolean = false): Scope = { - val scope = new Scope(flattenToParent) + def addChild(): Scope = { + val scope = new Scope() scope.parent = Some(this) childScopes ::= scope diff --git a/engine/runtime-parser/src/main/scala/org/enso/compiler/core/ir/expression/errors/Redefined.scala b/engine/runtime-parser/src/main/scala/org/enso/compiler/core/ir/expression/errors/Redefined.scala index e375d7e1e179..c23a6d921d02 100644 --- a/engine/runtime-parser/src/main/scala/org/enso/compiler/core/ir/expression/errors/Redefined.scala +++ b/engine/runtime-parser/src/main/scala/org/enso/compiler/core/ir/expression/errors/Redefined.scala @@ -755,10 +755,13 @@ object Redefined { || diagnostics != this.diagnostics || id != this.id ) { + /* val res = Binding(invalidBinding, passData) res.diagnostics = diagnostics res.id = id res + */ + this } else this }