Search code examples
clangllvmcoroutine

How to properly modify IR in llvm pass?


I am working on a LLVM pass, the goal of which is to transform user-defined function to coroutines. For example, a simple noop function:

void f() {}

Transforms to:

define ptr @f_coro() {
init:
  %promise = alloca %CoroPromise, align 8
  call void @init_promise(ptr %promise) ; my own API
  %id = call token @llvm.coro.id(i32 0, ptr %promise, ptr null, ptr null)
  %size = call i32 @llvm.coro.size.i32()
  %hdl_alloc = call ptr @malloc(i32 %size)
  %hdl = call ptr @llvm.coro.begin(token %id, ptr %hdl_alloc)
  %0 = call i8 @llvm.coro.suspend(token none, i1 false)
  switch i8 %0, label %suspend [
    i8 0, label %terminator.1
    i8 1, label %cleanup
  ]

cleanup:                                       
  %1 = call ptr @llvm.coro.free(token %id, ptr %hdl)
  call void @free(ptr %1)
  br label %suspend

suspend:                                         
  %2 = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
  ret ptr %hdl

terminator.1:                                   
  call void @set_ret_val(ptr %promise, i32 0) ; my own API
  %3 = call i8 @llvm.coro.suspend(token none, i1 false)
  switch i8 %3, label %suspend [
    i8 0, label %cleanup
    i8 1, label %cleanup
  ]
}

However, when I run the pass using clang (version 18.0.0):

clang++ -fno-discard-value-names -std=c++2a -emit-llvm -S -fpass-plugin=Pass.so sample.cpp

It omits some of the instructions I generated (specifically, @llvm.coro.id), which causes subsequent code to fail compilation. How should I correctly perform such manipulations?"

I've tried using opt on pre-existing bytecode instead of clang. However, this complicates the pipeline.


Solution

  • Forgot to set presplitcoroutine attribute for my function.