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

Report duplicates in the IR after each pass #11873

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
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
@@ -0,0 +1,79 @@
package org.enso.compiler.pass;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Set;
import org.enso.compiler.core.IR;
import org.enso.scala.wrapper.ScalaConversions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class IRHelpers {
private static final Logger LOG = LoggerFactory.getLogger(IRHelpers.class);
private static HashSet<Integer> reportedIdentityHashCodes;

private IRHelpers() {}

/**
* Processes whole IR subtree to find (top most) elements that are referenced multiple times.
*
* @param root the root of IR tree to process
* @return set of IR elements that appear at least twice
*/
private static Set<IR> findTopMostDuplicates(IR root) {
var foundIR = new IdentityHashMap<IR, Integer>();
var irToProcess = new ArrayDeque<IR>();
irToProcess.add(root);
while (!irToProcess.isEmpty()) {
var ir = irToProcess.remove();
if (foundIR.containsKey(ir)) {
foundIR.put(ir, 1);
} else {
foundIR.put(ir, 0);
irToProcess.addAll(ScalaConversions.asJava(ir.children()));
}
}
var it = foundIR.entrySet().iterator();
while (it.hasNext()) {
if (it.next().getValue() == 0) {
it.remove();
}
}
return foundIR.keySet();
}

static <IRType extends IR> IRType checkDuplicates(String msg, IRType ir) {
var duplicates = findTopMostDuplicates(ir);
if (duplicates.isEmpty()) {
return ir;
} else {
if (reportedIdentityHashCodes == null) {
reportedIdentityHashCodes = new HashSet<>();
}
var all = ir.preorder();
for (var dupl : duplicates) {
if (!reportedIdentityHashCodes.add(System.identityHashCode(dupl))) {
continue;
}
LOG.error("Duplicate found after " + msg + ": " + toString(dupl));
all.foreach(
e -> {
if (e.children().contains(dupl)) {
LOG.error(" referenced by " + toString(e));
}
return null;
});
}
}
return ir;
}

private static String toString(IR ir) {
return ir.getClass().getName()
+ "@"
+ Integer.toHexString(System.identityHashCode(ir))
+ ":"
+ ir.showCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ class PassManager(
pendingMiniPasses.clear()
if (combinedPass != null) {
logger.trace(" flushing pending mini pass: {}", combinedPass)
miniPassCompile(combinedPass, in)
val res = miniPassCompile(combinedPass, in)
IRHelpers.checkDuplicates(
"Flushing mini passes: " + combinedPass,
res
)
res
} else {
in
}
Expand Down Expand Up @@ -220,7 +225,8 @@ class PassManager(
" mega running: {}",
megaPass
)
megaPassCompile(megaPass, flushedIR, context)
val res = megaPassCompile(megaPass, flushedIR, context)
IRHelpers.checkDuplicates("" + megaPass.getClass().getName(), res)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ case object Imports extends IRPass {
val parts = newName.parts
if (parts.length == 2) {
i.copy(
name = newName.copy(parts = parts :+ mainModuleName),
name =
newName.copy(parts = parts :+ mainModuleName.duplicate()),
Copy link
Member Author

Choose a reason for hiding this comment

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

There was a report:

 Duplicate found after org.enso.compiler.pass.desugar.Imports$: org.enso.compiler.core.ir.Name$Literal@56476c16:Main
   referenced by org.enso.compiler.core.ir.Name$Qualified@76e9f00b:Standard.Base.Main
   referenced by org.enso.compiler.core.ir.Name$Qualified@314b9e4b:Standard.Test.Main

and with bb4aff2 it is gone.

rename = computeRename(
i.rename,
i.onlyNames.nonEmpty || i.isAll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ class TruffleLoggerWrapper(name: String, masking: Masking) extends Logger {

override def getName: String = underlying.getName

private def isEnabled(level: Level): Boolean =
private def isEnabled(level: Level): Boolean = try {
underlying.isLoggable(level)
} catch {
Copy link
Member Author

Choose a reason for hiding this comment

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

There is a runtime-instrument-common/testOnly *ChangesetBuilderTest failure which gets fixed by b4f3973

case _: IllegalStateException => false
}

private def log(
level: Level,
Expand Down
Loading