Skip to content

Commit

Permalink
simplify monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Oct 25, 2023
1 parent 7fc4341 commit 7ccdd40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
24 changes: 13 additions & 11 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint

private var _progress: Progress | Null = null // Set if progress reporting is enabled

/** Only safe to call if progress is being tracked. */
private inline def trackProgress(using Context)(inline op: Context ?=> Progress => Unit): Unit =
val local = _progress
if local != null then
op(using ctx)(local)
foldProgress(())(op)

private inline def foldProgress[T](using Context)(inline default: T)(inline op: Context ?=> Progress => T): T =
val local = _progress
Expand All @@ -184,11 +181,11 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
else
default

def didEnterUnit()(using Context): Boolean =
foldProgress(true /* should progress by default */)(_.tryEnterUnit(ctx.compilationUnit))
def didEnterUnit(unit: CompilationUnit)(using Context): Boolean =
foldProgress(true /* should progress by default */)(_.tryEnterUnit(unit))

def didEnterFinal()(using Context): Boolean =
foldProgress(true /* should progress by default */)(p => !p.checkCancellation())
def canProgress()(using Context): Boolean =
foldProgress(true /* not cancelled by default */)(p => !p.checkCancellation())

def doAdvanceUnit()(using Context): Unit =
trackProgress: progress =>
Expand Down Expand Up @@ -351,7 +348,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
if (!ctx.reporter.hasErrors)
Rewrites.writeBack()
suppressions.runFinished(hasErrors = ctx.reporter.hasErrors)
while (finalizeActions.nonEmpty && didEnterFinal()) {
while (finalizeActions.nonEmpty && canProgress()) {
val action = finalizeActions.remove(0)
action()
}
Expand Down Expand Up @@ -572,8 +569,13 @@ object Run {
extension (run: Run | Null)

/** record that the current phase has begun for the compilation unit of the current Context */
def enterUnit()(using Context): Boolean =
if run != null then run.didEnterUnit()
def enterUnit(unit: CompilationUnit)(using Context): Boolean =
if run != null then run.didEnterUnit(unit)
else true // don't check cancellation if we're not tracking progress

/** check progress cancellation, true if not cancelled */
def enterRegion()(using Context): Boolean =
if run != null then run.canProgress()
else true // don't check cancellation if we're not tracking progress

/** advance the unit count and record progress in the current phase */
Expand Down
31 changes: 14 additions & 17 deletions compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ object Phases {
val buf = List.newBuilder[CompilationUnit]
for unit <- units do
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
if ctx.run.enterUnit() then
if ctx.run.enterUnit(unit) then
try run
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
Expand Down Expand Up @@ -458,29 +458,26 @@ object Phases {
final def iterator: Iterator[Phase] =
Iterator.iterate(this)(_.next) takeWhile (_.hasNext)

/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
/** Cancellable region, if not cancelled, run the body in the context of the current compilation unit.
* Enrich crash messages.
*/
final def monitor(doing: String)(body: Context ?=> Unit)(using Context): Boolean =
if ctx.run.enterUnit() then
val unit = ctx.compilationUnit
if ctx.run.enterUnit(unit) then
try {body; true}
catch
case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing ${ctx.compilationUnit}"))
throw ex
catch case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing $unit"))
throw ex
finally ctx.run.advanceUnit()
else
false

/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
final def monitorOpt[T](doing: String)(body: Context ?=> Option[T])(using Context): Option[T] =
if ctx.run.enterUnit() then
try body
catch
case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing ${ctx.compilationUnit}"))
throw ex
finally ctx.run.advanceUnit()
/** Do not run if compile progress has been cancelled */
final def cancellable(body: Context ?=> Unit)(using Context): Boolean =
if ctx.run.enterRegion() then
{body; true}
else
None
false

override def toString: String = phaseName
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import NameOps._
import ast.Trees.Tree
import Phases.Phase


/** Load trees from TASTY files */
class ReadTasty extends Phase {

Expand All @@ -23,13 +22,14 @@ class ReadTasty extends Phase {

override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
withMode(Mode.ReadPositions) {
val unitContexts = units.map(unit => ctx.fresh.setCompilationUnit(unit))
unitContexts.flatMap(applyPhase()(using _))
val nextUnits = collection.mutable.ListBuffer.empty[CompilationUnit]
val unitContexts = units.view.map(ctx.fresh.setCompilationUnit)
for given Context <- unitContexts if addTasty(nextUnits += _) do ()
nextUnits.toList
}

private def applyPhase()(using Context): Option[CompilationUnit] = monitorOpt(phaseName):
val unit = ctx.compilationUnit
readTASTY(unit)
def addTasty(fn: CompilationUnit => Unit)(using Context): Boolean = monitor(phaseName):
readTASTY(ctx.compilationUnit).foreach(fn)

def readTASTY(unit: CompilationUnit)(using Context): Option[CompilationUnit] = unit match {
case unit: TASTYCompilationUnit =>
Expand Down
22 changes: 11 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/init/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ class Checker extends Phase:
val traverser = new InitTreeTraverser()
val unitContexts = units.map(unit => checkCtx.fresh.setCompilationUnit(unit))

val unitContexts0 =
for
given Context <- unitContexts
if traverse(traverser)
yield ctx
val units0 =
for given Context <- unitContexts if traverse(traverser) yield ctx.compilationUnit

val classes = traverser.getClasses()
cancellable {
val classes = traverser.getClasses()

if ctx.settings.YcheckInit.value then
Semantic.checkClasses(classes)(using checkCtx)
if ctx.settings.YcheckInit.value then
Semantic.checkClasses(classes)(using checkCtx)

if ctx.settings.YcheckInitGlobal.value then
Objects.checkClasses(classes)(using checkCtx)
if ctx.settings.YcheckInitGlobal.value then
Objects.checkClasses(classes)(using checkCtx)
}

unitContexts0.map(_.compilationUnit)
units0
end runOn

def run(using Context): Unit = unsupported("run")

Expand Down

0 comments on commit 7ccdd40

Please sign in to comment.