Skip to content

Commit 82e2bc0

Browse files
committed
PR feedback improvements
1 parent c64e1ce commit 82e2bc0

File tree

10 files changed

+30
-28
lines changed

10 files changed

+30
-28
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,8 +2807,7 @@ class MatrixSingleSubscriptExpr : public Expr {
28072807
SourceLocation RBracketLoc)
28082808
: Expr(MatrixSingleSubscriptExprClass, T,
28092809
Base->getValueKind(), // lvalue/rvalue follows the matrix base
2810-
OK_MatrixComponent) { // or OK_Ordinary/OK_VectorComponent if you
2811-
// prefer
2810+
OK_MatrixComponent) {
28122811
SubExprs[BASE] = Base;
28132812
SubExprs[ROW_IDX] = RowIdx;
28142813
ArrayOrMatrixSubscriptExprBits.RBracketLoc = RBracketLoc;

clang/include/clang/Basic/Specifiers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ namespace clang {
165165
/// Objective-C method calls.
166166
OK_ObjCSubscript,
167167

168-
/// A matrix component is a single element of a matrix.
168+
/// A matrix component is a single element or range of elements on a matrix.
169169
OK_MatrixComponent
170170
};
171171

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,22 +2473,18 @@ RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
24732473
unsigned NumCols = MT->getNumColumns();
24742474

24752475
llvm::Value *MatrixVec = EmitLoadOfScalar(LV, Loc);
2476-
24772476
llvm::Value *Row = LV.getMatrixRowIdx();
2478-
llvm::Value *Result =
2479-
llvm::PoisonValue::get(ConvertType(LV.getType())); // <NumCols x T>
2477+
llvm::Type *ElemTy = ConvertType(MT->getElementType());
2478+
llvm::Type *RowTy = llvm::FixedVectorType::get(ElemTy, MT->getNumColumns());
2479+
llvm::Value *Result = llvm::PoisonValue::get(RowTy); // <NumCols x T>
24802480

24812481
llvm::MatrixBuilder MB(Builder);
24822482

24832483
for (unsigned Col = 0; Col < NumCols; ++Col) {
24842484
llvm::Value *ColIdx = llvm::ConstantInt::get(Row->getType(), Col);
2485-
24862485
llvm::Value *EltIndex = MB.CreateIndex(Row, ColIdx, NumRows);
2487-
24882486
llvm::Value *Elt = Builder.CreateExtractElement(MatrixVec, EltIndex);
2489-
24902487
llvm::Value *Lane = llvm::ConstantInt::get(Builder.getInt32Ty(), Col);
2491-
24922488
Result = Builder.CreateInsertElement(Result, Elt, Lane);
24932489
}
24942490

@@ -4981,9 +4977,9 @@ LValue CodeGenFunction::EmitMatrixSingleSubscriptExpr(
49814977

49824978
unsigned Row = RowConst->getZExtValue();
49834979
unsigned Start = Row * NumCols;
4984-
for (unsigned C = 0; C < NumCols; ++C) {
4980+
for (unsigned C = 0; C < NumCols; ++C)
49854981
Indices.push_back(llvm::ConstantInt::get(Int32Ty, Start + C));
4986-
}
4982+
49874983
llvm::Constant *Elts = llvm::ConstantVector::get(Indices);
49884984
return LValue::MakeExtVectorElt(
49894985
MaybeConvertMatrixAddress(Base.getAddress(), *this), Elts,

clang/lib/CodeGen/CGValue.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class LValue {
200200
// Index into a vector subscript: V[i]
201201
llvm::Value *VectorIdx;
202202

203+
// Index into a matrix row subscript: M[i]
204+
llvm::Value *MatrixRowIdx;
205+
203206
// ExtVector element subset: V.xyx
204207
llvm::Constant *VectorElts;
205208

@@ -402,7 +405,7 @@ class LValue {
402405

403406
llvm::Value *getMatrixRowIdx() const {
404407
assert(isMatrixRow());
405-
return VectorIdx;
408+
return MatrixRowIdx;
406409
}
407410

408411
// extended vector elements.
@@ -498,7 +501,7 @@ class LValue {
498501
TBAAAccessInfo TBAAInfo) {
499502
LValue LV;
500503
LV.LVType = MatrixRow;
501-
LV.VectorIdx = RowIdx; // store the row index here
504+
LV.MatrixRowIdx = RowIdx; // store the row index here
502505
LV.Initialize(MatrixTy, MatrixTy.getQualifiers(), Addr, BaseInfo, TBAAInfo);
503506
return LV;
504507
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5160,8 +5160,6 @@ ExprResult Sema::CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx,
51605160
RowIdx = RowR.get();
51615161

51625162
if (!ColumnIdx) {
5163-
if (getLangOpts().HLSL)
5164-
return CreateBuiltinMatrixSingleSubscriptExpr(Base, RowIdx, RBLoc);
51655163
return new (Context) MatrixSubscriptExpr(
51665164
Base, RowIdx, ColumnIdx, Context.IncompleteMatrixIdxTy, RBLoc);
51675165
}
@@ -21641,12 +21639,17 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) {
2164121639
return ExprError();
2164221640
}
2164321641

21644-
case BuiltinType::IncompleteMatrixIdx:
21645-
Diag(cast<MatrixSubscriptExpr>(E->IgnoreParens())
21646-
->getRowIdx()
21647-
->getBeginLoc(),
21648-
diag::err_matrix_incomplete_index);
21642+
case BuiltinType::IncompleteMatrixIdx: {
21643+
auto *MS = cast<MatrixSubscriptExpr>(E->IgnoreParens());
21644+
// At this point, we know there was no second [] to complete the operator.
21645+
// In HLSL, treat "m[row]" as selecting a row lane of column sized vector.
21646+
if (getLangOpts().HLSL) {
21647+
return CreateBuiltinMatrixSingleSubscriptExpr(
21648+
MS->getBase(), MS->getRowIdx(), E->getExprLoc());
21649+
}
21650+
Diag(MS->getRowIdx()->getBeginLoc(), diag::err_matrix_incomplete_index);
2164921651
return ExprError();
21652+
}
2165021653

2165121654
// Expressions of unknown type.
2165221655
case BuiltinType::ArraySection:

clang/test/AST/HLSL/matrix-single-subscript-getter.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export float4 AddFloatMatrixConstant(float4x4 M) {
4848
// CHECK-NEXT: MatrixSingleSubscriptExpr {{.*}} 'vector<float, 4>' lvalue matrixcomponent
4949
// CHECK-NEXT: DeclRefExpr {{.*}} 'float4x4':'matrix<float, 4, 4>' lvalue ParmVar {{.*}} 'M' 'float4x4':'matrix<float, 4, 4>'
5050
// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 3
51-
return M[0] + M[1] + M[2] +M[3];
51+
return M[0] + M[1] + M[2] + M[3];
5252
}
5353

5454
export int4 AddIntMatrixConstant(int4x4 M) {
@@ -73,5 +73,5 @@ export int4 AddIntMatrixConstant(int4x4 M) {
7373
// CHECK-NEXT: MatrixSingleSubscriptExpr {{.*}} 'vector<int, 4>' lvalue matrixcomponent
7474
// CHECK-NEXT: DeclRefExpr {{.*}} 'int4x4':'matrix<int, 4, 4>' lvalue ParmVar {{.*}} 'M' 'int4x4':'matrix<int, 4, 4>'
7575
// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 3
76-
return M[0] + M[1] + M[2] +M[3];
77-
}
76+
return M[0] + M[1] + M[2] + M[3];
77+
}

clang/test/AST/HLSL/matrix-single-subscript-setter.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ export void setMatrixConstIndex(out int4x4 M, int4x4 N ) {
5656
// CHECK-NEXT: DeclRefExpr {{.*}} 'int4x4':'matrix<int, 4, 4>' lvalue ParmVar {{.*}} 'N' 'int4x4':'matrix<int, 4, 4>'
5757
// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 0
5858
M[3] = N[0];
59-
}
59+
}

clang/test/AST/HLSL/matrix-single-subscript-swizzle.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ export float3 getMatrix(float4x4 M, int index) {
5353
// CHECK-NEXT: DeclRefExpr {{.*}} 'float4x4':'matrix<float, 4, 4>' lvalue ParmVar {{.*}} 'M' 'float4x4':'matrix<float, 4, 4>'
5454
// CHECK-NEXT: DeclRefExpr {{.*}} 'int' lvalue ParmVar {{.*}} 'index' 'int'
5555
return M[index].rgb;
56-
}
56+
}

clang/test/CodeGenHLSL/BasicFeatures/MatrixSingleSubscriptDynamicSwizzle.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.7-library -disable-llvm-passes -emit-llvm -finclude-default-header -o - %s | FileCheck %s
2+
// BUG: https://github.com/llvm/llvm-project/issues/170777
23
// XFAIL: *
34

45
void setMatrix(out float4x4 M, int index, float4 V) {

clang/test/CodeGenHLSL/BasicFeatures/MatrixSingleSubscriptGetter.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int4 getIntMatrixDynamic(int4x4 M, int index) {
151151
// CHECK-NEXT: ret <4 x float> [[ADD32]]
152152
//
153153
float4 AddFloatMatrixConstant(float4x4 M) {
154-
return M[0] + M[1] + M[2] +M[3];
154+
return M[0] + M[1] + M[2] + M[3];
155155
}
156156

157157
// CHECK-LABEL: define hidden noundef <4 x i32> @_Z20AddIntMatrixConstantu11matrix_typeILm4ELm4EiE(
@@ -201,5 +201,5 @@ float4 AddFloatMatrixConstant(float4x4 M) {
201201
// CHECK-NEXT: ret <4 x i32> [[ADD32]]
202202
//
203203
int4 AddIntMatrixConstant(int4x4 M) {
204-
return M[0] + M[1] + M[2] +M[3];
204+
return M[0] + M[1] + M[2] + M[3];
205205
}

0 commit comments

Comments
 (0)