diff --git a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp --- a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp +++ b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp @@ -228,14 +228,14 @@ /// intrinsics. We need this function to be able to pass it to the async /// runtime execute API. static void addResumeFunction(ModuleOp module) { + if (module.lookupSymbol(kResume)) + return; + MLIRContext *ctx = module.getContext(); OpBuilder moduleBuilder(module.getBody()->getTerminator()); Location loc = module.getLoc(); - if (module.lookupSymbol(kResume)) - return; - auto voidTy = LLVM::LLVMVoidType::get(ctx); auto i8Ptr = LLVM::LLVMPointerType::get(IntegerType::get(ctx, 8)); @@ -637,6 +637,7 @@ Value handle = RuntimeAwaitAndResumeOpAdaptor(operands).handle(); // A pointer to coroutine resume intrinsic wrapper. + addResumeFunction(op->getParentOfType()); auto resumeFnTy = AsyncAPI::resumeFunctionType(op->getContext()); auto resumePtr = rewriter.create( op->getLoc(), LLVM::LLVMPointerType::get(resumeFnTy), kResume); @@ -663,6 +664,7 @@ matchAndRewrite(RuntimeResumeOp op, ArrayRef operands, ConversionPatternRewriter &rewriter) const override { // A pointer to coroutine resume intrinsic wrapper. + addResumeFunction(op->getParentOfType()); auto resumeFnTy = AsyncAPI::resumeFunctionType(op->getContext()); auto resumePtr = rewriter.create( op->getLoc(), LLVM::LLVMPointerType::get(resumeFnTy), kResume); @@ -862,8 +864,9 @@ ModuleOp module = getOperation(); MLIRContext *ctx = module->getContext(); - // Add declarations for all functions required by the coroutines lowering. - addResumeFunction(module); + // Add declarations for most functions required by the coroutines lowering. + // We delay adding the resume function until it's needed because it currently + // fails to compile unless '-O0' is specified. addAsyncRuntimeApiDeclarations(module); addCRuntimeDeclarations(module);