[clang] [CIR] Support CXXAddrspaceCastExpr in CIRGenFunction::emitLValue (PR #237423)

Aman Maurya via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 23:30:11 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/237423

>From eb411d813aea90d18f6e375df9d63212116b31fb Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 31 Oct 2026 04:01:50 +0530
Subject: [PATCH 1/2] [CIR] Support CXXAddrspaceCastExpr in
 CIRGenFunction::emitLValue

Handle CXXAddrspaceCastExpr alongside other CastExpr subclasses in CIRGenFunction::emitLValue by delegating to emitCastLValue.

Fixes #237405
---
 clang/lib/CIR/CodeGen/CIRGenFunction.cpp      |  4 ++--
 .../CIR/CodeGenOpenCL/addrspace-cast.clcpp    | 23 +++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp

diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 409a94b9acff8f..d91214bb0cfb0f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -1362,13 +1362,13 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
   case Expr::CXXDynamicCastExprClass:
   case Expr::CXXReinterpretCastExprClass:
   case Expr::CXXConstCastExprClass:
-    return emitCastLValue(cast<CastExpr>(e));
   case Expr::CXXAddrspaceCastExprClass:
+    return emitCastLValue(cast<CastExpr>(e));
   case Expr::ObjCBridgedCastExprClass:
     // TODO(cir): These can just be moved into the cast handling above, but
     //            they need test cases.
     getCIRGenModule().errorNYI(e->getSourceRange(),
-                               "emitLValue: addrspace or ObjC bridged cast");
+                               "emitLValue: ObjC bridged cast");
     return LValue();
   case Expr::MaterializeTemporaryExprClass:
     return emitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(e));
diff --git a/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp b/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp
new file mode 100644
index 00000000000000..d67ecb4979da99
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -emit-llvm -O0 %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct S {
+  int x;
+};
+
+// CIR-LABEL: cir.func {{.*}} @_Z3barPU3AS11S
+// OGCG-LABEL: define {{.*}} @_Z3barPU3AS11S
+void bar(__global S *s) {
+  // CIR: %[[LOAD:.*]] = cir.load align(8) %{{.*}} : !cir.ptr<!cir.ptr<!rec_S, target_address_space(1)>>, !cir.ptr<!rec_S, target_address_space(1)>
+  // CIR: %[[CAST:.*]] = cir.cast address_space %[[LOAD]] : !cir.ptr<!rec_S, target_address_space(1)> -> !cir.ptr<!rec_S, target_address_space(4)>
+  // CIR: %[[MEM:.*]] = cir.get_member %[[CAST]][0] {name = "x"} : !cir.ptr<!rec_S, target_address_space(4)> -> !cir.ptr<!s32i, target_address_space(4)>
+  // CIR: cir.store align(4) %{{.*}}, %[[MEM]] : !s32i, !cir.ptr<!s32i, target_address_space(4)>
+
+  // OGCG: %[[LOAD_OGCG:.*]] = load ptr addrspace(1), ptr %{{.*}}, align 8
+  // OGCG: %[[CAST_OGCG:.*]] = addrspacecast ptr addrspace(1) %[[LOAD_OGCG]] to ptr addrspace(4)
+  // OGCG: %[[MEM_OGCG:.*]] = getelementptr inbounds nuw %struct.S, ptr addrspace(4) %[[CAST_OGCG]], i32 0, i32 0
+  // OGCG: store i32 1, ptr addrspace(4) %[[MEM_OGCG]], align 4
+  addrspace_cast<__generic S *>(s)->x = 1;
+}

>From a8e3274bd167f69e8f015d3dec14eacf9ac44a43 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 7 Nov 2026 12:59:39 +0530
Subject: [PATCH 2/2] [CIR] Move CXXAddrspaceCastExpr test to
 CodeGenOpenCLCXX/addrspace_cast.clcpp

---
 .../CIR/CodeGenOpenCL/addrspace-cast.clcpp    | 23 -------------------
 .../CodeGenOpenCLCXX/addrspace_cast.clcpp     | 21 ++++++++++++++---
 2 files changed, 18 insertions(+), 26 deletions(-)
 delete mode 100644 clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp

diff --git a/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp b/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp
deleted file mode 100644
index d67ecb4979da99..00000000000000
--- a/clang/test/CIR/CodeGenOpenCL/addrspace-cast.clcpp
+++ /dev/null
@@ -1,23 +0,0 @@
-// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -fclangir -emit-cir %s -o %t.cir
-// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
-// RUN: %clang_cc1 -x clcpp -triple spir64 -cl-std=clc++ -emit-llvm -O0 %s -o %t.ll
-// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
-
-struct S {
-  int x;
-};
-
-// CIR-LABEL: cir.func {{.*}} @_Z3barPU3AS11S
-// OGCG-LABEL: define {{.*}} @_Z3barPU3AS11S
-void bar(__global S *s) {
-  // CIR: %[[LOAD:.*]] = cir.load align(8) %{{.*}} : !cir.ptr<!cir.ptr<!rec_S, target_address_space(1)>>, !cir.ptr<!rec_S, target_address_space(1)>
-  // CIR: %[[CAST:.*]] = cir.cast address_space %[[LOAD]] : !cir.ptr<!rec_S, target_address_space(1)> -> !cir.ptr<!rec_S, target_address_space(4)>
-  // CIR: %[[MEM:.*]] = cir.get_member %[[CAST]][0] {name = "x"} : !cir.ptr<!rec_S, target_address_space(4)> -> !cir.ptr<!s32i, target_address_space(4)>
-  // CIR: cir.store align(4) %{{.*}}, %[[MEM]] : !s32i, !cir.ptr<!s32i, target_address_space(4)>
-
-  // OGCG: %[[LOAD_OGCG:.*]] = load ptr addrspace(1), ptr %{{.*}}, align 8
-  // OGCG: %[[CAST_OGCG:.*]] = addrspacecast ptr addrspace(1) %[[LOAD_OGCG]] to ptr addrspace(4)
-  // OGCG: %[[MEM_OGCG:.*]] = getelementptr inbounds nuw %struct.S, ptr addrspace(4) %[[CAST_OGCG]], i32 0, i32 0
-  // OGCG: store i32 1, ptr addrspace(4) %[[MEM_OGCG]], align 4
-  addrspace_cast<__generic S *>(s)->x = 1;
-}
diff --git a/clang/test/CodeGenOpenCLCXX/addrspace_cast.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace_cast.clcpp
index 44e4e0f356f9b7..f7b6d092abd5b7 100644
--- a/clang/test/CodeGenOpenCLCXX/addrspace_cast.clcpp
+++ b/clang/test/CodeGenOpenCLCXX/addrspace_cast.clcpp
@@ -1,7 +1,22 @@
-//RUN: %clang_cc1 %s -triple spir -emit-llvm -O0 -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple spir -emit-llvm -O0 -o - | FileCheck %s --check-prefix=OGCG
+// RUN: %if cir-enabled %{ %clang_cc1 %s -triple spir -fclangir -emit-cir -o - | FileCheck %s --check-prefix=CIR %}
 
-//CHECK-LABEL: define{{.*}} spir_func void @_Z3barPU3AS1i
+// OGCG-LABEL: define{{.*}} spir_func void @_Z3barPU3AS1i
+// CIR-LABEL: cir.func {{.*}} @_Z3barPU3AS1i
 void bar(global int *gl) {
-  //CHECK: addrspacecast ptr addrspace(1) %{{[0-9]+}} to ptr addrspace(4)
+  // OGCG: addrspacecast ptr addrspace(1) %{{[0-9]+}} to ptr addrspace(4)
+  // CIR: %{{.*}} = cir.cast address_space %{{.*}} : !cir.ptr<!s32i, target_address_space(1)> -> !cir.ptr<!s32i, target_address_space(4)>
   int *gen = addrspace_cast<int *>(gl);
 }
+
+struct S {
+  int x;
+};
+
+// OGCG-LABEL: define{{.*}} spir_func void @_Z4bar2PU3AS11S
+// CIR-LABEL: cir.func {{.*}} @_Z4bar2PU3AS11S
+void bar2(global S *s) {
+  // OGCG: addrspacecast ptr addrspace(1) %{{.*}} to ptr addrspace(4)
+  // CIR: %{{.*}} = cir.cast address_space %{{.*}} : !cir.ptr<!rec_S, target_address_space(1)> -> !cir.ptr<!rec_S, target_address_space(4)>
+  addrspace_cast<S *>(s)->x = 1;
+}



More information about the cfe-commits mailing list