[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
Thu Jun 27 13:00:31 PDT 2024


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

>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 1/3] 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;

>From 346877d118700a748bffa8b0aee8633d6991582c Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 17 Jun 2024 12:58:04 +0100
Subject: [PATCH 2/3] Fix formatting.

---
 clang/include/clang/Basic/TargetInfo.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 8841ec5f910d9..a3b60db122cae 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1767,9 +1767,7 @@ class TargetInfo : public TransferrableTargetInfo,
   /// \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;
-  }
+  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

>From 8ee059a30fbad6be374a980447601339eea37645 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Wed, 19 Jun 2024 14:44:53 +0100
Subject: [PATCH 3/3] Rework interface to return optional; flesh out comment.

---
 clang/include/clang/Basic/TargetInfo.h | 14 ++++++++++----
 clang/lib/Basic/Targets/AMDGPU.h       |  2 +-
 clang/lib/Basic/Targets/SPIR.h         |  2 +-
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index a3b60db122cae..eb5b29dfbb3f0 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1764,10 +1764,16 @@ 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 exposes a flat ptr address space (a flat ptr is a ptr
+  /// that can be casted to / from all other target address spaces), then return
+  /// it. Otherwise, return std::nullopt and leave it up to the FE to correctly
+  /// insert specific address spaces. Note that this is target specific and
+  /// might not have a corresponding LangAS equivalent, thus it is not safe to
+  /// unconditionally invoke getLangASFromTargetAS on the value returned by this
+  /// interface.
+  virtual std::optional<unsigned> getFlatPtrAddressSpace() const {
+    return std::nullopt;
+  }
 
   /// \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
diff --git a/clang/lib/Basic/Targets/AMDGPU.h b/clang/lib/Basic/Targets/AMDGPU.h
index d06c7d58fe94c..5615bfe97c66c 100644
--- a/clang/lib/Basic/Targets/AMDGPU.h
+++ b/clang/lib/Basic/Targets/AMDGPU.h
@@ -381,7 +381,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
 
   /// \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 {
+  std::optional<unsigned> getFlatPtrAddressSpace() const override {
     return static_cast<unsigned>(llvm::AMDGPUAS::FLAT_ADDRESS);
   }
 
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index 14d235bace960..3bf9e22204c28 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -182,7 +182,7 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
     return TargetInfo::VoidPtrBuiltinVaList;
   }
 
-  unsigned getFlatPtrAddressSpace() const override {
+  std::optional<unsigned> getFlatPtrAddressSpace() const override {
     return 4u; // 4 is generic i.e. flat for SPIR & SPIR-V.
   }
 



More information about the cfe-commits mailing list