Skip to content

Conversation

MacDue
Copy link
Member

@MacDue MacDue commented Aug 21, 2025

This is done for consistency with LiveRegUnits (see #154325). This is technically not an NFC, as MBB.liveouts() excludes runtime-defined liveins, but no users currently depend on this.

This is done for consistency with LiveRegUnits (see llvm#154325). This is
technically not an NFC, as `MBB.liveouts()` excludes runtime-defined
liveins, but no users currently depend on this.
@llvmbot
Copy link
Member

llvmbot commented Aug 21, 2025

@llvm/pr-subscribers-llvm-regalloc

Author: Benjamin Maxwell (MacDue)

Changes

This is done for consistency with LiveRegUnits (see #154325). This is technically not an NFC, as MBB.liveouts() excludes runtime-defined liveins, but no users currently depend on this.


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

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/LivePhysRegs.h (+7)
  • (modified) llvm/lib/CodeGen/LivePhysRegs.cpp (+29-20)
  • (modified) llvm/lib/CodeGen/LiveRegUnits.cpp (+2-2)
diff --git a/llvm/include/llvm/CodeGen/LivePhysRegs.h b/llvm/include/llvm/CodeGen/LivePhysRegs.h
index 2a719571fde2d..4af5b00014cb7 100644
--- a/llvm/include/llvm/CodeGen/LivePhysRegs.h
+++ b/llvm/include/llvm/CodeGen/LivePhysRegs.h
@@ -165,10 +165,17 @@ class LivePhysRegs {
   void dump() const;
 
 private:
+  /// Adds a register, taking the lane mask into consideration.
+  void addRegMaskPair(const MachineBasicBlock::RegisterMaskPair &Pair);
+
   /// Adds live-in registers from basic block \p MBB, taking associated
   /// lane masks into consideration.
   void addBlockLiveIns(const MachineBasicBlock &MBB);
 
+  /// Adds live-out registers from basic block \p MBB, taking associated
+  /// lane masks into consideration.
+  void addBlockLiveOuts(const MachineBasicBlock &MBB);
+
   /// Adds pristine registers. Pristine registers are callee saved registers
   /// that are unused in the function.
   void addPristines(const MachineFunction &MF);
diff --git a/llvm/lib/CodeGen/LivePhysRegs.cpp b/llvm/lib/CodeGen/LivePhysRegs.cpp
index bc711382420be..f1d31daaea530 100644
--- a/llvm/lib/CodeGen/LivePhysRegs.cpp
+++ b/llvm/lib/CodeGen/LivePhysRegs.cpp
@@ -151,23 +151,34 @@ bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
   return true;
 }
 
+/// Adds a register, taking associated lane masks into consideration.
+void LivePhysRegs::addRegMaskPair(
+    const MachineBasicBlock::RegisterMaskPair &Pair) {
+  MCRegister Reg = Pair.PhysReg;
+  LaneBitmask Mask = Pair.LaneMask;
+  MCSubRegIndexIterator S(Reg, TRI);
+  assert(Mask.any() && "Invalid livein mask");
+  if (Mask.all() || !S.isValid()) {
+    addReg(Reg);
+    return;
+  }
+  for (; S.isValid(); ++S) {
+    unsigned SI = S.getSubRegIndex();
+    if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
+      addReg(S.getSubReg());
+  }
+}
+
 /// Add live-in registers of basic block \p MBB to \p LiveRegs.
 void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
-  for (const auto &LI : MBB.liveins()) {
-    MCRegister Reg = LI.PhysReg;
-    LaneBitmask Mask = LI.LaneMask;
-    MCSubRegIndexIterator S(Reg, TRI);
-    assert(Mask.any() && "Invalid livein mask");
-    if (Mask.all() || !S.isValid()) {
-      addReg(Reg);
-      continue;
-    }
-    for (; S.isValid(); ++S) {
-      unsigned SI = S.getSubRegIndex();
-      if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
-        addReg(S.getSubReg());
-    }
-  }
+  for (const auto &LI : MBB.liveins())
+    addRegMaskPair(LI);
+}
+
+/// Add live-out registers of basic block \p MBB to \p LiveRegs.
+void LivePhysRegs::addBlockLiveOuts(const MachineBasicBlock &MBB) {
+  for (const auto &LO : MBB.liveouts())
+    addRegMaskPair(LO);
 }
 
 /// Adds all callee saved registers to \p LiveRegs.
@@ -207,9 +218,7 @@ void LivePhysRegs::addPristines(const MachineFunction &MF) {
 }
 
 void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
-  // To get the live-outs we simply merge the live-ins of all successors.
-  for (const MachineBasicBlock *Succ : MBB.successors())
-    addBlockLiveIns(*Succ);
+  addBlockLiveOuts(MBB);
   if (MBB.isReturnBlock()) {
     // Return blocks are a special case because we currently don't mark up
     // return instructions completely: specifically, there is no explicit
@@ -356,8 +365,8 @@ bool llvm::isPhysRegUsedAfter(Register Reg, MachineBasicBlock::iterator MBI) {
 
   // If we hit the end of the block, check whether Reg is live into a
   //  successor.
-  for (MachineBasicBlock *Succ : MBB->successors())
-    if (Succ->isLiveIn(Reg))
+  for (const auto &LO : MBB->liveouts())
+    if (LO.PhysReg == MCRegister(Reg) && LO.LaneMask.any())
       return true;
 
   return false;
diff --git a/llvm/lib/CodeGen/LiveRegUnits.cpp b/llvm/lib/CodeGen/LiveRegUnits.cpp
index d9c56b87a6bf4..0d87062169585 100644
--- a/llvm/lib/CodeGen/LiveRegUnits.cpp
+++ b/llvm/lib/CodeGen/LiveRegUnits.cpp
@@ -94,8 +94,8 @@ static void addBlockLiveIns(LiveRegUnits &LiveUnits,
 /// Add live-out registers of basic block \p MBB to \p LiveUnits.
 static void addBlockLiveOuts(LiveRegUnits &LiveUnits,
                              const MachineBasicBlock &MBB) {
-  for (const auto &LI : MBB.liveouts())
-    LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
+  for (const auto &LO : MBB.liveouts())
+    LiveUnits.addRegMasked(LO.PhysReg, LO.LaneMask);
 }
 
 /// Adds all callee saved registers to \p LiveUnits.

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

We should be removing all uses of LivePhysRegs

@MacDue MacDue enabled auto-merge (squash) August 23, 2025 06:16
@MacDue MacDue disabled auto-merge August 23, 2025 06:16
@MacDue MacDue merged commit c44c325 into llvm:main Aug 23, 2025
11 of 12 checks passed
@MacDue MacDue deleted the liveouts branch August 23, 2025 06:22
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 23, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-win-fast running on as-builder-3 while building llvm at step 6 "build-unified-tree".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/2/builds/32040

Here is the relevant piece of the build log for the reference
Step 6 (build-unified-tree) failure: build (failure)
...
[1220/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\AMDGPUEmitPrintf.cpp.obj
[1221/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\OutputSections.cpp.obj
[1222/4283] Building CXX object lib\DWARFLinker\Classic\CMakeFiles\LLVMDWARFLinkerClassic.dir\DWARFLinker.cpp.obj
[1223/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DIEAttributeCloner.cpp.obj
[1224/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DWARFLinkerUnit.cpp.obj
[1225/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\AcceleratorRecordsSaver.cpp.obj
[1226/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DWARFLinker.cpp.obj
[1227/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DWARFLinkerTypeUnit.cpp.obj
[1228/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DWARFLinkerImpl.cpp.obj
[1229/4283] Building CXX object lib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LivePhysRegs.cpp.obj
FAILED: lib/CodeGen/CMakeFiles/LLVMCodeGen.dir/LivePhysRegs.cpp.obj 
C:\ninja\ccache.exe C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1438~1.331\bin\Hostx64\x64\cl.exe  /nologo /TP -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\build\lib\CodeGen -IC:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\lib\CodeGen -IC:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\build\include -IC:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\include /DWIN32 /D_WINDOWS   /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2 /DNDEBUG -MD  /EHs-c- /GR- -std:c++17 /showIncludes /Folib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LivePhysRegs.cpp.obj /Fdlib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LLVMCodeGen.pdb /FS -c C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): error C2440: '<function-style-cast>': cannot convert from 'llvm::Register' to 'llvm::MCRegister'
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): note: 'llvm::MCRegister::MCRegister': ambiguous call to overloaded function
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\include\llvm/MC/MCRegister.h(100): note: could be 'llvm::MCRegister::MCRegister(llvm::MCRegister &&)'
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\include\llvm/MC/MCRegister.h(38): note: or       'llvm::MCRegister::MCRegister(unsigned int)'
C:\buildbot\as-builder-3\llvm-clang-x86_64-win-fast\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): note: while trying to match the argument list '(llvm::Register)'
[1230/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\SyntheticTypeNameBuilder.cpp.obj
[1231/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DWARFEmitterImpl.cpp.obj
[1232/4283] Building CXX object lib\DWARFLinker\Parallel\CMakeFiles\LLVMDWARFLinkerParallel.dir\DependencyTracker.cpp.obj
[1233/4283] Building CXX object lib\Frontend\HLSL\CMakeFiles\LLVMFrontendHLSL.dir\CBuffer.cpp.obj
[1234/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CallGraphUpdater.cpp.obj
[1235/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\AddDiscriminators.cpp.obj
[1236/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\AssumeBundleBuilder.cpp.obj
[1237/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\ControlFlowUtils.cpp.obj
[1238/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CodeMoverUtils.cpp.obj
[1239/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\BuildLibCalls.cpp.obj
[1240/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\BypassSlowDivision.cpp.obj
[1241/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CodeExtractor.cpp.obj
[1242/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\DeclareRuntimeLibcalls.cpp.obj
[1243/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\BreakCriticalEdges.cpp.obj
[1244/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CallPromotionUtils.cpp.obj
[1245/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CanonicalizeFreezeInLoops.cpp.obj
[1246/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\BasicBlockUtils.cpp.obj
[1247/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CountVisits.cpp.obj
[1248/4283] Building CXX object lib\Transforms\InstCombine\CMakeFiles\LLVMInstCombine.dir\InstCombineAddSub.cpp.obj
[1249/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\HelloWorld.cpp.obj
[1250/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\LCSSA.cpp.obj
[1251/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\CtorUtils.cpp.obj
[1252/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\DXILUpgrade.cpp.obj
[1253/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\Evaluator.cpp.obj
[1254/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\InjectTLIMappings.cpp.obj
[1255/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\FlattenCFG.cpp.obj
[1256/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\Debugify.cpp.obj
[1257/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\GuardUtils.cpp.obj
[1258/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\EntryExitInstrumenter.cpp.obj
[1259/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\LibCallsShrinkWrap.cpp.obj
[1260/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\Local.cpp.obj
[1261/4283] Building CXX object lib\Transforms\Utils\CMakeFiles\LLVMTransformUtils.dir\IRNormalizer.cpp.obj

@slydiman
Copy link
Contributor

The following buildbots are also broken after this patch

https://lab.llvm.org/buildbot/#/builders/197/builds/8321
https://lab.llvm.org/buildbot/#/builders/211/builds/1549

Please take a look and fix ASAP.

@MacDue
Copy link
Member Author

MacDue commented Aug 23, 2025

The following buildbots are also broken after this patch

https://lab.llvm.org/buildbot/#/builders/197/builds/8321 https://lab.llvm.org/buildbot/#/builders/211/builds/1549

Please take a look and fix ASAP.

Will do 👍 It looks like MSVC does not like the cast to MCRegister.

@MacDue
Copy link
Member Author

MacDue commented Aug 23, 2025

Possible fix in: #155084

@joker-eph
Copy link
Collaborator

joker-eph commented Aug 23, 2025

Why did you disable auto-merge and merged over the failed build?
Seems like the premerge did what it's meant to and caught the issue?

@MacDue
Copy link
Member Author

MacDue commented Aug 23, 2025

Why did you disable auto-merge and merged over the failed build? Seems like the premerge did what it's meant to and caught the issue?

I think I misinterpreted in the Windows error as being " '#' is not recognized as an internal or external command,", which seemed like some random CI flake, my bad. I find that since we've switched over from buildkite it's much harder to see what the error is in the CI logs.

@slydiman
Copy link
Contributor

Possible fix in: #155084

Still failed.

LivePhysRegs.cpp(369): error C2440: '': cannot convert from 'llvm::Register' to 'llvm::MCRegister'

19.622 [3970/130/2834]Building CXX object lib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LivePhysRegs.cpp.obj
FAILED: lib/CodeGen/CMakeFiles/LLVMCodeGen.dir/LivePhysRegs.cpp.obj 
ccache C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\cl.exe  /nologo /TP -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:\buildbot\as-builder-10\lldb-x86-64\build\lib\CodeGen -IC:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\lib\CodeGen -IC:\buildbot\as-builder-10\lldb-x86-64\build\include -IC:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\include -D__OPTIMIZE__ /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2  -MD  /EHs-c- /GR- -UNDEBUG -std:c++17 /showIncludes /Folib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LivePhysRegs.cpp.obj /Fdlib\CodeGen\CMakeFiles\LLVMCodeGen.dir\LLVMCodeGen.pdb /FS -c C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): error C2440: '<function-style-cast>': cannot convert from 'llvm::Register' to 'llvm::MCRegister'
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): note: 'llvm::MCRegister::MCRegister': ambiguous call to overloaded function
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\include\llvm/MC/MCRegister.h(100): note: could be 'llvm::MCRegister::MCRegister(llvm::MCRegister &&)'
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\include\llvm/MC/MCRegister.h(100): note: or       'llvm::MCRegister::MCRegister(const llvm::MCRegister &)'
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\include\llvm/MC/MCRegister.h(38): note: or       'llvm::MCRegister::MCRegister(unsigned int)'
C:\buildbot\as-builder-10\lldb-x86-64\llvm-project\llvm\lib\CodeGen\LivePhysRegs.cpp(369): note: while trying to match the argument list '(llvm::Register)'

@MacDue
Copy link
Member Author

MacDue commented Aug 23, 2025

That error seems to be from before #155084 was applied (as that patch removed the "<function-style-cast>").

@slydiman
Copy link
Contributor

You are right. This problem has been fixed. Sorry to bother you.

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.

6 participants