Skip to content

Commit 9299cd7

Browse files
committed
Fix: Over-optimistic moving of variable declaration to first usage (when usage is on the rhs of a '+=').
1 parent 78216f8 commit 9299cd7

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/JoinVariableDeclarationsWithAssignmentsExtension.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,14 @@ public static void JoinVariableDeclarationsWithAssignments(this SyntaxNode rootN
146146

147147
// Or separated only by declarations all assigned in the single next statement?
148148
// (Only possible if the next statement is a multi-param function with out params.)
149-
if (nearestUseNode is FunctionCallSyntaxNode &&
150-
declWithNoDefs.NextSiblings.TakeWhile(o => o != nearestUseNode).All(o => o is VariableDeclarationSyntaxNode))
151-
break;
149+
if (nearestUseNode is FunctionCallSyntaxNode)
150+
{
151+
var nodes = declWithNoDefs.NextSiblings.TakeWhile(o => o != nearestUseNode).ToList();
152+
if (nodes.All(o => o is VariableDeclarationSyntaxNode))
153+
break;
154+
if (nodes.Any(o => o.Token is SymbolOperatorToken))
155+
break;
156+
}
152157

153158
// Move declaration to before definition.
154159
declWithNoDefs.Remove();
@@ -158,7 +163,7 @@ public static void JoinVariableDeclarationsWithAssignments(this SyntaxNode rootN
158163
return true;
159164
});
160165

161-
// Declare variables when first assigned, if it would reduce code size.
166+
// Declare variables when first assigned if it would reduce code size.
162167
// (I.e. If Variable name is longer than the type name)
163168
foreach (var functionNode in rootNode.FunctionDefinitions())
164169
{

ShaderShrinker/UnitTests/ShrinkerTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ public void CheckJoiningDeclarationsAndDefinitionsWhenUsingVectorAccess()
344344
Assert.That(rootNode.ToCode().ToSimple(), Is.EqualTo(Code));
345345
}
346346

347+
[Test]
348+
public void CheckDeclarationIsNotJoinedWithDefinitionFromFunctionCallOutParam()
349+
{
350+
var lexer = new Lexer();
351+
lexer.Load("void a(out vec3 v) { v = vec3(1); } void f() { int a; vec3 b = vec3(0); b += g(a); }");
352+
353+
var options = CustomOptions.None();
354+
options.JoinVariableDeclarationsWithAssignments = true;
355+
var rootNode = new Parser(lexer)
356+
.Parse()
357+
.Simplify(options);
358+
359+
Assert.That(rootNode.ToCode().ToSimple(), Is.EqualTo("void a(out vec3 v) { v = vec3(1); } void f() { int a; vec3 b = vec3(0); b += g(a); }"));
360+
}
361+
347362
[Test]
348363
public void CheckDeclarationIsNotMovedIfAssignedInBracedCodeBranch()
349364
{
@@ -417,8 +432,8 @@ public void CheckDeclarationIsNotJoinedWithDefinitionIfDeepWithinIfPragma()
417432
.Simplify(options);
418433

419434
Assert.That(rootNode.ToCode().ToSimple(), Is.EqualTo("void f() { #ifdef AA { vec2 v = vec2(1); #else vec2 v = vec2(2); #endif #ifdef AA } #endif }"));
420-
}
421-
435+
}
436+
422437
[Test, Sequential]
423438
public void CheckSimplifyingFloats(
424439
[Values("10.0", "1.1", "0.10", "0.0000", "-0.09", "100.0", "100.1", "1100000.", "1.23f", "-0.1f", ".0f", "0.f", "10.00F", "102.", "001.1", "3.141592653589793238462643383279502884197", "-3.141592653589793238462643383279502884197", "1.541182543454656e-4", "1e10", "0011.0", "-0011.0", "00.0", "0.00001", "0.00062")] string code,

0 commit comments

Comments
 (0)