Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit de47649

Browse files
authored
Reducer: remove unused interface blocks (#1163)
Adds functionality to the reducer to eliminate interface blocks that are not used. This involved adding support for tracking interface blocks in the ScopeTrackingVisitor, and some minor changes to the internals of InterfaceBlock were made in the process. Fixes #1162.
1 parent 1fcfc78 commit de47649

18 files changed

Lines changed: 338 additions & 64 deletions

File tree

ast/src/main/java/com/graphicsfuzz/common/ast/decl/InterfaceBlock.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525
import java.util.Collections;
26+
import java.util.HashMap;
2627
import java.util.HashSet;
2728
import java.util.List;
29+
import java.util.Map;
2830
import java.util.Optional;
2931
import java.util.Set;
3032
import java.util.stream.Collectors;
@@ -35,7 +37,7 @@ public class InterfaceBlock extends Declaration {
3537
private final List<TypeQualifier> interfaceQualifiers;
3638
private final String structName;
3739
private final List<String> memberNames;
38-
private final List<Type> memberTypes;
40+
private final Map<String, Type> memberTypes;
3941
private final Optional<String> instanceName;
4042

4143
public InterfaceBlock(
@@ -77,13 +79,15 @@ public InterfaceBlock(
7779
this.structName = structName;
7880
this.memberNames = new ArrayList<>();
7981
this.memberNames.addAll(memberNames);
80-
this.memberTypes = new ArrayList<>();
81-
this.memberTypes.addAll(memberTypes);
82+
this.memberTypes = new HashMap<>();
83+
for (int i = 0; i < memberNames.size(); i++) {
84+
this.memberTypes.put(memberNames.get(i), memberTypes.get(i));
85+
}
8286
this.instanceName = instanceName;
8387
}
8488

8589
public List<Type> getMemberTypes() {
86-
return Collections.unmodifiableList(memberTypes);
90+
return memberNames.stream().map(memberTypes::get).collect(Collectors.toList());
8791
}
8892

8993
public List<String> getMemberNames() {
@@ -115,13 +119,8 @@ public String getInstanceName() {
115119
return instanceName.get();
116120
}
117121

118-
public Optional<Type> getMemberType(String name) {
119-
for (int i = 0; i < memberNames.size(); i++) {
120-
if (memberNames.get(i).equals(name)) {
121-
return Optional.of(memberTypes.get(i));
122-
}
123-
}
124-
return Optional.empty();
122+
public Type getMemberType(String name) {
123+
return memberTypes.get(name);
125124
}
126125

127126
public boolean isUniformBlock() {
@@ -151,7 +150,7 @@ public InterfaceBlock clone() {
151150
interfaceQualifiers,
152151
structName,
153152
memberNames,
154-
memberTypes.stream().map(item -> item.clone()).collect(Collectors.toList()),
153+
memberNames.stream().map(memberTypes::get).map(Type::clone).collect(Collectors.toList()),
155154
instanceName);
156155
}
157156

ast/src/main/java/com/graphicsfuzz/common/tool/PrettyPrinterVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ public void visitInterfaceBlock(InterfaceBlock interfaceBlock) {
774774

775775
for (String memberName : interfaceBlock.getMemberNames()) {
776776
indent();
777-
final Type memberType = interfaceBlock.getMemberType(memberName).get();
777+
final Type memberType = interfaceBlock.getMemberType(memberName);
778778
visit(memberType);
779779
out.append(" ").append(memberName);
780780
emitArrayInfoAfterType(memberType);

ast/src/main/java/com/graphicsfuzz/common/typing/Scope.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.graphicsfuzz.common.typing;
1818

19+
import com.graphicsfuzz.common.ast.decl.InterfaceBlock;
1920
import com.graphicsfuzz.common.ast.decl.ParameterDecl;
2021
import com.graphicsfuzz.common.ast.decl.VariableDeclInfo;
2122
import com.graphicsfuzz.common.ast.decl.VariablesDeclaration;
@@ -47,18 +48,28 @@ public Scope(Scope parent) {
4748
this.parent = parent;
4849
}
4950

50-
public void add(String name, Type type, Optional<ParameterDecl> parameterDecl,
51-
VariableDeclInfo declInfo,
52-
VariablesDeclaration variablesDecl) {
53-
checkNameTypeAndParam(name, type, parameterDecl);
54-
variableMapping.put(name, new ScopeEntry(type, parameterDecl, declInfo, variablesDecl));
51+
public void add(String name, Type type) {
52+
checkNameTypeAndParam(name, type, Optional.empty());
53+
variableMapping.put(name, new ScopeEntry(type));
5554
}
5655

57-
public void add(String name, Type type, Optional<ParameterDecl> parameterDecl) {
58-
checkNameTypeAndParam(name, type, parameterDecl);
56+
public void add(String name, Type type,
57+
VariableDeclInfo declInfo,
58+
VariablesDeclaration variablesDecl) {
59+
checkNameTypeAndParam(name, type, Optional.empty());
60+
variableMapping.put(name, new ScopeEntry(type, declInfo, variablesDecl));
61+
}
62+
63+
public void add(String name, Type type, ParameterDecl parameterDecl) {
64+
checkNameTypeAndParam(name, type, Optional.of(parameterDecl));
5965
variableMapping.put(name, new ScopeEntry(type, parameterDecl));
6066
}
6167

68+
public void add(String name, Type type, InterfaceBlock interfaceBlock) {
69+
checkNameTypeAndParam(name, type, Optional.empty());
70+
variableMapping.put(name, new ScopeEntry(type, interfaceBlock));
71+
}
72+
6273
public void addStructDefinition(StructDefinitionType sdt) {
6374
assert sdt.hasStructNameType();
6475
structMapping.put(sdt.getStructNameType().getName(), sdt);

ast/src/main/java/com/graphicsfuzz/common/typing/ScopeEntry.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.graphicsfuzz.common.typing;
1818

19+
import com.graphicsfuzz.common.ast.decl.InterfaceBlock;
1920
import com.graphicsfuzz.common.ast.decl.ParameterDecl;
2021
import com.graphicsfuzz.common.ast.decl.VariableDeclInfo;
2122
import com.graphicsfuzz.common.ast.decl.VariablesDeclaration;
@@ -24,10 +25,12 @@
2425

2526
public class ScopeEntry {
2627

27-
private final Optional<ParameterDecl> parameterDecl;
28-
2928
private final Type type;
3029

30+
// Represents the ParameterDecl that this variable came from, if one exists. If there is no such
31+
// object, the optional is empty.
32+
private final Optional<ParameterDecl> parameterDecl;
33+
3134
// Represents the VariableDeclInfo that this variable came from, if one exists. If there is no
3235
// such object (e.g. because the variable came from a parameter, or was made up for purposes of
3336
// fuzzing, or some such, the optional is empty.
@@ -37,26 +40,38 @@ public class ScopeEntry {
3740
// If there is no such object, the optional is empty.
3841
private final Optional<VariablesDeclaration> variablesDeclaration;
3942

43+
// Represents the interface block that this variable is part of, if one exists. If there is no
44+
// such object, the optional is empty.
45+
private final Optional<InterfaceBlock> interfaceBlock;
46+
4047
private ScopeEntry(Type type, Optional<ParameterDecl> parameterDecl,
41-
Optional<VariableDeclInfo> variableDeclInfo,
42-
Optional<VariablesDeclaration> variablesDecl) {
48+
Optional<VariableDeclInfo> variableDeclInfo,
49+
Optional<VariablesDeclaration> variablesDecl,
50+
Optional<InterfaceBlock> interfaceBlock) {
4351
this.type = type;
4452
this.parameterDecl = parameterDecl;
4553
this.variableDeclInfo = variableDeclInfo;
4654
this.variablesDeclaration = variablesDecl;
55+
this.interfaceBlock = interfaceBlock;
4756
}
4857

4958
public ScopeEntry(Type type,
50-
Optional<ParameterDecl> parameterDecl,
5159
VariableDeclInfo variableDeclInfo,
5260
VariablesDeclaration variablesDecl) {
53-
this(type, parameterDecl, Optional.of(variableDeclInfo), Optional.of(variablesDecl));
54-
assert variableDeclInfo != null;
55-
assert variablesDecl != null;
61+
this(type, Optional.empty(), Optional.of(variableDeclInfo), Optional.of(variablesDecl),
62+
Optional.empty());
63+
}
64+
65+
public ScopeEntry(Type type) {
66+
this(type, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
5667
}
5768

58-
public ScopeEntry(Type type, Optional<ParameterDecl> parameterDecl) {
59-
this(type, parameterDecl, Optional.empty(), Optional.empty());
69+
public ScopeEntry(Type type, ParameterDecl parameterDecl) {
70+
this(type, Optional.of(parameterDecl), Optional.empty(), Optional.empty(), Optional.empty());
71+
}
72+
73+
public ScopeEntry(Type type, InterfaceBlock interfaceBlock) {
74+
this(type, Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(interfaceBlock));
6075
}
6176

6277
public Type getType() {
@@ -87,4 +102,12 @@ public boolean hasParameterDecl() {
87102
return parameterDecl.isPresent();
88103
}
89104

105+
public InterfaceBlock getInterfaceBlock() {
106+
return interfaceBlock.get();
107+
}
108+
109+
public boolean hasInterfaceBlock() {
110+
return interfaceBlock.isPresent();
111+
}
112+
90113
}

ast/src/main/java/com/graphicsfuzz/common/typing/ScopeTrackingVisitor.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.graphicsfuzz.common.ast.decl.FunctionDefinition;
2020
import com.graphicsfuzz.common.ast.decl.FunctionPrototype;
21+
import com.graphicsfuzz.common.ast.decl.InterfaceBlock;
2122
import com.graphicsfuzz.common.ast.decl.ParameterDecl;
2223
import com.graphicsfuzz.common.ast.decl.VariableDeclInfo;
2324
import com.graphicsfuzz.common.ast.decl.VariablesDeclaration;
@@ -31,7 +32,6 @@
3132
import java.util.Deque;
3233
import java.util.LinkedList;
3334
import java.util.List;
34-
import java.util.Optional;
3535

3636
/**
3737
* This class extends StandardVisitor to track details of what is in scope at each point of
@@ -130,7 +130,16 @@ public void visitFunctionPrototype(FunctionPrototype functionPrototype) {
130130
}
131131
currentScope.add(p.getName(),
132132
Typer.combineBaseTypeAndArrayInfo(p.getType(), p.getArrayInfo()),
133-
Optional.of(p));
133+
p);
134+
}
135+
}
136+
137+
@Override
138+
public void visitInterfaceBlock(InterfaceBlock interfaceBlock) {
139+
assert atGlobalScope();
140+
super.visitInterfaceBlock(interfaceBlock);
141+
for (String member : interfaceBlock.getMemberNames()) {
142+
currentScope.add(member, interfaceBlock.getMemberType(member), interfaceBlock);
134143
}
135144
}
136145

@@ -155,7 +164,6 @@ public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration)
155164
currentScope.add(declInfo.getName(),
156165
Typer.combineBaseTypeAndArrayInfo(variablesDeclaration.getBaseType(),
157166
declInfo.getArrayInfo()),
158-
Optional.empty(),
159167
declInfo, variablesDeclaration);
160168
visitVariableDeclInfoAfterAddedToScope(declInfo);
161169
}

ast/src/main/java/com/graphicsfuzz/common/typing/Typer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,6 @@ public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifie
235235
types.put(variableIdentifierExpr, type);
236236
return;
237237
}
238-
for (InterfaceBlock interfaceBlock : interfaceBlocks) {
239-
final Optional<Type> memberType =
240-
interfaceBlock.getMemberType(variableIdentifierExpr.getName());
241-
if (memberType.isPresent()) {
242-
types.put(variableIdentifierExpr, memberType.get());
243-
return;
244-
}
245-
}
246-
247238
maybeGetTypeOfBuiltinVariable(variableIdentifierExpr.getName())
248239
.ifPresent(item -> types.put(variableIdentifierExpr, item));
249240
}

generator/src/main/java/com/graphicsfuzz/generator/fuzzer/FuzzingContext.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import java.util.Optional;
2827

2928
public class FuzzingContext {
3029

@@ -50,7 +49,7 @@ public FuzzingContext() {
5049
}
5150

5251
public void addGlobal(String name, Type type) {
53-
findRootScope().add(name, type, Optional.empty());
52+
findRootScope().add(name, type);
5453
}
5554

5655
private Scope findRootScope() {
@@ -63,12 +62,12 @@ private Scope findRootScope() {
6362

6463
public void addLocal(String name, Type type) {
6564
assert currentScope.hasParent();
66-
currentScope.add(name, type, Optional.empty());
65+
currentScope.add(name, type);
6766
}
6867

6968
public void addParameter(ParameterDecl parameterDecl) {
7069
assert currentScope.hasParent();
71-
currentScope.add(parameterDecl.getName(), parameterDecl.getType(), Optional.of(parameterDecl));
70+
currentScope.add(parameterDecl.getName(), parameterDecl.getType(), parameterDecl);
7271
}
7372

7473
public void addFunction(FunctionPrototype prototype) {

generator/src/main/java/com/graphicsfuzz/generator/semanticspreserving/AddSwitchMutation.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.util.ArrayList;
5050
import java.util.Collections;
5151
import java.util.List;
52-
import java.util.Optional;
5352
import java.util.stream.Collectors;
5453

5554
public class AddSwitchMutation implements Mutation {
@@ -217,7 +216,6 @@ private void addVariablesToScope(Scope scope, DeclarationStmt declarationStmt) {
217216
scope.add(declInfo.getName(),
218217
Typer.combineBaseTypeAndArrayInfo(variablesDeclaration.getBaseType(),
219218
declInfo.getArrayInfo()),
220-
Optional.empty(),
221219
declInfo, variablesDeclaration);
222220
}
223221
}

generator/src/main/java/com/graphicsfuzz/generator/transformation/DonateCodeTransformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private TranslationUnit prepareTranslationUnit(File donorFile, IRandom generator
212212
// We will declare a global variable of the member's type with a prefixed version of the
213213
// member's name. However, we need to take care regarding array members, in particular
214214
// because interface blocks can have unsized arrays.
215-
final Type memberType = interfaceBlock.getMemberType(memberName).get();
215+
final Type memberType = interfaceBlock.getMemberType(memberName);
216216

217217
// This will be the array-free base type of the new global variable.
218218
Type plainVariableBaseType;

generator/src/test/java/com/graphicsfuzz/generator/semanticspreserving/OutlineStatementMutationTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.ArrayList;
3939
import java.util.Arrays;
4040
import java.util.Collections;
41-
import java.util.Optional;
4241
import org.junit.Before;
4342
import org.junit.Test;
4443

@@ -65,9 +64,9 @@ public void apply() throws Exception {
6564
BinOp.ADD), BinOp.ASSIGN));
6665

6766
final Scope fakeScope = new Scope();
68-
fakeScope.add("x", BasicType.VEC2, Optional.empty());
69-
fakeScope.add("y", BasicType.FLOAT, Optional.empty());
70-
fakeScope.add("z", BasicType.VEC2, Optional.empty());
67+
fakeScope.add("x", BasicType.VEC2);
68+
fakeScope.add("y", BasicType.FLOAT);
69+
fakeScope.add("z", BasicType.VEC2);
7170

7271
new OutlineStatementMutation(toOutline, fakeScope, tu, fakeFunction, new IdGenerator()).apply();
7372
final String expectedDecl = "vec2 " + Constants.OUTLINED_FUNCTION_PREFIX
@@ -91,9 +90,9 @@ public void apply2() throws Exception {
9190
BinOp.ASSIGN));
9291

9392
Scope fakeScope = new Scope();
94-
fakeScope.add("x", BasicType.VEC2, Optional.empty());
93+
fakeScope.add("x", BasicType.VEC2);
9594
fakeScope.add("y", new QualifiedType(BasicType.VEC2,
96-
Collections.singletonList(TypeQualifier.UNIFORM)), Optional.empty());
95+
Collections.singletonList(TypeQualifier.UNIFORM)));
9796

9897
new OutlineStatementMutation(toOutline, fakeScope, tu, fakeFunction, new IdGenerator()).apply();
9998

@@ -119,7 +118,7 @@ public void apply3() throws Exception {
119118
BinOp.ASSIGN));
120119

121120
Scope fakeScope = new Scope();
122-
fakeScope.add("x", BasicType.VEC2, Optional.empty());
121+
fakeScope.add("x", BasicType.VEC2);
123122

124123
new OutlineStatementMutation(toOutline, fakeScope, tu, fakeFunction, new IdGenerator()).apply();
125124

@@ -146,7 +145,7 @@ public void apply4() throws Exception {
146145

147146
Scope fakeScope = new Scope();
148147
fakeScope.add("x", new QualifiedType(BasicType.VEC2,
149-
Collections.singletonList(TypeQualifier.OUT_PARAM)), Optional.empty());
148+
Collections.singletonList(TypeQualifier.OUT_PARAM)));
150149

151150
new OutlineStatementMutation(toOutline, fakeScope, tu, fakeFunction, new IdGenerator()).apply();
152151

@@ -172,7 +171,7 @@ public void apply5() throws Exception {
172171
BinOp.ASSIGN));
173172

174173
Scope fakeScope = new Scope();
175-
fakeScope.add("x", BasicType.FLOAT, Optional.empty());
174+
fakeScope.add("x", BasicType.FLOAT);
176175

177176
new OutlineStatementMutation(toOutline, fakeScope, tu, fakeFunction, new IdGenerator()).apply();
178177

0 commit comments

Comments
 (0)