[llvm] [RegAlloc] Add scaffold for -regalloc=segmenttree (NFC) (PR #157830)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 10 03:47:04 PDT 2025


https://github.com/steven-studio created https://github.com/llvm/llvm-project/pull/157830

Adds a minimal factory + regalloc registration for . Currently delegates to Greedy allocator. No functional change.This patch introduces a scaffold for a new register allocator option
`-regalloc=segmenttree`, which currently delegates to the Greedy allocator.
No functional change is intended.

Motivation:
- Provide a separate regalloc entry point for future work on a
  segment-tree-based allocator.
- Enable early benchmarking and integration testing under a new mode.

Implementation:
- Added `RegAllocSegmentTree.cpp` / `.h` containing a factory function
  `createRegAllocSegmentTree()`.
- Registered this allocator in `RegisterRegAlloc` so that
  `llc -regalloc=segmenttree` is recognized.
- Hooked up pass initialization in `CodeGen.cpp`.

Testing:
- Verified that `llc -regalloc=segmenttree` produces identical codegen
  to `-regalloc=greedy`.

Future work:
- This is step 1 of a planned series to implement a segment-tree-based
  register allocator, targeting O(V · R · A · S · log E) allocation time
  complexity.

>From 9bff1daffc8283d4366902185902aefb3db0bdad Mon Sep 17 00:00:00 2001
From: steven-studio <stevenyu.supreme at gmail.com>
Date: Wed, 10 Sep 2025 17:34:31 +0800
Subject: [PATCH] [RegAlloc] Add scaffold for -regalloc=segmenttree (NFC)

Adds a minimal factory + regalloc registration for .
Currently delegates to Greedy allocator. No functional change.
---
 .../llvm/CodeGen/RegAllocSegmentTree.h        | 17 +++++++++++++++
 llvm/lib/CodeGen/CodeGen.cpp                  |  1 +
 llvm/lib/CodeGen/RegAllocSegmentTree.cpp      | 21 +++++++++++++++++++
 3 files changed, 39 insertions(+)
 create mode 100644 llvm/include/llvm/CodeGen/RegAllocSegmentTree.h
 create mode 100644 llvm/lib/CodeGen/RegAllocSegmentTree.cpp

diff --git a/llvm/include/llvm/CodeGen/RegAllocSegmentTree.h b/llvm/include/llvm/CodeGen/RegAllocSegmentTree.h
new file mode 100644
index 0000000000000..858b7d0ee565b
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/RegAllocSegmentTree.h
@@ -0,0 +1,17 @@
+//===- RegAllocSegmentTree.h - RA segtree scaffold --------------*- C++ -*-===//
+//
+// This file declares createRegAllocSegmentTree() factory (scaffold only).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_REGALLOCSEGMENTTREE_H
+#define LLVM_CODEGEN_REGALLOCSEGMENTTREE_H
+
+namespace llvm {
+class FunctionPass;
+
+// Factory (for -regalloc=segtre). For now it delegates to Greedy (NFC).
+FunctionPass *createRegAllocSegmentTree();
+} // end namespace llvm
+
+#endif
\ No newline at end of file
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 989cf4c4796ae..28181e89e4f54 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -113,6 +113,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeRABasicPass(Registry);
   initializeRAGreedyLegacyPass(Registry);
   initializeRegAllocFastPass(Registry);
+  // initializeRegAllocSegmentTreePass(Registry);  // Add this line
   initializeRegUsageInfoCollectorLegacyPass(Registry);
   initializeRegUsageInfoPropagationLegacyPass(Registry);
   initializeRegisterCoalescerLegacyPass(Registry);
diff --git a/llvm/lib/CodeGen/RegAllocSegmentTree.cpp b/llvm/lib/CodeGen/RegAllocSegmentTree.cpp
new file mode 100644
index 0000000000000..65b8b60ee28f6
--- /dev/null
+++ b/llvm/lib/CodeGen/RegAllocSegmentTree.cpp
@@ -0,0 +1,21 @@
+//===- RegAllocSegmentTree.cpp - RA segtree scaffold ----------------------===//
+//
+// Scaffold only: register -regalloc=segmenttree that delegates to Greedy (NFC).
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/RegAllocSegmentTree.h"
+#include "llvm/CodeGen/RegAllocRegistry.h"
+
+using namespace llvm;
+
+// 工廠:目前直接委派回 Greedy(零行為變更)
+FunctionPass *llvm::createRegAllocSegmentTree() {
+  extern FunctionPass *createGreedyRegisterAllocator();
+  return createGreedyRegisterAllocator();
+}
+
+// 把選項掛進 -regalloc= 名單(名稱、描述、工廠)
+static RegisterRegAlloc
+    RAReg("segmenttree", "Segment Tree Register Allocator (scaffold)",
+          createRegAllocSegmentTree);
\ No newline at end of file



More information about the llvm-commits mailing list