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
Changes from 2 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
Expand Up @@ -25,8 +25,9 @@ class PassManager(
protected val passes: List[PassGroup],
passConfiguration: PassConfiguration
) {
private val logger = LoggerFactory.getLogger(classOf[PassManager])
val allPasses = verifyPassOrdering(passes.flatMap(_.passes))
private val logger = LoggerFactory.getLogger(classOf[PassManager])
val allPasses = verifyPassOrdering(passes.flatMap(_.passes))
private val reported = new java.util.HashMap[Integer, Boolean]()

/** Computes a valid pass ordering for the compiler.
*
Expand Down Expand Up @@ -165,6 +166,41 @@ class PassManager(
): IRType = {
val pendingMiniPasses: ListBuffer[MiniPassFactory] = ListBuffer()

def checkDuplicates(msg: String, ir: IRType): IRType = {
def toString(ir: IR): String = {
ir.getClass().getName() + "@" + Integer.toHexString(
System.identityHashCode(ir)
) + ":" + ir.showCode()
}

val all = ir.preorder()
val idMap = new java.util.IdentityHashMap[Any, Boolean]()
val duplFound = all.find(e => {
if (reported.containsKey(System.identityHashCode(e))) {
false
} else {
if (idMap.containsKey(e)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Scans for current ir in preorder and finds first == IR element that appears twice and hasn't yet been reported.

If such a duplFound, then it scans the IR again to find all elements that have the duplFound.orNull among their children() and prints that up.

The results of such a scan are visible in the log for example like:

Duplicate found after org.enso.compiler.pass.desugar.FunctionBinding$: org.enso.compiler.core.ir.Name$Literal@6edf29c1:Named_Pattern
   referenced by org.enso.compiler.core.ir.module.scope.definition.Method$Conversion@3791f50e:Regex.from = (that : Named_Pattern) -> ((compile) Regex ((regex_pattern) that))
   referenced by org.enso.compiler.core.ir.DefinitionArgument$Specified@7c8c70d6:(that : Named_Pattern)

In some cases such a sharing seems innocent, but in others it may be dangerous. Such a dangerous sharing was probably triggered by #11365 - investigation pending.

true
} else {
idMap.put(e, true)
false
}
}
})
if (duplFound.nonEmpty) {
reported.put(System.identityHashCode(duplFound.orNull), true)
logger.error(
"Duplicate found after " + msg + ": " + toString(duplFound.orNull)
)
all.foreach(e => {
if (e.children().contains(duplFound.orNull)) {
logger.error(" referenced by " + toString(e))
}
})
}
ir
}

def flushMiniPasses(in: IRType): IRType = {
if (pendingMiniPasses.nonEmpty) {
val miniPasses =
Expand All @@ -173,7 +209,9 @@ class PassManager(
pendingMiniPasses.clear()
if (combinedPass != null) {
logger.trace(" flushing pending mini pass: {}", combinedPass)
miniPassCompile(combinedPass, in)
val res = miniPassCompile(combinedPass, in)
checkDuplicates("Flushing mini passes: " + combinedPass, res)
res
} else {
in
}
Expand Down Expand Up @@ -220,7 +258,8 @@ class PassManager(
" mega running: {}",
megaPass
)
megaPassCompile(megaPass, flushedIR, context)
val res = megaPassCompile(megaPass, flushedIR, context)
checkDuplicates("" + megaPass.getClass().getName(), res)
}
}

Expand Down
Loading