Skip to content

Commit abd9621

Browse files
committed
Remove unneeded check and add tests for variable index
1 parent 9d07a29 commit abd9621

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,18 @@ static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
408408
return subExpr;
409409
}
410410

411-
static mlir::IntegerAttr getConstantIndexOrNull(mlir::Value idx) {
411+
static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
412412
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
413413
if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
414-
return mlir::dyn_cast<mlir::IntegerAttr>(constantOp.getValue());
414+
return mlir::dyn_cast<cir::IntAttr>(constantOp.getValue());
415415
return {};
416416
}
417417

418418
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
419419
CharUnits eltSize) {
420420
// If we have a constant index, we can use the exact offset of the
421421
// element we're accessing.
422-
const mlir::IntegerAttr constantIdx = getConstantIndexOrNull(idx);
422+
const cir::IntAttr constantIdx = getConstantIndexOrNull(idx);
423423
if (constantIdx) {
424424
const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
425425
return arrayAlign.alignmentAtOffset(offset);
@@ -468,17 +468,12 @@ static Address emitArraySubscriptPtr(CIRGenFunction &cgf,
468468
const CharUnits eltAlign =
469469
getArrayElementAlign(addr.getAlignment(), idx, eltSize);
470470

471-
mlir::Value eltPtr;
472-
const mlir::IntegerAttr index = getConstantIndexOrNull(idx);
473-
if (!index) {
474471
assert(!cir::MissingFeatures::preservedAccessIndexRegion());
475-
eltPtr = emitArraySubscriptPtr(cgf, beginLoc, endLoc, addr.getPointer(),
476-
addr.getElementType(), idx, shouldDecay);
477-
} else {
478-
cgf.cgm.errorNYI("emitArraySubscriptExpr: Non Constant Index");
479-
}
480-
const mlir::Type elementType = cgf.convertTypeForMem(eltType);
481-
return Address(eltPtr, elementType, eltAlign);
472+
const mlir::Value eltPtr =
473+
emitArraySubscriptPtr(cgf, beginLoc, endLoc, addr.getPointer(),
474+
addr.getElementType(), idx, shouldDecay);
475+
const mlir::Type elementType = cgf.convertTypeForMem(eltType);
476+
return Address(eltPtr, elementType, eltAlign);
482477
}
483478

484479
LValue

clang/test/CIR/CodeGen/array.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,26 +151,55 @@ void func2() {
151151

152152
void func3() {
153153
int arr[2] = {5, 6};
154+
155+
int idx = 1;
156+
int e = arr[idx];
154157
}
155158

156-
// CIR: %[[ARR3:.*]] = cir.alloca !cir.array<!s32i x 2>, !cir.ptr<!cir.array<!s32i x 2>>, ["arr", init]
157-
// CIR: %[[ARR_3_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR3]] : !cir.ptr<!cir.array<!s32i x 2>>), !cir.ptr<!s32i>
159+
// CIR: %[[ARR:.*]] = cir.alloca !cir.array<!s32i x 2>, !cir.ptr<!cir.array<!s32i x 2>>, ["arr", init]
160+
// CIR: %[[IDX:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["idx", init]
161+
// CIR: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
162+
// CIR: %[[ARR_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR]] : !cir.ptr<!cir.array<!s32i x 2>>), !cir.ptr<!s32i>
158163
// CIR: %[[V0:.*]] = cir.const #cir.int<5> : !s32i
159-
// CIR: cir.store %[[V0]], %[[ARR_3_PTR]] : !s32i, !cir.ptr<!s32i>
164+
// CIR: cir.store %[[V0]], %[[ARR_PTR]] : !s32i, !cir.ptr<!s32i>
160165
// CIR: %[[OFFSET_0:.*]] = cir.const #cir.int<1> : !s64i
161-
// CIR: %[[ELE_1_PTR:.*]] = cir.ptr_stride(%[[ARR_3_PTR]] : !cir.ptr<!s32i>, %[[OFFSET_0]] : !s64i), !cir.ptr<!s32i>
166+
// CIR: %[[ELE_1_PTR:.*]] = cir.ptr_stride(%[[ARR_PTR]] : !cir.ptr<!s32i>, %[[OFFSET_0]] : !s64i), !cir.ptr<!s32i>
162167
// CIR: %[[V1:.*]] = cir.const #cir.int<6> : !s32i
163168
// CIR: cir.store %[[V1]], %[[ELE_1_PTR]] : !s32i, !cir.ptr<!s32i>
169+
// CIR: %[[IDX_V:.*]] = cir.const #cir.int<1> : !s32i
170+
// CIR: cir.store %[[IDX_V]], %[[IDX]] : !s32i, !cir.ptr<!s32i>
171+
// CIR: %[[TMP_IDX:.*]] = cir.load %[[IDX]] : !cir.ptr<!s32i>, !s32i
172+
// CIR: %[[ARR_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR]] : !cir.ptr<!cir.array<!s32i x 2>>), !cir.ptr<!s32i>
173+
// CIR: %[[ELE_PTR:.*]] = cir.ptr_stride(%[[ARR_PTR]] : !cir.ptr<!s32i>, %[[TMP_IDX]] : !s32i), !cir.ptr<!s32i>
174+
// CIR: %[[ELE_TMP:.*]] = cir.load %[[ELE_PTR]] : !cir.ptr<!s32i>, !s32i
175+
// CIR: cir.store %[[ELE_TMP]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
164176

165177
// LLVM: define void @func3()
166178
// LLVM: %[[ARR_ALLOCA:.*]] = alloca [2 x i32], i64 1, align 4
179+
// LLVM: %[[IDX:.*]] = alloca i32, i64 1, align 4
180+
// LLVM: %[[INIT:.*]] = alloca i32, i64 1, align 4
167181
// LLVM: %[[ARR_PTR:.*]] = getelementptr i32, ptr %[[ARR_ALLOCA]], i32 0
168182
// LLVM: store i32 5, ptr %[[ARR_PTR]], align 4
169183
// LLVM: %[[ELE_1_PTR:.*]] = getelementptr i32, ptr %[[ARR_PTR]], i64 1
170184
// LLVM: store i32 6, ptr %[[ELE_1_PTR]], align 4
185+
// LLVM: store i32 1, ptr %[[IDX]], align 4
186+
// LLVM: %[[TMP1:.*]] = load i32, ptr %[[IDX]], align 4
187+
// LLVM: %[[ARR_PTR:.*]] = getelementptr i32, ptr %[[ARR_ALLOCA]], i32 0
188+
// LLVM: %[[IDX_I64:.*]] = sext i32 %[[TMP1]] to i64
189+
// LLVM: %[[ELE:.*]] = getelementptr i32, ptr %[[ARR_PTR]], i64 %[[IDX_I64]]
190+
// LLVM: %[[TMP2:.*]] = load i32, ptr %[[ELE]], align 4
191+
// LLVM: store i32 %[[TMP2]], ptr %[[INIT]], align 4
171192

172-
// OGCG: %arr = alloca [2 x i32], align 4
173-
// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %arr, ptr align 4 @[[FUN3_ARR]], i64 8, i1 false)
193+
// OGCG: %arr = alloca [2 x i32], align 4
194+
// OGCG: %idx = alloca i32, align 4
195+
// OGCG: %e = alloca i32, align 4
196+
// OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %arr, ptr align 4 @[[FUN3_ARR]], i64 8, i1 false)
197+
// OGCG: store i32 1, ptr %idx, align 4
198+
// OGCG: %0 = load i32, ptr %idx, align 4
199+
// OGCG: %idxprom = sext i32 %0 to i64
200+
// OGCG: %arrayidx = getelementptr inbounds [2 x i32], ptr %arr, i64 0, i64 %idxprom
201+
// OGCG: %1 = load i32, ptr %arrayidx, align 4
202+
// OGCG: store i32 %1, ptr %e, align 4
174203

175204
void func4() {
176205
int arr[2][1] = {{5}, {6}};

0 commit comments

Comments
 (0)