From 0357e5f3e1504410cf3f53207af77890afb0919b Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Sun, 6 Jul 2025 15:11:22 +0200 Subject: [PATCH] [CIR] Backport ArraySubscript for ComplexType --- clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp | 2 +- clang/test/CIR/CodeGen/complex.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp index e519e4c5d440..5c0af383f9f6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp @@ -108,7 +108,7 @@ class ComplexExprEmitter : public StmtVisitor { mlir::Value VisitObjCMessageExpr(ObjCMessageExpr *E) { llvm_unreachable("NYI"); } - mlir::Value VisitArraySubscriptExpr(Expr *E) { llvm_unreachable("NYI"); } + mlir::Value VisitArraySubscriptExpr(Expr *E) { return emitLoadOfLValue(E); } mlir::Value VisitMemberExpr(MemberExpr *ME) { llvm_unreachable("NYI"); } mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *E) { llvm_unreachable("NYI"); diff --git a/clang/test/CIR/CodeGen/complex.c b/clang/test/CIR/CodeGen/complex.c index f3a2c6d8573a..b4b6ef24fb39 100644 --- a/clang/test/CIR/CodeGen/complex.c +++ b/clang/test/CIR/CodeGen/complex.c @@ -398,3 +398,23 @@ int extract_imag_and_add(int _Complex a, int _Complex b) { void complex_with_empty_init() { int _Complex c = {}; } // CHECK: {{.*}} = cir.const #cir.complex<#cir.int<0> : !s32i, #cir.int<0> : !s32i> : !cir.complex + +void complex_array_subscript() { + int _Complex arr[2]; + int _Complex r = arr[1]; +} + +// CHECK: %[[ARR:.*]] = cir.alloca !cir.array x 2>, !cir.ptr x 2>>, ["arr"] +// CHECK: %[[RESULT:.*]] = cir.alloca !cir.complex, !cir.ptr>, ["r", init] +// CHECK: %[[IDX:.*]] = cir.const #cir.int<1> : !s32i +// CHECK: %[[ARR_PTR:.*]] = cir.cast(array_to_ptrdecay, %[[ARR]] : !cir.ptr x 2>>), !cir.ptr> +// CHECK: %[[RESULT_VAL:.*]] = cir.ptr_stride(%[[ARR_PTR]] : !cir.ptr>, %[[IDX]] : !s32i), !cir.ptr> +// CHECK: %[[TMP:.*]] = cir.load{{.*}} %[[RESULT_VAL]] : !cir.ptr>, !cir.complex +// CHECK: cir.store{{.*}} %[[TMP]], %[[RESULT]] : !cir.complex, !cir.ptr> + +// LLVM: %[[ARR:.*]] = alloca [2 x { i32, i32 }], i64 1, align 16 +// LLVM: %[[RESULT:.*]] = alloca { i32, i32 }, i64 1, align 4 +// LLVM: %[[ARR_PTR:.*]] = getelementptr { i32, i32 }, ptr %[[ARR]], i32 0 +// LLVM: %[[RESULT_VAL:.*]] = getelementptr { i32, i32 }, ptr %[[ARR_PTR]], i64 1 +// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[RESULT_VAL]], align 8 +// LLVM: store { i32, i32 } %[[TMP]], ptr %[[RESULT]], align 4