Skip to content

Commit aa32d4c

Browse files
authored
Support for swizzles in the reducer (#1186)
Adds support for simplifying, shortening and removing swizzles. Part of #1179.
1 parent 3fc799b commit aa32d4c

19 files changed

+1038
-3
lines changed

ast/src/main/java/com/graphicsfuzz/common/glslversion/CompositeShadingLanguageVersion.java

+5
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,9 @@ public boolean supportedShaderMemoryControlFunctions() {
337337
public boolean supportedPushConstants() {
338338
return prototype.supportedPushConstants();
339339
}
340+
341+
@Override
342+
public boolean supportedScalarSwizzle() {
343+
return prototype.supportedScalarSwizzle();
344+
}
340345
}

ast/src/main/java/com/graphicsfuzz/common/glslversion/Essl100.java

+5
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,9 @@ public boolean supportedShaderMemoryControlFunctions() {
338338
public boolean supportedPushConstants() {
339339
return false;
340340
}
341+
342+
@Override
343+
public boolean supportedScalarSwizzle() {
344+
return false;
345+
}
341346
}

ast/src/main/java/com/graphicsfuzz/common/glslversion/Glsl110.java

+5
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,9 @@ public boolean supportedShaderMemoryControlFunctions() {
341341
public boolean supportedPushConstants() {
342342
return false;
343343
}
344+
345+
@Override
346+
public boolean supportedScalarSwizzle() {
347+
return false;
348+
}
344349
}

ast/src/main/java/com/graphicsfuzz/common/glslversion/Glsl420.java

+6
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,10 @@ public boolean supportedUnpackSnorm2x16() {
5959
public boolean supportedGlFragColor() {
6060
return false;
6161
}
62+
63+
@Override
64+
public boolean supportedScalarSwizzle() {
65+
return true;
66+
}
67+
6268
}

ast/src/main/java/com/graphicsfuzz/common/glslversion/ShadingLanguageVersion.java

+6
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,10 @@ static ShadingLanguageVersion webGlFromVersionString(String versionString) {
374374
* @return true if and only if push constants are supported.
375375
*/
376376
boolean supportedPushConstants();
377+
378+
/**
379+
* GLSL versions 4.2+ support scalar swizzles, such as v.x.x
380+
* @return true if and only if scalar swizzles are supported.
381+
*/
382+
boolean supportedScalarSwizzle();
377383
}

ast/src/test/java/com/graphicsfuzz/common/typing/TyperTest.java

+50
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,56 @@ public void visitBinaryExpr(BinaryExpr binaryExpr) {
10001000
}.visit(tu);
10011001
}
10021002

1003+
@Test
1004+
public void testScalarSwizzleTyped() throws Exception {
1005+
final TranslationUnit tu = ParseHelper.parse("#version 420\n"
1006+
+ "void main() {\n"
1007+
+ " float f;\n"
1008+
+ " vec3 fv;\n"
1009+
+ " int i;\n"
1010+
+ " ivec3 iv;\n"
1011+
+ " uint u;\n"
1012+
+ " uvec3 uv;\n"
1013+
+ " bool b;\n"
1014+
+ " bvec3 bv;\n"
1015+
+ " f.x;\n"
1016+
+ " f.r;\n"
1017+
+ " f.s;\n"
1018+
+ " fv.x.x;\n"
1019+
+ " fv.g.g;\n"
1020+
+ " fv.t.t;\n"
1021+
+ " i.x;\n"
1022+
+ " i.r;\n"
1023+
+ " i.s;\n"
1024+
+ " iv.x.x;\n"
1025+
+ " iv.g.g;\n"
1026+
+ " iv.t.t;\n"
1027+
+ " u.x;\n"
1028+
+ " u.r;\n"
1029+
+ " u.s;\n"
1030+
+ " uv.x.x;\n"
1031+
+ " uv.g.g;\n"
1032+
+ " uv.t.t;\n"
1033+
+ " b.x;\n"
1034+
+ " b.r;\n"
1035+
+ " b.s;\n"
1036+
+ " bv.x.x;\n"
1037+
+ " bv.g.g;\n"
1038+
+ " bv.t.t;\n"
1039+
+ "}\n");
1040+
new NullCheckTyper(tu) {
1041+
private int counter = 0;
1042+
@Override
1043+
public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) {
1044+
super.visitMemberLookupExpr(memberLookupExpr);
1045+
assertTrue(Arrays.asList("x", "r", "g", "s", "t").contains(memberLookupExpr.getMember()));
1046+
final BasicType type =
1047+
(BasicType) lookupType(memberLookupExpr.getStructure()).getWithoutQualifiers();
1048+
assertSame(type.getElementType(), lookupType(memberLookupExpr));
1049+
}
1050+
}.visit(tu);
1051+
}
1052+
10031053
private void checkComputeShaderBuiltin(String builtin, String builtinConstant, BasicType baseType,
10041054
TypeQualifier qualifier) throws IOException, ParseTimeoutException, InterruptedException,
10051055
GlslParserException {

build/travis/check_headers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def exclude_filename(f: str):
138138

139139
def go():
140140
fail = False
141-
copyright_pattern = re.compile(r"Copyright 20(18|19|20|21) The GraphicsFuzz Project Authors")
141+
copyright_pattern = re.compile(r"Copyright 20(18|19|20|21|22) The GraphicsFuzz Project Authors")
142142
generated_pattern = re.compile(r"(g|G)enerated")
143143

144144
for (dirpath, dirnames, filenames) in os.walk(os.curdir, topdown=True):

reducer/src/main/java/com/graphicsfuzz/reducer/ReductionDriver.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ private static IReductionPassManager getDefaultPassManager(
126126
IReductionOpportunityFinder.redundantUniformMetadataFinder(),
127127
IReductionOpportunityFinder.variableDeclToExprFinder(),
128128
IReductionOpportunityFinder.globalVariableDeclToExprFinder(),
129-
IReductionOpportunityFinder.globalPrecisionDeclarationFinder())) {
129+
IReductionOpportunityFinder.globalPrecisionDeclarationFinder(),
130+
IReductionOpportunityFinder.removeSwizzleFinder(),
131+
IReductionOpportunityFinder.shortenSwizzleFinder(),
132+
IReductionOpportunityFinder.simplifySwizzleFinder())) {
130133
cleanupPasses.add(new SystematicReductionPass(context,
131134
verbose,
132135
finder));

reducer/src/main/java/com/graphicsfuzz/reducer/reductionopportunities/IReductionOpportunityFinder.java

+45
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,49 @@ public String getName() {
593593
};
594594
}
595595

596+
static IReductionOpportunityFinder<RemoveSwizzleReductionOpportunity> removeSwizzleFinder() {
597+
return new IReductionOpportunityFinder<RemoveSwizzleReductionOpportunity>() {
598+
@Override
599+
public List<RemoveSwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
600+
ReducerContext context) {
601+
return RemoveSwizzleReductionOpportunities.findOpportunities(shaderJob, context);
602+
}
603+
604+
@Override
605+
public String getName() {
606+
return "removeSwizzle";
607+
}
608+
};
609+
}
610+
611+
static IReductionOpportunityFinder<SimplifySwizzleReductionOpportunity> simplifySwizzleFinder() {
612+
return new IReductionOpportunityFinder<SimplifySwizzleReductionOpportunity>() {
613+
@Override
614+
public List<SimplifySwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
615+
ReducerContext context) {
616+
return SimplifySwizzleReductionOpportunities.findOpportunities(shaderJob, context);
617+
}
618+
619+
@Override
620+
public String getName() {
621+
return "simplifySwizzle";
622+
}
623+
};
624+
}
625+
626+
static IReductionOpportunityFinder<ShortenSwizzleReductionOpportunity> shortenSwizzleFinder() {
627+
return new IReductionOpportunityFinder<ShortenSwizzleReductionOpportunity>() {
628+
@Override
629+
public List<ShortenSwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
630+
ReducerContext context) {
631+
return ShortenSwizzleReductionOpportunities.findOpportunities(shaderJob, context);
632+
}
633+
634+
@Override
635+
public String getName() {
636+
return "shortenSwizzle";
637+
}
638+
};
639+
}
640+
596641
}

reducer/src/main/java/com/graphicsfuzz/reducer/reductionopportunities/ReductionOpportunities.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public static List<IReductionOpportunity> getReductionOpportunities(
7171
IReductionOpportunityFinder.redundantUniformMetadataFinder(),
7272
IReductionOpportunityFinder.variableDeclToExprFinder(),
7373
IReductionOpportunityFinder.switchToLoopFinder(),
74-
IReductionOpportunityFinder.globalVariableDeclToExprFinder())) {
74+
IReductionOpportunityFinder.globalVariableDeclToExprFinder(),
75+
IReductionOpportunityFinder.shortenSwizzleFinder(),
76+
IReductionOpportunityFinder.removeSwizzleFinder(),
77+
IReductionOpportunityFinder.simplifySwizzleFinder())) {
7578
final List<? extends IReductionOpportunity> currentOpportunities = ros
7679
.findOpportunities(shaderJob, context);
7780
if (ReductionDriver.DEBUG_REDUCER) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2022 The GraphicsFuzz Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.graphicsfuzz.reducer.reductionopportunities;
18+
19+
import com.graphicsfuzz.common.ast.IAstNode;
20+
import com.graphicsfuzz.common.ast.TranslationUnit;
21+
import com.graphicsfuzz.common.ast.expr.MemberLookupExpr;
22+
import com.graphicsfuzz.common.ast.type.BasicType;
23+
import com.graphicsfuzz.common.transformreduce.ShaderJob;
24+
import com.graphicsfuzz.common.typing.Typer;
25+
import com.graphicsfuzz.common.util.ListConcat;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
/**
30+
* <p>This class finds opportunities to remove redundant swizzles in swizzle chains.</p>
31+
*/
32+
public class RemoveSwizzleReductionOpportunities
33+
extends ReductionOpportunitiesBase<RemoveSwizzleReductionOpportunity> {
34+
35+
private final Typer typer;
36+
37+
static List<RemoveSwizzleReductionOpportunity> findOpportunities(
38+
ShaderJob shaderJob,
39+
ReducerContext context) {
40+
return shaderJob.getShaders()
41+
.stream()
42+
.map(item -> findOpportunitiesForShader(item, context))
43+
.reduce(Collections.emptyList(), ListConcat::concatenate);
44+
}
45+
46+
private static List<RemoveSwizzleReductionOpportunity> findOpportunitiesForShader(
47+
TranslationUnit tu,
48+
ReducerContext context) {
49+
RemoveSwizzleReductionOpportunities finder =
50+
new RemoveSwizzleReductionOpportunities(tu, context);
51+
finder.visit(tu);
52+
return finder.getOpportunities();
53+
}
54+
55+
private RemoveSwizzleReductionOpportunities(TranslationUnit tu,
56+
ReducerContext context) {
57+
super(tu, context);
58+
this.typer = new Typer(tu);
59+
}
60+
61+
@Override
62+
public void visitTranslationUnit(TranslationUnit translationUnit) {
63+
64+
if (!context.reduceEverywhere()) {
65+
// This class finds semantics-changing reduction opportunities, so we cannot use it unless we
66+
// are reducing everywhere.
67+
return;
68+
}
69+
70+
super.visitTranslationUnit(translationUnit);
71+
}
72+
73+
@Override
74+
public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) {
75+
super.visitMemberLookupExpr(memberLookupExpr);
76+
if (!RemoveSwizzleReductionOpportunity.isSwizzle(memberLookupExpr, typer)) {
77+
return;
78+
}
79+
final IAstNode parent = parentMap.getParent(memberLookupExpr);
80+
if (typer.lookupType(memberLookupExpr) == typer.lookupType(memberLookupExpr.getStructure())
81+
.getWithoutQualifiers()) {
82+
addOpportunity(new RemoveSwizzleReductionOpportunity(parent,
83+
memberLookupExpr.getStructure(), memberLookupExpr, typer, getVistitationDepth()));
84+
return;
85+
}
86+
if (!RemoveSwizzleReductionOpportunity.isSwizzle(parent, typer)) {
87+
return;
88+
}
89+
final BasicType basicType =
90+
(BasicType) typer.lookupType(memberLookupExpr.getStructure()).getWithoutQualifiers();
91+
if (basicType.getNumElements() > RemoveSwizzleReductionOpportunity
92+
.getLargestSwizzleComponent(parent)) {
93+
addOpportunity(new RemoveSwizzleReductionOpportunity(parent,
94+
memberLookupExpr.getStructure(), memberLookupExpr, typer, getVistitationDepth()));
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)