[llvm-bugs] [Bug 50660] New: LLVM-IR for coroutines not properly lowered by opt

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jun 10 09:13:49 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50660

            Bug ID: 50660
           Summary: LLVM-IR for coroutines not properly lowered by opt
           Product: tools
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: llvmc
          Assignee: unassignedbugs at nondot.org
          Reporter: C.Perivol at ed.ac.uk
                CC: llvm-bugs at lists.llvm.org

I am trying to understand how llvm lowers coroutines and I think I encountered
a bug in the LLVM compiler. I created a small c program called
llvm_intrinsics.c that calls llvm builtins:

```
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

void* f(int n) {
  __builtin_coro_id(0, 0, 0, 0);
  int8_t* hdl = __builtin_coro_begin((int*)malloc(__builtin_coro_size()));
  for (;;) {
    printf("%d\n",++n);
    switch (__builtin_coro_suspend(0)) {
      case 0:
        continue;
      case 1:
        goto CLEANUP;
      default:
        goto SUSPEND;
    }
  }
CLEANUP:
  free(__builtin_coro_free(hdl));
SUSPEND:
  __builtin_coro_end(hdl, 0);
  return hdl;
}

int main() {
  void* hdl = f(4);
  __builtin_coro_resume(hdl);
  __builtin_coro_resume(hdl);
  __builtin_coro_destroy(hdl);
  return 0;
}
```

This should (and upon inspection does indeed) generate LLVM-IR code that is
almost exactly the example in the LLVM documentation [1]. Clang when building C
does not run the llvm transformation passes at all, so one needs to run opt by
hand. This is unfortunate generally but since the purposes of this exercise are
pedagogic it is not a problem. Here is a build script that builds the C file
also emitting the intermediate LLVM-IR files.

```
#!/usr/bin/env bash
set -e

echo "Compiling into LLVM IR"
clang -fcoroutines-ts  -emit-llvm -S llvm_intrinsics.c -o llvm_intrinsics.ll

# echo "Early.."
if [ "$1" == "--holistic-opt" ]; then
    echo "Running all coroutine passes"
    opt -coro-early -coro-split -coro-elide -coro-cleanup llvm_intrinsics.ll -S
-o llvm_intrinsics_clean.ll
else
    echo "Running each pass"
    echo "Early.."
    opt -coro-early llvm_intrinsics.ll -S -o llvm_intrinsics_early.ll
    echo "Split.."
    opt -coro-split llvm_intrinsics_early.ll -S -o llvm_intrinsics_split.ll
    echo "Elide.."
    opt -coro-elide llvm_intrinsics_split.ll -S -o llvm_intrinsics_elide.ll
    echo "Cleanup.."
    opt -coro-cleanup llvm_intrinsics_elide.ll -S -o llvm_intrinsics_clean.ll
fi

echo "Compiling LLVM"
clang -lm llvm_intrinsics_clean.ll -o llvm_intrinsics
./llvm_intrinsics
```

The result I expect when using this script to build and run llvm_intrinsics.c
is a) a successful build regardless of the argument provided to the script and
b) an executable that only prints 4\n5\n. However here is what it does:

```
$ ./build.sh
Compiling into LLVM IR
Running each pass
Early..
Split..
Elide..
Cleanup..
Compiling LLVM
fatal error: error in backend: Cannot select: intrinsic %llvm.coro.size
clang-10: error: clang frontend command failed with exit code 70 (use -v to see
invocation)
clang version 10.0.1 
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /nix/store/6pzqj9q656vc1msa675k75hmhsrfizsy-clang-10.0.1/bin
clang-10: note: diagnostic msg: PLEASE submit a bug report to  and include the
crash backtrace, preprocessed source, and associated run script.
clang-10: note: diagnostic msg: Error generating preprocessed source(s) - no
preprocessable inputs.


$ ./build.sh --holistic-opt | head
Compiling into LLVM IR
Running all coroutine passes
Compiling LLVM
5
6
7
8
9
10
11
```

To put it in words opt fails to run the passes separately and when it runs them
together it seems to run them but yields a wrong result. I also tried LLVM 12
and had the same results. Since C++ coroutines work fine I assume this has to
do with something I have misunderstood? Is there another way other than opt to
run the passes?

This report is adapted from an issue I opened in stackoverflow [2]


[1] https://llvm.org/docs/Coroutines.html#coroutine-representation
[2]
https://stackoverflow.com/questions/67923082/llvm-ir-for-coroutines-not-properly-lowered-by-opt?noredirect=1#comment120055691_67923082

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210610/3c4a64f1/attachment-0001.html>


More information about the llvm-bugs mailing list