[llvm] [RegAlloc] Add a hook for default target specific CSR cost scale (PR #214857)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 21:57:29 PDT 2026


================
@@ -866,10 +866,19 @@ class LLVM_ABI TargetRegisterInfo : public MCRegisterInfo {
   /// Allow the target to override the cost of using a callee-saved register for
   /// the first time. Default value of 0 means we will use a callee-saved
   /// register if it is available.
-  virtual unsigned getCSRFirstUseCost() const { return 0; }
+  virtual unsigned getCSRFirstUseCost(const MachineFunction &MF) const {
+    return 0;
+  }
   /// FIXME: We should deprecate this usage.
   virtual unsigned getCSRCost() const { return 0; }
 
+  /// Scale the CSRFirstUseCost with this number.
+  /// The scale is a percentage (e.g., 30 means 30% of the base cost).
+  /// Target can tune and override this default value.
+  virtual unsigned getCSRCostScale(const MachineFunction &MF) const {
+    return 30;
+  }
----------------
weiguozhi wrote:

**getCSRFirstUseCost** reflects the target's property, usually it's the cost of push+pop instructions, so once it's set, it should never be changed.

**getCSRCostScale** is used to tune the performance by scale down the raw CSR cost. It can also be override by a command line option.

Both parameters have very clear semantics, they are easy to be understood and tuned by developers and users. If they are combined, it will be much more difficult to tune it.

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


More information about the llvm-commits mailing list