<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Zero-sized alloca causes assertion failure in coroutine lowering"
   href="https://bugs.llvm.org/show_bug.cgi?id=49916">49916</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Zero-sized alloca causes assertion failure in coroutine lowering
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>aykevanlaethem@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, rjmccall@apple.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>See: <a href="https://llvm.godbolt.org/z/e9eGvrjd8">https://llvm.godbolt.org/z/e9eGvrjd8</a>

The code below crashes when running `opt -O2 --enable-coroutines`. This looks
like a bug in the coroutine passes: I would expect zero-sized alloca
instructions to work fine (and take up no space in the coroutine frame) but
instead this code crashes with the following error:

opt:
/home/ayke/src/github.com/tinygo-org/tinygo/llvm-project/llvm/include/llvm/Support/OptimizedStructLayout.h:52:
llvm::OptimizedStructLayoutField::OptimizedStructLayoutField(const void *,
uint64_t, llvm::Align, uint64_t): Assertion `Size > 0 && "adding an empty field
to the layout"' failed.
PLEASE submit a bug report to <a href="https://bugs.llvm.org/">https://bugs.llvm.org/</a> and include the crash
backtrace.
Stack dump:
0.      Program arguments: llvm-build/bin/opt -enable-coroutines -O2 -o
bugpoint.ll bugpoint-reduced-function.ll 
1.      Running pass 'CallGraph Pass Manager' on module
'bugpoint-reduced-function.ll'.
2.      While splitting coroutine @main.main
 #0 0x0000000003151c84 PrintStackTraceSignalHandler(void*)
(llvm-build/bin/opt+0x3151c84)
 #1 0x000000000314f85e llvm::sys::RunSignalHandlers()
(llvm-build/bin/opt+0x314f85e)
 #2 0x0000000003151fa5 SignalHandler(int) (llvm-build/bin/opt+0x3151fa5)
 #3 0x00007f6db4c55730 __restore_rt
(/lib/x86_64-linux-gnu/libpthread.so.0+0x12730)
 #4 0x00007f6db47877bb raise (/lib/x86_64-linux-gnu/libc.so.6+0x377bb)
 #5 0x00007f6db4772535 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22535)
 #6 0x00007f6db477240f (/lib/x86_64-linux-gnu/libc.so.6+0x2240f)
 #7 0x00007f6db4780102 (/lib/x86_64-linux-gnu/libc.so.6+0x30102)
 #8 0x0000000002a67c42 llvm::coro::buildCoroutineFrame(llvm::Function&,
llvm::coro::Shape&) (llvm-build/bin/opt+0x2a67c42)
 #9 0x0000000002a57649 splitCoroutine(llvm::Function&,
llvm::SmallVectorImpl<llvm::Function*>&) (llvm-build/bin/opt+0x2a57649)
#10 0x0000000002a5f332 (anonymous
namespace)::CoroSplitLegacy::runOnSCC(llvm::CallGraphSCC&)
(llvm-build/bin/opt+0x2a5f332)
#11 0x00000000021a7ed8 (anonymous
namespace)::CGPassManager::runOnModule(llvm::Module&)
(llvm-build/bin/opt+0x21a7ed8)
#12 0x00000000029c5964 llvm::legacy::PassManagerImpl::run(llvm::Module&)
(llvm-build/bin/opt+0x29c5964)
#13 0x00000000019390dd main (llvm-build/bin/opt+0x19390dd)
#14 0x00007f6db477409b __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x2409b)
#15 0x000000000192445a _start (llvm-build/bin/opt+0x192445a)

The code which causes this crash is below (and in the Godbolt link above):

---

target datalayout =
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64--linux"

declare i8* @malloc(i64)
declare void @abort()
declare void @free(i8*)
declare void @usePointer(i8*)

declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*)
declare i64 @llvm.coro.size.i64()
declare i8* @llvm.coro.begin(token, i8* writeonly)
declare i8 @llvm.coro.suspend(token, i1)
declare i1 @llvm.coro.end(i8*, i1)
declare i8* @llvm.coro.free(token, i8* nocapture readonly)
declare token @llvm.coro.save(i8*)

define void @main.main() {
entry:
  %stackalloc.alloca = alloca [0 x i8]
  %stackalloc = bitcast [0 x i8]* %stackalloc.alloca to i8*
  %coro.id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
  %coro.size = call i64 @llvm.coro.size.i64()
  %coro.alloc = call i8* @malloc(i64 %coro.size)
  %coro.state = call i8* @llvm.coro.begin(token %coro.id, i8* %coro.alloc)
  %coro.save = call token @llvm.coro.save(i8* %coro.state)
  %call.suspend = call i8 @llvm.coro.suspend(token %coro.save, i1 false)
  switch i8 %call.suspend, label %suspend [
    i8 0, label %wakeup
    i8 1, label %cleanup
  ]

wakeup:                                           ; preds = %entry
  call void @usePointer(i8* %stackalloc)
  br label %cleanup

suspend:                                          ; preds = %cleanup, %entry
  %unused = call i1 @llvm.coro.end(i8* %coro.state, i1 false)
  ret void

cleanup:                                          ; preds = %wakeup, %entry
  %coro.memFree = call i8* @llvm.coro.free(token %coro.id, i8* %coro.state)
  call void @free(i8* %coro.memFree)
  br label %suspend
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>