swift
swift copied to clipboard
Optimizer crashes due to calling `SmallVector::front()` on an empty vector
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?
This might be a duplicate of #75468, in which I found a medium sized project that produces this crash
@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);
+ }
}
}
Any update ? It seems introduced in https://github.com/swiftlang/swift/commit/ae797d43e93cbd6af9207ecc197537d7abd687e1#r144613237
I fixed this in: https://github.com/swiftlang/swift/pull/75596
This seems to be fixed by https://github.com/swiftlang/swift/pull/75596. Closing.