• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; Fifth example from Doc/Coroutines.rst (final suspend)
2; RUN: opt < %s -O2 -enable-coroutines -preserve-alignment-assumptions-during-inlining=false -S | FileCheck %s
3; RUN: opt < %s -aa-pipeline=basic-aa -passes='default<O2>' -enable-coroutines -preserve-alignment-assumptions-during-inlining=false -S | FileCheck %s
4
5define i8* @f(i32 %n) {
6entry:
7  %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
8  %size = call i32 @llvm.coro.size.i32()
9  %alloc = call i8* @malloc(i32 %size)
10  %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
11  br label %while.cond
12while.cond:
13  %n.val = phi i32 [ %n, %entry ], [ %dec, %while.body ]
14  %cmp = icmp sgt i32 %n.val, 0
15  br i1 %cmp, label %while.body, label %while.end
16
17while.body:
18  %dec = add nsw i32 %n.val, -1
19  call void @print(i32 %n.val) #4
20  %s = call i8 @llvm.coro.suspend(token none, i1 false)
21  switch i8 %s, label %suspend [i8 0, label %while.cond
22                                i8 1, label %cleanup]
23while.end:
24  %s.final = call i8 @llvm.coro.suspend(token none, i1 true)
25  switch i8 %s.final, label %suspend [i8 0, label %trap
26                                      i8 1, label %cleanup]
27trap:
28  call void @llvm.trap()
29  unreachable
30cleanup:
31  %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
32  call void @free(i8* %mem)
33  br label %suspend
34suspend:
35  call i1 @llvm.coro.end(i8* %hdl, i1 false)
36  ret i8* %hdl
37}
38
39declare noalias i8* @malloc(i32)
40declare void @print(i32)
41declare void @llvm.trap()
42declare void @free(i8* nocapture)
43
44declare token @llvm.coro.id( i32, i8*, i8*, i8*)
45declare i32 @llvm.coro.size.i32()
46declare i8* @llvm.coro.begin(token, i8*)
47declare token @llvm.coro.save(i8*)
48declare i8 @llvm.coro.suspend(token, i1)
49declare i8* @llvm.coro.free(token, i8*)
50declare i1 @llvm.coro.end(i8*, i1)
51
52; CHECK-LABEL: @main
53define i32 @main() {
54entry:
55  %hdl = call i8* @f(i32 4)
56  br label %while
57while:
58  call void @llvm.coro.resume(i8* %hdl)
59  %done = call i1 @llvm.coro.done(i8* %hdl)
60  br i1 %done, label %end, label %while
61end:
62  call void @llvm.coro.destroy(i8* %hdl)
63  ret i32 0
64
65; CHECK:      call void @print(i32 4)
66; CHECK:      call void @print(i32 3)
67; CHECK:      call void @print(i32 2)
68; CHECK:      call void @print(i32 1)
69; CHECK:      ret i32 0
70}
71
72declare i1 @llvm.coro.done(i8*)
73declare void @llvm.coro.resume(i8*)
74declare void @llvm.coro.destroy(i8*)
75