Skip to content

Commit 491c79f

Browse files
authored
[CIR] Implement ArraySubscript for ComplexType (#146283)
Implement ArraySubscript for ComplexType #141365
1 parent 5035d20 commit 491c79f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
3939
void emitStoreOfComplex(mlir::Location loc, mlir::Value val, LValue lv,
4040
bool isInit);
4141

42+
mlir::Value VisitArraySubscriptExpr(Expr *e);
4243
mlir::Value VisitBinAssign(const BinaryOperator *e);
4344
mlir::Value VisitCallExpr(const CallExpr *e);
4445
mlir::Value VisitChooseExpr(ChooseExpr *e);
@@ -119,6 +120,10 @@ void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val,
119120
builder.createStore(loc, val, destAddr);
120121
}
121122

123+
mlir::Value ComplexExprEmitter::VisitArraySubscriptExpr(Expr *e) {
124+
return emitLoadOfLValue(e);
125+
}
126+
122127
mlir::Value ComplexExprEmitter::VisitBinAssign(const BinaryOperator *e) {
123128
mlir::Value value;
124129
LValue lv = emitBinAssignLValue(e, value);

clang/test/CIR/CodeGen/complex.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,35 @@ void foo23(int _Complex a, int _Complex b) {
473473
// OGCG: %[[RESULT_IMAG_PT:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 1
474474
// OGCG: store i32 %[[B_REAL]], ptr %[[RESULT_REAL_PT]], align 4
475475
// OGCG: store i32 %[[B_IMAG]], ptr %[[RESULT_IMAG_PT]], align 4
476+
477+
void foo24() {
478+
int _Complex arr[2];
479+
int _Complex r = arr[1];
480+
}
481+
482+
// CIR: %[[ARR:.*]] = cir.alloca !cir.array<!cir.complex<!s32i> x 2>, !cir.ptr<!cir.array<!cir.complex<!s32i> x 2>>, ["arr"]
483+
// CIR: %[[RESULT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["r", init]
484+
// CIR: %[[IDX:.*]] = cir.const #cir.int<1> : !s32i
485+
// CIR: %[[ARR_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR]] : !cir.ptr<!cir.array<!cir.complex<!s32i> x 2>>), !cir.ptr<!cir.complex<!s32i>>
486+
// CIR: %[[RESULT_VAL:.*]] = cir.ptr_stride(%[[ARR_PTR]] : !cir.ptr<!cir.complex<!s32i>>, %[[IDX]] : !s32i), !cir.ptr<!cir.complex<!s32i>>
487+
// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[RESULT_VAL]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
488+
// CIR: cir.store{{.*}} %[[TMP]], %[[RESULT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
489+
490+
// LLVM: %[[ARR:.*]] = alloca [2 x { i32, i32 }], i64 1, align 16
491+
// LLVM: %[[RESULT:.*]] = alloca { i32, i32 }, i64 1, align 4
492+
// LLVM: %[[ARR_PTR:.*]] = getelementptr { i32, i32 }, ptr %[[ARR]], i32 0
493+
// LLVM: %[[RESULT_VAL:.*]] = getelementptr { i32, i32 }, ptr %[[ARR_PTR]], i64 1
494+
// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[RESULT_VAL]], align 8
495+
// LLVM: store { i32, i32 } %[[TMP]], ptr %[[RESULT]], align 4
496+
497+
// OGCG: %[[ARR:.*]] = alloca [2 x { i32, i32 }], align 16
498+
// OGCG: %[[RESULT:.*]] = alloca { i32, i32 }, align 4
499+
// OGCG: %[[ELEM_PTR:.*]] = getelementptr inbounds [2 x { i32, i32 }], ptr %[[ARR]], i64 0, i64 1
500+
// OGCG: %[[ELEM_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[ELEM_PTR]], i32 0, i32 0
501+
// OGCG: %[[ELEM_REAL:.*]] = load i32, ptr %[[ELEM_REAL_PTR]]
502+
// OGCG: %[[ELEM_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[ELEM_PTR]], i32 0, i32 1
503+
// OGCG: %[[ELEM_IMAG:.*]] = load i32, ptr %[[ELEM_IMAG_PTR]]
504+
// OGCG: %[[RESULT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 0
505+
// OGCG: %[[RESULT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 1
506+
// OGCG: store i32 %[[ELEM_REAL]], ptr %[[RESULT_REAL_PTR]], align 4
507+
// OGCG: store i32 %[[ELEM_IMAG]], ptr %[[RESULT_IMAG_PTR]], align 4

0 commit comments

Comments
 (0)