Skip to content

Commit c9e1f99

Browse files
committed
Print dim with script
1 parent 42e6e46 commit c9e1f99

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/Nncase.Core/IR/Expr.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ internal Expr(IEnumerable<Expr> operands)
4747
{
4848
operand.AddUser(this);
4949
}
50+
51+
RefreshDepth();
5052
}
5153

5254
internal Expr(Expr[] operands)
@@ -57,8 +59,12 @@ internal Expr(Expr[] operands)
5759
{
5860
operand.AddUser(this);
5961
}
62+
63+
RefreshDepth();
6064
}
6165

66+
public int Depth { get; private set; }
67+
6268
public IRMetadata Metadata { get; set; } = new();
6369

6470
/// <summary>
@@ -318,6 +324,7 @@ private void OnOperandsReplaced()
318324
InvalidateTypeInference();
319325
InvalidateHashCodeCache();
320326
InvalidateRange();
327+
RefreshDepth();
321328
}
322329

323330
private void InvalidateTypeInference()
@@ -370,4 +377,9 @@ private void InvalidateUsersRange()
370377
user.InvalidateRange();
371378
}
372379
}
380+
381+
private void RefreshDepth()
382+
{
383+
Depth = _operands.Length == 0 ? 0 : _operands.Max(x => x.Depth) + 1;
384+
}
373385
}

src/Nncase.Diagnostics/Diagnostics/ILPrintVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ private string VisitDimension(Dimension dimension) =>
721721
{
722722
DimensionKind.Any => "any",
723723
DimensionKind.Fixed => dimension.FixedValue.ToString(),
724-
DimensionKind.Unknown => dimension.Value is Var var ? $"%{var.Name}" : "?",
724+
DimensionKind.Unknown => dimension.Value is Var var ? $"%{var.Name}" : (dimension.Value.Depth > 4 ? "..." : CompilerServices.Print(dimension.Value, true)),
725725
_ => throw new NotSupportedException(dimension.Kind.ToString()),
726726
};
727727

src/Nncase.Diagnostics/Diagnostics/ScriptPrintVisitor.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,44 @@ protected override IPrintSymbol VisitCall(Call expr)
324324
return doc;
325325
}
326326

327+
/// <inheritdoc/>
328+
protected override IPrintSymbol VisitIf(If expr)
329+
{
330+
if (_exprMemo.TryGetValue(expr, out var doc))
331+
{
332+
return doc;
333+
}
334+
335+
doc = new(new($"if ..."));
336+
_exprMemo.Add(expr, doc!);
337+
return doc!;
338+
339+
// var target = Visit(expr.Target);
340+
// var args = expr.Arguments.AsValueEnumerable().Select(Visit).ToArray();
341+
// _context.CurrentCall = expr;
342+
// _scope.Push();
343+
// switch (expr.Target)
344+
// {
345+
// case Op op:
346+
// _scope.Append(CompilerServices.PrintOp(op, _context, false));
347+
// break;
348+
// case Function:
349+
// _scope.Append($"{target.Name}({string.Join(", ", from a in args select a.ToString())})");
350+
// break;
351+
// case TIR.PrimFunction:
352+
// _scope.AppendLine(string.Empty);
353+
// _scope.IndWrite($"{target.Name}({string.Join(", ", from a in args select a.ToString())})");
354+
// break;
355+
// default:
356+
// _scope.Append($"{target}({string.Join(", ", from a in args select a.ToString())})");
357+
// break;
358+
// }
359+
360+
// doc = new(_scope.Pop());
361+
// _exprMemo.Add(expr, doc);
362+
// return doc;
363+
}
364+
327365
/// <inheritdoc/>
328366
protected override IPrintSymbol VisitTensorConst(TensorConst @const)
329367
{

0 commit comments

Comments
 (0)