-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Canaan Inc. All rights reserved. | ||
// Licensed under the Apache license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Nncase.IR; | ||
using Nncase.IR.Tensors; | ||
|
||
namespace Nncase.PatternMatch; | ||
|
||
/// <summary> | ||
/// Pattern for <see cref="If"/>. | ||
/// </summary> | ||
/// <param name="Then">Then pattern.</param> | ||
/// <param name="Else">Else pattern.</param> | ||
/// <param name="Arguments">Arguments pattern.</param> | ||
/// <param name="Name"> name. </param> | ||
public sealed record IfPattern(Pattern Then, Pattern Else, VArgsPattern Arguments, string? Name) : Pattern<If>(Name) | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="IfPattern"/> class. | ||
/// </summary> | ||
/// <param name="if"><see cref="If"/> expression.</param> | ||
/// <param name="name">name.</param> | ||
public IfPattern(If @if, string? name) | ||
: this(@if.Then, @if.Else, new VArgsPattern(@if.Arguments.ToArray(), null), name) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Get parameter pattern. | ||
/// </summary> | ||
/// <param name="parameter">Parameter info.</param> | ||
/// <returns>Parameter pattern.</returns> | ||
public Pattern this[ParameterInfo parameter] | ||
{ | ||
get => Arguments.Fields[parameter.Index]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// PatternMatch Utility. | ||
/// </summary> | ||
public static partial class Utility | ||
{ | ||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="name">name.</param> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
/// <returns>call pattern.</returns> | ||
public static IfPattern IsIf(string? name, Pattern then, Pattern @else, VArgsPattern parameters) => new IfPattern(then, @else, parameters, name); | ||
|
||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
/// <returns>call pattern.</returns> | ||
public static IfPattern IsIf(Pattern then, Pattern @else, VArgsPattern parameters) => IsIf(null, then, @else, parameters); | ||
|
||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="name">name.</param> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
/// <returns>call pattern.</returns> | ||
public static IfPattern IsIf(string? name, Pattern then, Pattern @else, params Pattern[] parameters) => new IfPattern(then, @else, new VArgsPattern(parameters, null), name); | ||
|
||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="name">name.</param> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
public static IfPattern IsIf(string? name, FunctionPattern then, FunctionPattern @else, params Pattern[] parameters) => new IfPattern(then, @else, new VArgsPattern(parameters, null), name); | ||
|
||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="name">name.</param> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
public static IfPattern IsIf(string? name, FunctionPattern then, FunctionPattern @else, VArgsPattern parameters) => new IfPattern(then, @else, parameters, name); | ||
|
||
/// <summary> | ||
/// is call . | ||
/// </summary> | ||
/// <param name="then">then.</param> | ||
/// <param name="else">else.</param> | ||
/// <param name="parameters">params.</param> | ||
public static IfPattern IsIf(Pattern then, Pattern @else, params Pattern[] parameters) => IsIf(null, then, @else, parameters); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Canaan Inc. All rights reserved. | ||
// Licensed under the Apache license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using NetFabric.Hyperlinq; | ||
using Nncase.IR; | ||
using Nncase.IR.Math; | ||
using Nncase.IR.Tensors; | ||
using Nncase.PatternMatch; | ||
using Nncase.Targets; | ||
using static Nncase.PatternMatch.F.Math; | ||
using static Nncase.PatternMatch.Utility; | ||
using static Nncase.Utilities.ReplaceUtility; | ||
|
||
namespace Nncase.Passes.Rules.ShapeBucket; | ||
|
||
public sealed class RemoveUnusedFunctions : ModulePass | ||
{ | ||
public RemoveUnusedFunctions(CompileOptions compileOptions) | ||
{ | ||
CompileOptions = compileOptions; | ||
} | ||
|
||
public CompileOptions CompileOptions { get; } | ||
|
||
protected override Task<IRModule> RunCoreAsync(IRModule input, RunPassContext context) | ||
{ | ||
while (true) | ||
{ | ||
var funcsToRemove = new HashSet<BaseFunction>(ReferenceEqualityComparer.Instance); | ||
foreach (var func in input.Functions) | ||
{ | ||
if (!ReferenceEquals(func, input.Entry) | ||
&& func.Users.Count == 1) | ||
{ | ||
funcsToRemove.Add(func); | ||
} | ||
} | ||
|
||
if (funcsToRemove.Count == 0) | ||
{ | ||
break; | ||
} | ||
|
||
foreach (var func in funcsToRemove) | ||
{ | ||
input.Remove(func); | ||
} | ||
} | ||
|
||
return Task.FromResult(input); | ||
} | ||
} |
Oops, something went wrong.