Skip to content

Commit f76d2f2

Browse files
authored
Merge pull request #9210 from fhahn/matrix-sign-warning
* [Matrix] Add test showing unintended implicit sign conversion warning. * [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
2 parents 497ed74 + 3af81e8 commit f76d2f2

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
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();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx -std=c++11 -fenable-matrix -fsyntax-only -verify -Wsign-conversion %s
2+
3+
template <typename T, int R, int C> using m __attribute__((__matrix_type__(R,C))) = T;
4+
5+
double index1(m<double,3,1> X, int i) { return X[i][0]; }
6+
7+
double index2(m<double,3,1> X, unsigned i) { return X[i][0]; }
8+
9+
double index3(m<double,3,1> X, char i) { return X[i][0]; }
10+
11+
double index4(m<double,3,1> X, int i) { return X[0][i]; }
12+
13+
double index5(m<double,3,1> X, unsigned i) { return X[0][i]; }
14+
15+
double index6(m<double,3,1> X, char i) { return X[0][i]; }
16+
17+
// expected-no-diagnostics

0 commit comments

Comments
 (0)