Skip to content

Commit 3af81e8

Browse files
committed
[Matrix] Preserve signedness when extending matrix index expression. (llvm#103044)
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 PR: llvm#103044
1 parent b0051d2 commit 3af81e8

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,13 +4390,24 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
43904390
return LV;
43914391
}
43924392

4393+
llvm::Value *CodeGenFunction::EmitMatrixIndexExpr(const Expr *E) {
4394+
llvm::Value *Idx = EmitScalarExpr(E);
4395+
if (Idx->getType() == IntPtrTy)
4396+
return Idx;
4397+
bool IsSigned = E->getType()->isSignedIntegerOrEnumerationType();
4398+
return Builder.CreateIntCast(Idx, IntPtrTy, IsSigned);
4399+
}
4400+
43934401
LValue CodeGenFunction::EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E) {
43944402
assert(
43954403
!E->isIncomplete() &&
43964404
"incomplete matrix subscript expressions should be rejected during Sema");
43974405
LValue Base = EmitLValue(E->getBase());
4398-
llvm::Value *RowIdx = EmitScalarExpr(E->getRowIdx());
4399-
llvm::Value *ColIdx = EmitScalarExpr(E->getColumnIdx());
4406+
4407+
// Extend or truncate the index type to 32 or 64-bits if needed.
4408+
llvm::Value *RowIdx = EmitMatrixIndexExpr(E->getRowIdx());
4409+
llvm::Value *ColIdx = EmitMatrixIndexExpr(E->getColumnIdx());
4410+
44004411
llvm::Value *NumRows = Builder.getIntN(
44014412
RowIdx->getType()->getScalarSizeInBits(),
44024413
E->getBase()->getType()->castAs<ConstantMatrixType>()->getNumRows());

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,8 +1995,8 @@ Value *ScalarExprEmitter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E) {
19951995

19961996
// Handle the vector case. The base must be a vector, the index must be an
19971997
// integer value.
1998-
Value *RowIdx = Visit(E->getRowIdx());
1999-
Value *ColumnIdx = Visit(E->getColumnIdx());
1998+
Value *RowIdx = CGF.EmitMatrixIndexExpr(E->getRowIdx());
1999+
Value *ColumnIdx = CGF.EmitMatrixIndexExpr(E->getColumnIdx());
20002000

20012001
const auto *MatrixTy = E->getBase()->getType()->castAs<ConstantMatrixType>();
20022002
unsigned NumRows = MatrixTy->getNumRows();

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,6 +4273,7 @@ class CodeGenFunction : public CodeGenTypeCache {
42734273
LValue EmitUnaryOpLValue(const UnaryOperator *E);
42744274
LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
42754275
bool Accessed = false);
4276+
llvm::Value *EmitMatrixIndexExpr(const Expr *E);
42764277
LValue EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E);
42774278
LValue EmitArraySectionExpr(const ArraySectionExpr *E,
42784279
bool IsLowerBound = true);

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5021,8 +5021,7 @@ ExprResult Sema::CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx,
50215021
}
50225022
}
50235023

5024-
ExprResult ConvExpr =
5025-
tryConvertExprToType(IndexExpr, Context.getSizeType());
5024+
ExprResult ConvExpr = IndexExpr;
50265025
assert(!ConvExpr.isInvalid() &&
50275026
"should be able to convert any integer type to size type");
50285027
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)