Skip to content

Commit 03c9285

Browse files
authored
Use ErrorPrinter in PatternRewrite facilities (#445)
When pattern rewriters catch an exception, they use the new `ErrorPrinter` to print the current state of IR, indicate which exact op was worked on by the failing pattern, and the full exception that happened.
1 parent ab1e871 commit 03c9285

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

core/src/ir/IRUtils.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scair.ir
22

3+
import scala.annotation.tailrec
34
import scala.collection.mutable.LinkedHashMap
45
import scala.collection.mutable.ListBuffer
56
import scala.collection.mutable.Map
@@ -25,6 +26,12 @@ import scala.collection.mutable.Map
2526
trait IRNode:
2627
def parent: Option[IRNode]
2728

29+
@tailrec
30+
final def topLevel: IRNode =
31+
parent match
32+
case Some(p) => p.topLevel
33+
case None => this
34+
2835
final def isAncestor(other: IRNode): Boolean =
2936
other.parent match
3037
case Some(parent) if parent == this => true

core/src/print/ErrorPrinter.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,33 @@ private final class ErrorPrinterFilter(writer: Writer)
6161
override def write(str: String): Unit =
6262
writeRec(str, 0)
6363

64-
final class ErrorPrinter(
64+
object ErrorPrinter:
65+
66+
def apply(
67+
error: Err,
68+
w: Writer = PrintWriter(System.out),
69+
_indent: String = " ",
70+
_valueNextID: Int = 0,
71+
_blockNextID: Int = 0,
72+
_valueNameMap: mutable.Map[Value[? <: Attribute], String] = mutable.Map
73+
.empty,
74+
_blockNameMap: mutable.Map[Block, String] = mutable.Map.empty,
75+
_aliasesMap: Map[Attribute, String] = Map.empty,
76+
_indentLevel: Int = 0,
77+
): ErrorPrinter =
78+
new ErrorPrinter(
79+
error,
80+
new ErrorPrinterFilter(w),
81+
_indent,
82+
_valueNextID,
83+
_blockNextID,
84+
_valueNameMap,
85+
_blockNameMap,
86+
_aliasesMap,
87+
_indentLevel,
88+
)
89+
90+
final class ErrorPrinter private (
6591
error: Err,
6692
w: ErrorPrinterFilter = ErrorPrinterFilter(new PrintWriter(System.out)),
6793
_indent: String = " ",

core/src/transformations/PatternRewriter.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package scair.transformations
22

33
import scair.helpers.*
44
import scair.ir.*
5+
import scair.print.ErrorPrinter
6+
import scair.utils.Err
57

8+
import java.io.StringWriter
69
import scala.annotation.tailrec
710
import scala.collection.mutable.LinkedHashSet
811

@@ -24,6 +27,21 @@ import scala.collection.mutable.LinkedHashSet
2427
|| Utils realm ||
2528
\*≡==---==≡==---==≡*/
2629

30+
private def augmentException(
31+
e: Exception,
32+
op: Operation,
33+
pattern: RewritePattern,
34+
): Nothing =
35+
val errorStringWriter = StringWriter()
36+
val printer = ErrorPrinter(
37+
Err(s"Caught exception applying $pattern", Some(op)),
38+
errorStringWriter,
39+
)
40+
printer.printTopLevel(op.topLevel.asInstanceOf[Operation])
41+
val errorString = errorStringWriter.toString()
42+
errorStringWriter.close()
43+
throw Exception(errorString, e)
44+
2745
object InsertPoint:
2846

2947
def before(op: Operation) =
@@ -199,7 +217,8 @@ case class GreedyRewritePatternApplier(patterns: Seq[RewritePattern])
199217
patterns match
200218
case Nil => ()
201219
case h :: t =>
202-
h.matchAndRewrite(op, rewriter)
220+
try h.matchAndRewrite(op, rewriter)
221+
catch case e: Exception => augmentException(e, op, h)
203222
if !rewriter.hasDoneAction then matchAndRewriteRec(op, rewriter, t)
204223

205224
override def matchAndRewrite(
@@ -345,8 +364,7 @@ class PatternRewriteWalker(
345364
rewriter.currentOp = op
346365

347366
try pattern.matchAndRewrite(op, rewriter)
348-
catch
349-
case e: Exception => throw e // throw new Exception("Caught exception!")
367+
catch case e: Exception => augmentException(e, op, pattern)
350368

351369
rewriter_done_action |= rewriter.hasDoneAction
352370

tools/opt/src/ScairOpt.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ trait ScairOptBase extends ScairToolBase[ScairOptArgs]:
116116
): OK[Operation] =
117117
error match
118118
case Err(msg, Some(_)) =>
119-
val p = new ErrorPrinter(error)
120-
p.print(operation)
119+
val p = ErrorPrinter(error)
120+
p.printTopLevel(operation)
121121
if verifyDiagnostics then error else sys.exit(42)
122122

123123
def main(args: Array[String]): Unit =

0 commit comments

Comments
 (0)