Author: Shilei Tian
Date: 2026-01-13T00:47:27Z
New Revision: 30596a0b037b4010fa18cd1db64706c018007d7a

URL: 
https://github.com/llvm/llvm-project/commit/30596a0b037b4010fa18cd1db64706c018007d7a
DIFF: 
https://github.com/llvm/llvm-project/commit/30596a0b037b4010fa18cd1db64706c018007d7a.diff

LOG: [Clang][AMDGPU] Get correct nullptr value for AS3 and AS5 (#175610)

Added: 
    clang/test/CodeGen/AMDGPU/nullptr-in-different-address-spaces.cpp

Modified: 
    clang/lib/Basic/Targets/AMDGPU.h
    llvm/include/llvm/Support/AMDGPUAddrSpace.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Targets/AMDGPU.h 
b/clang/lib/Basic/Targets/AMDGPU.h
index 0076f822c02a1..35bca74505b37 100644
--- a/clang/lib/Basic/Targets/AMDGPU.h
+++ b/clang/lib/Basic/Targets/AMDGPU.h
@@ -445,11 +445,13 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : 
public TargetInfo {
   // address space has value 0 but in private and local address space has
   // value ~0.
   uint64_t getNullPointerValue(LangAS AS) const override {
-    // FIXME: Also should handle region.
-    return (AS == LangAS::opencl_local || AS == LangAS::opencl_private ||
-            AS == LangAS::sycl_local || AS == LangAS::sycl_private)
-               ? ~0
-               : 0;
+    // Check language-specific address spaces
+    if (AS == LangAS::opencl_local || AS == LangAS::opencl_private ||
+        AS == LangAS::sycl_local || AS == LangAS::sycl_private)
+      return ~0;
+    if (isTargetAddressSpace(AS))
+      return llvm::AMDGPU::getNullPointerValue(toTargetAddressSpace(AS));
+    return 0;
   }
 
   void setAuxTarget(const TargetInfo *Aux) override;

diff  --git a/clang/test/CodeGen/AMDGPU/nullptr-in-
diff erent-address-spaces.cpp b/clang/test/CodeGen/AMDGPU/nullptr-in-
diff erent-address-spaces.cpp
new file mode 100644
index 0000000000000..b820b71ddc070
--- /dev/null
+++ b/clang/test/CodeGen/AMDGPU/nullptr-in-
diff erent-address-spaces.cpp
@@ -0,0 +1,55 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 6
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -target-cpu gfx900 -O1 -emit-llvm 
-o - %s | FileCheck %s
+
+extern __attribute__((address_space(3))) unsigned char * ptr_as3; // local 
address space
+extern __attribute__((address_space(5))) unsigned char * ptr_as5; // private 
address space
+
+// CHECK-LABEL: define dso_local noundef ptr @_Z11get_ptr_as3b(
+// CHECK-SAME: i1 noundef zeroext [[V:%.*]]) local_unnamed_addr 
#[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(3), ptr addrspace(1) 
@ptr_as3, align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[V]], ptr addrspace(3) [[TMP0]], 
ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3))
+// CHECK-NEXT:    [[TMP2:%.*]] = addrspacecast ptr addrspace(3) [[TMP1]] to ptr
+// CHECK-NEXT:    ret ptr [[TMP2]]
+//
+unsigned char *get_ptr_as3(bool v) {
+  return v ? ptr_as3 : nullptr;
+}
+
+// CHECK-LABEL: define dso_local noundef ptr @_Z20get_ptr_as3_with_tmpb(
+// CHECK-SAME: i1 noundef zeroext [[V:%.*]]) local_unnamed_addr #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(3), ptr addrspace(1) 
@ptr_as3, align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[V]], ptr addrspace(3) [[TMP0]], 
ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3))
+// CHECK-NEXT:    [[TMP2:%.*]] = addrspacecast ptr addrspace(3) [[TMP1]] to ptr
+// CHECK-NEXT:    ret ptr [[TMP2]]
+//
+unsigned char *get_ptr_as3_with_tmp(bool v) {
+  unsigned char *tmp = nullptr;
+  return v ? ptr_as3 : tmp;
+}
+
+// CHECK-LABEL: define dso_local noundef ptr @_Z11get_ptr_as5b(
+// CHECK-SAME: i1 noundef zeroext [[V:%.*]]) local_unnamed_addr 
#[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(5), ptr addrspace(1) 
@ptr_as5, align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[V]], ptr addrspace(5) [[TMP0]], 
ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5))
+// CHECK-NEXT:    [[TMP2:%.*]] = addrspacecast ptr addrspace(5) [[TMP1]] to ptr
+// CHECK-NEXT:    ret ptr [[TMP2]]
+//
+unsigned char *get_ptr_as5(bool v) {
+  return v ? ptr_as5 : nullptr;
+}
+
+// CHECK-LABEL: define dso_local noundef ptr @_Z20get_ptr_as5_with_tmpb(
+// CHECK-SAME: i1 noundef zeroext [[V:%.*]]) local_unnamed_addr #[[ATTR1]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(5), ptr addrspace(1) 
@ptr_as5, align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[V]], ptr addrspace(5) [[TMP0]], 
ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5))
+// CHECK-NEXT:    [[TMP2:%.*]] = addrspacecast ptr addrspace(5) [[TMP1]] to ptr
+// CHECK-NEXT:    ret ptr [[TMP2]]
+//
+unsigned char *get_ptr_as5_with_tmp(bool v) {
+  unsigned char *tmp = nullptr;
+  return v ? ptr_as5 : tmp;
+}

diff  --git a/llvm/include/llvm/Support/AMDGPUAddrSpace.h 
b/llvm/include/llvm/Support/AMDGPUAddrSpace.h
index 0c89512310289..3fe6492584d84 100644
--- a/llvm/include/llvm/Support/AMDGPUAddrSpace.h
+++ b/llvm/include/llvm/Support/AMDGPUAddrSpace.h
@@ -15,6 +15,8 @@
 #ifndef LLVM_SUPPORT_AMDGPUADDRSPACE_H
 #define LLVM_SUPPORT_AMDGPUADDRSPACE_H
 
+#include <cstdint>
+
 namespace llvm {
 /// OpenCL uses address spaces to 
diff erentiate between
 /// various memory regions on the hardware. On the CPU
@@ -165,6 +167,19 @@ constexpr int mapToDWARFAddrSpace(unsigned LLVMAddrSpace) {
     return impl::LLVMToDWARFAddrSpaceMapping[LLVMAddrSpace];
   return -1;
 }
+
+/// Get the null pointer value for the given address space.
+constexpr int64_t getNullPointerValue(unsigned AS) {
+  switch (AS) {
+    using namespace AMDGPUAS;
+  case PRIVATE_ADDRESS:
+  case LOCAL_ADDRESS:
+  case REGION_ADDRESS:
+    return -1;
+  default:
+    return 0;
+  }
+}
 } // end namespace AMDGPU
 
 } // end namespace llvm


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to