Skip to content

Commit 91d415b

Browse files
[Clang][AArch64][SVE] Allow write to SVE vector elements using the subscript operator (#91965)
The patch at https://reviews.llvm.org/D122732 introduced using the array subscript operator for SVE vectors, however it also causes an ICE when the subscripting expression is used as an lvalue. This patches fixes the error. Lvalue subscripting expressions are emitted as LLVM IR `insertelement`.
1 parent 15710bb commit 91d415b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4180,7 +4180,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
41804180

41814181
// If the base is a vector type, then we are forming a vector element lvalue
41824182
// with this subscript.
4183-
if (E->getBase()->getType()->isVectorType() &&
4183+
if (E->getBase()->getType()->isSubscriptableVectorType() &&
41844184
!isa<ExtVectorElementExpr>(E->getBase())) {
41854185
// Emit the vector as an lvalue to get its address.
41864186
LValue LHS = EmitLValue(E->getBase());

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5185,7 +5185,7 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
51855185
}
51865186

51875187
// Perform default conversions.
5188-
if (!LHSExp->getType()->getAs<VectorType>()) {
5188+
if (!LHSExp->getType()->isSubscriptableVectorType()) {
51895189
ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp);
51905190
if (Result.isInvalid())
51915191
return ExprError();

clang/test/CodeGen/aarch64-sve-vector-subscript-ops.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@ float subscript_float32(svfloat32_t a, size_t b) {
8888
double subscript_float64(svfloat64_t a, size_t b) {
8989
return a[b];
9090
}
91+
92+
// CHECK-LABEL: @subscript_write_float32(
93+
// CHECK-NEXT: entry:
94+
// CHECK-NEXT: [[VECINS:%.*]] = insertelement <vscale x 4 x float> [[A:%.*]], float 1.000000e+00, i64 [[B:%.*]]
95+
// CHECK-NEXT: ret <vscale x 4 x float> [[VECINS]]
96+
//
97+
svfloat32_t subscript_write_float32(svfloat32_t a, size_t b) {
98+
a[b] = 1.0f;
99+
return a;
100+
}
101+
102+
// CHECK-LABEL: @subscript_read_write_float32(
103+
// CHECK-NEXT: entry:
104+
// CHECK-NEXT: [[VECEXT:%.*]] = extractelement <vscale x 4 x float> [[A:%.*]], i64 [[B:%.*]]
105+
// CHECK-NEXT: [[ADD:%.*]] = fadd float [[VECEXT]], 1.000000e+00
106+
// CHECK-NEXT: [[VECINS:%.*]] = insertelement <vscale x 4 x float> [[A]], float [[ADD]], i64 [[B]]
107+
// CHECK-NEXT: ret <vscale x 4 x float> [[VECINS]]
108+
//
109+
svfloat32_t subscript_read_write_float32(svfloat32_t a, size_t b) {
110+
a[b] += 1.0f;
111+
return a;
112+
}

0 commit comments

Comments
 (0)