Skip to content
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

[StackColoring] Delete dead stack slots #75351

Merged
merged 2 commits into from
Dec 15, 2023

Conversation

mohammed-nurulhoque
Copy link
Contributor

@mohammed-nurulhoque mohammed-nurulhoque commented Dec 13, 2023

Fixes #72633 which got merged then reverted because of a test failure. It wasn't rebased before merging and in the meantime 3114bd32e75 made remapInstructions not run when SlotRemap is empty. This fixes the issue.

@llvmbot
Copy link
Collaborator

llvmbot commented Dec 13, 2023

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-debuginfo

Author: None (mohammed-nurulhoque)

Changes

The PR above got merged then reverted because of a test failure. It wasn't rebased before merging and in the meantime 3114bd32e75 made remapInstructions not run when SlotRemap is empty. This fixes the issue.


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

5 Files Affected:

  • (modified) llvm/lib/CodeGen/StackColoring.cpp (+17-1)
  • (modified) llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll (-1)
  • (modified) llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll (+2-6)
  • (added) llvm/test/CodeGen/RISCV/dead-stack-slot.ll (+25)
  • (modified) llvm/test/DebugInfo/COFF/lexicalblock.ll (+2-22)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 37f7aa9290054e..0f298f5c9c09fd 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -900,6 +900,15 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
   unsigned FixedMemOp = 0;
   unsigned FixedDbg = 0;
 
+  // Remove debug information for deleted slots.
+  erase_if(MF->getVariableDbgInfo(), [&](auto &VI) {
+    if (!VI.inStackSlot())
+      return false;
+    int Slot = VI.getStackSlot();
+    return Slot >= 0 && Intervals[Slot]->empty() &&
+           InterestingSlots.test(Slot) && !ConservativeSlots.test(Slot);
+  });
+
   // Remap debug information that refers to stack slots.
   for (auto &VI : MF->getVariableDbgInfo()) {
     if (!VI.Var || !VI.inStackSlot())
@@ -1250,8 +1259,15 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
 
   // Do not bother looking at empty intervals.
   for (unsigned I = 0; I < NumSlots; ++I) {
-    if (Intervals[SortedSlots[I]]->empty())
+    int Slot = SortedSlots[I];
+    if (Intervals[Slot]->empty()) {
+      if (InterestingSlots.test(Slot) && !ConservativeSlots.test(Slot)) {
+        RemovedSlots += 1;
+        ReducedSize += MFI->getObjectSize(Slot);
+        MFI->RemoveStackObject(Slot);
+      }
       SortedSlots[I] = -1;
+    }
   }
 
   // This is a simple greedy algorithm for merging allocas. First, sort the
diff --git a/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll b/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
index bf66a1ed042d22..1b0a803734ae9f 100644
--- a/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
+++ b/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
@@ -347,7 +347,6 @@ entry:
 
 ; 32BIT-LABEL:   stack:
 ; 32BIT-DAG:     - { id: 0, name: arg1, type: default, offset: 0, size: 4, alignment: 4,
-; 32BIT-DAG:     - { id: 1, name: arg2, type: default, offset: 0, size: 4, alignment: 4,
 ; 32BIT-DAG:     - { id: 2, name: '', type: default, offset: 0, size: 8, alignment: 8,
 ; 32BIT-DAG:     - { id: 3, name: '', type: default, offset: 0, size: 8, alignment: 8,
 
diff --git a/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll b/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
index ccf89aac2d5408..a8684fdfe1c568 100644
--- a/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
+++ b/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
@@ -138,9 +138,7 @@
 ; 64BIT-LABEL:   fixedStack:
 ; 64BIT-DAG:     - { id: 0, type: default, offset: 112, size: 8, alignment: 16, stack-id: default,
 
-; 64BIT-LABEL:   stack:
-; 64BIT-DAG:     - { id: 0, name: arg1, type: default, offset: 0, size: 8, alignment: 8,
-; 64BIT-DAG:     - { id: 1, name: arg2, type: default, offset: 0, size: 8, alignment: 8,
+; 64BIT-LABEL:   stack: []
 
 ; 64BIT-LABEL:   body:             |
 ; 64BIT-DAG:     liveins: $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10
@@ -305,9 +303,7 @@
 ; 64BIT-LABEL:   fixedStack:
 ; 64BIT-DAG:       - { id: 0, type: default, offset: 152, size: 8
 
-; 64BIT-LABEL:   stack:
-; 64BIT-DAG:       - { id: 0, name: arg1, type: default, offset: 0, size: 8
-; 64BIT-DAG:       - { id: 1, name: arg2, type: default, offset: 0, size: 8
+; 64BIT-LABEL:   stack:           []
 
 ; 64BIT-LABEL:     body:             |
 ; 64BIT-DAG:       liveins: $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9, $f10, $f11, $f12, $f13
diff --git a/llvm/test/CodeGen/RISCV/dead-stack-slot.ll b/llvm/test/CodeGen/RISCV/dead-stack-slot.ll
new file mode 100644
index 00000000000000..49b0d2ab58c4f6
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/dead-stack-slot.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s
+; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s
+
+; Remove the lifetime-marked alloca, but not the unmarked one.
+define signext i32 @f1() nounwind {
+; CHECK-LABEL: f1:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    addi sp, sp, -32
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:    addi sp, sp, 32
+; CHECK-NEXT:    ret
+  %1 = alloca [32 x i8], align 4
+  %2 = alloca [32 x i8], align 4
+  %3 = getelementptr inbounds [32 x i8], ptr %1, i64 0, i64 0
+  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3)
+  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3)
+  ret i32 0
+}
+
+declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
+
diff --git a/llvm/test/DebugInfo/COFF/lexicalblock.ll b/llvm/test/DebugInfo/COFF/lexicalblock.ll
index 40dd8f894252c2..3bfae85f6c9bad 100644
--- a/llvm/test/DebugInfo/COFF/lexicalblock.ll
+++ b/llvm/test/DebugInfo/COFF/lexicalblock.ll
@@ -63,32 +63,12 @@
 ; CHECK: LocalSym {
 ; CHECK:   VarName: localA
 ; CHECK: }
-; CHECK: LocalSym {
-; CHECK:   VarName: localB
-; CHECK: }
-; CHECK: BlockSym {
-; CHECK:   Kind: S_BLOCK32 {{.*}}
-; CHECK:   BlockName: 
-; CHECK: }
-; CHECK: ScopeEndSym {
-; CHECK:   Kind: S_END {{.*}}
-; CHECK: }
-; CHECK: BlockSym {
-; CHECK:   Kind: S_BLOCK32 {{.*}}
-; CHECK:   BlockName: 
-; CHECK: }
-; CHECK: ScopeEndSym {
-; CHECK:   Kind: S_END {{.*}}
-; CHECK: }
 ; CHECK: BlockSym {
 ; CHECK:   Kind: S_BLOCK32 {{.*}}
 ; CHECK:   BlockName: 
 ; CHECK: }
-; CHECK: ScopeEndSym {
-; CHECK: }
-; CHECK: BlockSym {
-; CHECK:   Kind: S_BLOCK32 {{.*}}
-; CHECK:   BlockName: 
+; CHECK: LocalSym {
+; CHECK:   VarName: localB
 ; CHECK: }
 ; CHECK: ScopeEndSym {
 ; CHECK:   Kind: S_END {{.*}}

@mohammed-nurulhoque mohammed-nurulhoque changed the title Fix https://github.com/llvm/llvm-project/pull/72633 [StackColoring] Delete dead stack slots Dec 13, 2023
deletes slots that have lifetime markers and the lifetime ranges are empty.
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@mohammed-nurulhoque mohammed-nurulhoque merged commit 08b306d into llvm:main Dec 15, 2023
5 checks passed
@ilovepi
Copy link
Contributor

ilovepi commented Dec 15, 2023

Hi, our Linux CI is seing the following failure after this patch, and based on the assert, I'm pretty sure its related to this change.

clang-cl: llvm/include/llvm/CodeGen/MachineFrameInfo.h:530: int64_t llvm::MachineFrameInfo::getObjectOffset(int) const: Assertion `!isDeadObjectIndex(ObjectIdx) && "Getting frame offset for a dead object?"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /b/s/w/ir/x/w/llvm_build/./bin/clang-cl --target=x86_64-pc-windows-msvc /nologo -TP -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_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/orc/.. -I/b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/orc/../../include -Xclang -ivfsoverlay -Xclang /b/s/w/ir/cache/windows_sdk/llvm-vfsoverlay.yaml /winsysroot /b/s/w/ir/cache/windows_sdk /Zc:inline /Zc:__cplusplus /Oi /bigobj /permissive- -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 -w14062 -we4238 /Gw -no-canonical-prefixes /W4 -Wno-unused-parameter /Z7 /O2 /Ob1 -std:c++17 -MD -fno-builtin -fno-sanitize=safe-stack -fno-lto /Oy- /GS- /Zc:threadSafeInit- -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta /Z7 -Wno-gnu -Wno-variadic-macros -Wno-c99-extensions /wd4146 /wd4291 /wd4391 /wd4722 /wd4800 -I/b/s/w/ir/x/w/llvm_build/include -I/b/s/w/ir/x/w/llvm-llvm-project/llvm/include /showIncludes /Focompiler-rt/lib/orc/CMakeFiles/RTOrc.x86_64.dir/coff_platform.cpp.obj /Fdcompiler-rt/lib/orc/CMakeFiles/RTOrc.x86_64.dir/ -c -- /b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/orc/coff_platform.cpp
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module '/b/s/w/ir/x/w/llvm-llvm-project/compiler-rt/lib/orc/coff_platform.cpp'.
4.	Running pass 'Prologue/Epilogue Insertion & Frame Finalization' on function '@"??$call@V?$Expected@V?$unordered_map@VExecutorAddr@__orc_rt@@V?$vector@VExecutorAddr@__orc_rt@@V?$allocator@VExecutorAddr@__orc_rt@@@std@@@std@@U?$hash@VExecutorAddr@__orc_rt@@@4@U?$equal_to@VExecutorAddr@__orc_rt@@@4@V?$allocator@U?$pair@$$CBVExecutorAddr@__orc_rt@@V?$vector@VExecutorAddr@__orc_rt@@V?$allocator@VExecutorAddr@__orc_rt@@@std@@@std@@@std@@@4@@std@@@__orc_rt@@VExecutorAddr@2@@?$WrapperFunction@$$A6A?AV?$SPSExpected@V?$SPSSequence@V?$SPSTuple@VSPSExecutorAddr@__orc_rt@@V?$SPSSequence@VSPSExecutorAddr@__orc_rt@@@2@@__orc_rt@@@__orc_rt@@@__orc_rt@@VSPSExecutorAddr@2@@Z@__orc_rt@@SA?AVError@1@PEBXAEAV?$Expected@V?$unordered_map@VExecutorAddr@__orc_rt@@V?$vector@VExecutorAddr@__orc_rt@@V?$allocator@VExecutorAddr@__orc_rt@@@std@@@std@@U?$hash@VExecutorAddr@__orc_rt@@@4@U?$equal_to@VExecutorAddr@__orc_rt@@@4@V?$allocator@U?$pair@$$CBVExecutorAddr@__orc_rt@@V?$vector@VExecutorAddr@__orc_rt@@V?$allocator@VExecutorAddr@__orc_rt@@@std@@@std@@@std@@@4@@std@@@1@AEBVExecutorAddr@1@@Z"'
#0 0x0000557e30175ef8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/b/s/w/ir/x/w/llvm_build/./bin/clang-cl+0x8643ef8)
clang-cl: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Fuchsia clang version 18.0.0git (https://llvm.googlesource.com/llvm-project 70579c95bd12cb3035178f80a65718fd94cbf2ea)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: /b/s/w/ir/x/w/llvm_build/./bin
clang-cl: note: diagnostic msg: 
********************

repro 2.zip

If this is going to be hard to fix, can you revert this until its ready to reland?

@uweigand
Copy link
Member

I'm seeing the same on s390x. Looks like there's still debug metadata referencing the deleted stack slot, and this causes the assert. I've attached a reduced test case.
bug-dead-object.ll.txt

ilovepi added a commit that referenced this pull request Dec 15, 2023
ilovepi added a commit to ilovepi/llvm-project that referenced this pull request Dec 15, 2023
This reverts commit 08b306d.

it causes the following assertion failure:
llvm/include/llvm/CodeGen/MachineFrameInfo.h:530: int64_t llvm::MachineFrameInfo::getObjectOffset(int) const: Assertion `!isDeadObjectIndex(ObjectIdx) && "Getting frame offset for a dead object?"' failed.
ilovepi added a commit that referenced this pull request Dec 15, 2023
This reverts commit 08b306d.

it causes the following assertion failure:
llvm/include/llvm/CodeGen/MachineFrameInfo.h:530: int64_t
llvm::MachineFrameInfo::getObjectOffset(int) const: Assertion
`!isDeadObjectIndex(ObjectIdx) && "Getting frame offset for a dead
object?"' failed.
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.

None yet

5 participants