Skip to content

Commit

Permalink
Support for swizzles in the reducer (#1186)
Browse files Browse the repository at this point in the history
Adds support for simplifying, shortening and removing swizzles.

Part of #1179.
  • Loading branch information
afd authored Mar 10, 2022
1 parent 3fc799b commit aa32d4c
Show file tree
Hide file tree
Showing 19 changed files with 1,038 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,9 @@ public boolean supportedShaderMemoryControlFunctions() {
public boolean supportedPushConstants() {
return prototype.supportedPushConstants();
}

@Override
public boolean supportedScalarSwizzle() {
return prototype.supportedScalarSwizzle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,9 @@ public boolean supportedShaderMemoryControlFunctions() {
public boolean supportedPushConstants() {
return false;
}

@Override
public boolean supportedScalarSwizzle() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,9 @@ public boolean supportedShaderMemoryControlFunctions() {
public boolean supportedPushConstants() {
return false;
}

@Override
public boolean supportedScalarSwizzle() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ public boolean supportedUnpackSnorm2x16() {
public boolean supportedGlFragColor() {
return false;
}

@Override
public boolean supportedScalarSwizzle() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,10 @@ static ShadingLanguageVersion webGlFromVersionString(String versionString) {
* @return true if and only if push constants are supported.
*/
boolean supportedPushConstants();

/**
* GLSL versions 4.2+ support scalar swizzles, such as v.x.x
* @return true if and only if scalar swizzles are supported.
*/
boolean supportedScalarSwizzle();
}
50 changes: 50 additions & 0 deletions ast/src/test/java/com/graphicsfuzz/common/typing/TyperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,56 @@ public void visitBinaryExpr(BinaryExpr binaryExpr) {
}.visit(tu);
}

@Test
public void testScalarSwizzleTyped() throws Exception {
final TranslationUnit tu = ParseHelper.parse("#version 420\n"
+ "void main() {\n"
+ " float f;\n"
+ " vec3 fv;\n"
+ " int i;\n"
+ " ivec3 iv;\n"
+ " uint u;\n"
+ " uvec3 uv;\n"
+ " bool b;\n"
+ " bvec3 bv;\n"
+ " f.x;\n"
+ " f.r;\n"
+ " f.s;\n"
+ " fv.x.x;\n"
+ " fv.g.g;\n"
+ " fv.t.t;\n"
+ " i.x;\n"
+ " i.r;\n"
+ " i.s;\n"
+ " iv.x.x;\n"
+ " iv.g.g;\n"
+ " iv.t.t;\n"
+ " u.x;\n"
+ " u.r;\n"
+ " u.s;\n"
+ " uv.x.x;\n"
+ " uv.g.g;\n"
+ " uv.t.t;\n"
+ " b.x;\n"
+ " b.r;\n"
+ " b.s;\n"
+ " bv.x.x;\n"
+ " bv.g.g;\n"
+ " bv.t.t;\n"
+ "}\n");
new NullCheckTyper(tu) {
private int counter = 0;
@Override
public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) {
super.visitMemberLookupExpr(memberLookupExpr);
assertTrue(Arrays.asList("x", "r", "g", "s", "t").contains(memberLookupExpr.getMember()));
final BasicType type =
(BasicType) lookupType(memberLookupExpr.getStructure()).getWithoutQualifiers();
assertSame(type.getElementType(), lookupType(memberLookupExpr));
}
}.visit(tu);
}

private void checkComputeShaderBuiltin(String builtin, String builtinConstant, BasicType baseType,
TypeQualifier qualifier) throws IOException, ParseTimeoutException, InterruptedException,
GlslParserException {
Expand Down
2 changes: 1 addition & 1 deletion build/travis/check_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def exclude_filename(f: str):

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

for (dirpath, dirnames, filenames) in os.walk(os.curdir, topdown=True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ private static IReductionPassManager getDefaultPassManager(
IReductionOpportunityFinder.redundantUniformMetadataFinder(),
IReductionOpportunityFinder.variableDeclToExprFinder(),
IReductionOpportunityFinder.globalVariableDeclToExprFinder(),
IReductionOpportunityFinder.globalPrecisionDeclarationFinder())) {
IReductionOpportunityFinder.globalPrecisionDeclarationFinder(),
IReductionOpportunityFinder.removeSwizzleFinder(),
IReductionOpportunityFinder.shortenSwizzleFinder(),
IReductionOpportunityFinder.simplifySwizzleFinder())) {
cleanupPasses.add(new SystematicReductionPass(context,
verbose,
finder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,49 @@ public String getName() {
};
}

static IReductionOpportunityFinder<RemoveSwizzleReductionOpportunity> removeSwizzleFinder() {
return new IReductionOpportunityFinder<RemoveSwizzleReductionOpportunity>() {
@Override
public List<RemoveSwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
ReducerContext context) {
return RemoveSwizzleReductionOpportunities.findOpportunities(shaderJob, context);
}

@Override
public String getName() {
return "removeSwizzle";
}
};
}

static IReductionOpportunityFinder<SimplifySwizzleReductionOpportunity> simplifySwizzleFinder() {
return new IReductionOpportunityFinder<SimplifySwizzleReductionOpportunity>() {
@Override
public List<SimplifySwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
ReducerContext context) {
return SimplifySwizzleReductionOpportunities.findOpportunities(shaderJob, context);
}

@Override
public String getName() {
return "simplifySwizzle";
}
};
}

static IReductionOpportunityFinder<ShortenSwizzleReductionOpportunity> shortenSwizzleFinder() {
return new IReductionOpportunityFinder<ShortenSwizzleReductionOpportunity>() {
@Override
public List<ShortenSwizzleReductionOpportunity> findOpportunities(ShaderJob shaderJob,
ReducerContext context) {
return ShortenSwizzleReductionOpportunities.findOpportunities(shaderJob, context);
}

@Override
public String getName() {
return "shortenSwizzle";
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public static List<IReductionOpportunity> getReductionOpportunities(
IReductionOpportunityFinder.redundantUniformMetadataFinder(),
IReductionOpportunityFinder.variableDeclToExprFinder(),
IReductionOpportunityFinder.switchToLoopFinder(),
IReductionOpportunityFinder.globalVariableDeclToExprFinder())) {
IReductionOpportunityFinder.globalVariableDeclToExprFinder(),
IReductionOpportunityFinder.shortenSwizzleFinder(),
IReductionOpportunityFinder.removeSwizzleFinder(),
IReductionOpportunityFinder.simplifySwizzleFinder())) {
final List<? extends IReductionOpportunity> currentOpportunities = ros
.findOpportunities(shaderJob, context);
if (ReductionDriver.DEBUG_REDUCER) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2022 The GraphicsFuzz Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphicsfuzz.reducer.reductionopportunities;

import com.graphicsfuzz.common.ast.IAstNode;
import com.graphicsfuzz.common.ast.TranslationUnit;
import com.graphicsfuzz.common.ast.expr.MemberLookupExpr;
import com.graphicsfuzz.common.ast.type.BasicType;
import com.graphicsfuzz.common.transformreduce.ShaderJob;
import com.graphicsfuzz.common.typing.Typer;
import com.graphicsfuzz.common.util.ListConcat;
import java.util.Collections;
import java.util.List;

/**
* <p>This class finds opportunities to remove redundant swizzles in swizzle chains.</p>
*/
public class RemoveSwizzleReductionOpportunities
extends ReductionOpportunitiesBase<RemoveSwizzleReductionOpportunity> {

private final Typer typer;

static List<RemoveSwizzleReductionOpportunity> findOpportunities(
ShaderJob shaderJob,
ReducerContext context) {
return shaderJob.getShaders()
.stream()
.map(item -> findOpportunitiesForShader(item, context))
.reduce(Collections.emptyList(), ListConcat::concatenate);
}

private static List<RemoveSwizzleReductionOpportunity> findOpportunitiesForShader(
TranslationUnit tu,
ReducerContext context) {
RemoveSwizzleReductionOpportunities finder =
new RemoveSwizzleReductionOpportunities(tu, context);
finder.visit(tu);
return finder.getOpportunities();
}

private RemoveSwizzleReductionOpportunities(TranslationUnit tu,
ReducerContext context) {
super(tu, context);
this.typer = new Typer(tu);
}

@Override
public void visitTranslationUnit(TranslationUnit translationUnit) {

if (!context.reduceEverywhere()) {
// This class finds semantics-changing reduction opportunities, so we cannot use it unless we
// are reducing everywhere.
return;
}

super.visitTranslationUnit(translationUnit);
}

@Override
public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) {
super.visitMemberLookupExpr(memberLookupExpr);
if (!RemoveSwizzleReductionOpportunity.isSwizzle(memberLookupExpr, typer)) {
return;
}
final IAstNode parent = parentMap.getParent(memberLookupExpr);
if (typer.lookupType(memberLookupExpr) == typer.lookupType(memberLookupExpr.getStructure())
.getWithoutQualifiers()) {
addOpportunity(new RemoveSwizzleReductionOpportunity(parent,
memberLookupExpr.getStructure(), memberLookupExpr, typer, getVistitationDepth()));
return;
}
if (!RemoveSwizzleReductionOpportunity.isSwizzle(parent, typer)) {
return;
}
final BasicType basicType =
(BasicType) typer.lookupType(memberLookupExpr.getStructure()).getWithoutQualifiers();
if (basicType.getNumElements() > RemoveSwizzleReductionOpportunity
.getLargestSwizzleComponent(parent)) {
addOpportunity(new RemoveSwizzleReductionOpportunity(parent,
memberLookupExpr.getStructure(), memberLookupExpr, typer, getVistitationDepth()));
}
}
}
Loading

0 comments on commit aa32d4c

Please sign in to comment.