• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; Third example from Doc/Coroutines.rst (two suspend points)
2; RUN: opt < %s -O2 -enable-coroutines -S | FileCheck %s
3; RUN: opt < %s -aa-pipeline=basic-aa -passes='default<O2>' -enable-coroutines -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  %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
10  br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
11dyn.alloc:
12  %alloc = call i8* @malloc(i32 %size)
13  br label %coro.begin
14coro.begin:
15  %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
16  %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
17  br label %loop
18loop:
19  %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop.resume ]
20  call void @print(i32 %n.val) #4
21  %0 = call i8 @llvm.coro.suspend(token none, i1 false)
22  switch i8 %0, label %suspend [i8 0, label %loop.resume
23                                i8 1, label %cleanup]
24loop.resume:
25  %inc = add nsw i32 %n.val, 1
26  %sub = xor i32 %n.val, -1
27  call void @print(i32 %sub)
28  %1 = call i8 @llvm.coro.suspend(token none, i1 false)
29  switch i8 %1, label %suspend [i8 0, label %loop
30                                i8 1, label %cleanup]
31cleanup:
32  %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
33  call void @free(i8* %mem)
34  br label %suspend
35suspend:
36  call i1 @llvm.coro.end(i8* %hdl, i1 false)
37  ret i8* %hdl
38}
39
40; CHECK-LABEL: @main
41define i32 @main() {
42entry:
43  %hdl = call i8* @f(i32 4)
44  call void @llvm.coro.resume(i8* %hdl)
45  call void @llvm.coro.resume(i8* %hdl)
46  %c = ptrtoint i8* %hdl to i64
47  %to = icmp eq i64 %c, 0
48  br i1 %to, label %return, label %destroy
49destroy:
50  call void @llvm.coro.destroy(i8* %hdl)
51  br label %return
52return:
53  ret i32 0
54; CHECK-NOT:  i8* @malloc
55; CHECK:      call void @print(i32 4)
56; CHECK-NEXT: call void @print(i32 -5)
57; CHECK-NEXT: call void @print(i32 5)
58; CHECK:      ret i32 0
59}
60
61declare i8* @malloc(i32)
62declare void @free(i8*)
63declare void @print(i32)
64
65declare token @llvm.coro.id(i32, i8*, i8*, i8*)
66declare i1 @llvm.coro.alloc(token)
67declare i32 @llvm.coro.size.i32()
68declare i8* @llvm.coro.begin(token, i8*)
69declare i8 @llvm.coro.suspend(token, i1)
70declare i8* @llvm.coro.free(token, i8*)
71declare i1 @llvm.coro.end(i8*, i1)
72
73declare void @llvm.coro.resume(i8*)
74declare void @llvm.coro.destroy(i8*)
75