[clang] [clang][CodeGen] Fix invalid bitcast in InitCatchParam for non-zero address space pointers (PR #224680)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 18 10:02:05 PDT 2026
https://github.com/im-lunex updated https://github.com/llvm/llvm-project/pull/224680
>From 1f39fd518ea82aef0f20086329151e975b4c6464 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 18 Sep 2026 23:01:46 +0600
Subject: [PATCH] add test for the rest of the cases
---
clang/lib/CodeGen/ItaniumCXXABI.cpp | 11 ++++---
.../CodeGenCXX/exception-addrspace-ms.cpp | 31 +++++++++++++++++++
clang/test/CodeGenCXX/exceptions.cpp | 12 +++++++
3 files changed, 49 insertions(+), 5 deletions(-)
create mode 100644 clang/test/CodeGenCXX/exception-addrspace-ms.cpp
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index c17813140b10f..5c3832d915ac0 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -5032,7 +5032,8 @@ static void InitCatchParam(CodeGenFunction &CGF,
// Create the temporary and write the adjusted pointer into it.
Address ExnPtrTmp =
CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
- llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
+ llvm::Value *Casted =
+ CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(AdjustedExn, PtrTy);
CGF.Builder.CreateStore(Casted, ExnPtrTmp);
// Bind the reference to the temporary.
@@ -5040,8 +5041,8 @@ static void InitCatchParam(CodeGenFunction &CGF,
}
}
- llvm::Value *ExnCast =
- CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
+ llvm::Value *ExnCast = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
+ AdjustedExn, LLVMCatchTy, "exn.byref");
CGF.Builder.CreateStore(ExnCast, ParamAddr);
return;
}
@@ -5054,8 +5055,8 @@ static void InitCatchParam(CodeGenFunction &CGF,
// If the catch type is a pointer type, __cxa_begin_catch returns
// the pointer by value.
if (CatchType->hasPointerRepresentation()) {
- llvm::Value *CastExn =
- CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
+ llvm::Value *CastExn = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
+ AdjustedExn, LLVMCatchTy, "exn.casted");
switch (CatchType.getQualifiers().getObjCLifetime()) {
case Qualifiers::OCL_Strong:
diff --git a/clang/test/CodeGenCXX/exception-addrspace-ms.cpp b/clang/test/CodeGenCXX/exception-addrspace-ms.cpp
new file mode 100644
index 0000000000000..0130abe32e977
--- /dev/null
+++ b/clang/test/CodeGenCXX/exception-addrspace-ms.cpp
@@ -0,0 +1,31 @@
+// Test for issue - (https://github.com/llvm/llvm-project/issues/222931)
+// RUN: %clang_cc1 -no-enable-noundef-analysis %s -triple=x86_64-linux-gnu -emit-llvm -std=c++11 -o - -fcxx-exceptions -fexceptions -fms-extensions | FileCheck -check-prefix=CHECK-PTR32 %s
+// RUN: %clang_cc1 -no-enable-noundef-analysis %s -triple=x86_64-linux-gnu -emit-llvm -std=c++11 -o - -fcxx-exceptions -fexceptions -fms-extensions | FileCheck -check-prefix=CHECK-AS270-BYREF %s
+// RUN: %clang_cc1 -no-enable-noundef-analysis %s -triple=x86_64-linux-gnu -emit-llvm -std=c++11 -o - -fcxx-exceptions -fexceptions -fms-extensions | FileCheck -check-prefix=CHECK-AS270-REF %s
+
+void sink_ptr32(int);
+
+void test_ptr32() {
+ try { throw (int * __ptr32)0; } catch (int * __ptr32 p) { sink_ptr32(*p); }
+}
+
+// CHECK-PTR32: %exn.casted = addrspacecast ptr %{{.*}} to ptr addrspace(270)
+
+typedef int __attribute__((address_space(270))) as270_int;
+void sink_as270(as270_int &);
+
+void test_byref() {
+ try { throw (as270_int)0; } catch (as270_int &p) { sink_as270(p); }
+}
+
+// CHECK-AS270-BYREF: %exn.byref = addrspacecast ptr {{.*}} to ptr addrspace(270)
+
+struct S2 {};
+typedef S2 __attribute__((address_space(270))) *PtrRec;
+void sink_ptrrec(PtrRec &);
+
+void test_ptrrec() {
+ try { throw (PtrRec)0; } catch (PtrRec &p) { sink_ptrrec(p); }
+}
+
+// CHECK-AS270-REF: store ptr addrspace(270) %{{.*}}, ptr %exn.byref.tmp, align 8
\ No newline at end of file
diff --git a/clang/test/CodeGenCXX/exceptions.cpp b/clang/test/CodeGenCXX/exceptions.cpp
index 6c920d7186709..7986aefb1eafd 100644
--- a/clang/test/CodeGenCXX/exceptions.cpp
+++ b/clang/test/CodeGenCXX/exceptions.cpp
@@ -623,3 +623,15 @@ void test(int c) {
}
// CHECK98: attributes [[NI_NR_NUW]] = { noinline noreturn nounwind {{.*}} }
+
+// Test for issue - (https://github.com/llvm/llvm-project/issues/222931)
+// RUN: %clang_cc1 -no-enable-noundef-analysis %s -triple=x86_64-linux-gnu -emit-llvm -std=c++11 -o - -fcxx-exceptions -fexceptions | FileCheck -check-prefix=CHECK-AS1 %s
+
+typedef int __attribute__((address_space(1))) *as1_int_ptr;
+void sink_as1(int);
+
+void test_as1() {
+ try { throw (as1_int_ptr)0; } catch (as1_int_ptr p) { sink_as1(*p); }
+}
+
+// CHECK-AS1: %exn.casted = addrspacecast ptr %{{.*}} to ptr addrspace(1)
More information about the cfe-commits
mailing list