[llvm] [TableGen] Allow targets to enforce regunits assignment as intervals (PR #175823)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 20 05:54:43 PST 2026
================
@@ -1929,6 +1929,101 @@ void CodeGenRegBank::computeRegUnitWeights() {
}
}
+// Enforce that all registers are intervals of regunits if the target
+// requests this property. This will renumber regunits to ensure the
+// interval property holds, or error out if it cannot be satisfied.
+void CodeGenRegBank::enforceRegUnitIntervals() {
+ std::vector<const Record *> Targets =
+ Records.getAllDerivedDefinitions("Target");
+
+ if (Targets.empty())
+ return;
+
+ const Record *Target = Targets[0];
+ if (!Target->getValueAsBit("RegistersAreIntervals"))
+ return;
+
+ LLVM_DEBUG(dbgs() << "Enforcing regunit intervals for target\n");
+ std::vector<unsigned> RegUnitRenumbering(RegUnits.size(), ~0u);
+
+ // RegUnits that have been renumbered from X -> Y. Y is what is marked so that
+ // it doesn't create a chain of swaps.
+ SparseBitVector<> DontRenumberUnits;
+
+ auto GetRenumberedUnit = [&](unsigned RegUnit) -> unsigned {
+ if (RegUnitRenumbering[RegUnit] != ~0u)
+ return RegUnitRenumbering[RegUnit];
+ return RegUnit;
+ };
+
+ auto IsContiguous = [&](CodeGenRegister::RegUnitList &Units) -> bool {
+ unsigned LastUnit = Units.find_first();
+ for (auto ThisUnit : llvm::make_range(++Units.begin(), Units.end())) {
+ if (ThisUnit != LastUnit + 1)
+ return false;
+ LastUnit = ThisUnit;
+ }
+ return true;
+ };
+
+ // Process registers in definition order
+ for (CodeGenRegister &Reg : Registers) {
+ LLVM_DEBUG(dbgs() << "Processing register " << Reg.getName() << "\n");
+ const auto &Units = Reg.getNativeRegUnits();
+ if (Units.empty())
+ continue;
+ SparseBitVector<> RenumberedUnits;
+ // First renumber all the units for this register according to previous
+ // renumbering.
+ LLVM_DEBUG(dbgs() << " Original (Renumbered) units:");
+ for (unsigned U : Units) {
+ LLVM_DEBUG(dbgs() << " " << U << "(" << GetRenumberedUnit(U) << "), ");
+ RenumberedUnits.set(GetRenumberedUnit(U));
+ }
+ LLVM_DEBUG(dbgs() << "\n");
+
+ unsigned LastUnit = RenumberedUnits.find_first();
+ for (auto ThisUnit :
+ llvm::make_range(++RenumberedUnits.begin(), RenumberedUnits.end())) {
+ if (ThisUnit != LastUnit + 1) {
+ if (DontRenumberUnits.test(LastUnit + 1)) {
+ PrintFatalError(
+ "Cannot enforce regunit intervals for register " + Reg.getName() +
+ ": unit " + Twine(LastUnit + 1) +
+ " (root: " + RegUnits[LastUnit + 1].Roots[0]->getName() +
+ ") has already been renumbered and cannot be swapped");
+ }
+ LLVM_DEBUG(dbgs() << " Renumbering unit " << ThisUnit << " to "
+ << (LastUnit + 1) << "\n");
+ RegUnitRenumbering[LastUnit + 1] = ThisUnit;
+ RegUnitRenumbering[ThisUnit] = LastUnit + 1;
+ DontRenumberUnits.set(LastUnit + 1);
+ ThisUnit = LastUnit + 1;
+ }
+ LastUnit = ThisUnit;
+ }
+ }
+
+ // Apply the renumbering to all registers
+ for (CodeGenRegister &Reg : Registers) {
+ CodeGenRegister::RegUnitList NewRegUnits;
+ for (unsigned OldUnit : Reg.getRegUnits())
+ NewRegUnits.set(GetRenumberedUnit(OldUnit));
+ Reg.setNewRegUnits(NewRegUnits);
+
+ CodeGenRegister::RegUnitList NewNativeUnits;
+ for (unsigned OldUnit : Reg.getNativeRegUnits())
+ NewNativeUnits.set(GetRenumberedUnit(OldUnit));
+ if (!IsContiguous(NewNativeUnits)) {
+ reportFatalInternalError("Cannot enforce regunit intervals, final "
----------------
arsenm wrote:
Yes
https://github.com/llvm/llvm-project/pull/175823
More information about the llvm-commits
mailing list