Skip to content

Commit b68f638

Browse files
committed
[Matrix] Preserve signedness when extending matrix index expression.
As per [1] the indices for a matrix element access operator shall have integral or unscoped enumeration types and be non-negative. At the moment, the index expression is converted to SizeType irrespective of the signedness of the index expression. This causes implicit sign conversion warnings if any of the indices is signed. As per the spec, using signed types as indices is allowed and should not cause any warnings. If the index expression is signed, extend to SignedSizeType to avoid the warning. [1] https://clang.llvm.org/docs/MatrixTypes.html#matrix-type-element-access-operator
1 parent 05f6630 commit b68f638

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,8 +5025,10 @@ ExprResult Sema::CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx,
50255025
}
50265026
}
50275027

5028-
ExprResult ConvExpr =
5029-
tryConvertExprToType(IndexExpr, Context.getSizeType());
5028+
ExprResult ConvExpr = tryConvertExprToType(
5029+
IndexExpr, IndexExpr->getType()->isSignedIntegerType()
5030+
? Context.getSignedSizeType()
5031+
: Context.getSizeType());
50305032
assert(!ConvExpr.isInvalid() &&
50315033
"should be able to convert any integer type to size type");
50325034
return ConvExpr.get();

clang/test/SemaCXX/matrix-index-operator-sign-conversion.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
template <typename T, int R, int C> using m __attribute__((__matrix_type__(R,C))) = T;
44

5-
// FIXME: should not warn here.
65
double index1(m<double,3,1> X, int i) { return X[i][0]; }
7-
// expected-warning@-1 {{implicit conversion changes signedness: 'int' to 'unsigned long'}}
86

97
double index2(m<double,3,1> X, unsigned i) { return X[i][0]; }
108

119
double index3(m<double,3,1> X, char i) { return X[i][0]; }
12-
// expected-warning@-1 {{implicit conversion changes signedness: 'char' to 'unsigned long'}}
1310

1411
double index4(m<double,3,1> X, int i) { return X[0][i]; }
15-
// expected-warning@-1 {{implicit conversion changes signedness: 'int' to 'unsigned long'}}
1612

1713
double index5(m<double,3,1> X, unsigned i) { return X[0][i]; }
1814

1915
double index6(m<double,3,1> X, char i) { return X[0][i]; }
20-
// expected-warning@-1 {{implicit conversion changes signedness: 'char' to 'unsigned long'}}
16+
17+
// expected-no-diagnostics

0 commit comments

Comments
 (0)