swift icon indicating copy to clipboard operation
swift copied to clipboard

Optimizer crashes due to calling `SmallVector::front()` on an empty vector

Open ellishg opened this issue 1 year ago • 2 comments

Description

Swift is crashing during an optimization pass in RegionAnalysis.cpp. The code is trying to call SmallVector::front() on assignOperands when it is empty.

https://github.com/swiftlang/swift/blob/6906fc208423e26cd883f2d042d150d3f2bf9c3a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp#L1747-L1752

Reproduction

I do not have a small reproducer, but I am happy to provided additional information if needed.

Stack dump

(lldb)
frame #6: 0x00005555581a0f68 swiftc`void swift::regionanalysisimpl::PartitionOpTranslator::translateSILMultiAssign<llvm::SmallVector<swift::SILValue, 8u>, llvm::SmallVector<swift::Operand*, 8u>>(llvm::SmallVector<swift::SILValue, 8u> const&, llvm::SmallVector<swift::Operand*, 8u> const&, swift::SILIsolationInfo, bool) [inlined] llvm::SmallVectorTemplateCommon<std::pair<swift::Operand*, swift::SILValue>, void>::front(this=0x00007ffff4ff62b8) at SmallVector.h:303:5
   300 	  }
   301
   302 	  reference front() {
-> 303 	    assert(!empty());
   304 	    return begin()[0];
   305 	  }
   306 	  const_reference front() const {
(lldb)
frame #7: 0x00005555581a0f49 swiftc`void swift::regionanalysisimpl::PartitionOpTranslator::translateSILMultiAssign<llvm::SmallVector<swift::SILValue, 8u>, llvm::SmallVector<swift::Operand*, 8u>>(this=0x00007ffff0fe3ed0, resultValues=0x00007ffff4ff6470, sourceValues=<unavailable>, resultIsolationInfoOverride=SILIsolationInfo @ 0x00007ffff4ff63b0, requireSrcValues=true) at RegionAnalysis.cpp:1751:46
   1748	      SILValue next = assignResultsRef.front();
   1749	      assignResultsRef = assignResultsRef.drop_front();
   1750
-> 1751	      builder.addAssign(next, assignOperands.front().first);
   1752	    }
   1753	  }
   1754
(lldb) p assignOperands.size()
(size_t) 0

Expected behavior

The compiler should not crash.

Environment

Swift version 6.0

Additional information

The code was recently changed in https://github.com/swiftlang/swift/commit/753f06ef376ce95ad9e9001f968837d4202c151f. I'm hopeful the fix will be as simple as checking if assignOperands is empty and handling that case. @gottesmm would you mind taking a look?

ellishg avatar Jul 26 '24 04:07 ellishg

This might be a duplicate of #75468, in which I found a medium sized project that produces this crash

ChristophKaser avatar Jul 26 '24 07:07 ChristophKaser

@gottesmm Here is my temporary fix to unbreak our builds, but honestly it's just a guess. Can you see something wrong with this?

diff --git a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp
index 16220c17a35..4a034bc383f 100644
--- a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp
+++ b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp
@@ -1748,6 +1748,12 @@ public:
       SILValue next = assignResultsRef.front();
       assignResultsRef = assignResultsRef.drop_front();

+      if (assignOperands.empty()) {
+        builder.addAssignFresh(next);
+      } else
         builder.addAssign(next, assignOperands.front().first);
+      }
     }
   }

ellishg avatar Jul 26 '24 23:07 ellishg

Any update ? It seems introduced in https://github.com/swiftlang/swift/commit/ae797d43e93cbd6af9207ecc197537d7abd687e1#r144613237

dreampiggy avatar Jul 31 '24 09:07 dreampiggy

I fixed this in: https://github.com/swiftlang/swift/pull/75596

gottesmm avatar Jul 31 '24 23:07 gottesmm

This seems to be fixed by https://github.com/swiftlang/swift/pull/75596. Closing.

ellishg avatar Aug 05 '24 22:08 ellishg