[clang] Port some CodeGenSYCL tests from clang to CIR CodeGen (PR #236512)

Qi Ye via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 28 10:57:54 PDT 2026


https://github.com/Qi-Ye-079 updated https://github.com/llvm/llvm-project/pull/236512

>From 9c6e07dce42c3c7a5701bbee8e2c05a8a42a34ea Mon Sep 17 00:00:00 2001
From: "Ye, Qi" <qi.ye at intel.com>
Date: Wed, 28 Oct 2026 09:53:21 -0700
Subject: [PATCH 1/3] Port some SYCL tests from clang CodeGen to CIR CodeGen

---
 clang/test/CIR/CodeGenSYCL/filescope_asm.cpp  |  12 ++
 clang/test/CIR/CodeGenSYCL/function-attrs.cpp |  69 ++++++++
 .../sycl-kernel-entry-point-exceptions.cpp    | 163 ++++++++++++++++++
 .../unique_stable_name_windows_diff.cpp       |  71 ++++++++
 4 files changed, 315 insertions(+)
 create mode 100644 clang/test/CIR/CodeGenSYCL/filescope_asm.cpp
 create mode 100644 clang/test/CIR/CodeGenSYCL/function-attrs.cpp
 create mode 100644 clang/test/CIR/CodeGenSYCL/sycl-kernel-entry-point-exceptions.cpp
 create mode 100644 clang/test/CIR/CodeGenSYCL/unique_stable_name_windows_diff.cpp

diff --git a/clang/test/CIR/CodeGenSYCL/filescope_asm.cpp b/clang/test/CIR/CodeGenSYCL/filescope_asm.cpp
new file mode 100644
index 00000000000000..d21554001752e2
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/filescope_asm.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+// Check that file-scope asm is ignored during device-side SYCL compilation.
+
+// CIR: cir.module_asm = []
+// LLVM-NOT: module asm "foo"
+__asm__("foo");
diff --git a/clang/test/CIR/CodeGenSYCL/function-attrs.cpp b/clang/test/CIR/CodeGenSYCL/function-attrs.cpp
new file mode 100644
index 00000000000000..aae30ba65bb30f
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/function-attrs.cpp
@@ -0,0 +1,69 @@
+// RUN: %clang_cc1 -fsycl-is-device -disable-llvm-passes -triple spir64 -fexceptions -fno-ident -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -fsycl-is-device -disable-llvm-passes -triple spir64 -fexceptions -fno-ident -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -fsycl-is-device -disable-llvm-passes -triple spir64 -fexceptions -fno-ident -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+int foo();
+
+// CIR:      cir.func {{.*}}@_Z3barv() cc(spir_function) attributes {convergent, mustprogress, norecurse, nothrow, nounwind, optnone} {
+// CIR-NEXT:   %[[A:.*]] = cir.alloca "a" align(4) init : !cir.ptr<!s32i>
+// CIR-NEXT:   %[[A_ASCAST:.*]] = cir.cast address_space %[[A]] : !cir.ptr<!s32i> -> !cir.ptr<!s32i, target_address_space(4)>
+// CIR-NEXT:   %[[CALL:.*]] = cir.call @_Z3foov() cc(spir_function) nothrow nounwind {convergent} : () -> (!s32i {llvm.noundef})
+// CIR-NEXT:   cir.store align(4) %[[CALL]], %[[A_ASCAST]] : !s32i, !cir.ptr<!s32i, target_address_space(4)>
+// CIR-NEXT:   cir.return
+// CIR-NEXT: }
+
+// LLVM:      define dso_local spir_func void @_Z3barv() #[[ATTR0:[0-9]+]] {
+// OGCG-NEXT: entry:
+// LLVM-NEXT:   %[[A:.*]] = alloca i32, align 4
+// LLVM-NEXT:   %[[A_ASCAST:.*]] = addrspacecast ptr %[[A]] to ptr addrspace(4)
+// LLVM-NEXT:   %[[CALL:.*]] = call spir_func noundef i32 @_Z3foov() #[[ATTR1:[0-9]+]]
+// LLVM-NEXT:   store i32 %[[CALL]], ptr addrspace(4) %[[A_ASCAST]], align 4
+// LLVM-NEXT:   ret void
+// LLVM-NEXT: }
+void bar() {
+  int a = foo();
+}
+
+// CIR:      cir.func {{.*}}@_Z3foov() -> (!s32i {llvm.noundef}) cc(spir_function) attributes {convergent, mustprogress, norecurse, nothrow, nounwind, optnone} {
+// CIR-NEXT:   %[[RETVAL:.*]] = cir.alloca "__retval" align(4) : !cir.ptr<!s32i>
+// CIR-NEXT:   %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-NEXT:   cir.store %[[ONE]], %[[RETVAL]] : !s32i, !cir.ptr<!s32i>
+// CIR-NEXT:   %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!s32i>, !s32i
+// CIR-NEXT:   cir.return %[[RET]] : !s32i
+// CIR-NEXT: }
+
+// ClangIR returns through a __retval slot, while classic CodeGen folds the
+// single return into the entry block.
+// LLVM:          define dso_local spir_func noundef i32 @_Z3foov() #[[ATTR0]] {
+// LLVM-CIR-NEXT:   %[[RETVAL:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:   store i32 1, ptr %[[RETVAL]], align 4
+// LLVM-CIR-NEXT:   %[[RET:.*]] = load i32, ptr %[[RETVAL]], align 4
+// LLVM-CIR-NEXT:   ret i32 %[[RET]]
+// OGCG-NEXT:     entry:
+// OGCG-NEXT:       ret i32 1
+// LLVM-NEXT:     }
+int foo() {
+  return 1;
+}
+
+template <typename Name, typename... Ts>
+void sycl_kernel_launch(Ts...) {}
+
+template <typename Name, typename Func>
+[[clang::sycl_kernel_entry_point(Name)]] void kernel_single_task(const Func &kernelFunc) {
+  kernelFunc();
+}
+
+int main() {
+  kernel_single_task<class fake_kernel>([] { bar(); });
+  return 0;
+}
+
+// ClangIR does not yet emit the "no-trapping-math" and
+// "stack-protector-buffer-size" default function attributes.
+// LLVM-CIR: attributes #[[ATTR0]] = { convergent mustprogress noinline norecurse nounwind optnone }
+// OGCG: attributes #[[ATTR0]] = { convergent mustprogress noinline norecurse nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// LLVM: attributes #[[ATTR1]] = { convergent nounwind }
diff --git a/clang/test/CIR/CodeGenSYCL/sycl-kernel-entry-point-exceptions.cpp b/clang/test/CIR/CodeGenSYCL/sycl-kernel-entry-point-exceptions.cpp
new file mode 100644
index 00000000000000..3e8cf27cb213db
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/sycl-kernel-entry-point-exceptions.cpp
@@ -0,0 +1,163 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++17 -fcxx-exceptions -fexceptions -fsycl-is-host -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++17 -fcxx-exceptions -fexceptions -fsycl-is-host -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++17 -fcxx-exceptions -fexceptions -fsycl-is-host -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Validate generation of exception handling code for functions declared
+// with the sycl_kernel_entry_point attribute that implicitly call a
+// sycl_kernel_launch function that may throw an exception. Exception
+// handling is not relevant for the generated offload kernel entry point
+// function, so device compilation is intentionally not exercised.
+
+// A unique kernel name type is required for each declared kernel entry point.
+template<int> struct KN;
+
+// A generic kernel object type.
+template<int, int = 0>
+struct KT {
+  void operator()() const;
+};
+
+
+// Validate that exception handling instructions are omitted when a
+// potentially throwing sycl_kernel_entry_point attributed function
+// calls a potentially throwing sycl_kernel_launch function (a thrown
+// exception will propagate with no explicit handling required).
+namespace ns1 {
+  template<typename KN, typename... Ts>
+  void sycl_kernel_launch(const char *, Ts...);
+  [[clang::sycl_kernel_entry_point(KN<1>)]]
+  void skep(KT<1> k) {
+    k();
+  }
+}
+// CIR: cir.func {{.*}}@_ZN3ns14skepE2KTILi1ELi0EE() attributes {{{.*}}mustprogress, optnone} {
+// CIR-NOT: cir.try
+// CIR: cir.call @_ZN3ns118sycl_kernel_launchI2KNILi1EEJ2KTILi1ELi0EEEEEvPKcDpT0_(%{{.*}}) : (
+// CIR: cir.return
+
+// LLVM: ; Function Attrs: mustprogress noinline optnone
+// LLVM-NEXT: define dso_local void @_ZN3ns14skepE2KTILi1ELi0EE() #{{[0-9]+}} {
+// LLVM-NOT: invoke
+// LLVM: call void @_ZN3ns118sycl_kernel_launchI2KNILi1EEJ2KTILi1ELi0EEEEEvPKcDpT0_(ptr noundef @.str)
+// LLVM: ret void
+
+
+// Validate that exception handling instructions are emitted when a
+// non-throwing sycl_kernel_entry_point attributed function calls
+// a potentially throwing sycl_kernel_launch function.
+namespace ns2 {
+  template<typename KN, typename... Ts>
+  void sycl_kernel_launch(const char *, Ts...);
+  [[clang::sycl_kernel_entry_point(KN<2>)]]
+  void skep(KT<2> k) noexcept {
+    k();
+  }
+}
+// CIR:      cir.func {{.*}}@_ZN3ns24skepE2KTILi2ELi0EE() personality(@__gxx_personality_v0) attributes {{{.*}}mustprogress, nothrow, optnone} {
+// CIR:        cir.try {
+// CIR:          %[[STR:.*]] = cir.get_global @".str.1" : !cir.ptr<!cir.array<!s8i x 14>>
+// CIR-NEXT:     %[[STR_PTR:.*]] = cir.cast array_to_ptrdecay %[[STR]] : !cir.ptr<!cir.array<!s8i x 14>> -> !cir.ptr<!s8i>
+// CIR:          cir.call @_ZN3ns218sycl_kernel_launchI2KNILi2EEJ2KTILi2ELi0EEEEEvPKcDpT0_(%[[STR_PTR]]) : (!cir.ptr<!s8i> {llvm.noundef}) -> ()
+// CIR-NEXT:     cir.yield
+// CIR-NEXT:   } catch all (%[[TOK:[a-z0-9]+]]: !cir.eh_token
+// CIR-NEXT:     cir.eh.terminate %[[TOK]] : !cir.eh_token
+// CIR-NEXT:   }
+// CIR-NEXT:   cir.return
+// CIR-NEXT: }
+
+// LLVM:      ; Function Attrs: mustprogress noinline nounwind optnone
+// LLVM-NEXT: define dso_local void @_ZN3ns24skepE2KTILi2ELi0EE() #{{[0-9]+}} personality ptr @__gxx_personality_v0 {
+// LLVM:        invoke void @_ZN3ns218sycl_kernel_launchI2KNILi2EEJ2KTILi2ELi0EEEEEvPKcDpT0_(ptr noundef @.str.1)
+// LLVM-NEXT:     to label %[[CONT:[a-z0-9.]+]] unwind label %[[LPAD:[a-z0-9.]+]]
+// LLVM-EMPTY:
+// LLVM-NEXT: {{^}}[[CONT]]:
+// OGCG-NEXT:   ret void
+// LLVM-CIR-NEXT: br label %[[RET:[0-9]+]]
+// LLVM-EMPTY:
+// LLVM-NEXT: {{^}}[[LPAD]]:
+// LLVM-NEXT:   %[[LP:.*]] = landingpad { ptr, i32 }
+// LLVM-NEXT:     catch ptr null
+// LLVM-NEXT:   %[[EXN:.*]] = extractvalue { ptr, i32 } %[[LP]], 0
+// OGCG-NEXT:   call void @__clang_call_terminate(ptr %[[EXN]]) #[[TERM_ATTR:[0-9]+]]
+// OGCG-NEXT:   unreachable
+// OGCG-NEXT: }
+
+// ClangIR forwards the exception to the terminate call through two extra
+// single-predecessor blocks and places the normal return block last.
+// LLVM-CIR-NEXT:   %[[SEL:.*]] = extractvalue { ptr, i32 } %[[LP]], 1
+// LLVM-CIR-NEXT:   br label %[[BB1:[0-9]+]]
+// LLVM-CIR-EMPTY:
+// LLVM-CIR-NEXT: {{^}}[[BB1]]:
+// LLVM-CIR-NEXT:   %[[EXN1:.*]] = phi ptr [ %[[EXN]], %[[LPAD]] ]
+// LLVM-CIR-NEXT:   %[[SEL1:.*]] = phi i32 [ %[[SEL]], %[[LPAD]] ]
+// LLVM-CIR-NEXT:   br label %[[BB2:[0-9]+]]
+// LLVM-CIR-EMPTY:
+// LLVM-CIR-NEXT: {{^}}[[BB2]]:
+// LLVM-CIR-NEXT:   %[[EXN2:.*]] = phi ptr [ %[[EXN1]], %[[BB1]] ]
+// LLVM-CIR-NEXT:   %{{.*}} = phi i32 [ %[[SEL1]], %[[BB1]] ]
+// LLVM-CIR-NEXT:   call void @__clang_call_terminate(ptr %[[EXN2]]) #[[TERM_ATTR:[0-9]+]]
+// LLVM-CIR-NEXT:   unreachable
+// LLVM-CIR-EMPTY:
+// LLVM-CIR-NEXT: {{^}}[[RET]]:
+// LLVM-CIR-NEXT:   ret void
+// LLVM-CIR-NEXT: }
+
+
+// Validate that exception handling instructions are omitted when a
+// potentially throwing sycl_kernel_entry_point attributed function
+// calls a non-throwing sycl_kernel_launch function (a thrown
+// exception will terminate within sycl_kernel_launch).
+namespace ns3 {
+  template<typename KN, typename... Ts>
+  void sycl_kernel_launch(const char *, Ts...) noexcept;
+  [[clang::sycl_kernel_entry_point(KN<3>)]]
+  void skep(KT<3> k) {
+    k();
+  }
+}
+// CIR: cir.func {{.*}}@_ZN3ns34skepE2KTILi3ELi0EE() attributes {{{.*}}mustprogress, optnone} {
+// CIR-NOT: cir.try
+// CIR: cir.call @_ZN3ns318sycl_kernel_launchI2KNILi3EEJ2KTILi3ELi0EEEEEvPKcDpT0_(%{{.*}}) nothrow : (
+// CIR: cir.return
+
+// ClangIR does not yet infer nounwind for functions that contain no
+// potentially throwing calls.
+// LLVM-CIR: ; Function Attrs: mustprogress noinline optnone
+// OGCG: ; Function Attrs: mustprogress noinline nounwind optnone
+// LLVM-NEXT: define dso_local void @_ZN3ns34skepE2KTILi3ELi0EE() #{{[0-9]+}} {
+// LLVM-NOT: invoke
+// LLVM: call void @_ZN3ns318sycl_kernel_launchI2KNILi3EEJ2KTILi3ELi0EEEEEvPKcDpT0_(ptr noundef @.str.2) #{{[0-9]+}}
+// LLVM: ret void
+
+
+// Validate that exception handling instructions are omitted when a
+// non-throwing sycl_kernel_entry_point attributed function calls a
+// non-throwing sycl_kernel_launch function.
+namespace ns4 {
+  template<typename KN, typename... Ts>
+  void sycl_kernel_launch(const char *, Ts...) noexcept;
+  [[clang::sycl_kernel_entry_point(KN<4>)]]
+  void skep(KT<4> k) noexcept {
+    k();
+  }
+}
+// ClangIR wraps the body of a noexcept function in a terminate scope even
+// when the only call it contains is nothrow.
+// CIR: cir.func {{.*}}@_ZN3ns44skepE2KTILi4ELi0EE() personality(@__gxx_personality_v0) attributes {{{.*}}mustprogress, nothrow, optnone} {
+// CIR: cir.try {
+// CIR: cir.call @_ZN3ns418sycl_kernel_launchI2KNILi4EEJ2KTILi4ELi0EEEEEvPKcDpT0_(%{{.*}}) nothrow : (
+// CIR: cir.eh.terminate
+
+// The terminate scope lowers to no EH instructions, but ClangIR still attaches
+// a personality function.
+// LLVM: ; Function Attrs: mustprogress noinline nounwind optnone
+// LLVM-CIR-NEXT: define dso_local void @_ZN3ns44skepE2KTILi4ELi0EE() #{{[0-9]+}} personality ptr @__gxx_personality_v0 {
+// OGCG-NEXT: define dso_local void @_ZN3ns44skepE2KTILi4ELi0EE() #{{[0-9]+}} {
+// LLVM-NOT: invoke
+// LLVM: call void @_ZN3ns418sycl_kernel_launchI2KNILi4EEJ2KTILi4ELi0EEEEEvPKcDpT0_(ptr noundef @.str.3) #{{[0-9]+}}
+// LLVM: ret void
+
+// LLVM: attributes #[[TERM_ATTR]] = { noreturn nounwind }
diff --git a/clang/test/CIR/CodeGenSYCL/unique_stable_name_windows_diff.cpp b/clang/test/CIR/CodeGenSYCL/unique_stable_name_windows_diff.cpp
new file mode 100644
index 00000000000000..96feb88b692457
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/unique_stable_name_windows_diff.cpp
@@ -0,0 +1,71 @@
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -aux-triple x86_64-pc-windows-msvc -fsycl-is-device -disable-llvm-passes -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -aux-triple x86_64-pc-windows-msvc -fsycl-is-device -disable-llvm-passes -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM-CIR
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -aux-triple x86_64-pc-windows-msvc -fsycl-is-device -disable-llvm-passes -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+// TODO(cir): Add the host RUN lines (-triple x86_64-pc-windows-msvc
+// -fsycl-is-host) once ClangIR implements Microsoft ABI string-literal
+// mangling.
+
+template<typename KN, typename... Ts>
+void sycl_kernel_launch(const char *, Ts...) {}
+
+template<typename KN, typename Func>
+[[clang::sycl_kernel_entry_point(KN)]] void kernel(Func F){
+  F();
+}
+
+template<typename KN, typename Func>
+[[clang::sycl_kernel_entry_point(KN)]] void kernel2(Func F){
+  F(1);
+}
+
+template<typename KN, typename Func>
+[[clang::sycl_kernel_entry_point(KN)]] void kernel3(Func F){
+  F(1.1);
+}
+
+int main() {
+  int i;
+  double d;
+  float f;
+  auto lambda1 = [](){};
+  auto lambda2 = [](int){};
+  auto lambda3 = [](double){};
+
+  kernel<class K1>(lambda1);
+  kernel2<class K2>(lambda2);
+  kernel3<class K3>(lambda3);
+
+  // Ensure the kernels are named the same between the device and host
+  // invocations.
+  // Call from host.
+  (void)__builtin_sycl_unique_stable_name(decltype(lambda1));
+  (void)__builtin_sycl_unique_stable_name(decltype(lambda2));
+  (void)__builtin_sycl_unique_stable_name(decltype(lambda3));
+
+  // Call from device.
+  auto lambda4 = [](){
+    (void)__builtin_sycl_unique_stable_name(decltype(lambda1));
+    (void)__builtin_sycl_unique_stable_name(decltype(lambda2));
+    (void)__builtin_sycl_unique_stable_name(decltype(lambda3));
+  };
+  kernel<class K4>(lambda4);
+
+  // Make sure the following 3 are the same between the host and device compile.
+  // Note that these are NOT the same value as each other, they differ by the
+  // signature.
+  // CIR: cir.global "private" constant cir_private dso_local target_address_space(1) @__usn_str = #cir.const_array<"_ZTSZ4mainEUlvE_" : !cir.array<!s8i x 16>, trailing_zeros> : !cir.array<!s8i x 17> align(1)
+  // CIR: cir.global "private" constant cir_private dso_local target_address_space(1) @__usn_str.1 = #cir.const_array<"_ZTSZ4mainEUliE_" : !cir.array<!s8i x 16>, trailing_zeros> : !cir.array<!s8i x 17> align(1)
+  // CIR: cir.global "private" constant cir_private dso_local target_address_space(1) @__usn_str.2 = #cir.const_array<"_ZTSZ4mainEUldE_" : !cir.array<!s8i x 16>, trailing_zeros> : !cir.array<!s8i x 17> align(1)
+
+  // ClangIR does not yet emit unnamed_addr on string literal globals.
+  // LLVM-CIR: @__usn_str = private addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUlvE_\00", align 1
+  // LLVM-CIR: @__usn_str.1 = private addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUliE_\00", align 1
+  // LLVM-CIR: @__usn_str.2 = private addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUldE_\00", align 1
+  // OGCG: @__usn_str = private unnamed_addr addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUlvE_\00", align 1
+  // OGCG: @__usn_str.1 = private unnamed_addr addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUliE_\00", align 1
+  // OGCG: @__usn_str.2 = private unnamed_addr addrspace(1) constant [17 x i8] c"_ZTSZ4mainEUldE_\00", align 1
+}

>From a072b922223fffa4eb11555ae97b95ac88d981bb Mon Sep 17 00:00:00 2001
From: "Ye, Qi" <qi.ye at intel.com>
Date: Wed, 28 Oct 2026 10:19:49 -0700
Subject: [PATCH 2/3] Fix nullptr assignment for AMD and CUDA local addrspace

---
 clang/lib/CIR/CodeGen/TargetInfo.cpp          |  15 +
 clang/lib/CIR/CodeGen/Targets/NVPTX.cpp       |  16 +
 .../amd-address-space-conversions.cpp         | 324 ++++++++++++++++++
 .../cuda-address-space-conversions.cpp        | 266 ++++++++++++++
 4 files changed, 621 insertions(+)
 create mode 100644 clang/test/CIR/CodeGenSYCL/amd-address-space-conversions.cpp
 create mode 100644 clang/test/CIR/CodeGenSYCL/cuda-address-space-conversions.cpp

diff --git a/clang/lib/CIR/CodeGen/TargetInfo.cpp b/clang/lib/CIR/CodeGen/TargetInfo.cpp
index 79620906baa0c2..01aacb1a2ac94d 100644
--- a/clang/lib/CIR/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CIR/CodeGen/TargetInfo.cpp
@@ -146,6 +146,21 @@ class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
         &getABIInfo().cgt.getMLIRContext(),
         cir::LangAddressSpace::OffloadPrivate);
   }
+
+  // The null value of the local and private address spaces is non-zero on
+  // AMDGPU, so materialize it via an address space cast from null in the
+  // generic address space.
+  mlir::Value getNullPointer(CIRGenModule &cgm, cir::PointerType ptrTy,
+                             QualType qt, mlir::Location loc) const override {
+    CIRGenBuilderTy &builder = cgm.getBuilder();
+    if (cgm.getASTContext().getTargetNullPointerValue(qt) == 0)
+      return builder.getNullPtr(ptrTy, loc);
+
+    cir::PointerType genericPtrTy =
+        builder.getPointerTo(ptrTy.getPointee(), LangAS::opencl_generic);
+    return builder.createAddrSpaceCast(
+        loc, builder.getNullPtr(genericPtrTy, loc), ptrTy);
+  }
 };
 
 } // namespace
diff --git a/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp b/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp
index 2ed323dad06529..b82dfbe949606e 100644
--- a/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp
+++ b/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp
@@ -120,6 +120,22 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {
   mlir::Type getCUDADeviceBuiltinTextureDeviceType() const override {
     return cir::CUDADeviceTextureType::get(&getABIInfo().cgt.getMLIRContext());
   }
+
+  // A null pointer in the local address space is materialized via an address
+  // space cast from null in the generic address space, so that it converts
+  // back to a generic null pointer.
+  mlir::Value getNullPointer(CIRGenModule &cgm, cir::PointerType ptrTy,
+                             QualType qt, mlir::Location loc) const override {
+    CIRGenBuilderTy &builder = cgm.getBuilder();
+    if (cgm.getTypes().getTargetAddressSpace(qt->getPointeeType()) !=
+        cgm.getASTContext().getTargetAddressSpace(LangAS::opencl_local))
+      return builder.getNullPtr(ptrTy, loc);
+
+    cir::PointerType genericPtrTy =
+        builder.getPointerTo(ptrTy.getPointee(), LangAS::opencl_generic);
+    return builder.createAddrSpaceCast(
+        loc, builder.getNullPtr(genericPtrTy, loc), ptrTy);
+  }
 };
 
 } // namespace
diff --git a/clang/test/CIR/CodeGenSYCL/amd-address-space-conversions.cpp b/clang/test/CIR/CodeGenSYCL/amd-address-space-conversions.cpp
new file mode 100644
index 00000000000000..6903c7153a8bc9
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/amd-address-space-conversions.cpp
@@ -0,0 +1,324 @@
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Port of clang/test/CodeGenSYCL/amd-address-space-conversions.cpp.
+
+void bar(int &Data) {}
+void bar2(int &Data) {}
+void bar(int [[clang::sycl_local]] &Data) {}
+void foo(int *Data) {}
+void foo2(int *Data) {}
+void foo(int [[clang::sycl_local]] *Data) {}
+
+template <typename T>
+void tmpl(T t) {}
+
+[[clang::sycl_external]] void usages() {
+  int *NoAS;
+  int [[clang::sycl_global]] *GLOB;
+  int [[clang::sycl_local]] *LOC;
+  int [[clang::sycl_private]] *PRIV;
+  int __attribute__((opencl_global_device)) *GLOBDEVICE;
+  int __attribute__((opencl_global_host)) *GLOBHOST;
+  LOC = nullptr;
+  GLOB = nullptr;
+  NoAS = (int *)GLOB;
+  NoAS = (int *)LOC;
+  NoAS = (int *)PRIV;
+  GLOB = (int [[clang::sycl_global]] *)NoAS;
+  LOC = (int [[clang::sycl_local]] *)NoAS;
+  PRIV = (int [[clang::sycl_private]] *)NoAS;
+  GLOB = (int [[clang::sycl_global]] *)GLOBDEVICE;
+  GLOB = (int [[clang::sycl_global]] *)GLOBHOST;
+  bar(*GLOB);
+  bar2(*GLOB);
+  bar(*LOC);
+  bar2(*LOC);
+  bar(*NoAS);
+  bar2(*NoAS);
+  foo(GLOB);
+  foo2(GLOB);
+  foo(LOC);
+  foo2(LOC);
+  foo(NoAS);
+  foo2(NoAS);
+
+  // Ensure that we still get 3 different template instantiations.
+  tmpl(GLOB);
+  tmpl(LOC);
+  tmpl(PRIV);
+  tmpl(NoAS);
+}
+
+// CIR-LABEL: cir.func {{.*}}@_Z6usagesv(
+// CIR-NEXT: %[[NOAS:.*]] = cir.alloca "NoAS" align(8) : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)>
+// CIR-NEXT: %[[GLOB:.*]] = cir.alloca "GLOB" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)>
+// CIR-NEXT: %[[LOC:.*]] = cir.alloca "LOC" align(4) : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)>
+// CIR-NEXT: %[[PRIV:.*]] = cir.alloca "PRIV" align(4) : !cir.ptr<!cir.ptr<!s32i, target_address_space(5)>, target_address_space(5)>
+// CIR-NEXT: %[[GLOBDEVICE:.*]] = cir.alloca "GLOBDEVICE" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)>
+// CIR-NEXT: %[[GLOBHOST:.*]] = cir.alloca "GLOBHOST" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)>
+// CIR-NEXT: %[[GLOBHOST_ASCAST:.*]] = cir.cast address_space %[[GLOBHOST]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// CIR-NEXT: %[[GLOBDEVICE_ASCAST:.*]] = cir.cast address_space %[[GLOBDEVICE]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// CIR-NEXT: %[[PRIV_ASCAST:.*]] = cir.cast address_space %[[PRIV]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(5)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(5)>>
+// CIR-NEXT: %[[LOC_ASCAST:.*]] = cir.cast address_space %[[LOC]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: %[[GLOB_ASCAST:.*]] = cir.cast address_space %[[GLOB]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// CIR-NEXT: %[[NOAS_ASCAST:.*]] = cir.cast address_space %[[NOAS]] : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i>>
+// The null value of the local address space is ~0 on AMDGPU, so a null
+// local pointer is an address space cast of the generic null pointer.
+// CIR-NEXT: %[[NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>
+// CIR-NEXT: %[[LOC_NULL:.*]] = cir.cast address_space %[[NULL]] : !cir.ptr<!s32i> -> !cir.ptr<!s32i, target_address_space(3)>
+// CIR-NEXT: cir.store align(4) %[[LOC_NULL]], %[[LOC_ASCAST]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: %[[GLOB_NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i, target_address_space(1)>
+// CIR-NEXT: cir.store align(8) %[[GLOB_NULL]], %[[GLOB_ASCAST]] : !cir.ptr<!s32i, target_address_space(1)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+
+// CIR-LABEL: cir.func {{.*}}@_Z3barRi(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init const : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)>
+// CIR-NEXT: %[[DATA_ASCAST:.*]] = cir.cast address_space %[[DATA]] : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.store %arg0, %[[DATA_ASCAST]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3barRU3AS3i(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(4) init const : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)>
+// CIR-NEXT: %[[DATA_ASCAST:.*]] = cir.cast address_space %[[DATA]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.store %arg0, %[[DATA_ASCAST]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3fooPi(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)>
+// CIR-NEXT: %[[DATA_ASCAST:.*]] = cir.cast address_space %[[DATA]] : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.store %arg0, %[[DATA_ASCAST]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3fooPU3AS3i(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(4) init : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)>
+// CIR-NEXT: %[[DATA_ASCAST:.*]] = cir.cast address_space %[[DATA]] : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>, target_address_space(5)> -> !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.store %arg0, %[[DATA_ASCAST]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.return
+
+// LLVM-CIR-LABEL: define {{.*}}void @_Z6usagesv(
+// LLVM-CIR: %[[NOAS:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-CIR-NEXT: %[[GLOB:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// LLVM-CIR-NEXT: %[[LOC:.*]] = alloca ptr addrspace(3), align 4, addrspace(5)
+// LLVM-CIR-NEXT: %[[PRIV:.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// LLVM-CIR-NEXT: %[[GLOBDEVICE:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// LLVM-CIR-NEXT: %[[GLOBHOST:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// LLVM-CIR-NEXT: %[[GLOBHOST_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOBHOST]] to ptr
+// LLVM-CIR-NEXT: %[[GLOBDEVICE_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOBDEVICE]] to ptr
+// LLVM-CIR-NEXT: %[[PRIV_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[PRIV]] to ptr
+// LLVM-CIR-NEXT: %[[LOC_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[LOC]] to ptr
+// LLVM-CIR-NEXT: %[[GLOB_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOB]] to ptr
+// LLVM-CIR-NEXT: %[[NOAS_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[NOAS]] to ptr
+// LLVM-CIR-NEXT: store ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: store ptr addrspace(1) null, ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP0:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP1:.*]] = addrspacecast ptr addrspace(1) %[[TMP0]] to ptr
+// LLVM-CIR-NEXT: store ptr %[[TMP1]], ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP2:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP3:.*]] = addrspacecast ptr addrspace(3) %[[TMP2]] to ptr
+// LLVM-CIR-NEXT: store ptr %[[TMP3]], ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP4:.*]] = load ptr addrspace(5), ptr %[[PRIV_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP5:.*]] = addrspacecast ptr addrspace(5) %[[TMP4]] to ptr
+// LLVM-CIR-NEXT: store ptr %[[TMP5]], ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP6:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP7:.*]] = addrspacecast ptr %[[TMP6]] to ptr addrspace(1)
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP7]], ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP8:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP9:.*]] = addrspacecast ptr %[[TMP8]] to ptr addrspace(3)
+// LLVM-CIR-NEXT: store ptr addrspace(3) %[[TMP9]], ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP10:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP11:.*]] = addrspacecast ptr %[[TMP10]] to ptr addrspace(5)
+// LLVM-CIR-NEXT: store ptr addrspace(5) %[[TMP11]], ptr %[[PRIV_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP12:.*]] = load ptr addrspace(1), ptr %[[GLOBDEVICE_ASCAST]], align 8
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP12]], ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP13:.*]] = load ptr addrspace(1), ptr %[[GLOBHOST_ASCAST]], align 8
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP13]], ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP14:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP15:.*]] = addrspacecast ptr addrspace(1) %[[TMP14]] to ptr
+// LLVM-CIR-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP15]])
+// LLVM-CIR-NEXT: %[[TMP16:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP17:.*]] = addrspacecast ptr addrspace(1) %[[TMP16]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP17]])
+// LLVM-CIR-NEXT: %[[TMP18:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: call void @_Z3barRU3AS3i(ptr addrspace(3) noundef align 4 dereferenceable(4) %[[TMP18]])
+// LLVM-CIR-NEXT: %[[TMP19:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP20:.*]] = addrspacecast ptr addrspace(3) %[[TMP19]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP20]])
+// LLVM-CIR-NEXT: %[[TMP21:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP21]])
+// LLVM-CIR-NEXT: %[[TMP22:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP22]])
+// LLVM-CIR-NEXT: %[[TMP23:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP24:.*]] = addrspacecast ptr addrspace(1) %[[TMP23]] to ptr
+// LLVM-CIR-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP24]])
+// LLVM-CIR-NEXT: %[[TMP25:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: %[[TMP26:.*]] = addrspacecast ptr addrspace(1) %[[TMP25]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP26]])
+// LLVM-CIR-NEXT: %[[TMP27:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: call void @_Z3fooPU3AS3i(ptr addrspace(3) noundef %[[TMP27]])
+// LLVM-CIR-NEXT: %[[TMP28:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: %[[TMP29:.*]] = addrspacecast ptr addrspace(3) %[[TMP28]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP29]])
+// LLVM-CIR-NEXT: %[[TMP30:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP30]])
+// LLVM-CIR-NEXT: %[[TMP31:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP31]])
+// LLVM-CIR-NEXT: %[[TMP32:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPU3AS1iEvT_(ptr addrspace(1) noundef %[[TMP32]])
+// LLVM-CIR-NEXT: %[[TMP33:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// LLVM-CIR-NEXT: call void @_Z4tmplIPU3AS3iEvT_(ptr addrspace(3) noundef %[[TMP33]])
+// LLVM-CIR-NEXT: %[[TMP34:.*]] = load ptr addrspace(5), ptr %[[PRIV_ASCAST]], align 4
+// LLVM-CIR-NEXT: call void @_Z4tmplIPU3AS5iEvT_(ptr addrspace(5) noundef %[[TMP34]])
+// LLVM-CIR-NEXT: %[[TMP35:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP35]])
+// LLVM-CIR-NEXT: ret void
+
+// OGCG-LABEL: define {{.*}}void @_Z6usagesv(
+// OGCG: %[[NOAS:.*]] = alloca ptr, align 8, addrspace(5)
+// OGCG-NEXT: %[[GLOB:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// OGCG-NEXT: %[[LOC:.*]] = alloca ptr addrspace(3), align 4, addrspace(5)
+// OGCG-NEXT: %[[PRIV:.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// OGCG-NEXT: %[[GLOBDEVICE:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// OGCG-NEXT: %[[GLOBHOST:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// OGCG-NEXT: %[[NOAS_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[NOAS]] to ptr
+// OGCG-NEXT: %[[GLOB_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOB]] to ptr
+// OGCG-NEXT: %[[LOC_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[LOC]] to ptr
+// OGCG-NEXT: %[[PRIV_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[PRIV]] to ptr
+// OGCG-NEXT: %[[GLOBDEVICE_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOBDEVICE]] to ptr
+// OGCG-NEXT: %[[GLOBHOST_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[GLOBHOST]] to ptr
+// OGCG-NEXT: store ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: store ptr addrspace(1) null, ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP0:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP1:.*]] = addrspacecast ptr addrspace(1) %[[TMP0]] to ptr
+// OGCG-NEXT: store ptr %[[TMP1]], ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP2:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP3:.*]] = addrspacecast ptr addrspace(3) %[[TMP2]] to ptr
+// OGCG-NEXT: store ptr %[[TMP3]], ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP4:.*]] = load ptr addrspace(5), ptr %[[PRIV_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP5:.*]] = addrspacecast ptr addrspace(5) %[[TMP4]] to ptr
+// OGCG-NEXT: store ptr %[[TMP5]], ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP6:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP7:.*]] = addrspacecast ptr %[[TMP6]] to ptr addrspace(1)
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP7]], ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP8:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP9:.*]] = addrspacecast ptr %[[TMP8]] to ptr addrspace(3)
+// OGCG-NEXT: store ptr addrspace(3) %[[TMP9]], ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP10:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP11:.*]] = addrspacecast ptr %[[TMP10]] to ptr addrspace(5)
+// OGCG-NEXT: store ptr addrspace(5) %[[TMP11]], ptr %[[PRIV_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP12:.*]] = load ptr addrspace(1), ptr %[[GLOBDEVICE_ASCAST]], align 8
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP12]], ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP13:.*]] = load ptr addrspace(1), ptr %[[GLOBHOST_ASCAST]], align 8
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP13]], ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP14:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP15:.*]] = addrspacecast ptr addrspace(1) %[[TMP14]] to ptr
+// OGCG-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP15]])
+// OGCG-NEXT: %[[TMP16:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP17:.*]] = addrspacecast ptr addrspace(1) %[[TMP16]] to ptr
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP17]])
+// OGCG-NEXT: %[[TMP18:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: call void @_Z3barRU3AS3i(ptr addrspace(3) noundef align 4 dereferenceable(4) %[[TMP18]])
+// OGCG-NEXT: %[[TMP19:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP20:.*]] = addrspacecast ptr addrspace(3) %[[TMP19]] to ptr
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP20]])
+// OGCG-NEXT: %[[TMP21:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP21]])
+// OGCG-NEXT: %[[TMP22:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP22]])
+// OGCG-NEXT: %[[TMP23:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP24:.*]] = addrspacecast ptr addrspace(1) %[[TMP23]] to ptr
+// OGCG-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP24]])
+// OGCG-NEXT: %[[TMP25:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: %[[TMP26:.*]] = addrspacecast ptr addrspace(1) %[[TMP25]] to ptr
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP26]])
+// OGCG-NEXT: %[[TMP27:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: call void @_Z3fooPU3AS3i(ptr addrspace(3) noundef %[[TMP27]])
+// OGCG-NEXT: %[[TMP28:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: %[[TMP29:.*]] = addrspacecast ptr addrspace(3) %[[TMP28]] to ptr
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP29]])
+// OGCG-NEXT: %[[TMP30:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP30]])
+// OGCG-NEXT: %[[TMP31:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP31]])
+// OGCG-NEXT: %[[TMP32:.*]] = load ptr addrspace(1), ptr %[[GLOB_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPU3AS1iEvT_(ptr addrspace(1) noundef %[[TMP32]])
+// OGCG-NEXT: %[[TMP33:.*]] = load ptr addrspace(3), ptr %[[LOC_ASCAST]], align 4
+// OGCG-NEXT: call void @_Z4tmplIPU3AS3iEvT_(ptr addrspace(3) noundef %[[TMP33]])
+// OGCG-NEXT: %[[TMP34:.*]] = load ptr addrspace(5), ptr %[[PRIV_ASCAST]], align 4
+// OGCG-NEXT: call void @_Z4tmplIPU3AS5iEvT_(ptr addrspace(5) noundef %[[TMP34]])
+// OGCG-NEXT: %[[TMP35:.*]] = load ptr, ptr %[[NOAS_ASCAST]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP35]])
+// OGCG-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3barRi(
+// LLVM-SAME: ptr noundef nonnull align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4bar2Ri(
+// LLVM-SAME: ptr noundef nonnull align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3barRU3AS3i(
+// LLVM-SAME: ptr addrspace(3) noundef align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr addrspace(3), align 4, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr addrspace(3) %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 4
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3fooPi(
+// LLVM-SAME: ptr noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4foo2Pi(
+// LLVM-SAME: ptr noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3fooPU3AS3i(
+// LLVM-SAME: ptr addrspace(3) noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr addrspace(3), align 4, addrspace(5)
+// LLVM-NEXT: %[[DATA_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[DATA_ADDR]] to ptr
+// LLVM-NEXT: store ptr addrspace(3) %[[DATA]], ptr %[[DATA_ADDR_ASCAST]], align 4
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4tmplIPU3AS1iEvT_(
+// LLVM-SAME: ptr addrspace(1) noundef %[[T:.*]])
+// LLVM: %[[T_ADDR:.*]] = alloca ptr addrspace(1), align 8, addrspace(5)
+// LLVM-NEXT: %[[T_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[T_ADDR]] to ptr
+// LLVM-NEXT: store ptr addrspace(1) %[[T]], ptr %[[T_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4tmplIPU3AS3iEvT_(
+// LLVM-SAME: ptr addrspace(3) noundef %[[T:.*]])
+// LLVM: %[[T_ADDR:.*]] = alloca ptr addrspace(3), align 4, addrspace(5)
+// LLVM-NEXT: %[[T_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[T_ADDR]] to ptr
+// LLVM-NEXT: store ptr addrspace(3) %[[T]], ptr %[[T_ADDR_ASCAST]], align 4
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4tmplIPU3AS5iEvT_(
+// LLVM-SAME: ptr addrspace(5) noundef %[[T:.*]])
+// LLVM: %[[T_ADDR:.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// LLVM-NEXT: %[[T_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[T_ADDR]] to ptr
+// LLVM-NEXT: store ptr addrspace(5) %[[T]], ptr %[[T_ADDR_ASCAST]], align 4
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4tmplIPiEvT_(
+// LLVM-SAME: ptr noundef %[[T:.*]])
+// LLVM: %[[T_ADDR:.*]] = alloca ptr, align 8, addrspace(5)
+// LLVM-NEXT: %[[T_ADDR_ASCAST:.*]] = addrspacecast ptr addrspace(5) %[[T_ADDR]] to ptr
+// LLVM-NEXT: store ptr %[[T]], ptr %[[T_ADDR_ASCAST]], align 8
+// LLVM-NEXT: ret void
diff --git a/clang/test/CIR/CodeGenSYCL/cuda-address-space-conversions.cpp b/clang/test/CIR/CodeGenSYCL/cuda-address-space-conversions.cpp
new file mode 100644
index 00000000000000..7ae8186d3c8f3e
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/cuda-address-space-conversions.cpp
@@ -0,0 +1,266 @@
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fsycl-is-device -disable-llvm-passes -Wno-deprecated-attributes -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Port of clang/test/CodeGenSYCL/cuda-address-space-conversions.cpp.
+
+void bar(int &Data) {}
+void bar2(int &Data) {}
+void bar(int [[clang::sycl_local]] &Data) {}
+void foo(int *Data) {}
+void foo2(int *Data) {}
+void foo(int [[clang::sycl_local]] *Data) {}
+
+template <typename T>
+void tmpl(T t);
+
+[[clang::sycl_external]] void usages() {
+  int *NoAS;
+  int [[clang::sycl_global]] *GLOB;
+  int [[clang::sycl_local]] *LOC;
+  int [[clang::sycl_private]] *PRIV;
+  int __attribute__((opencl_global_device)) *GLOBDEVICE;
+  int __attribute__((opencl_global_host)) *GLOBHOST;
+  LOC = nullptr;
+  GLOB = nullptr;
+  NoAS = (int *)GLOB;
+  NoAS = (int *)LOC;
+  NoAS = (int *)PRIV;
+  GLOB = (int [[clang::sycl_global]] *)NoAS;
+  LOC = (int [[clang::sycl_local]] *)NoAS;
+  PRIV = (int [[clang::sycl_private]] *)NoAS;
+  GLOB = (int [[clang::sycl_global]] *)GLOBDEVICE;
+  GLOB = (int [[clang::sycl_global]] *)GLOBHOST;
+  bar(*GLOB);
+  bar2(*GLOB);
+  bar(*LOC);
+  bar2(*LOC);
+  bar(*NoAS);
+  bar2(*NoAS);
+  foo(GLOB);
+  foo2(GLOB);
+  foo(LOC);
+  foo2(LOC);
+  foo(NoAS);
+  foo2(NoAS);
+  tmpl(GLOB);
+  tmpl(LOC);
+  tmpl(PRIV);
+  tmpl(NoAS);
+}
+
+// CIR-LABEL: cir.func {{.*}}@_Z6usagesv(
+// CIR-NEXT: %[[NOAS:.*]] = cir.alloca "NoAS" align(8) : !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: %[[GLOB:.*]] = cir.alloca "GLOB" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// CIR-NEXT: %[[LOC:.*]] = cir.alloca "LOC" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: %[[PRIV:.*]] = cir.alloca "PRIV" align(8) : !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: %[[GLOBDEVICE:.*]] = cir.alloca "GLOBDEVICE" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// CIR-NEXT: %[[GLOBHOST:.*]] = cir.alloca "GLOBHOST" align(8) : !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+// On NVPTX, a null local pointer is an address space cast of the generic
+// null pointer, so that it converts back to a generic null pointer.
+// CIR-NEXT: %[[NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>
+// CIR-NEXT: %[[LOC_NULL:.*]] = cir.cast address_space %[[NULL]] : !cir.ptr<!s32i> -> !cir.ptr<!s32i, target_address_space(3)>
+// CIR-NEXT: cir.store align(8) %[[LOC_NULL]], %[[LOC]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: %[[GLOB_NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i, target_address_space(1)>
+// CIR-NEXT: cir.store align(8) %[[GLOB_NULL]], %[[GLOB]] : !cir.ptr<!s32i, target_address_space(1)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(1)>>
+
+// CIR-LABEL: cir.func {{.*}}@_Z3barRi(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init const : !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.store %arg0, %[[DATA]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3barRU3AS3i(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init const : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.store %arg0, %[[DATA]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3fooPi(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init : !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.store %arg0, %[[DATA]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR-NEXT: cir.return
+
+// CIR-LABEL: cir.func {{.*}}@_Z3fooPU3AS3i(
+// CIR-NEXT: %[[DATA:.*]] = cir.alloca "Data" align(8) init : !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.store %arg0, %[[DATA]] : !cir.ptr<!s32i, target_address_space(3)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(3)>>
+// CIR-NEXT: cir.return
+
+// LLVM-CIR-LABEL: define {{.*}}void @_Z6usagesv(
+// LLVM-CIR: %[[NOAS:.*]] = alloca ptr, align 8
+// LLVM-CIR-NEXT: %[[GLOB:.*]] = alloca ptr addrspace(1), align 8
+// LLVM-CIR-NEXT: %[[LOC:.*]] = alloca ptr addrspace(3), align 8
+// LLVM-CIR-NEXT: %[[PRIV:.*]] = alloca ptr, align 8
+// LLVM-CIR-NEXT: %[[GLOBDEVICE:.*]] = alloca ptr addrspace(1), align 8
+// LLVM-CIR-NEXT: %[[GLOBHOST:.*]] = alloca ptr addrspace(1), align 8
+// LLVM-CIR-NEXT: store ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: store ptr addrspace(1) null, ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP0:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP1:.*]] = addrspacecast ptr addrspace(1) %[[TMP0]] to ptr
+// LLVM-CIR-NEXT: store ptr %[[TMP1]], ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: %[[TMP2:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: %[[TMP3:.*]] = addrspacecast ptr addrspace(3) %[[TMP2]] to ptr
+// LLVM-CIR-NEXT: store ptr %[[TMP3]], ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: %[[TMP4:.*]] = load ptr, ptr %[[PRIV]], align 8
+// LLVM-CIR-NEXT: store ptr %[[TMP4]], ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: %[[TMP5:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: %[[TMP6:.*]] = addrspacecast ptr %[[TMP5]] to ptr addrspace(1)
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP6]], ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP7:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: %[[TMP8:.*]] = addrspacecast ptr %[[TMP7]] to ptr addrspace(3)
+// LLVM-CIR-NEXT: store ptr addrspace(3) %[[TMP8]], ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: %[[TMP9:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: store ptr %[[TMP9]], ptr %[[PRIV]], align 8
+// LLVM-CIR-NEXT: %[[TMP10:.*]] = load ptr addrspace(1), ptr %[[GLOBDEVICE]], align 8
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP10]], ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP11:.*]] = load ptr addrspace(1), ptr %[[GLOBHOST]], align 8
+// LLVM-CIR-NEXT: store ptr addrspace(1) %[[TMP11]], ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP12:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP13:.*]] = addrspacecast ptr addrspace(1) %[[TMP12]] to ptr
+// LLVM-CIR-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP13]])
+// LLVM-CIR-NEXT: %[[TMP14:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP15:.*]] = addrspacecast ptr addrspace(1) %[[TMP14]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP15]])
+// LLVM-CIR-NEXT: %[[TMP16:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: call void @_Z3barRU3AS3i(ptr addrspace(3) noundef align 4 dereferenceable(4) %[[TMP16]])
+// LLVM-CIR-NEXT: %[[TMP17:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: %[[TMP18:.*]] = addrspacecast ptr addrspace(3) %[[TMP17]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP18]])
+// LLVM-CIR-NEXT: %[[TMP19:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP19]])
+// LLVM-CIR-NEXT: %[[TMP20:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP20]])
+// LLVM-CIR-NEXT: %[[TMP21:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP22:.*]] = addrspacecast ptr addrspace(1) %[[TMP21]] to ptr
+// LLVM-CIR-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP22]])
+// LLVM-CIR-NEXT: %[[TMP23:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: %[[TMP24:.*]] = addrspacecast ptr addrspace(1) %[[TMP23]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP24]])
+// LLVM-CIR-NEXT: %[[TMP25:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: call void @_Z3fooPU3AS3i(ptr addrspace(3) noundef %[[TMP25]])
+// LLVM-CIR-NEXT: %[[TMP26:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: %[[TMP27:.*]] = addrspacecast ptr addrspace(3) %[[TMP26]] to ptr
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP27]])
+// LLVM-CIR-NEXT: %[[TMP28:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP28]])
+// LLVM-CIR-NEXT: %[[TMP29:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP29]])
+// LLVM-CIR-NEXT: %[[TMP30:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPU3AS1iEvT_(ptr addrspace(1) noundef %[[TMP30]])
+// LLVM-CIR-NEXT: %[[TMP31:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPU3AS3iEvT_(ptr addrspace(3) noundef %[[TMP31]])
+// LLVM-CIR-NEXT: %[[TMP32:.*]] = load ptr, ptr %[[PRIV]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP32]])
+// LLVM-CIR-NEXT: %[[TMP33:.*]] = load ptr, ptr %[[NOAS]], align 8
+// LLVM-CIR-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP33]])
+// LLVM-CIR-NEXT: ret void
+
+// OGCG-LABEL: define {{.*}}void @_Z6usagesv(
+// OGCG: %[[NOAS:.*]] = alloca ptr, align 8
+// OGCG-NEXT: %[[GLOB:.*]] = alloca ptr addrspace(1), align 8
+// OGCG-NEXT: %[[LOC:.*]] = alloca ptr addrspace(3), align 8
+// OGCG-NEXT: %[[PRIV:.*]] = alloca ptr, align 8
+// OGCG-NEXT: %[[GLOBDEVICE:.*]] = alloca ptr addrspace(1), align 8
+// OGCG-NEXT: %[[GLOBHOST:.*]] = alloca ptr addrspace(1), align 8
+// OGCG-NEXT: store ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr %[[LOC]], align 8
+// OGCG-NEXT: store ptr addrspace(1) null, ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP0:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP1:.*]] = addrspacecast ptr addrspace(1) %[[TMP0]] to ptr
+// OGCG-NEXT: store ptr %[[TMP1]], ptr %[[NOAS]], align 8
+// OGCG-NEXT: %[[TMP2:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: %[[TMP3:.*]] = addrspacecast ptr addrspace(3) %[[TMP2]] to ptr
+// OGCG-NEXT: store ptr %[[TMP3]], ptr %[[NOAS]], align 8
+// OGCG-NEXT: %[[TMP4:.*]] = load ptr, ptr %[[PRIV]], align 8
+// OGCG-NEXT: store ptr %[[TMP4]], ptr %[[NOAS]], align 8
+// OGCG-NEXT: %[[TMP5:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: %[[TMP6:.*]] = addrspacecast ptr %[[TMP5]] to ptr addrspace(1)
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP6]], ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP7:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: %[[TMP8:.*]] = addrspacecast ptr %[[TMP7]] to ptr addrspace(3)
+// OGCG-NEXT: store ptr addrspace(3) %[[TMP8]], ptr %[[LOC]], align 8
+// OGCG-NEXT: %[[TMP9:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: store ptr %[[TMP9]], ptr %[[PRIV]], align 8
+// OGCG-NEXT: %[[TMP10:.*]] = load ptr addrspace(1), ptr %[[GLOBDEVICE]], align 8
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP10]], ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP11:.*]] = load ptr addrspace(1), ptr %[[GLOBHOST]], align 8
+// OGCG-NEXT: store ptr addrspace(1) %[[TMP11]], ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP12:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP13:.*]] = addrspacecast ptr addrspace(1) %[[TMP12]] to ptr
+// OGCG-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP13]])
+// OGCG-NEXT: %[[TMP14:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP15:.*]] = addrspacecast ptr addrspace(1) %[[TMP14]] to ptr
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP15]])
+// OGCG-NEXT: %[[TMP16:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: call void @_Z3barRU3AS3i(ptr addrspace(3) noundef align 4 dereferenceable(4) %[[TMP16]])
+// OGCG-NEXT: %[[TMP17:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: %[[TMP18:.*]] = addrspacecast ptr addrspace(3) %[[TMP17]] to ptr
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP18]])
+// OGCG-NEXT: %[[TMP19:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: call void @_Z3barRi(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP19]])
+// OGCG-NEXT: %[[TMP20:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: call void @_Z4bar2Ri(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP20]])
+// OGCG-NEXT: %[[TMP21:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP22:.*]] = addrspacecast ptr addrspace(1) %[[TMP21]] to ptr
+// OGCG-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP22]])
+// OGCG-NEXT: %[[TMP23:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: %[[TMP24:.*]] = addrspacecast ptr addrspace(1) %[[TMP23]] to ptr
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP24]])
+// OGCG-NEXT: %[[TMP25:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: call void @_Z3fooPU3AS3i(ptr addrspace(3) noundef %[[TMP25]])
+// OGCG-NEXT: %[[TMP26:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: %[[TMP27:.*]] = addrspacecast ptr addrspace(3) %[[TMP26]] to ptr
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP27]])
+// OGCG-NEXT: %[[TMP28:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: call void @_Z3fooPi(ptr noundef %[[TMP28]])
+// OGCG-NEXT: %[[TMP29:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: call void @_Z4foo2Pi(ptr noundef %[[TMP29]])
+// OGCG-NEXT: %[[TMP30:.*]] = load ptr addrspace(1), ptr %[[GLOB]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPU3AS1iEvT_(ptr addrspace(1) noundef %[[TMP30]])
+// OGCG-NEXT: %[[TMP31:.*]] = load ptr addrspace(3), ptr %[[LOC]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPU3AS3iEvT_(ptr addrspace(3) noundef %[[TMP31]])
+// OGCG-NEXT: %[[TMP32:.*]] = load ptr, ptr %[[PRIV]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP32]])
+// OGCG-NEXT: %[[TMP33:.*]] = load ptr, ptr %[[NOAS]], align 8
+// OGCG-NEXT: call void @_Z4tmplIPiEvT_(ptr noundef %[[TMP33]])
+// OGCG-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3barRi(
+// LLVM-SAME: ptr noundef nonnull align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4bar2Ri(
+// LLVM-SAME: ptr noundef nonnull align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3barRU3AS3i(
+// LLVM-SAME: ptr addrspace(3) noundef align 4 dereferenceable(4) %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr addrspace(3), align 8
+// LLVM-NEXT: store ptr addrspace(3) %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3fooPi(
+// LLVM-SAME: ptr noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z4foo2Pi(
+// LLVM-SAME: ptr noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr, align 8
+// LLVM-NEXT: store ptr %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM-LABEL: define {{.*}}void @_Z3fooPU3AS3i(
+// LLVM-SAME: ptr addrspace(3) noundef %[[DATA:.*]])
+// LLVM: %[[DATA_ADDR:.*]] = alloca ptr addrspace(3), align 8
+// LLVM-NEXT: store ptr addrspace(3) %[[DATA]], ptr %[[DATA_ADDR]], align 8
+// LLVM-NEXT: ret void
+
+// LLVM: declare void @_Z4tmplIPU3AS1iEvT_(ptr addrspace(1) noundef)
+// LLVM: declare void @_Z4tmplIPU3AS3iEvT_(ptr addrspace(3) noundef)
+// LLVM: declare void @_Z4tmplIPiEvT_(ptr noundef)

>From 109ff34e05fd14eeab6e656d1e47d1b2be472929 Mon Sep 17 00:00:00 2001
From: "Ye, Qi" <qi.ye at intel.com>
Date: Wed, 28 Oct 2026 10:57:35 -0700
Subject: [PATCH 3/3] Add address-space-mangling test with Microsoft RUN lines
 disabled

---
 .../CodeGenSYCL/address-space-mangling.cpp    | 74 +++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100644 clang/test/CIR/CodeGenSYCL/address-space-mangling.cpp

diff --git a/clang/test/CIR/CodeGenSYCL/address-space-mangling.cpp b/clang/test/CIR/CodeGenSYCL/address-space-mangling.cpp
new file mode 100644
index 00000000000000..02f58ce391c8ab
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/address-space-mangling.cpp
@@ -0,0 +1,74 @@
+// RUN: %clang_cc1 -triple spir64 -fsycl-is-device -disable-llvm-passes -fclangir -emit-cir %s -o %t-spir.cir
+// RUN: FileCheck --input-file=%t-spir.cir %s --check-prefix=CIR-SPIR
+// RUN: %clang_cc1 -triple spir64 -fsycl-is-device -disable-llvm-passes -fclangir -emit-llvm %s -o %t-spir-cir.ll
+// RUN: FileCheck --input-file=%t-spir-cir.ll %s --check-prefix=SPIR
+// RUN: %clang_cc1 -triple spir64 -fsycl-is-device -disable-llvm-passes -emit-llvm %s -o %t-spir.ll
+// RUN: FileCheck --input-file=%t-spir.ll %s --check-prefix=SPIR
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsycl-is-host -fclangir -emit-cir %s -o %t-itanium.cir
+// RUN: FileCheck --input-file=%t-itanium.cir %s --check-prefix=CIR-ITANIUM
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsycl-is-host -fclangir -emit-llvm %s -o %t-itanium-cir.ll
+// RUN: FileCheck --input-file=%t-itanium-cir.ll %s --check-prefix=ITANIUM
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsycl-is-host -emit-llvm %s -o %t-itanium.ll
+// RUN: FileCheck --input-file=%t-itanium.ll %s --check-prefix=ITANIUM
+
+// TODO(cir): Add the MS RUN lines (-triple x86_64-windows-msvc -fsycl-is-host)
+// once ClangIR implements EH ABI lowering for the Microsoft ABI.
+
+// REQUIRES: x86-registered-target
+
+void foo(int [[clang::sycl_global]] *);
+void foo(int [[clang::sycl_local]] *);
+void foo(int [[clang::sycl_private]] *);
+void foo(int [[clang::sycl_generic]] *);
+void foo(int [[clang::sycl_constant]] *);
+void foo(int *);
+
+// CIR-SPIR: cir.func private @_Z3fooPU3AS1i(!cir.ptr<!s32i, target_address_space(1)> {llvm.noundef}) cc(spir_function)
+// CIR-SPIR: cir.func private @_Z3fooPU3AS3i(!cir.ptr<!s32i, target_address_space(3)> {llvm.noundef}) cc(spir_function)
+// CIR-SPIR: cir.func private @_Z3fooPU3AS0i(!cir.ptr<!s32i> {llvm.noundef}) cc(spir_function)
+// CIR-SPIR: cir.func private @_Z3fooPU3AS4i(!cir.ptr<!s32i, target_address_space(4)> {llvm.noundef}) cc(spir_function)
+// CIR-SPIR: cir.func private @_Z3fooPU3AS2i(!cir.ptr<!s32i, target_address_space(2)> {llvm.noundef}) cc(spir_function)
+// CIR-SPIR: cir.func private @_Z3fooPi(!cir.ptr<!s32i, target_address_space(4)> {llvm.noundef}) cc(spir_function)
+
+// SPIR: declare spir_func void @_Z3fooPU3AS1i(ptr addrspace(1) noundef) #1
+// SPIR: declare spir_func void @_Z3fooPU3AS3i(ptr addrspace(3) noundef) #1
+// SPIR: declare spir_func void @_Z3fooPU3AS0i(ptr noundef) #1
+// SPIR: declare spir_func void @_Z3fooPU3AS4i(ptr addrspace(4) noundef) #1
+// SPIR: declare spir_func void @_Z3fooPU3AS2i(ptr addrspace(2) noundef) #1
+// SPIR: declare spir_func void @_Z3fooPi(ptr addrspace(4) noundef) #1
+
+// CIR-ITANIUM: cir.func private @_Z3fooPU8SYglobali(!cir.ptr<!s32i> {llvm.noundef})
+// CIR-ITANIUM: cir.func private @_Z3fooPU7SYlocali(!cir.ptr<!s32i> {llvm.noundef})
+// CIR-ITANIUM: cir.func private @_Z3fooPU9SYprivatei(!cir.ptr<!s32i> {llvm.noundef})
+// CIR-ITANIUM: cir.func private @_Z3fooPU9SYgenerici(!cir.ptr<!s32i> {llvm.noundef})
+// CIR-ITANIUM: cir.func private @_Z3fooPU10SYconstanti(!cir.ptr<!s32i> {llvm.noundef})
+// CIR-ITANIUM: cir.func private @_Z3fooPi(!cir.ptr<!s32i> {llvm.noundef})
+
+// ITANIUM: declare void @_Z3fooPU8SYglobali(ptr noundef)
+// ITANIUM: declare void @_Z3fooPU7SYlocali(ptr noundef)
+// ITANIUM: declare void @_Z3fooPU9SYprivatei(ptr noundef)
+// ITANIUM: declare void @_Z3fooPU9SYgenerici(ptr noundef)
+// ITANIUM: declare void @_Z3fooPU10SYconstanti(ptr noundef)
+// ITANIUM: declare void @_Z3fooPi(ptr noundef)
+
+// MS: declare dso_local void @"?foo@@YAXPEAU?$_ASSYglobal@$$CAH at __clang@@@Z"
+// MS: declare dso_local void @"?foo@@YAXPEAU?$_ASSYlocal@$$CAH at __clang@@@Z"
+// MS: declare dso_local void @"?foo@@YAXPEAU?$_ASSYprivate@$$CAH at __clang@@@Z"
+// MS: declare dso_local void @"?foo@@YAXPEAU?$_ASSYgeneric@$$CAH at __clang@@@Z"
+// MS: declare dso_local void @"?foo@@YAXPEAU?$_ASSYconstant@$$CAH at __clang@@@Z"
+// MS: declare dso_local void @"?foo@@YAXPEAH at Z"
+
+[[clang::sycl_external]] void test() {
+  int [[clang::sycl_global]] *glob;
+  int [[clang::sycl_local]] *loc;
+  int [[clang::sycl_private]] *priv;
+  int [[clang::sycl_generic]] *gen;
+  int [[clang::sycl_constant]] *cnst;
+  int *def;
+  foo(glob);
+  foo(loc);
+  foo(priv);
+  foo(gen);
+  foo(cnst);
+  foo(def);
+}



More information about the cfe-commits mailing list