Skip to content

Commit

Permalink
Towards single Scope for all if-then-else branches
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Oct 28, 2024
1 parent c440305 commit 9c747d2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<GraphOccurrence>) 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) -> {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
*
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -407,7 +370,6 @@ object Graph {
)
val newScope =
new Scope(
this.flattenToParent,
childScopeCopies.toList,
occurrences,
allDefinitions
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9c747d2

Please sign in to comment.