[clang] [clang][CodeGen] Fix casting 'AddrLabelExpr' to a differernt addrspace (PR #237887)
Ben Shi via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 2 01:28:10 PST 2026
https://github.com/benshi001 created https://github.com/llvm/llvm-project/pull/237887
fixes https://github.com/llvm/llvm-project/issues/236067
>From ef7e75625befa01099c12ad487b498a509d6e73b 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] [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 e769ad6a24e6bf..9354edd7eb841d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1221,6 +1221,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
type. (#GH88592)
- 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
More information about the cfe-commits
mailing list