[llvm] [InstCombine] Extend folding of aggregate construction to cases when source aggregates are partially available (PR #100828)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 26 23:00:26 PDT 2024


================
@@ -1117,9 +1118,35 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse(
     // aggregate produced by OrigIVI must have been originally extracted from
     // the same aggregate. Is that so? Can we find said original aggregate?
     SourceAggregate = FindCommonSourceAggregate(UseBB, Pred);
-    if (Describe(SourceAggregate) != AggregateDescription::Found)
-      return nullptr; // Give up.
-    IV.first->second = *SourceAggregate;
+    if (Describe(SourceAggregate) == AggregateDescription::Found) {
+      FoundSrcAgg = true;
+      IV.first->second = *SourceAggregate;
+    } else {
+      // If UseBB is the single successor of Pred, we can add InsertValue to
+      // Pred.
+      if (succ_size(Pred) != 1)
+        return nullptr; // Give up.
+    }
+  }
+
+  if (!FoundSrcAgg)
+    return nullptr;
+
+  // For predecessors without appropriate source aggregate, create one in the
+  // predecessor.
+  for (auto &It : SourceAggregates) {
+    if (Describe(It.second) == AggregateDescription::Found)
+      continue;
+
+    BasicBlock *Pred = It.first;
+    Builder.SetInsertPoint(Pred, Pred->getTerminator()->getIterator());
----------------
dtcxzyw wrote:

```
; opt -passes=instcombine reduced.ll -S
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define { ptr, i64 } @"_ZN91_$LT$core..str..iter..SplitWhitespace$u20$as$u20$core..iter..traits..iterator..Iterator$GT$4next17h160f3816bb012b94E"({ ptr, i64 } %0, ptr %1, i1 %.not) {
  br i1 %.not, label %3, label %4

3:                                                ; preds = %2
  store ptr null, ptr %1, align 8
  br label %5

4:                                                ; preds = %2
  %.elt1 = extractvalue { ptr, i64 } %0, 0
  br label %5

5:                                                ; preds = %4, %3
  %6 = phi ptr [ %.elt1, %4 ], [ null, %3 ]
  %.elt2 = extractvalue { ptr, i64 } %0, 1
  %7 = insertvalue { ptr, i64 } zeroinitializer, ptr %6, 0
  %8 = insertvalue { ptr, i64 } %7, i64 %.elt2, 1
  ret { ptr, i64 } %8
}
```
```
dtcxzyw at dtcxzyw:~/WorkSpace/Projects/compilers/LLVM/llvm-build$ bin/opt -passes=instcombine -S reduced.ll
Instruction does not dominate all uses!
  %.elt2 = extractvalue { ptr, i64 } %0, 1
  %4 = insertvalue { ptr, i64 } { ptr null, i64 poison }, i64 %.elt2, 1
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: bin/opt -passes=instcombine -S reduced.ll
1.      Running pass "verify" on module "reduced.ll"
 #0 0x0000722e04e0f222 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMSupport.so.20.0git+0x20f222)
 #1 0x0000722e04e0c0ef llvm::sys::RunSignalHandlers() (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMSupport.so.20.0git+0x20c0ef)
 #2 0x0000722e04e0c235 SignalHandler(int) Signals.cpp:0:0
 #3 0x0000722e04842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000722e048969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x0000722e048969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x0000722e048969fc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x0000722e04842476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x0000722e048287f3 abort ./stdlib/abort.c:81:7
 #9 0x0000722e04c6c56b llvm::report_fatal_error(llvm::Twine const&, bool) (.cold) ErrorHandling.cpp:0:0
#10 0x0000722e04d1b1a0 unsigned long std::uniform_int_distribution<unsigned long>::operator()<std::random_device>(std::random_device&, std::uniform_int_distribution<unsigned long>::param_type const&) (.isra.0) ExponentialBackoff.cpp:0:0
#11 0x0000722dfed8f642 llvm::VerifierPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x38f642)
#12 0x0000722e04fff3c5 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMOptDriver.so.20.0git+0x1f3c5)
#13 0x0000722dfed4b11a llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x34b11a)
#14 0x0000722e0500d1d7 llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMOptDriver.so.20.0git+0x2d1d7)
#15 0x0000722e05019192 optMain (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMOptDriver.so.20.0git+0x39192)
#16 0x0000722e04829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#17 0x0000722e04829e40 call_init ./csu/../csu/libc-start.c:128:20
#18 0x0000722e04829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#19 0x000063043daba095 _start (bin/opt+0x1095)
Aborted (core dumped)
```

https://github.com/llvm/llvm-project/pull/100828


More information about the llvm-commits mailing list