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

Commit 79aa124

Browse files
authored
Update of the typer with shifts and assignments (#1154)
Updates the Typer class to produce a result in the case of assignments and the TyperHelper class to correctly type shift operations in the case of left and right scalar types (the resulting type is the one from the left operand). Adds a test for the typer with left and right shift operators. Ensures that the typer produce the correct type for shift operations on vectors and rewrite comments accordingly.
1 parent 067af84 commit 79aa124

3 files changed

Lines changed: 131 additions & 14 deletions

File tree

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,13 @@ public void visitBinaryExpr(BinaryExpr binaryExpr) {
402402
types.put(binaryExpr, rhsType);
403403
return;
404404
case BAND_ASSIGN:
405-
break;
406405
case BOR_ASSIGN:
407-
break;
408406
case BXOR_ASSIGN:
409-
break;
410407
case MOD_ASSIGN:
411-
break;
412408
case SHL_ASSIGN:
413-
break;
414409
case SHR_ASSIGN:
415-
break;
410+
types.put(binaryExpr, lhsType);
411+
return;
416412
default:
417413
break;
418414

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,23 @@ public static Type resolveTypeOfCommonBinary(Type lhsType, Type rhsType) {
5858
return lhsType;
5959
}
6060

61-
// If one side is scalar and the other side is basic, the result has to be that of the other
62-
// side
63-
if (lhsType == BasicType.FLOAT || lhsType == BasicType.INT || lhsType == BasicType.UINT) {
64-
assert rhsType instanceof BasicType;
65-
return rhsType;
66-
}
61+
// If one side is scalar, the other side is either a scalar of different type or a non-scalar
62+
// basic type:
63+
// - If the two operands are scalar of different types, the operation is a shift and
64+
// the result type is the type of the left operand
65+
// - If one operand is scalar and the other is basic, the result type is the one of the basic
66+
// type
6767
if (rhsType == BasicType.FLOAT || rhsType == BasicType.INT || rhsType == BasicType.UINT) {
6868
assert lhsType instanceof BasicType;
6969
return lhsType;
7070
}
71+
if (lhsType == BasicType.FLOAT || lhsType == BasicType.INT || lhsType == BasicType.UINT) {
72+
assert rhsType instanceof BasicType;
73+
return rhsType;
74+
}
7175
// Now we are in a position where if we know that one type is vector
72-
// or matrix, the other side must be also
76+
// or matrix, the other side must be also and the result type is the one from the type on the
77+
// left if the two types are different (shift operations)
7378
for (BasicType t : Arrays.asList(
7479
BasicType.VEC2,
7580
BasicType.VEC3,
@@ -89,7 +94,7 @@ public static Type resolveTypeOfCommonBinary(Type lhsType, Type rhsType) {
8994
BasicType.MAT4X2,
9095
BasicType.MAT4X3,
9196
BasicType.MAT4X4)) {
92-
if (lhsType == t || rhsType == t) {
97+
if (lhsType == t) {
9398
return t;
9499
}
95100
}

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,122 @@ public void visitLengthExpr(LengthExpr lengthExpr) {
884884
}.visit(tu);
885885
}
886886

887+
@Test
888+
public void testLeftShiftTyped() throws Exception {
889+
final TranslationUnit tu = ParseHelper.parse("#version 320 es\n"
890+
+ "void main() {\n"
891+
+ " int a;\n"
892+
+ " uint b;\n"
893+
+ " ivec2 av;\n"
894+
+ " uvec2 bv;\n"
895+
+ "\n"
896+
+ " int x0 = a << a;\n"
897+
+ " int x1 = a << b;\n"
898+
+ " uint x2 = b << a;\n"
899+
+ " uint x3 = b << b;\n"
900+
+ " ivec2 x4 = av << a;\n"
901+
+ " ivec2 x5 = av << b;\n"
902+
+ " ivec2 x6 = av << av;\n"
903+
+ " ivec2 x7 = av << bv;\n"
904+
+ " uvec2 x8 = bv << a;\n"
905+
+ " uvec2 x9 = bv << b;\n"
906+
+ " uvec2 x10 = bv << av;\n"
907+
+ " uvec2 x11 = bv << bv;\n"
908+
+ "}\n");
909+
new NullCheckTyper(tu) {
910+
private int counter = 0;
911+
@Override
912+
public void visitBinaryExpr(BinaryExpr binaryExpr) {
913+
super.visitBinaryExpr(binaryExpr);
914+
if (binaryExpr.getOp() == BinOp.SHL) {
915+
switch (counter) {
916+
case 0:
917+
case 1:
918+
assertSame(BasicType.INT, lookupType(binaryExpr));
919+
break;
920+
case 2:
921+
case 3:
922+
assertSame(BasicType.UINT, lookupType(binaryExpr));
923+
break;
924+
case 4:
925+
case 5:
926+
case 6:
927+
case 7:
928+
assertSame(BasicType.IVEC2, lookupType(binaryExpr));
929+
break;
930+
case 8:
931+
case 9:
932+
case 10:
933+
case 11:
934+
assertSame(BasicType.UVEC2, lookupType(binaryExpr));
935+
break;
936+
default:
937+
fail();
938+
}
939+
counter++;
940+
}
941+
}
942+
}.visit(tu);
943+
}
944+
945+
@Test
946+
public void testRightShiftTyped() throws Exception {
947+
final TranslationUnit tu = ParseHelper.parse("#version 320 es\n"
948+
+ "void main() {\n"
949+
+ " int a;\n"
950+
+ " uint b;\n"
951+
+ " ivec2 av;\n"
952+
+ " uvec2 bv;\n"
953+
+ "\n"
954+
+ " int x0 = a >> a;\n"
955+
+ " int x1 = a >> b;\n"
956+
+ " uint x2 = b >> a;\n"
957+
+ " uint x3 = b >> b;\n"
958+
+ " ivec2 x4 = av >> a;\n"
959+
+ " ivec2 x5 = av >> b;\n"
960+
+ " ivec2 x6 = av >> av;\n"
961+
+ " ivec2 x7 = av >> bv;\n"
962+
+ " uvec2 x8 = bv >> a;\n"
963+
+ " uvec2 x9 = bv >> b;\n"
964+
+ " uvec2 x10 = bv >> av;\n"
965+
+ " uvec2 x11 = bv >> bv;\n"
966+
+ "}\n");
967+
new NullCheckTyper(tu) {
968+
private int counter = 0;
969+
@Override
970+
public void visitBinaryExpr(BinaryExpr binaryExpr) {
971+
super.visitBinaryExpr(binaryExpr);
972+
if (binaryExpr.getOp() == BinOp.SHR) {
973+
switch (counter) {
974+
case 0:
975+
case 1:
976+
assertSame(BasicType.INT, lookupType(binaryExpr));
977+
break;
978+
case 2:
979+
case 3:
980+
assertSame(BasicType.UINT, lookupType(binaryExpr));
981+
break;
982+
case 4:
983+
case 5:
984+
case 6:
985+
case 7:
986+
assertSame(BasicType.IVEC2, lookupType(binaryExpr));
987+
break;
988+
case 8:
989+
case 9:
990+
case 10:
991+
case 11:
992+
assertSame(BasicType.UVEC2, lookupType(binaryExpr));
993+
break;
994+
default:
995+
fail();
996+
}
997+
counter++;
998+
}
999+
}
1000+
}.visit(tu);
1001+
}
1002+
8871003
private void checkComputeShaderBuiltin(String builtin, String builtinConstant, BasicType baseType,
8881004
TypeQualifier qualifier) throws IOException, ParseTimeoutException, InterruptedException,
8891005
GlslParserException {

0 commit comments

Comments
 (0)