Skip to content

Commit 2bb03de

Browse files
committed
Address code review comments
1 parent 650a240 commit 2bb03de

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,19 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
552552
// in lexical order (this complexity is, sadly, required by C++17).
553553
assert((e->getIdx() == e->getLHS() || e->getIdx() == e->getRHS()) &&
554554
"index was neither LHS nor RHS");
555-
const mlir::Value idx = emitScalarExpr(e->getIdx());
555+
556+
auto emitIdxAfterBase = [&]() -> mlir::Value {
557+
const mlir::Value idx = emitScalarExpr(e->getIdx());
558+
559+
// Extend or truncate the index type to 32 or 64-bits.
560+
auto ptrTy = mlir::dyn_cast<cir::PointerType>(idx.getType());
561+
if (ptrTy && mlir::isa<cir::IntType>(ptrTy.getPointee()))
562+
cgm.errorNYI(e->getSourceRange(),
563+
"emitArraySubscriptExpr: index type cast");
564+
return idx;
565+
};
566+
567+
const mlir::Value idx = emitIdxAfterBase();
556568
if (const Expr *array = getSimpleArrayDecayOperand(e->getBase())) {
557569
LValue arrayLV;
558570
if (const auto *ase = dyn_cast<ArraySubscriptExpr>(array))
@@ -566,7 +578,13 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
566578
arrayLV.getAddress(), e->getType(), idx, cgm.getLoc(e->getExprLoc()),
567579
/*shouldDecay=*/true);
568580

569-
return LValue::makeAddr(addr, e->getType(), LValueBaseInfo());
581+
const LValue lv = LValue::makeAddr(addr, e->getType(), LValueBaseInfo());
582+
583+
if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
584+
cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
585+
}
586+
587+
return lv;
570588
}
571589

572590
// The base must be a pointer; emit it with an estimate of its alignment.
@@ -580,7 +598,14 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
580598
*this, cgm.getLoc(e->getBeginLoc()), cgm.getLoc(e->getEndLoc()), ptrAddr,
581599
e->getType(), idx, cgm.getLoc(e->getExprLoc()),
582600
/*shouldDecay=*/false);
583-
return LValue::makeAddr(addxr, e->getType(), eltBaseInfo);
601+
602+
const LValue lv = LValue::makeAddr(addxr, e->getType(), eltBaseInfo);
603+
604+
if (getLangOpts().ObjC && getLangOpts().getGC() != LangOptions::NonGC) {
605+
cgm.errorNYI(e->getSourceRange(), "emitArraySubscriptExpr: ObjC with GC");
606+
}
607+
608+
return lv;
584609
}
585610

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

clang/test/CIR/CodeGen/array.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,34 @@ void func9(int arr[10][5]) {
434434
// OGCG: %[[ARR_1_2:.*]] = getelementptr inbounds [5 x i32], ptr %[[ARR_1]], i64 0, i64 2
435435
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ARR_1_2]], align 4
436436
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT]], align 4
437+
438+
void func10(int *a) {
439+
int e = a[5];
440+
}
441+
442+
// CIR: cir.func @func10(%[[ARG:.*]]: !cir.ptr<!s32i>
443+
// CIR: %[[ARR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["a", init]
444+
// CIR: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
445+
// CIR: cir.store %[[ARG]], %[[ARR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
446+
// CIR: %[[IDX:.*]] = cir.const #cir.int<5> : !s32i
447+
// CIR: %[[TMP_1:.*]] = cir.load %[[ARR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
448+
// CIR: %[[ELE:.*]] = cir.ptr_stride(%[[TMP_1]] : !cir.ptr<!s32i>, %[[IDX]] : !s32i), !cir.ptr<!s32i>
449+
// CIR: %[[TMP_2:.*]] = cir.load %[[ELE]] : !cir.ptr<!s32i>, !s32i
450+
// CIR: cir.store %[[TMP_2]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
451+
452+
// LLVM: define void @func10(ptr %[[ARG:.*]]) {
453+
// LLVM: %[[ARR:.*]] = alloca ptr, i64 1, align 8
454+
// LLVM: %[[INIT:.*]] = alloca i32, i64 1, align 4
455+
// LLVM: store ptr %[[ARG]], ptr %[[ARR]], align 8
456+
// LLVM: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
457+
// LLVM: %[[ELE:.*]] = getelementptr i32, ptr %[[TMP_1]], i64 5
458+
// LLVM: %[[TMP_2:.*]] = load i32, ptr %[[ELE]], align 4
459+
// LLVM: store i32 %[[TMP_2]], ptr %[[INIT]], align 4
460+
461+
// OGCG: %[[ARR:.*]] = alloca ptr, align 8
462+
// OGCG: %[[INIT:.*]] = alloca i32, align 4
463+
// OGCG: store ptr {{%.*}}, ptr %[[ARR]], align 8
464+
// OGCG: %[[TMP_1:.*]] = load ptr, ptr %[[ARR]], align 8
465+
// OGCG: %[[ELE:.*]] = getelementptr inbounds i32, ptr %[[TMP_1]], i64 5
466+
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ELE]], align 4
467+
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT]], align 4

0 commit comments

Comments
 (0)