From c97a89ec22a7bc271b20e1d3e5ec8819456c666a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=90=AF=E8=88=AA?= <597323109@qq.com> Date: Thu, 2 Jan 2025 16:36:17 +0800 Subject: [PATCH] speed up dump ir --- src/Nncase.Core/CompilerServices.cs | 11 +++---- src/Nncase.Core/Diagnostics/IDumpper.cs | 2 +- src/Nncase.Core/Diagnostics/NullDumpper.cs | 2 +- src/Nncase.Core/IR/IIRPrinterProvider.cs | 3 +- src/Nncase.Diagnostics/Diagnostics/Dumpper.cs | 2 +- .../Diagnostics/ILPrintVisitor.cs | 30 +++++++++---------- .../Diagnostics/IRPrinterProvider.cs | 6 ++-- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Nncase.Core/CompilerServices.cs b/src/Nncase.Core/CompilerServices.cs index 2f731a7fdf..3db45bd4d6 100644 --- a/src/Nncase.Core/CompilerServices.cs +++ b/src/Nncase.Core/CompilerServices.cs @@ -91,8 +91,9 @@ public interface ICompilerServicesProvider /// /// the expression. /// Print script format. + /// display callable. /// the string. - string Print(Expr expr, bool useScript); + string Print(Expr expr, bool useScript, bool display_callable); /// /// Evaluate the expression tree. @@ -475,7 +476,7 @@ public static bool TryEMatchRoot(Expr expr, IPattern pattern, [MaybeNullWhen(fal /// Result. public static string PrintOp(Op op, IIRPrinterContext context, bool iLmode) => Provider.PrintOp(op, context, iLmode); - public static void DumpIR(Expr expr, string prefix, string dumpPath, bool display_callable = true) => + public static void DumpIR(Expr expr, string prefix, string dumpPath, bool display_callable = false) => Provider.DumpIR(expr, prefix, dumpPath, display_callable); /// @@ -484,7 +485,7 @@ public static void DumpIR(Expr expr, string prefix, string dumpPath, bool displa /// not support prim func/prim func wrapper. /// /// - public static void DumpDotIR(Expr expr, string prefix, string dumpPath, bool display_callable = true) => + public static void DumpDotIR(Expr expr, string prefix, string dumpPath, bool display_callable = false) => Provider.DumpDotIR(expr, prefix, dumpPath, display_callable); /// @@ -508,7 +509,7 @@ public static void DumpPatternIR(Expr expr, string prefix, string dumpDir) => public static string Print(IRType type) => Provider.Print(type); - public static string Print(Expr expr, bool useScript = false) => Provider.Print(expr, useScript); + public static string Print(Expr expr, bool useScript = false, bool display_callable = false) => Provider.Print(expr, useScript, display_callable); /// /// Get target. @@ -629,7 +630,7 @@ public void DumpPatternIR(Expr expr, string prefix, string dumpDir) => public string Print(IRType type) => _irprinterProvider.Print(type); /// - public string Print(Expr expr, bool useScript) => _irprinterProvider.Print(expr, useScript); + public string Print(Expr expr, bool useScript, bool display_callable) => _irprinterProvider.Print(expr, useScript, display_callable); /// public bool TryMatch(Expr expr, IPattern pattern, MatchOptions options, [MaybeNullWhen(false)] out IMatchResult result) diff --git a/src/Nncase.Core/Diagnostics/IDumpper.cs b/src/Nncase.Core/Diagnostics/IDumpper.cs index cc84bf46cb..e3b1aa9120 100644 --- a/src/Nncase.Core/Diagnostics/IDumpper.cs +++ b/src/Nncase.Core/Diagnostics/IDumpper.cs @@ -36,7 +36,7 @@ public interface IDumpper /// Sub dummper. IDumpper CreateSubDummper(string subDirectory, DumpFlags? dumpFlags = null); - void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = true); + void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = false); void DumpDotIR(Expr expr, string prefix, string? reletivePath = null); diff --git a/src/Nncase.Core/Diagnostics/NullDumpper.cs b/src/Nncase.Core/Diagnostics/NullDumpper.cs index 3120fa25e5..ea76b2af35 100644 --- a/src/Nncase.Core/Diagnostics/NullDumpper.cs +++ b/src/Nncase.Core/Diagnostics/NullDumpper.cs @@ -27,7 +27,7 @@ public sealed class NullDumpper : IDumpper public IDumpper CreateSubDummper(string subDirectory, DumpFlags? dumpFlags) => this; /// - public void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = true) + public void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = false) { } diff --git a/src/Nncase.Core/IR/IIRPrinterProvider.cs b/src/Nncase.Core/IR/IIRPrinterProvider.cs index f411167f2a..65e4862649 100644 --- a/src/Nncase.Core/IR/IIRPrinterProvider.cs +++ b/src/Nncase.Core/IR/IIRPrinterProvider.cs @@ -91,6 +91,7 @@ public interface IIRPrinterProvider /// /// the expression. /// tir mode. + /// display callable. /// the string. - string Print(Expr expr, bool useScript); + string Print(Expr expr, bool useScript, bool display_callable); } diff --git a/src/Nncase.Diagnostics/Diagnostics/Dumpper.cs b/src/Nncase.Diagnostics/Diagnostics/Dumpper.cs index 8bd3794074..25c6027732 100644 --- a/src/Nncase.Diagnostics/Diagnostics/Dumpper.cs +++ b/src/Nncase.Diagnostics/Diagnostics/Dumpper.cs @@ -34,7 +34,7 @@ public IDumpper CreateSubDummper(string subDirectory, DumpFlags? dumpFlags) return new Dumpper(subDumpFlags, Path.Join(_dumpDirectory, subDirectory)); } - public void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = true) + public void DumpIR(Expr expr, string prefix, string? reletivePath = null, bool displayCallable = false) { var path = Path.Join(_dumpDirectory, reletivePath); CompilerServices.DumpIR(expr, prefix, EnsureWritable(path), displayCallable); diff --git a/src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs b/src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs index f0e14122c9..c0ee6d1499 100644 --- a/src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs +++ b/src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs @@ -390,9 +390,16 @@ protected override string VisitFunction(Function expr) _scope.IndWriteLine("{"); // 2. Function body - using (_scope.IndentUp()) + if (_scope.IndentLevel == 0 || _displayCallable) { - var body = Visit(expr.Body); + using (_scope.IndentUp()) + { + var body = Visit(expr.Body); + } + } + else + { + _scope.IndWriteLine("..."); } // 3. Function closing @@ -418,22 +425,15 @@ protected override string VisitFusion(Fusion expr) _scope.IndWriteLine("{"); // 2. Function body - if (_displayCallable) + using (_scope.IndentUp()) { - using (_scope.IndentUp()) + var body_builder = new StringBuilder(); + using (var body_writer = new StringWriter(body_builder)) { - var body_builder = new StringBuilder(); - using (var body_writer = new StringWriter(body_builder)) - { - var visitor = new ILPrintVisitor(body_writer, true, _scope.IndentLevel).Visit(expr.Body); - _scope.Append(body_writer.ToString()); - } + var visitor = new ILPrintVisitor(body_writer, _displayCallable, _scope.IndentLevel).Visit(expr.Body); + _scope.Append(body_writer.ToString()); } } - else - { - _scope.IndWriteLine("..."); - } // 3. Function closing _scope.IndWriteLine("}"); @@ -462,7 +462,7 @@ protected override string VisitPrimFunctionWrapper(PrimFunctionWrapper expr) { using (_scope.IndentUp()) { - using (var bodys = new StringReader(CompilerServices.Print(expr.Target))) + using (var bodys = new StringReader(CompilerServices.Print(expr.Target, false, false))) { while (bodys.ReadLine() is string line) { diff --git a/src/Nncase.Diagnostics/Diagnostics/IRPrinterProvider.cs b/src/Nncase.Diagnostics/Diagnostics/IRPrinterProvider.cs index 9d2b7c87a9..dc24323303 100644 --- a/src/Nncase.Diagnostics/Diagnostics/IRPrinterProvider.cs +++ b/src/Nncase.Diagnostics/Diagnostics/IRPrinterProvider.cs @@ -130,13 +130,13 @@ public string Print(IRType type) } /// - public string Print(Expr expr, bool useScript) + public string Print(Expr expr, bool useScript, bool dispalycallable) { var sb = new StringBuilder(); using var dumpWriter = new StringWriter(sb); var text = expr is PrimFunction || useScript - ? new ScriptPrintVisitor(dumpWriter, true).Visit(expr).Serialize() - : new ILPrintVisitor(dumpWriter, true, 0).Visit(expr); + ? new ScriptPrintVisitor(dumpWriter, dispalycallable).Visit(expr).Serialize() + : new ILPrintVisitor(dumpWriter, dispalycallable, 0).Visit(expr); return useScript ? text : expr switch {