Skip to content

[CodeGen] For ad hoc aliasing, additional regUnits are needed to fix lanemask representation #139206

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vg0204
Copy link
Contributor

@vg0204 vg0204 commented May 9, 2025

With ad-hoc aliasing, new register unit are defined for each leaf register nodes that uniquely identifies them, alongwith an additional register unit connecting them (in the ad hoc alias graph that shows the register overlap between the aliasing leaf register nodes)

It solves the issue of using the aliasing register as the immediate subregister of a register, thus need to have disjoint lanemask, which is now possible by virtue of uniquely defined register units. At the same time, aliasing is accounted by the shared register unit, having lanemask as 0x0 just as previously, not a problem now.

It fixes #138552 and acts as an alternative solution to #79835

@vg0204 vg0204 requested review from jayfoad, arsenm and qcolombet May 9, 2025 06:18
@vg0204 vg0204 self-assigned this May 9, 2025
@vg0204 vg0204 force-pushed the vg0204/handling-regunit-for-ad-hoc-aliaising branch from c185c2f to 4bd26bd Compare May 9, 2025 06:32
@vg0204 vg0204 marked this pull request as ready for review May 9, 2025 06:34
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-tablegen

Author: Vikash Gupta (vg0204)

Changes

With ad-hoc aliasing, new register unit are defined for each leaf register nodes that uniquely identifies them, alongwith an additional register unit connecting them (in the ad hoc alias graph that shows the register overlap between the aliasing leaf register nodes)

It solves the issue of using the aliasing register as the immediate subregister of a register, thus need to have disjoint lanemask, which is now possible by virtue of uniquely defined register units. At the same time, aliasing is accounted by the shared register unit, having lanemask as 0x0 just as previously, not a problem now.

It fixes #138552 and acts as an alternative solution to #79835


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

1 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenRegisters.cpp (+30-12)
diff --git a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
index 639f94f79ecd7..44005747f24df 100644
--- a/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenRegisters.cpp
@@ -424,20 +424,33 @@ CodeGenRegister::computeSubRegs(CodeGenRegBank &RegBank) {
   // These units correspond to the maximal cliques in the register overlap
   // graph which is optimal.
   //
-  // When there is ad hoc aliasing, we simply create one unit per edge in the
-  // undirected ad hoc aliasing graph. Technically, we could do better by
-  // identifying maximal cliques in the ad hoc graph, but cliques larger than 2
-  // are extremely rare anyway (I've never seen one), so we don't bother with
-  // the added complexity.
+  // When there is ad hoc aliasing, while we create one unit per edge in the
+  // undirected ad hoc aliasing graph to represent aliasing, one unit per each
+  // node leaf register is needed extra to identify them uniquely, in case these
+  // aliasing register are used as subregister(with disjoint lanemasks) to have
+  // an accurate lanemask generation for these leaf register.
+  // For example, In VE, SX0 is made out of disjoint subregister SW0 & SF0
+  // respectively, where SF0 is an alias for SW0. So while 2 register units will
+  // uniquely define these 2 subregister, the shared register unit will account
+  // for aliasing.
+  //
+  // Technically, we could do better by identifying maximal cliques in the ad
+  // hoc graph, but cliques larger than 2 are extremely rare anyway (I've never
+  // seen one), so we don't bother with the added complexity.
   for (CodeGenRegister *AR : ExplicitAliases) {
     // Only visit each edge once.
     if (AR->SubRegsComplete)
       continue;
     // Create a RegUnit representing this alias edge, and add it to both
     // registers.
-    unsigned Unit = RegBank.newRegUnit(this, AR);
-    RegUnits.set(Unit);
-    AR->RegUnits.set(Unit);
+    unsigned SharedUnit = RegBank.newRegUnit(this, AR);
+    RegUnits.set(SharedUnit);
+    AR->RegUnits.set(SharedUnit);
+
+    // Create a RegUnit that now corresponds uniquely to each of the both
+    // alias leaf register nodes.
+    RegUnits.set(RegBank.newRegUnit(this));
+    AR->RegUnits.set(RegBank.newRegUnit(AR));
   }
 
   // Finally, create units for leaf registers without ad hoc aliases. Note that
@@ -2669,12 +2682,17 @@ CodeGenRegBank::computeCoveredRegisters(ArrayRef<const Record *> Regs) {
   return BV;
 }
 
-void CodeGenRegBank::printRegUnitNames(ArrayRef<unsigned> Units) const {
-  for (unsigned Unit : Units) {
+void CodeGenRegBank::printRegUnitName(unsigned Unit) const {
+  if (Unit < NumNativeRegUnits)
+    dbgs() << ' ' << RegUnits[Unit].Roots[0]->getName();
+  else
+    dbgs() << " #" << Unit;
+
+  if (RegUnits[Unit].Roots[1]) {
     if (Unit < NumNativeRegUnits)
-      dbgs() << ' ' << RegUnits[Unit].Roots[0]->getName();
+      dbgs() << '~' << RegUnits[Unit].Roots[1]->getName();
     else
-      dbgs() << " #" << Unit;
+      dbgs() << "~#" << Unit;
   }
   dbgs() << '\n';
 }

register unit defined that uniquely identifies them, alongwith a
an addtional register unit per edge in ad hoc alias graph that shows
the register overlap between the connected aliasing leaf register nodes

It solves the issue of using the aliasing register as the immediate
subregister of a register, thus need to have disjoint lanemask, which
is now possible by virtue of uniquely defined register units. At the
same time, aliasing is accounted by the shared register unit, having
lanemask as 0x0 just as previously, not a problem now.
@vg0204 vg0204 force-pushed the vg0204/handling-regunit-for-ad-hoc-aliaising branch from 4bd26bd to 5b309af Compare May 9, 2025 06:55
Copy link
Contributor

@jayfoad jayfoad left a comment

Choose a reason for hiding this comment

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

This could do with a test. See test/TableGen/ArtificialSubregs.td for example. If you extend the -register-info-debug to print regunits as well as subregs, then you can test this patch in the same kind of way.

Comment on lines 450 to 451
// Create a RegUnit that now corresponds uniquely to each of the both
// alias leaf register nodes.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is assuming that a register that has aliases is also a leaf, i.e. it has no sburegs. I am not sure whether that is a valid assumption.

Copy link
Contributor Author

@vg0204 vg0204 May 9, 2025

Choose a reason for hiding this comment

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

This is assuming that a register that has aliases is also a leaf, i.e. it has no sburegs. I am not sure whether that is a valid assumption.

It should not have subregs at this point, as the register units are created corresponding only to leaf registers, while other regs (including aliases) inherit the regunits of their corresponding subregs. If you see few lines above this changes you would observe.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am asking whether any target defines a register with both Aliases and SubRegs. Currently I don't think any in-tree target does that, but it seems like it should be possible (and maybe out-of-tree targets do it?). For example, if the VE register SW0 had subregs for its upper and lower 16 bit halves.

// Create a RegUnit that now corresponds uniquely to each of the both
// alias leaf register nodes.
RegUnits.set(RegBank.newRegUnit(this));
AR->RegUnits.set(RegBank.newRegUnit(AR));
}

// Finally, create units for leaf registers without ad hoc aliases. Note that
Copy link
Contributor

Choose a reason for hiding this comment

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

As an alternative implementation, could you simply move this block before the "Absent any ad hoc aliasing..." block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why so? Because the handling of leaf register without ad hoc aliasing is really happening after this from line 456 if you see!

Copy link
Contributor

Choose a reason for hiding this comment

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

The current logic is:

  1. Inherit regunits from subregs.
  2. Add a regunit for each alias.
  3. If we have no regunits after #⁠1 and #⁠2, it must be a leaf without aliases. Give it a new unique regunit.

I am suggesting swapping #⁠2 and #⁠3:

  1. Inherit regunits from subregs.
  2. If we have no regunits after #⁠1, it must be a leaf. Give it a new unique regunit.
  3. Add a regunit for each alias.

I think this gives the desired behavior.

@vg0204
Copy link
Contributor Author

vg0204 commented May 9, 2025

This could do with a test. See test/TableGen/ArtificialSubregs.td for example. If you extend the -register-info-debug to print regunits as well as subregs, then you can test this patch in the same kind of way.

Sure, I'll look into it

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.

[VE][TableGen] Corrupted LaneBitmask representing regLiveUnits for few registerClasses
3 participants