[clang] [CIR] Fix array-to-pointer decay (PR #230276)
Kunal Dubey via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 8 21:45:29 PDT 2026
https://github.com/xakep8 updated https://github.com/llvm/llvm-project/pull/230276
>From b9476f8184d06071077e6f3d348bb8c45d0cff5b Mon Sep 17 00:00:00 2001
From: Kunal Dubey <xakep8 at protonmail.com>
Date: Fri, 9 Oct 2026 02:12:45 +0530
Subject: [PATCH 1/2] [CIR] Fix array-to-pointer decay
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 9 ++----
.../CIR/CodeGen/array-to-pointer-decay.cpp | 30 +++++++++++++++++++
2 files changed, 32 insertions(+), 7 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/array-to-pointer-decay.cpp
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index c03f7b355b6959..a58acbb3a9821a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2603,17 +2603,12 @@ Address CIRGenFunction::emitArrayToPointerDecay(const Expr *e,
// If the array type was an incomplete type, we need to make sure
// the decay ends up being the right type.
- auto lvalueAddrTy = mlir::cast<cir::PointerType>(addr.getPointer().getType());
-
if (e->getType()->isVariableArrayType())
return addr;
- [[maybe_unused]] auto pointeeTy =
- mlir::cast<cir::ArrayType>(lvalueAddrTy.getPointee());
-
- [[maybe_unused]] mlir::Type arrayTy = convertType(e->getType());
+ mlir::Type arrayTy = convertType(e->getType());
assert(mlir::isa<cir::ArrayType>(arrayTy) && "expected array");
- assert(pointeeTy == arrayTy);
+ addr = addr.withElementType(builder, arrayTy);
// The result of this decay conversion points to an array element within the
// base lvalue. However, since TBAA currently does not support representing
diff --git a/clang/test/CIR/CodeGen/array-to-pointer-decay.cpp b/clang/test/CIR/CodeGen/array-to-pointer-decay.cpp
new file mode 100644
index 00000000000000..11d1b0adc298f7
--- /dev/null
+++ b/clang/test/CIR/CodeGen/array-to-pointer-decay.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
+
+template <class T> void consume(T);
+
+void incomplete_array_temporary(long x) {
+ consume((int[])(char)x);
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z26incomplete_array_temporaryl
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!cir.array<!s32i x 1>>
+// CIR: %[[INCOMPLETE:.*]] = cir.cast bitcast %[[TMP]] : !cir.ptr<!cir.array<!s32i x 1>> -> !cir.ptr<!cir.array<!s32i x 0>>
+// CIR-NEXT: %[[DECAY:.*]] = cir.cast array_to_ptrdecay %[[INCOMPLETE]] : !cir.ptr<!cir.array<!s32i x 0>> -> !cir.ptr<!s32i>
+// CIR-NEXT: cir.call @{{.*}}(%[[DECAY]])
+
+// LLVM-LABEL: define {{.*}} void @_Z26incomplete_array_temporaryl
+// LLVM: %[[TMP:.*]] = alloca [1 x i32], align 4
+// LLVM: store i32 {{.*}}, ptr {{.*}}, align 4
+// LLVM-NEXT: %[[DECAY:.*]] = getelementptr i32, ptr %[[TMP]], i32 0
+// LLVM-NEXT: call void @{{.*}}(ptr noundef %[[DECAY]])
+
+// OGCG-LABEL: define {{.*}} void @_Z26incomplete_array_temporaryl
+// OGCG: %[[TMP:.*]] = alloca [1 x i32], align 4
+// OGCG: store i32 {{.*}}, ptr %[[TMP]], align 4
+// OGCG-NEXT: %[[DECAY:.*]] = getelementptr inbounds [0 x i32], ptr %[[TMP]], i64 0, i64 0
+// OGCG-NEXT: call void @{{.*}}(ptr noundef %[[DECAY]])
>From c81796950657239f3d5f1fa02168cbfef0c12cb5 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <xakep8 at protonmail.com>
Date: Fri, 9 Oct 2026 10:14:54 +0530
Subject: [PATCH 2/2] [CIR] Fixed the comment placement
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a58acbb3a9821a..e152e75b5eacc1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2601,11 +2601,11 @@ Address CIRGenFunction::emitArrayToPointerDecay(const Expr *e,
LValue lv = emitLValue(e);
Address addr = lv.getAddress();
- // If the array type was an incomplete type, we need to make sure
- // the decay ends up being the right type.
if (e->getType()->isVariableArrayType())
return addr;
+ // If the array type was an incomplete type, we need to make sure
+ // the decay ends up being the right type.
mlir::Type arrayTy = convertType(e->getType());
assert(mlir::isa<cir::ArrayType>(arrayTy) && "expected array");
addr = addr.withElementType(builder, arrayTy);
More information about the cfe-commits
mailing list