LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 48188 - Assertion failed: (TmpVec.size() > 1), function buildUnmerge, llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp, line 609
Summary: Assertion failed: (TmpVec.size() > 1), function buildUnmerge, llvm/lib/CodeGe...
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: GlobalISel (show other bugs)
Version: 11.0
Hardware: Macintosh MacOS X
: P normal
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: release-12.0.0
  Show dependency tree
 
Reported: 2020-11-15 17:21 PST by Jake Goulding
Modified: 2021-03-05 09:38 PST (History)
6 users (show)

See Also:
Fixed By Commit(s): c35761db0f078f74550ef56bfc0745c162d76967 52510d84802b


Attachments
LLVM IR to reproduce the crash (9.89 KB, text/plain)
2020-11-15 17:21 PST, Jake Goulding
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jake Goulding 2020-11-15 17:21:03 PST
Created attachment 24164 [details]
LLVM IR to reproduce the crash

```
 % ./bin/llc ../build/bugpoint-reduced-simplified.ll -filetype=obj -mcpu=apple-a12 -march=arm64 -O0
Assertion failed: (TmpVec.size() > 1), function buildUnmerge, file /Users/shepmaster/Projects/llvm-project/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp, line 609.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./bin/llc ../build/bugpoint-reduced-simplified.ll -filetype=obj -mcpu=apple-a12 -march=arm64 -O0
1.      Running pass 'Function Pass Manager' on module '../build/bugpoint-reduced-simplified.ll'.
2.      Running pass 'Legalizer' on function '@_ZN3std2io5stdio8print_to17h83af8c48359573cfE'
0  llc                      0x0000000106c22658 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 76
1  llc                      0x0000000106c22b68 PrintStackTraceSignalHandler(void*) + 32
2  llc                      0x0000000106c20bd8 llvm::sys::RunSignalHandlers() + 124
3  llc                      0x0000000106c24ac4 SignalHandler(int) + 208
4  libsystem_platform.dylib 0x000000018941cc44 _sigtramp + 56
5  libsystem_pthread.dylib  0x00000001893d4c24 pthread_kill + 292
6  libsystem_c.dylib        0x000000018931c864 abort + 104
7  libsystem_c.dylib        0x000000018931bb68 err + 0
8  llc                      0x000000010729d1dc llvm::MachineIRBuilder::buildUnmerge(llvm::ArrayRef<llvm::Register>, llvm::SrcOp const&) + 168
9  llc                      0x000000010724c894 llvm::LegalizerHelper::extractParts(llvm::Register, llvm::LLT, int, llvm::SmallVectorImpl<llvm::Register>&) + 248
10 llc                      0x0000000107244c08 llvm::LegalizerHelper::narrowScalar(llvm::MachineInstr&, unsigned int, llvm::LLT) + 8564
11 llc                      0x0000000107241ec0 llvm::LegalizerHelper::legalizeInstrStep(llvm::MachineInstr&) + 492
12 llc                      0x000000010722bf48 llvm::Legalizer::legalizeMachineFunction(llvm::MachineFunction&, llvm::LegalizerInfo const&, llvm::ArrayRef<llvm::GISelChangeObserver*>, llvm::LostDebugLocObserver&, llvm::MachineIRBuilder&) + 1104
13 llc                      0x000000010722da2c llvm::Legalizer::runOnMachineFunction(llvm::MachineFunction&) + 964
14 llc                      0x000000010552ce70 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 460
15 llc                      0x0000000105d4a29c llvm::FPPassManager::runOnFunction(llvm::Function&) + 548
16 llc                      0x0000000105d51a68 llvm::FPPassManager::runOnModule(llvm::Module&) + 116
17 llc                      0x0000000105d4ab08 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) + 688
18 llc                      0x0000000105d4a6a0 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 272
19 llc                      0x0000000105d51e84 llvm::legacy::PassManager::run(llvm::Module&) + 36
20 llc                      0x0000000104597258 compileModule(char**, llvm::LLVMContext&) + 5304
21 llc                      0x0000000104595590 main + 1288
22 libdyld.dylib            0x00000001893f0f54 start + 4
```

Doing some light debugging, I see that `NumParts` is `1`; this is what triggers the assertion. This is computed from `SizeOp0 / NarrowSize` when `SizeOp0` is `120` and `NarrowSize` is `64`.

The Rust fork is currently at 2e10b7a39b930ef8d9c4362509d8835b221fbc0a. git-bisect says that the attached LLVM IR compiles successfully starting at db464a3dbf0e8fed363a7b2b9a5b320514ca60f8.

However, I don't believe that the bug is actually *fixed*, merely hidden. The commit in question performs more transformations to the code and causes `SizeOp0` to be 128. When I cherry-picked that commit back to the Rust fork, the problems continue to manifest.
Comment 1 Jake Goulding 2020-11-15 17:22:57 PST
Looking elsewhere in the file, I see this code:

```
// FIXME: add support for when SizeOp0 isn't an exact multiple of
// NarrowSize.
if (SizeOp0 % NarrowSize != 0)
  return UnableToLegalize;
```

Applying an equivalent patch to the `G_PHI` case **does** appear to fix the issue in the Rust repository:

```
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 244e7a9583d..5ee9ba91760 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -1024,6 +1024,11 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
     Observer.changedInstr(MI);
     return Legalized;
   case TargetOpcode::G_PHI: {
+    // FIXME: add support for when SizeOp0 isn't an exact multiple of
+    // NarrowSize.
+    if (SizeOp0 % NarrowSize != 0)
+      return UnableToLegalize;
+
     unsigned NumParts = SizeOp0 / NarrowSize;
     SmallVector<Register, 2> DstRegs(NumParts);
     SmallVector<SmallVector<Register, 2>, 2> SrcRegs(MI.getNumOperands() / 2);
```
Comment 2 Nikita Popov 2021-03-01 12:01:40 PST
Encountered what I believe is the same issue during the LLVM 12 upgrade in rustc, with the following reduced test case:

; RUN: llc -O0 < %s
target triple = "aarch64-unknown-linux-gnu"

define void @test() {
entry:
  br label %loop

loop:                                             
  %p = phi i72 [ 0, %entry ], [ %p, %loop ]
  br label %loop
}
Comment 3 Nikita Popov 2021-03-01 12:48:01 PST
Existing candidate patch: https://reviews.llvm.org/D92446
Comment 5 Tom Stellard 2021-03-05 09:38:29 PST
Merged: 52510d84802b