Skip to content

Commit 650a240

Browse files
committed
[CIR] Upstream ArraySubscriptExpr for base type as pointer
1 parent 6de1537 commit 650a240

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,17 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
570570
}
571571

572572
// The base must be a pointer; emit it with an estimate of its alignment.
573-
cgm.errorNYI(e->getSourceRange(),
574-
"emitArraySubscriptExpr: The base must be a pointer");
575-
return {};
573+
assert(e->getBase()->getType()->isPointerType() &&
574+
"The base must be a pointer");
575+
576+
LValueBaseInfo eltBaseInfo;
577+
const Address ptrAddr = emitPointerWithAlignment(e->getBase(), &eltBaseInfo);
578+
// Propagate the alignment from the array itself to the result.
579+
const Address addxr = emitArraySubscriptPtr(
580+
*this, cgm.getLoc(e->getBeginLoc()), cgm.getLoc(e->getEndLoc()), ptrAddr,
581+
e->getType(), idx, cgm.getLoc(e->getExprLoc()),
582+
/*shouldDecay=*/false);
583+
return LValue::makeAddr(addxr, e->getType(), eltBaseInfo);
576584
}
577585

578586
LValue CIRGenFunction::emitBinaryOperatorLValue(const BinaryOperator *e) {

clang/test/CIR/CodeGen/array.cpp

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,87 @@ void func7() {
350350
// OGCG: %[[ARR:.*]] = alloca [1 x ptr], align 8
351351
// OGCG: call void @llvm.memset.p0.i64(ptr align 8 %[[ARR]], i8 0, i64 8, i1 false)
352352

353-
void func8(int p[10]) {}
354-
// CIR: cir.func @func8(%arg0: !cir.ptr<!s32i>
355-
// CIR: cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["p", init]
356-
357-
// LLVM: define void @func8(ptr {{%.*}})
358-
// LLVM-NEXT: alloca ptr, i64 1, align 8
359-
360-
// OGCG: alloca ptr, align 8
353+
void func8(int arr[10]) {
354+
int e = arr[0];
355+
int e2 = arr[1];
356+
}
361357

362-
void func9(int pp[10][5]) {}
363-
// CIR: cir.func @func9(%arg0: !cir.ptr<!cir.array<!s32i x 5>>
364-
// CIR: cir.alloca !cir.ptr<!cir.array<!s32i x 5>>, !cir.ptr<!cir.ptr<!cir.array<!s32i x 5>>>
358+
// CIR: cir.func @func8(%[[ARG:.*]]: !cir.ptr<!s32i>
359+
// CIR: %[[ARR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["arr", init]
360+
// CIR: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
361+
// CIR: %[[INIT_2:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e2", init]
362+
// CIR: cir.store %[[ARG]], %[[ARR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
363+
// CIR: %[[IDX:.*]] = cir.const #cir.int<0> : !s32i
364+
// CIR: %[[TMP_1:.*]] = cir.load %[[ARR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
365+
// CIR: %[[ELE_0:.*]] = cir.ptr_stride(%[[TMP_1]] : !cir.ptr<!s32i>, %[[IDX]] : !s32i), !cir.ptr<!s32i>
366+
// CIR: %[[TMP_2:.*]] = cir.load %[[ELE_0]] : !cir.ptr<!s32i>, !s32i
367+
// CIR: cir.store %[[TMP_2]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
368+
// CIR: %[[IDX_1:.*]] = cir.const #cir.int<1> : !s32i
369+
// CIR: %[[TMP_3:.*]] = cir.load %[[ARR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
370+
// CIR: %[[ELE_1:.*]] = cir.ptr_stride(%[[TMP_3]] : !cir.ptr<!s32i>, %[[IDX_1]] : !s32i), !cir.ptr<!s32i>
371+
// CIR: %[[TMP_4:.*]] = cir.load %[[ELE_1]] : !cir.ptr<!s32i>, !s32i
372+
// CIR: cir.store %[[TMP_4]], %[[INIT_2]] : !s32i, !cir.ptr<!s32i>
373+
374+
// LLVM: define void @func8(ptr %[[ARG:.*]])
375+
// LLVM: %[[ARR:.*]] = alloca ptr, i64 1, align 8
376+
// LLVM: %[[INIT:.*]] = alloca i32, i64 1, align 4
377+
// LLVM: %[[INIT_2:.*]] = alloca i32, i64 1, align 4
378+
// LLVM: store ptr %[[ARG]], ptr %[[ARR]], align 8
379+
// LLVM: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
380+
// LLVM: %[[ELE_0:.*]] = getelementptr i32, ptr %[[TMP_1]], i64 0
381+
// LLVM: %[[TMP_2:.*]] = load i32, ptr %[[ELE_0]], align 4
382+
// LLVM: store i32 %[[TMP_2]], ptr %[[INIT]], align 4
383+
// LLVM: %[[TMP_3:.*]] = load ptr, ptr %[[ARR]], align 8
384+
// LLVM: %[[ELE_1:.*]] = getelementptr i32, ptr %[[TMP_3]], i64 1
385+
// LLVM: %[[TMP_4:.*]] = load i32, ptr %[[ELE_1]], align 4
386+
// LLVM: store i32 %[[TMP_4]], ptr %[[INIT_2]], align 4
387+
388+
// OGCG: %[[ARR:.*]] = alloca ptr, align 8
389+
// OGCG: %[[INIT:.*]] = alloca i32, align 4
390+
// OGCG: %[[INIT_2:.*]] = alloca i32, align 4
391+
// OGCG: store ptr {{%.*}}, ptr %[[ARR]], align 8
392+
// OGCG: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
393+
// OGCG: %[[ELE_0:.*]] = getelementptr inbounds i32, ptr %[[TMP_1]], i64 0
394+
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ELE_0]], align 4
395+
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT]], align 4
396+
// OGCG: %[[TMP_3:.*]] = load ptr, ptr %[[ARR]], align 8
397+
// OGCG: %[[ELE_1:.*]] = getelementptr inbounds i32, ptr %[[TMP_3]], i64 1
398+
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ELE_1]], align 4
399+
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT_2]], align 4
365400

366-
// LLVM: define void @func9(ptr {{%.*}})
367-
// LLVM-NEXT: alloca ptr, i64 1, align 8
401+
void func9(int arr[10][5]) {
402+
int e = arr[1][2];
403+
}
368404

369-
// OGCG: alloca ptr, align 8
405+
// CIR: cir.func @func9(%[[ARG:.*]]: !cir.ptr<!cir.array<!s32i x 5>>
406+
// CIR: %[[ARR:.*]] = cir.alloca !cir.ptr<!cir.array<!s32i x 5>>, !cir.ptr<!cir.ptr<!cir.array<!s32i x 5>>>, ["arr", init]
407+
// CIR: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
408+
// CIR: cir.store %[[ARG]], %[[ARR]] : !cir.ptr<!cir.array<!s32i x 5>>, !cir.ptr<!cir.ptr<!cir.array<!s32i x 5>>>
409+
// CIR: %[[IDX:.*]] = cir.const #cir.int<2> : !s32i
410+
// CIR: %[[IDX_1:.*]] = cir.const #cir.int<1> : !s32i
411+
// CIR: %[[TMP_1:.*]] = cir.load %[[ARR]] : !cir.ptr<!cir.ptr<!cir.array<!s32i x 5>>>, !cir.ptr<!cir.array<!s32i x 5>>
412+
// CIR: %[[ARR_1:.*]] = cir.ptr_stride(%[[TMP_1]] : !cir.ptr<!cir.array<!s32i x 5>>, %[[IDX_1]] : !s32i), !cir.ptr<!cir.array<!s32i x 5>>
413+
// CIR: %[[ARR_1_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR_1]] : !cir.ptr<!cir.array<!s32i x 5>>), !cir.ptr<!s32i>
414+
// CIR: %[[ARR_1_2:.*]] = cir.ptr_stride(%[[ARR_1_PTR]] : !cir.ptr<!s32i>, %[[IDX]] : !s32i), !cir.ptr<!s32i>
415+
// CIR: %[[TMP_2:.*]] = cir.load %[[ARR_1_2]] : !cir.ptr<!s32i>, !s32i
416+
// CIR: cir.store %[[TMP_2]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
417+
418+
// LLVM: define void @func9(ptr %[[ARG:.*]])
419+
// LLVM: %[[ARR:.*]] = alloca ptr, i64 1, align 8
420+
// LLVM: %[[INIT:.*]] = alloca i32, i64 1, align 4
421+
// LLVM: store ptr %[[ARG]], ptr %[[ARR]], align 8
422+
// LLVM: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
423+
// LLVM: %[[ARR_1:.*]] = getelementptr [5 x i32], ptr %[[TMP_1]], i64 1
424+
// LLVM: %[[ARR_1_PTR:.*]] = getelementptr i32, ptr %[[ARR_1]], i32 0
425+
// LLVM: %[[ARR_1_2:.*]] = getelementptr i32, ptr %[[ARR_1_PTR]], i64 2
426+
// LLVM: %[[TMP_2:.*]] = load i32, ptr %[[ARR_1_2]], align 4
427+
// LLVM: store i32 %[[TMP_2]], ptr %[[INIT]], align 4
428+
429+
// OGCG: %[[ARR:.*]] = alloca ptr, align 8
430+
// OGCG: %[[INIT:.*]] = alloca i32, align 4
431+
// OGCG: store ptr {{%.*}}, ptr %[[ARR]], align 8
432+
// OGCG: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
433+
// OGCG: %[[ARR_1:.*]] = getelementptr inbounds [5 x i32], ptr %[[TMP_1]], i64 1
434+
// OGCG: %[[ARR_1_2:.*]] = getelementptr inbounds [5 x i32], ptr %[[ARR_1]], i64 0, i64 2
435+
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ARR_1_2]], align 4
436+
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT]], align 4

0 commit comments

Comments
 (0)