Skip to content

Commit

Permalink
Reducer: Add --no-ub-guards argument (#1168)
Browse files Browse the repository at this point in the history
For integration with GLSLsmith, we don't want glsl-reduce to add
undefined behaviour guards (because GLSLsmith will add them).
  • Loading branch information
afd authored Nov 17, 2021
1 parent 1bd63c3 commit d7688f3
Show file tree
Hide file tree
Showing 35 changed files with 281 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public String doReduction(
boolean makeArrayAccessesInBounds = false;
boolean addInitializers = false;

if (context.reduceEverywhere()) {
if (context.addUbGuards() && context.reduceEverywhere()) {
LOGGER.info("We are not preserving semantics, so see whether adding guards against "
+ "undefined behaviour preserves interestingness.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ public class ReducerContext {
private static final int DEFAULT_AGGRESSION_DECREASE_STEP = 5;

private final boolean reduceEverywhere;
private final boolean addUbGuards;
private final ShadingLanguageVersion shadingLanguageVersion;
private final IRandom random;
private final IdGenerator idGenerator;
private final int maxPercentageToReduce;
private final int aggressionDecreaseStep;

public ReducerContext(boolean reduceEverywhere,
boolean addUbGuards,
ShadingLanguageVersion shadingLanguageVersion,
IRandom random, IdGenerator idGenerator, int maxPercentageToReduce,
int aggressionDecreaseStep) {
this.reduceEverywhere = reduceEverywhere;
this.addUbGuards = addUbGuards;
this.shadingLanguageVersion = shadingLanguageVersion;
this.random = random;
this.idGenerator = idGenerator;
Expand All @@ -45,16 +48,21 @@ public ReducerContext(boolean reduceEverywhere,
}

public ReducerContext(boolean reduceEverywhere,
boolean addUbGuards,
ShadingLanguageVersion shadingLanguageVersion,
IRandom random, IdGenerator idGenerator) {
this(reduceEverywhere, shadingLanguageVersion, random, idGenerator,
this(reduceEverywhere, addUbGuards, shadingLanguageVersion, random, idGenerator,
DEFAULT_MAX_PERCENTAGE_TO_REDUCE, DEFAULT_AGGRESSION_DECREASE_STEP);
}

public boolean reduceEverywhere() {
return reduceEverywhere;
}

public boolean addUbGuards() {
return addUbGuards;
}

public ShadingLanguageVersion getShadingLanguageVersion() {
assert shadingLanguageVersion != null;
return shadingLanguageVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ private static ArgumentParser getParser() {
+ "numbers with uniforms.")
.action(Arguments.storeTrue());

parser.addArgument("--no-ub-guards")
.help("Do not emit guards against undefined behaviour (such as loop limiters and array "
+ "bounds clamping).")
.action(Arguments.storeTrue());

return parser;

}
Expand Down Expand Up @@ -295,6 +300,7 @@ public static void mainHelper(
final IRandom random = new RandomWrapper(ArgsUtil.getSeedArgument(ns));
final String errorString = ns.get("error_string");
final boolean reduceEverywhere = !ns.getBoolean("preserve_semantics");
final boolean addUbGuards = !ns.getBoolean("no_ub_guards");
final boolean stopOnError = ns.get("stop_on_error");

final String server = ns.get("server");
Expand Down Expand Up @@ -465,6 +471,7 @@ public static void mainHelper(
workDir,
maxSteps,
reduceEverywhere,
addUbGuards,
continuePreviousReduction,
literalsToUniforms,
verbose,
Expand Down Expand Up @@ -492,6 +499,7 @@ public static void doReductionHelper(
File workDir,
int stepLimit,
boolean reduceEverywhere,
boolean addUbGuards,
boolean continuePreviousReduction,
boolean literalsToUniforms,
boolean verbose,
Expand Down Expand Up @@ -526,6 +534,7 @@ public static void doReductionHelper(
new ReductionDriver(
new ReducerContext(
reduceEverywhere,
addUbGuards,
shadingLanguageVersion,
random,
idGenerator),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ public boolean isInteresting(

List<IReductionOpportunity> ops = ReductionOpportunities.getReductionOpportunities(
MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false, version, generator, new IdGenerator()),
new ReducerContext(false, true, version, generator, new IdGenerator()),
fileOps);
assertEquals(3, ops.size());

new ReductionDriver(
new ReducerContext(
false,
true,
version,
generator,
new IdGenerator()),
Expand Down Expand Up @@ -230,7 +231,7 @@ private String reduce(IFileJudge judge,
Optional.empty(), new PipelineInfo(tempJsonFile),
translationUnits);

return new ReductionDriver(new ReducerContext(reduceEverywhere, version,
return new ReductionDriver(new ReducerContext(reduceEverywhere, true, version,
generator, new IdGenerator()), false, fileOps,
judge, testFolder.getRoot())
.doReduction(state, getPrefix(tempFragmentShaderFile), 0, stepLimit);
Expand Down Expand Up @@ -283,7 +284,7 @@ public void testInitializersAreInlined() throws Exception {
};

final String reducedFilesPrefix = new ReductionDriver(
new ReducerContext(false, version, generator, new IdGenerator()),
new ReducerContext(false, true, version, generator, new IdGenerator()),
false, fileOps,
referencesSinCosAnd3, testFolder.getRoot())
.doReduction(state, getPrefix(tempFile), 0, -1);
Expand Down Expand Up @@ -548,6 +549,7 @@ public void testReductionWithUniformBindings() throws Exception {
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_300,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -602,6 +604,7 @@ public void testReductionWithPushConstantBinding() throws Exception {
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(false,
true,
ShadingLanguageVersion.ESSL_300,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -636,6 +639,7 @@ public void testReductionOfUnreferencedUniform() throws Exception {
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -693,6 +697,7 @@ public void visitFunctionCallExpr(FunctionCallExpr functionCallExpr) {
fileOps);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -767,6 +772,7 @@ public void visitReturnStmt(ReturnStmt returnStmt) {
fileOps);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -840,6 +846,7 @@ public void visitBinaryExpr(BinaryExpr binaryExpr) {
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -894,6 +901,7 @@ public void visitIntConstantExpr(IntConstantExpr intConstantExpr) {
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -951,6 +959,7 @@ public boolean isInteresting(File shaderJobFile, File shaderResultFileOutput)
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

new ReductionDriver(new ReducerContext(true,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down Expand Up @@ -1040,6 +1049,7 @@ public boolean isInteresting(
fileOps.writeShaderJobFile(shaderJob, tempShaderJobFile);

final String resultsPrefix = new ReductionDriver(new ReducerContext(false,
true,
ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void noEffectInRegularCode() throws Exception {
final TranslationUnit tu = ParseHelper.parse(original);
final List<SimplifyExprReductionOpportunity> ops = CompoundExprToSubExprReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false, ShadingLanguageVersion.GLSL_440,
new ReducerContext(false, true, ShadingLanguageVersion.GLSL_440,
new RandomWrapper(0), new IdGenerator()));
assertTrue(ops.isEmpty());
}
Expand Down Expand Up @@ -170,7 +170,7 @@ private List<SimplifyExprReductionOpportunity> getOps(TranslationUnit tu,
boolean reduceEverywhere) {
return CompoundExprToSubExprReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(reduceEverywhere, ShadingLanguageVersion.GLSL_440,
new ReducerContext(reduceEverywhere, true, ShadingLanguageVersion.GLSL_440,
new RandomWrapper(0), new IdGenerator()));
}

Expand All @@ -186,7 +186,7 @@ public void testSwitch() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<SimplifyExprReductionOpportunity> ops = CompoundExprToSubExprReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0),
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0),
new IdGenerator()));
assertEquals(0, ops.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void checkForVariableInScope() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false,
ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
// There should be no opportunities as there is already a variable called 'dist' in scope
assertEquals(0, ops.size());
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public void checkForVariableInScope2() throws Exception {
final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false,
ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
// There should be one opportunity as variable dist is in a different scope and not used in this
// scope.
assertEquals(1, ops.size());
Expand All @@ -99,7 +99,7 @@ public void checkForVariableInScope3() throws Exception {
final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false,
ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
// There should be no opportunities as there is already a variable called 'dist' in scope,
// and it is used.
assertEquals(0, ops.size());
Expand Down Expand Up @@ -136,7 +136,7 @@ public void misc() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<DestructifyReductionOpportunity> ops = DestructifyReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false,
ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
// There should be no opportunities as there is already a variable called 'dist' in scope,
// and it is used.
assertEquals(1, ops.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testOut() throws Exception {
TranslationUnit tu = ParseHelper.parse(prog);
List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_100,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0), new IdGenerator()));
for (SimplifyExprReductionOpportunity op : ops) {
op.applyReduction();
Expand All @@ -51,7 +51,7 @@ public void testInOut() throws Exception {
TranslationUnit tu = ParseHelper.parse(prog);
List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_100,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0), new IdGenerator()));
for (SimplifyExprReductionOpportunity op : ops) {
op.applyReduction();
Expand All @@ -67,7 +67,7 @@ public void testIn() throws Exception {
TranslationUnit tu = ParseHelper.parse(prog);
List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_100,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0), new IdGenerator()));
for (SimplifyExprReductionOpportunity op : ops) {
op.applyReduction();
Expand All @@ -82,7 +82,7 @@ public void testSingleLiveVariable() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false, ShadingLanguageVersion.ESSL_100,
new ReducerContext(false, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0),
new IdGenerator()));
assertEquals(1, ops.size());
Expand All @@ -102,7 +102,7 @@ public void testSwitch() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_310,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()));
assertEquals(0, ops.size());
Expand All @@ -124,7 +124,7 @@ public void testSwitchMultipleCases() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_310,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_310,
new RandomWrapper(0),
new IdGenerator()));
assertEquals(0, ops.size());
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testSimplifyLiterals() throws Exception {
final TranslationUnit tu = ParseHelper.parse(program);
final List<SimplifyExprReductionOpportunity> ops = ExprToConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(true, ShadingLanguageVersion.ESSL_100,
new ReducerContext(true, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0),
new IdGenerator()));
for (SimplifyExprReductionOpportunity op : ops) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ private List<AbstractReductionOpportunity> getOps(TranslationUnit tu,
boolean reduceEverywhere) {
return FlattenControlFlowReductionOpportunities.findOpportunities(
MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(reduceEverywhere,
new ReducerContext(reduceEverywhere, true,
tu.getShadingLanguageVersion(), new RandomWrapper(0),
new IdGenerator()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ private void check(String before, int numOps, String after) throws IOException,
final TranslationUnit tu = ParseHelper.parse(before);
final List<SimplifyExprReductionOpportunity> ops = FoldConstantReductionOpportunities
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true,
ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator()));
ops.forEach(item -> item.applyReduction());
CompareAsts.assertEqualAsts(after, tu);
assertEquals(numOps, ops.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testRemovable() throws Exception {
TranslationUnit tu = ParseHelper.parse(program);
assertEquals(1, FunctionReductionOpportunities.findOpportunities(
MakeShaderJobFromFragmentShader.make(tu),
new ReducerContext(false, ShadingLanguageVersion.ESSL_100,
new ReducerContext(false, true, ShadingLanguageVersion.ESSL_100,
new RandomWrapper(0), new IdGenerator())).size());
}

Expand Down
Loading

0 comments on commit d7688f3

Please sign in to comment.