Skip to content

Commit

Permalink
speed up dump ir
Browse files Browse the repository at this point in the history
  • Loading branch information
zhen8838 committed Jan 2, 2025
1 parent 7213bc9 commit c97a89e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
11 changes: 6 additions & 5 deletions src/Nncase.Core/CompilerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public interface ICompilerServicesProvider
/// </summary>
/// <param name="expr"> the expression. </param>
/// <param name="useScript">Print script format.</param>
/// <param name="display_callable">display callable.</param>
/// <returns>the string.</returns>
string Print(Expr expr, bool useScript);
string Print(Expr expr, bool useScript, bool display_callable);

/// <summary>
/// Evaluate the expression tree.
Expand Down Expand Up @@ -475,7 +476,7 @@ public static bool TryEMatchRoot(Expr expr, IPattern pattern, [MaybeNullWhen(fal
/// <returns>Result.</returns>
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);

/// <summary>
Expand All @@ -484,7 +485,7 @@ public static void DumpIR(Expr expr, string prefix, string dumpPath, bool displa
/// not support prim func/prim func wrapper.
/// </remarks>
/// </summary>
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);

/// <summary>
Expand All @@ -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);

/// <summary>
/// Get target.
Expand Down Expand Up @@ -629,7 +630,7 @@ public void DumpPatternIR(Expr expr, string prefix, string dumpDir) =>
public string Print(IRType type) => _irprinterProvider.Print(type);

/// <inheritdoc/>
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);

/// <inheritdoc/>
public bool TryMatch(Expr expr, IPattern pattern, MatchOptions options, [MaybeNullWhen(false)] out IMatchResult result)
Expand Down
2 changes: 1 addition & 1 deletion src/Nncase.Core/Diagnostics/IDumpper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IDumpper
/// <returns>Sub dummper.</returns>
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);

Expand Down
2 changes: 1 addition & 1 deletion src/Nncase.Core/Diagnostics/NullDumpper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed class NullDumpper : IDumpper
public IDumpper CreateSubDummper(string subDirectory, DumpFlags? dumpFlags) => this;

/// <inheritdoc/>
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)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/Nncase.Core/IR/IIRPrinterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public interface IIRPrinterProvider
/// </summary>
/// <param name="expr"> the expression.</param>
/// <param name="useScript"> tir mode.</param>
/// <param name="display_callable"> display callable.</param>
/// <returns>the string.</returns>
string Print(Expr expr, bool useScript);
string Print(Expr expr, bool useScript, bool display_callable);
}
2 changes: 1 addition & 1 deletion src/Nncase.Diagnostics/Diagnostics/Dumpper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("}");
Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Nncase.Diagnostics/Diagnostics/IRPrinterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public string Print(IRType type)
}

/// <inheritdoc/>
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
{
Expand Down

0 comments on commit c97a89e

Please sign in to comment.