Reputation: 1109
I am working on learning the coroutine feature in LLVM and have been working on a simple test based on one of the examples from the documents.
When I try to run llc
on the generated the bitcode, I get the error message LLVM ERROR: Cannot select: intrinsic %llvm.coro.begin
. So far, the only similar error I've found was due to some platforms not supporting i1
for boolean, but none of the intrinsics I am calling use i1
.
Here is the LLVM IR, I have removed all the logic that would occur after the call to llvm.coro.begin
to get down to the minimum necessary for the error to happen:
; ModuleID = 'coro example'
source_filename = "coro example"
; Function Attrs: nounwind readnone
declare i32 @llvm.coro.size.i32() #0
; Function Attrs: argmemonly nounwind readonly
declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*) #1
; Function Attrs: nounwind
declare i8* @llvm.coro.begin(token, i8* writeonly) #2
define void @test() {
entry:
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
%size = call i32 @llvm.coro.size.i32()
%alloc = tail call i8* @malloc(i32 %size)
%hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc)
ret void
}
declare noalias i8* @malloc(i32)
attributes #0 = { nounwind readnone }
attributes #1 = { argmemonly nounwind readonly }
attributes #2 = { nounwind }
and the code which generated the above:
// Get coro intrinsics
std::vector<Type *> types(1, Type::getInt32Ty(*TheContext));
Function *decl_coro_size = Intrinsic::getDeclaration(TheModule.get(), Intrinsic::coro_size, types);
Function *decl_coro_id = Intrinsic::getDeclaration(TheModule.get(), Intrinsic::coro_id);
Function *decl_coro_begin = Intrinsic::getDeclaration(TheModule.get(), Intrinsic::coro_begin);
// create example function
std::vector<Type *> args;
FunctionType *FT =
FunctionType::get(Type::getVoidTy(*TheContext), args, false);
Function *F =
Function::Create(FT, Function::ExternalLinkage, name, TheModule.get());
BasicBlock* bb = BasicBlock::Create(*TheContext, "entry", F);
Builder->SetInsertPoint(bb);
// Get the coro token
std::vector<Value *> argsV;
argsV.push_back(ConstantInt::get(*TheContext, APInt(32, 0, true)));
PointerType* pty = PointerType::getInt8PtrTy(*TheContext);
auto cpn = ConstantPointerNull::get(pty);
argsV.push_back(cpn);
argsV.push_back(cpn);
argsV.push_back(cpn);
Value* val_coro_id = Builder->CreateCall(decl_coro_id, argsV, "id");
// Get the coro size
Value* val_coro_size = Builder->CreateCall(decl_coro_size, None, "size");
// Allocate space
Type* ity = Type::getInt32Ty(*TheContext);
Type* ty = Type::getInt8Ty(*TheContext);
Instruction* call_malloc = CallInst::CreateMalloc(bb, ity, ty, val_coro_size, nullptr, nullptr, "");
Value* val_alloc = Builder->Insert(call_malloc, "alloc");
// Begin coroutine
std::vector<Value *> beginArgs;
beginArgs.push_back(val_coro_id);
beginArgs.push_back(val_alloc);
Value* val_coro_begin = Builder->CreateCall(decl_coro_begin, beginArgs, "hdl");
Builder->CreateRetVoid();
Upvotes: 1
Views: 1192
Reputation: 125
I hit the same thing when experimenting with coroutines.
There might be a simpler way, but I solved it by building on the module optimization passes described here
llvm::PipelineTuningOptions pto;
pto.Coroutines = true; // <<<<<<<<<<<<<<<<
llvm::PassBuilder passBuilder(/*DebugLogging=*/true,
/*TargetMachine=*/nullptr, pto);
// ...
llvm::ModulePassManager modulePassManager =
passBuilder.buildPerModuleDefaultPipeline(olevel);
modulePassManager.run(*Module, moduleAnalysisManager);
Upvotes: 1