From d1a0b9d8969ab6c07dd5edf6ca5f3f7d3e612d11 Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Thu, 3 Jun 2021 16:05:12 +0200 Subject: [PATCH 1/2] Add more tests for externals and function overloading --- tests/unit/externals.expected | 28 +++++++++++++++++ tests/unit/externals.frag | 16 ++++++++++ tests/unit/externals.preserved.expected | 25 +++++++++++++++ tests/unit/function_overload.expected | 41 +++++++++++++++++++++++++ tests/unit/function_overload.frag | 15 +++++++++ 5 files changed, 125 insertions(+) create mode 100644 tests/unit/externals.expected create mode 100644 tests/unit/externals.frag create mode 100644 tests/unit/externals.preserved.expected create mode 100644 tests/unit/function_overload.expected create mode 100644 tests/unit/function_overload.frag diff --git a/tests/unit/externals.expected b/tests/unit/externals.expected new file mode 100644 index 00000000..8d2ccfab --- /dev/null +++ b/tests/unit/externals.expected @@ -0,0 +1,28 @@ +/* File generated with Shader Minifier 1.1.6 + * http://www.ctrl-alt-test.fr + */ +#ifndef EXTERNALS_EXPECTED_ +# define EXTERNALS_EXPECTED_ +# define VAR_E "t" +# define VAR_I "i" +# define VAR_N "n" + +const char *externals_frag = + "uniform int i,n,t;" + "int r()" + "{" + "int n,t,r,u,e;" + "return i;" + "}" + "int u()" + "{" + "int i,t,r,u,e;" + "return n;" + "}" + "int e()" + "{" + "int i,r,u,n,e;" + "return t;" + "}"; + +#endif // EXTERNALS_EXPECTED_ diff --git a/tests/unit/externals.frag b/tests/unit/externals.frag new file mode 100644 index 00000000..1f65fbb6 --- /dev/null +++ b/tests/unit/externals.frag @@ -0,0 +1,16 @@ +uniform int i, n, e; + +int f() { + int var1, var2, var3, var4, var5; + return i; +} + +int g() { + int var1, var2, var3, var4, var5; + return n; +} + +int h() { + int var1, var2, var3, var4, var5; + return e; +} diff --git a/tests/unit/externals.preserved.expected b/tests/unit/externals.preserved.expected new file mode 100644 index 00000000..9b2e9ab4 --- /dev/null +++ b/tests/unit/externals.preserved.expected @@ -0,0 +1,25 @@ +/* File generated with Shader Minifier 1.1.6 + * http://www.ctrl-alt-test.fr + */ +#ifndef EXTERNALS_PRESERVED_EXPECTED_ +# define EXTERNALS_PRESERVED_EXPECTED_ + +const char *externals_frag = + "uniform int i,n,e;" + "int t()" + "{" + "int t,r,u,o,m;" + "return i;" + "}" + "int r()" + "{" + "int t,r,u,o,m;" + "return n;" + "}" + "int u()" + "{" + "int t,r,u,o,m;" + "return e;" + "}"; + +#endif // EXTERNALS_PRESERVED_EXPECTED_ diff --git a/tests/unit/function_overload.expected b/tests/unit/function_overload.expected new file mode 100644 index 00000000..17f5ee56 --- /dev/null +++ b/tests/unit/function_overload.expected @@ -0,0 +1,41 @@ +/* File generated with Shader Minifier 1.1.6 + * http://www.ctrl-alt-test.fr + */ +#ifndef FUNCTION_OVERLOAD_EXPECTED_ +# define FUNCTION_OVERLOAD_EXPECTED_ + +const char *function_overload_frag = + "int t()" + "{" + "return 0;" + "}" + "int t(int n)" + "{" + "return t()+n;" + "}" + "int t(int i,int n)" + "{" + "return t(i)+n;" + "}" + "int t(int i,int n,int r)" + "{" + "return t(i,n)+r;" + "}" + "int t(int i,int n,int r,int u)" + "{" + "return t(i,n,r)+u;" + "}" + "int n(int n)" + "{" + "return n;" + "}" + "int n(int i,int r)" + "{" + "return n(i)*r;" + "}" + "int n(int i,int r,int u)" + "{" + "return n(i,r)*u;" + "}"; + +#endif // FUNCTION_OVERLOAD_EXPECTED_ diff --git a/tests/unit/function_overload.frag b/tests/unit/function_overload.frag new file mode 100644 index 00000000..cd0d5e0f --- /dev/null +++ b/tests/unit/function_overload.frag @@ -0,0 +1,15 @@ +int sum0() { return 0; } + +int sum1(int x) { return sum0() + x; } + +int sum2(int x, int y) { return sum1(x) + y; } + +int sum3(int x, int y, int z) { return sum2(x, y) + z; } + +int sum4(int x, int y, int z, int w) { return sum3(x, y, z) + w; } + +int mult1(int x) { return x; } + +int mult2(int x, int y) { return mult1(x) * y; } + +int mult3(int x, int y, int z) { return mult2(x, y) * z; } From e44be0816230ddb32db20295439e87512915d39b Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Thu, 3 Jun 2021 17:36:20 +0200 Subject: [PATCH 2/2] Allow shadowing of external values Handle external values (e.g. uniforms) like other variables. They can be shadowed. This is more consistent and allows us to simplify the code. --- src/renamer.fs | 26 ++++++++++++------------- tests/unit/externals.preserved.expected | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/renamer.fs b/src/renamer.fs index 131dffc0..dc0b3809 100644 --- a/src/renamer.fs +++ b/src/renamer.fs @@ -144,6 +144,15 @@ module private RenamerImpl = let l = env.reusableNames |> List.filter (fun x -> x.[0] <> newName.[0]) {env with varRenames = Map.add id newName env.varRenames; reusableNames = l}, newName + let dontRename env name = + let names = env.reusableNames |> List.filter ((<>) name) + {env with varRenames = env.varRenames.Add(name, name); reusableNames = names} + + let dontRenameList env names = + let mutable env = env + for name in names do env <- dontRename env name + env + let renFunction env nbArgs id = if List.exists ((=) id) options.noRenamingList then env, id // don't rename "main" else @@ -200,7 +209,7 @@ module private RenamerImpl = | None -> false if isTopLevel && (ext || options.hlsl || options.preserveAllGlobals) then if options.preserveExternals then - {env with reusableNames = List.filter ((<>)decl.name) env.reusableNames}, decl.name + dontRename env decl.name, decl.name else let env, newName = env.newName env decl.name Formatter.export "" decl.name newName // TODO: first argument seems now useless @@ -231,9 +240,9 @@ module private RenamerImpl = | e -> e mapStmt (mapEnv collect id) block |> ignore let set = HashSet(Seq.choose env.varRenames.TryFind d) - let varRenames, availableNames = Map.partition (fun _ id -> set.Contains id) env.varRenames + let varRenames, availableNames = env.varRenames |> Map.partition (fun _ id -> id.Length > 2 || set.Contains id) let availableNames = availableNames |> Seq.filter (fun x -> not (List.exists ((=) x.Value) options.noRenamingList)) - let merged = [for i in availableNames -> i.Value] @ env.reusableNames |> Seq.distinct |> Seq.toList // |> List.sort + let merged = [for i in availableNames -> i.Value] @ env.reusableNames |> Seq.distinct |> Seq.toList {env with varRenames=varRenames; reusableNames=merged} let rec renStmt env = @@ -286,18 +295,9 @@ module private RenamerImpl = Function({fct with args=args}, body) | e -> e - // Remove the values from the env - // so that the functions are not overloaded - let rec doNotOverload env = function - | [] -> env - | name::li -> - let names = env.reusableNames |> List.filter ((<>) name) - let env = {env with varRenames = Map.add name name env.varRenames; reusableNames = names} - doNotOverload env li - let renameTopLevel li env = // Rename top-level values first - let env = doNotOverload env options.noRenamingList + let env = dontRenameList env options.noRenamingList let env, li = renList env renTopLevelName li // Then, rename local values diff --git a/tests/unit/externals.preserved.expected b/tests/unit/externals.preserved.expected index 9b2e9ab4..bf5caf61 100644 --- a/tests/unit/externals.preserved.expected +++ b/tests/unit/externals.preserved.expected @@ -8,17 +8,17 @@ const char *externals_frag = "uniform int i,n,e;" "int t()" "{" - "int t,r,u,o,m;" + "int n,e,t,r,u;" "return i;" "}" "int r()" "{" - "int t,r,u,o,m;" + "int i,e,t,r,u;" "return n;" "}" "int u()" "{" - "int t,r,u,o,m;" + "int i,t,r,n,u;" "return e;" "}";