[clang] [clang][CodeGen] Fix casting 'AddrLabelExpr' to a differernt addrspace (PR #237887)
Ben Shi via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 19:54:31 PST 2026
https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/237887
>From 5ef5710cf24c879a47c7de03c0abc608113d351f Mon Sep 17 00:00:00 2001
From: Ben Shi <2283975856 at qq.com>
Date: Mon, 2 Nov 2026 17:01:10 +0800
Subject: [PATCH 1/2] [clang][CodeGen] Fix casting 'AddrLabelExpr' to a
differernt addrspace
fixes https://github.com/llvm/llvm-project/issues/236067
---
clang/docs/ReleaseNotes.md | 2 ++
clang/lib/CodeGen/CGExprScalar.cpp | 4 +++-
clang/test/CodeGen/avr/issue-236067.c | 10 ++++++++++
3 files changed, 15 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/avr/issue-236067.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..bd018aad85f224 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1250,6 +1250,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
subexpression, e.g. due to unclosed parenthesis. (#GH226555)
- Fixed an assertion when emitting base destructor as alias on targets (such
as AVR) which have different address spaces for code and data. (#GH236285)
+- Fixed an assertion when casting 'AddrLabelExpr' on targets (such as AVR)
+ which which have different address spaces for code and data. (#GH236067)
### OpenACC Specific Changes
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 0e2e29f0745d27..fe2a9bfd002e12 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -582,7 +582,9 @@ class ScalarExprEmitter
Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
- return Builder.CreateBitCast(V, ConvertType(E->getType()));
+ // For some targets (such as AVR), code and data have different addrspaces.
+ return Builder.CreatePointerBitCastOrAddrSpaceCast(
+ V, ConvertType(E->getType()));
}
Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
diff --git a/clang/test/CodeGen/avr/issue-236067.c b/clang/test/CodeGen/avr/issue-236067.c
new file mode 100644
index 00000000000000..78c6b798273d46
--- /dev/null
+++ b/clang/test/CodeGen/avr/issue-236067.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple avr -emit-llvm < %s | FileCheck %s
+
+// This test case verifies https://github.com/llvm/llvm-project/issues/236067 .
+
+void foo() {
+ void *bar = &&label;
+label:
+}
+
+// CHECK: store ptr addrspacecast (ptr addrspace(1) blockaddress(@foo, %label) to ptr), ptr %bar, align 1
>From d043ca0168526008970453fa0749950c6ffeb03d Mon Sep 17 00:00:00 2001
From: Ben Shi <2283975856 at qq.com>
Date: Wed, 4 Nov 2026 11:39:26 +0800
Subject: [PATCH 2/2] qe
---
clang/lib/CodeGen/CGExprScalar.cpp | 9 ++++++---
clang/test/CodeGen/avr/issue-236067.c | 19 ++++++++++++++-----
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index fe2a9bfd002e12..320a06e854e860 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -581,10 +581,13 @@ class ScalarExprEmitter
Value *VisitOffsetOfExpr(OffsetOfExpr *E);
Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
- llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
// For some targets (such as AVR), code and data have different addrspaces.
- return Builder.CreatePointerBitCastOrAddrSpaceCast(
- V, ConvertType(E->getType()));
+ llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
+ llvm::PointerType *DstTy =
+ cast<llvm::PointerType>(ConvertType(E->getType()));
+ if (V->getType()->getPointerAddressSpace() != DstTy->getAddressSpace())
+ return Builder.CreateAddrSpaceCast(V, DstTy);
+ return V;
}
Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
diff --git a/clang/test/CodeGen/avr/issue-236067.c b/clang/test/CodeGen/avr/issue-236067.c
index 78c6b798273d46..d6629b5ee40754 100644
--- a/clang/test/CodeGen/avr/issue-236067.c
+++ b/clang/test/CodeGen/avr/issue-236067.c
@@ -1,10 +1,19 @@
-// RUN: %clang_cc1 -triple avr -emit-llvm < %s | FileCheck %s
+// RUN: %clang_cc1 -triple avr -emit-llvm %s -o - | FileCheck %s
// This test case verifies https://github.com/llvm/llvm-project/issues/236067 .
-void foo() {
- void *bar = &&label;
-label:
+extern void foo_0(void);
+extern void foo_1(void);
+
+void foo(int a) {
+ void *p = &&label_0;
+ if (a > 0)
+ goto *p;
+ foo_0();
+ return;
+label_0:
+ foo_1();
}
-// CHECK: store ptr addrspacecast (ptr addrspace(1) blockaddress(@foo, %label) to ptr), ptr %bar, align 1
+// CHECK: store ptr addrspacecast (ptr addrspace(1) blockaddress(@foo, %label_0) to ptr), ptr %p, align 1
+// CHECK: indirectbr ptr {{.*}} [label %label_0]
More information about the cfe-commits
mailing list