[clang] [clang][CodeGen] Add query for a target's flat address space (PR #95728)

Alex Voicu via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 16 18:25:50 PDT 2024


https://github.com/AlexVlx created https://github.com/llvm/llvm-project/pull/95728

Often, targets which are not address space agnostic expose a flat/generic address space, which acts as a shared, legal target for address space casts. Whilst today we accidentally (e.g. by using `PointerType::getUnqual`) treat 0 as corresponding to this flat address space, there is no binding requirement placed on targets in this regard, which leads to issues such as those reflected in #93601 and #93914. This patch adds a `getFlatPtrAddressSpace()` interface in `TargetInfo`, allowing targets to inform the front-end. A possible alternative name would be `getGenericPtrAddressSpace()`, but that was not chosen since generic has a fairly specific meaning in C++ and it seemed somewhat confusing in this context.

The interface is not used anywhere at the moment, but the intention is to employ it, for example, to specify the pointer type for the `llvm.used` array's elements.

>From 2b500ad9ef2baf27da29146ddddb5a4123dcb75e Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 17 Jun 2024 02:15:00 +0100
Subject: [PATCH] Add interface for exposing a target's flat address space, if
 it exists.

---
 clang/include/clang/Basic/TargetInfo.h | 7 +++++++
 clang/lib/Basic/Targets/AMDGPU.h       | 6 ++++++
 clang/lib/Basic/Targets/SPIR.h         | 4 ++++
 3 files changed, 17 insertions(+)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 8a6511b9ced83..8841ec5f910d9 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1764,6 +1764,13 @@ class TargetInfo : public TransferrableTargetInfo,
     return 0;
   }
 
+  /// \returns Target specific flat ptr address space; a flat ptr is a ptr that
+  /// can be casted to / from all other target address spaces. If the target
+  /// exposes no such address space / does not care, we return 0.
+  virtual unsigned getFlatPtrAddressSpace() const {
+    return 0;
+  }
+
   /// \returns If a target requires an address within a target specific address
   /// space \p AddressSpace to be converted in order to be used, then return the
   /// corresponding target specific DWARF address space.
diff --git a/clang/lib/Basic/Targets/AMDGPU.h b/clang/lib/Basic/Targets/AMDGPU.h
index 94d9ba93ed226..d06c7d58fe94c 100644
--- a/clang/lib/Basic/Targets/AMDGPU.h
+++ b/clang/lib/Basic/Targets/AMDGPU.h
@@ -379,6 +379,12 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
     return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS);
   }
 
+  /// \returns Target specific flat ptr address space; a flat ptr is a ptr that
+  /// can be casted to / from all other target address spaces.
+  unsigned getFlatPtrAddressSpace() const override {
+    return static_cast<unsigned>(llvm::AMDGPUAS::FLAT_ADDRESS);
+  }
+
   /// \returns If a target requires an address within a target specific address
   /// space \p AddressSpace to be converted in order to be used, then return the
   /// corresponding target specific DWARF address space.
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index 37cf9d7921bac..14d235bace960 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -182,6 +182,10 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
     return TargetInfo::VoidPtrBuiltinVaList;
   }
 
+  unsigned getFlatPtrAddressSpace() const override {
+    return 4u; // 4 is generic i.e. flat for SPIR & SPIR-V.
+  }
+
   std::optional<unsigned>
   getDWARFAddressSpace(unsigned AddressSpace) const override {
     return AddressSpace;



More information about the cfe-commits mailing list