Skip to content

[CodeGen] LiveRegMatrix: Use allocator through a unique_ptr #120556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

optimisan
Copy link
Contributor

@optimisan optimisan commented Dec 19, 2024

LIU::Matrix holds on to a pointer to the allocator in LiveRegMatrix and is left hanging when the allocator moves with the LiveRegMatrix.

This extends the lifetime of the allocator so that it does not get destroyed when moving a LiveRegMatrix object.

Copy link
Contributor Author

optimisan commented Dec 19, 2024

@llvmbot
Copy link
Member

llvmbot commented Dec 19, 2024

@llvm/pr-subscribers-llvm-regalloc

Author: Akshat Oke (optimisan)

Changes

LIU::Matrix holds on to a pointer to the allocator in LiveRegMatrix and is left hanging when the allocator moves with the LiveRegMatrix.

This extends the lifetime of the allocator so that it does not get destroyed when moving a LiveRegMatrix object.


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

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/LiveRegMatrix.h (+3-7)
  • (modified) llvm/lib/CodeGen/LiveRegMatrix.cpp (+1-1)
diff --git a/llvm/include/llvm/CodeGen/LiveRegMatrix.h b/llvm/include/llvm/CodeGen/LiveRegMatrix.h
index 486392ca3c49d5..a7c1e2bcfe1c43 100644
--- a/llvm/include/llvm/CodeGen/LiveRegMatrix.h
+++ b/llvm/include/llvm/CodeGen/LiveRegMatrix.h
@@ -48,7 +48,7 @@ class LiveRegMatrix {
   unsigned UserTag = 0;
 
   // The matrix is represented as a LiveIntervalUnion per register unit.
-  LiveIntervalUnion::Allocator LIUAlloc;
+  std::unique_ptr<LiveIntervalUnion::Allocator> LIUAlloc;
   LiveIntervalUnion::Array Matrix;
 
   // Cached queries per register unit.
@@ -59,15 +59,11 @@ class LiveRegMatrix {
   unsigned RegMaskVirtReg = 0;
   BitVector RegMaskUsable;
 
-  LiveRegMatrix() = default;
+  LiveRegMatrix() : LIUAlloc(new LiveIntervalUnion::Allocator()) {};
   void releaseMemory();
 
 public:
-  LiveRegMatrix(LiveRegMatrix &&Other)
-      : TRI(Other.TRI), LIS(Other.LIS), VRM(Other.VRM), UserTag(Other.UserTag),
-        Matrix(std::move(Other.Matrix)), Queries(std::move(Other.Queries)),
-        RegMaskTag(Other.RegMaskTag), RegMaskVirtReg(Other.RegMaskVirtReg),
-        RegMaskUsable(std::move(Other.RegMaskUsable)) {}
+  LiveRegMatrix(LiveRegMatrix &&Other) = default;
 
   void init(MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM);
 
diff --git a/llvm/lib/CodeGen/LiveRegMatrix.cpp b/llvm/lib/CodeGen/LiveRegMatrix.cpp
index 9744c47d5a8510..3367171a15662f 100644
--- a/llvm/lib/CodeGen/LiveRegMatrix.cpp
+++ b/llvm/lib/CodeGen/LiveRegMatrix.cpp
@@ -66,7 +66,7 @@ void LiveRegMatrix::init(MachineFunction &MF, LiveIntervals &pLIS,
   unsigned NumRegUnits = TRI->getNumRegUnits();
   if (NumRegUnits != Matrix.size())
     Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
-  Matrix.init(LIUAlloc, NumRegUnits);
+  Matrix.init(*LIUAlloc, NumRegUnits);
 
   // Make sure no stale queries get reused.
   invalidateVirtRegs();

@@ -59,15 +59,11 @@ class LiveRegMatrix {
unsigned RegMaskVirtReg = 0;
BitVector RegMaskUsable;

LiveRegMatrix() = default;
LiveRegMatrix() : LIUAlloc(new LiveIntervalUnion::Allocator()) {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could also do LIUAlloc(std::make_unique<LiveIntervalUnion::Allocator>) up to you.

@optimisan optimisan force-pushed the users/Akshat-Oke/12-19-_support_recycler_implement_move_constructor branch from 982ad41 to df8e573 Compare January 1, 2025 06:39
@optimisan optimisan force-pushed the users/Akshat-Oke/12-19-_codegen_liveregmatrix_use_allocator_through_a_unique_ptr branch from 516482f to 7f36763 Compare January 1, 2025 06:39
@optimisan optimisan force-pushed the users/Akshat-Oke/12-19-_codegen_liveregmatrix_use_allocator_through_a_unique_ptr branch from 7f36763 to 686d1cd Compare January 1, 2025 06:54
@optimisan optimisan changed the base branch from users/Akshat-Oke/12-19-_support_recycler_implement_move_constructor to main January 1, 2025 06:54
Copy link
Contributor Author

optimisan commented Jan 1, 2025

Merge activity

  • Jan 1, 4:22 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Jan 1, 4:24 AM EST: A user merged this pull request with Graphite.

@optimisan optimisan merged commit 50054ba into main Jan 1, 2025
5 of 8 checks passed
@optimisan optimisan deleted the users/Akshat-Oke/12-19-_codegen_liveregmatrix_use_allocator_through_a_unique_ptr branch January 1, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants