[llvm] [ADT][DWARFLinker] Template AddressRangesMap on the value type (PR #160013)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 21 14:20:03 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Jessica Clarke (jrtc27)

<details>
<summary>Changes</summary>

Currently it hard-codes int64_t as the value type, but other types may
be desired.


---
Full diff: https://github.com/llvm/llvm-project/pull/160013.diff


7 Files Affected:

- (modified) llvm/include/llvm/ADT/AddressRanges.h (+11-7) 
- (modified) llvm/include/llvm/DWARFLinker/AddressesMap.h (+1-1) 
- (modified) llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h (+1-1) 
- (modified) llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp (+4-4) 
- (modified) llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp (+3-3) 
- (modified) llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp (+1-1) 
- (modified) llvm/unittests/Support/AddressRangeTest.cpp (+2-2) 


``````````diff
diff --git a/llvm/include/llvm/ADT/AddressRanges.h b/llvm/include/llvm/ADT/AddressRanges.h
index 79ba5d5a3eddb..f26a2ebfc7700 100644
--- a/llvm/include/llvm/ADT/AddressRanges.h
+++ b/llvm/include/llvm/ADT/AddressRanges.h
@@ -140,16 +140,17 @@ class AddressRanges : public AddressRangesBase<AddressRange> {
   }
 };
 
-class AddressRangeValuePair {
+template <typename T> class AddressRangeValuePair {
 public:
   explicit operator AddressRange() const { return Range; }
 
   AddressRange Range;
-  int64_t Value = 0;
+  T Value = T();
 };
 
-inline bool operator==(const AddressRangeValuePair &LHS,
-                       const AddressRangeValuePair &RHS) {
+template <typename T>
+inline bool operator==(const AddressRangeValuePair<T> &LHS,
+                       const AddressRangeValuePair<T> &RHS) {
   return LHS.Range == RHS.Range && LHS.Value == RHS.Value;
 }
 
@@ -160,15 +161,18 @@ inline bool operator==(const AddressRangeValuePair &LHS,
 /// Intersecting([100,200), [150,300)) ranges splitted into non-conflicting
 /// parts([100,200), [200,300)). Adjacent([100,200), [200,300)) address
 /// ranges are not combined during insertion.
-class AddressRangesMap : public AddressRangesBase<AddressRangeValuePair> {
+template <typename T>
+class AddressRangesMap : public AddressRangesBase<AddressRangeValuePair<T>> {
+  using AddressRangesBase<AddressRangeValuePair<T>>::Ranges;
+
 public:
-  void insert(AddressRange Range, int64_t Value) {
+  void insert(AddressRange Range, T Value) {
     if (Range.empty())
       return;
 
     // Search for range which is less than or equal incoming Range.
     auto It =
-        llvm::partition_point(Ranges, [=](const AddressRangeValuePair &R) {
+        llvm::partition_point(Ranges, [=](const AddressRangeValuePair<T> &R) {
           return R.Range.start() <= Range.start();
         });
 
diff --git a/llvm/include/llvm/DWARFLinker/AddressesMap.h b/llvm/include/llvm/DWARFLinker/AddressesMap.h
index e2215c70dc34e..c708c9c2a2d6c 100644
--- a/llvm/include/llvm/DWARFLinker/AddressesMap.h
+++ b/llvm/include/llvm/DWARFLinker/AddressesMap.h
@@ -21,7 +21,7 @@ namespace dwarf_linker {
 
 /// Mapped value in the address map is the offset to apply to the
 /// linked address.
-using RangesTy = AddressRangesMap;
+using RangesTy = AddressRangesMap<int64_t>;
 
 /// AddressesMap represents information about valid addresses used
 /// by debug information. Valid addresses are those which points to
diff --git a/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h b/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
index 714badfabf89c..0cd34d5be966a 100644
--- a/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
+++ b/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
@@ -24,7 +24,7 @@ class DeclContext;
 
 /// Mapped value in the address map is the offset to apply to the
 /// linked address.
-using RangesTy = AddressRangesMap;
+using RangesTy = AddressRangesMap<int64_t>;
 
 // This structure keeps patch for the attribute and, optionally,
 // the value of relocation which should be applied. Currently,
diff --git a/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp b/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
index 8052773812a2c..8fefc53f524d2 100644
--- a/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
+++ b/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
@@ -2061,7 +2061,7 @@ void DWARFLinker::generateUnitRanges(CompileUnit &Unit, const DWARFFile &File,
 
   // Build set of linked address ranges for unit function ranges.
   AddressRanges LinkedFunctionRanges;
-  for (const AddressRangeValuePair &Range : FunctionRanges)
+  for (const AddressRangeValuePair<int64_t> &Range : FunctionRanges)
     LinkedFunctionRanges.insert(
         {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value});
 
@@ -2074,7 +2074,7 @@ void DWARFLinker::generateUnitRanges(CompileUnit &Unit, const DWARFFile &File,
       Unit.getUnitRangesAttribute();
 
   if (!AllRngListAttributes.empty() || UnitRngListAttribute) {
-    std::optional<AddressRangeValuePair> CachedRange;
+    std::optional<AddressRangeValuePair<int64_t>> CachedRange;
     MCSymbol *EndLabel = TheDwarfEmitter->emitDwarfDebugRangeListHeader(Unit);
 
     // Read original address ranges, apply relocation value, emit linked address
@@ -2329,7 +2329,7 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
       Seq.reserve(InputRows.size());
 
       const auto &FunctionRanges = Unit.getFunctionRanges();
-      std::optional<AddressRangeValuePair> CurrRange;
+      std::optional<AddressRangeValuePair<int64_t>> CurrRange;
 
       // FIXME: This logic is meant to generate exactly the same output as
       // Darwin's classic dsymutil. There is a nicer way to implement this
@@ -2562,7 +2562,7 @@ void DWARFLinker::patchFrameInfoForObject(LinkContext &Context) {
     // the function entry point, thus we can't just lookup the address
     // in the debug map. Use the AddressInfo's range map to see if the FDE
     // describes something that we can relocate.
-    std::optional<AddressRangeValuePair> Range =
+    std::optional<AddressRangeValuePair<int64_t>> Range =
         AllUnitsRanges.getRangeThatContains(Loc);
     if (!Range) {
       // The +4 is to account for the size of the InitialLength field itself.
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
index 8d065bf793f35..f1f8e5876640e 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
@@ -674,7 +674,7 @@ Error CompileUnit::cloneAndEmitRanges() {
 
   // Build set of linked address ranges for unit function ranges.
   AddressRanges LinkedFunctionRanges;
-  for (const AddressRangeValuePair &Range : getFunctionRanges())
+  for (const AddressRangeValuePair<int64_t> &Range : getFunctionRanges())
     LinkedFunctionRanges.insert(
         {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value});
 
@@ -697,7 +697,7 @@ void CompileUnit::cloneAndEmitRangeList(DebugSectionKind RngSectionKind,
       getOrCreateSectionDescriptor(RngSectionKind);
 
   if (!DebugInfoSection.ListDebugRangePatch.empty()) {
-    std::optional<AddressRangeValuePair> CachedRange;
+    std::optional<AddressRangeValuePair<int64_t>> CachedRange;
     uint64_t OffsetAfterUnitLength = emitRangeListHeader(OutRangeSection);
 
     DebugRangePatch *CompileUnitRangePtr = nullptr;
@@ -1544,7 +1544,7 @@ Error CompileUnit::cloneAndEmitLineTable(const Triple &TargetTriple) {
     std::vector<DWARFDebugLine::Row> Seq;
 
     const auto &FunctionRanges = getFunctionRanges();
-    std::optional<AddressRangeValuePair> CurrRange;
+    std::optional<AddressRangeValuePair<int64_t>> CurrRange;
 
     // FIXME: This logic is meant to generate exactly the same output as
     // Darwin's classic dsymutil. There is a nicer way to implement this
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
index e9ee3ce1b8b27..c4947e2a11c22 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
@@ -786,7 +786,7 @@ Error DWARFLinkerImpl::LinkContext::cloneAndEmitDebugFrame() {
     // the function entry point, thus we can't just lookup the address
     // in the debug map. Use the AddressInfo's range map to see if the FDE
     // describes something that we can relocate.
-    std::optional<AddressRangeValuePair> Range =
+    std::optional<AddressRangeValuePair<int64_t>> Range =
         AllUnitsRanges.getRangeThatContains(Loc);
     if (!Range) {
       // The +4 is to account for the size of the InitialLength field itself.
diff --git a/llvm/unittests/Support/AddressRangeTest.cpp b/llvm/unittests/Support/AddressRangeTest.cpp
index 76e7e5e78d964..808eb5b4a3f11 100644
--- a/llvm/unittests/Support/AddressRangeTest.cpp
+++ b/llvm/unittests/Support/AddressRangeTest.cpp
@@ -172,7 +172,7 @@ TEST(AddressRangeTest, TestRangesRandom) {
 }
 
 TEST(AddressRangeTest, TestRangesMap) {
-  AddressRangesMap Ranges;
+  AddressRangesMap<int64_t> Ranges;
 
   EXPECT_EQ(Ranges.size(), 0u);
   EXPECT_TRUE(Ranges.empty());
@@ -389,7 +389,7 @@ TEST(AddressRangeTest, TestRangesMap) {
 }
 
 TEST(AddressRangeTest, TestRangesMapRandom) {
-  AddressRangesMap Ranges;
+  AddressRangesMap<int64_t> Ranges;
   size_t NumElements = 100;
 
   std::srand(std::time(nullptr));

``````````

</details>


https://github.com/llvm/llvm-project/pull/160013


More information about the llvm-commits mailing list