125f80e16SEugene Zhulenev //===- AsyncToAsyncRuntime.cpp - Lower from Async to Async Runtime --------===//
225f80e16SEugene Zhulenev //
325f80e16SEugene Zhulenev // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
425f80e16SEugene Zhulenev // See https://llvm.org/LICENSE.txt for license information.
525f80e16SEugene Zhulenev // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
625f80e16SEugene Zhulenev //
725f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
825f80e16SEugene Zhulenev //
925f80e16SEugene Zhulenev // This file implements lowering from high level async operations to async.coro
1025f80e16SEugene Zhulenev // and async.runtime operations.
1125f80e16SEugene Zhulenev //
1225f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
1325f80e16SEugene Zhulenev 
1425f80e16SEugene Zhulenev #include "PassDetail.h"
1525f80e16SEugene Zhulenev #include "mlir/Dialect/Async/IR/Async.h"
1625f80e16SEugene Zhulenev #include "mlir/Dialect/Async/Passes.h"
1725f80e16SEugene Zhulenev #include "mlir/Dialect/StandardOps/IR/Ops.h"
1825f80e16SEugene Zhulenev #include "mlir/IR/BlockAndValueMapping.h"
1925f80e16SEugene Zhulenev #include "mlir/IR/ImplicitLocOpBuilder.h"
2025f80e16SEugene Zhulenev #include "mlir/IR/PatternMatch.h"
2125f80e16SEugene Zhulenev #include "mlir/Transforms/DialectConversion.h"
2225f80e16SEugene Zhulenev #include "mlir/Transforms/RegionUtils.h"
2325f80e16SEugene Zhulenev #include "llvm/ADT/SetVector.h"
24297a5b7cSNico Weber #include "llvm/Support/Debug.h"
2525f80e16SEugene Zhulenev 
2625f80e16SEugene Zhulenev using namespace mlir;
2725f80e16SEugene Zhulenev using namespace mlir::async;
2825f80e16SEugene Zhulenev 
2925f80e16SEugene Zhulenev #define DEBUG_TYPE "async-to-async-runtime"
3025f80e16SEugene Zhulenev // Prefix for functions outlined from `async.execute` op regions.
3125f80e16SEugene Zhulenev static constexpr const char kAsyncFnPrefix[] = "async_execute_fn";
3225f80e16SEugene Zhulenev 
3325f80e16SEugene Zhulenev namespace {
3425f80e16SEugene Zhulenev 
3525f80e16SEugene Zhulenev class AsyncToAsyncRuntimePass
3625f80e16SEugene Zhulenev     : public AsyncToAsyncRuntimeBase<AsyncToAsyncRuntimePass> {
3725f80e16SEugene Zhulenev public:
3825f80e16SEugene Zhulenev   AsyncToAsyncRuntimePass() = default;
3925f80e16SEugene Zhulenev   void runOnOperation() override;
4025f80e16SEugene Zhulenev };
4125f80e16SEugene Zhulenev 
4225f80e16SEugene Zhulenev } // namespace
4325f80e16SEugene Zhulenev 
4425f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
4525f80e16SEugene Zhulenev // async.execute op outlining to the coroutine functions.
4625f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
4725f80e16SEugene Zhulenev 
4825f80e16SEugene Zhulenev /// Function targeted for coroutine transformation has two additional blocks at
4925f80e16SEugene Zhulenev /// the end: coroutine cleanup and coroutine suspension.
5025f80e16SEugene Zhulenev ///
5125f80e16SEugene Zhulenev /// async.await op lowering additionaly creates a resume block for each
5225f80e16SEugene Zhulenev /// operation to enable non-blocking waiting via coroutine suspension.
5325f80e16SEugene Zhulenev namespace {
5425f80e16SEugene Zhulenev struct CoroMachinery {
5539957aa4SEugene Zhulenev   FuncOp func;
5639957aa4SEugene Zhulenev 
5725f80e16SEugene Zhulenev   // Async execute region returns a completion token, and an async value for
5825f80e16SEugene Zhulenev   // each yielded value.
5925f80e16SEugene Zhulenev   //
6025f80e16SEugene Zhulenev   //   %token, %result = async.execute -> !async.value<T> {
6125f80e16SEugene Zhulenev   //     %0 = constant ... : T
6225f80e16SEugene Zhulenev   //     async.yield %0 : T
6325f80e16SEugene Zhulenev   //   }
6425f80e16SEugene Zhulenev   Value asyncToken; // token representing completion of the async region
6525f80e16SEugene Zhulenev   llvm::SmallVector<Value, 4> returnValues; // returned async values
6625f80e16SEugene Zhulenev 
6725f80e16SEugene Zhulenev   Value coroHandle; // coroutine handle (!async.coro.handle value)
6839957aa4SEugene Zhulenev   Block *setError;  // switch completion token and all values to error state
6925f80e16SEugene Zhulenev   Block *cleanup;   // coroutine cleanup block
7025f80e16SEugene Zhulenev   Block *suspend;   // coroutine suspension block
7125f80e16SEugene Zhulenev };
7225f80e16SEugene Zhulenev } // namespace
7325f80e16SEugene Zhulenev 
7425f80e16SEugene Zhulenev /// Builds an coroutine template compatible with LLVM coroutines switched-resume
7525f80e16SEugene Zhulenev /// lowering using `async.runtime.*` and `async.coro.*` operations.
7625f80e16SEugene Zhulenev ///
7725f80e16SEugene Zhulenev /// See LLVM coroutines documentation: https://llvm.org/docs/Coroutines.html
7825f80e16SEugene Zhulenev ///
7925f80e16SEugene Zhulenev ///  - `entry` block sets up the coroutine.
8039957aa4SEugene Zhulenev ///  - `set_error` block sets completion token and async values state to error.
8125f80e16SEugene Zhulenev ///  - `cleanup` block cleans up the coroutine state.
8225f80e16SEugene Zhulenev ///  - `suspend block after the @llvm.coro.end() defines what value will be
8325f80e16SEugene Zhulenev ///    returned to the initial caller of a coroutine. Everything before the
8425f80e16SEugene Zhulenev ///    @llvm.coro.end() will be executed at every suspension point.
8525f80e16SEugene Zhulenev ///
8625f80e16SEugene Zhulenev /// Coroutine structure (only the important bits):
8725f80e16SEugene Zhulenev ///
8825f80e16SEugene Zhulenev ///   func @async_execute_fn(<function-arguments>)
8925f80e16SEugene Zhulenev ///        -> (!async.token, !async.value<T>)
9025f80e16SEugene Zhulenev ///   {
9125f80e16SEugene Zhulenev ///     ^entry(<function-arguments>):
9225f80e16SEugene Zhulenev ///       %token = <async token> : !async.token    // create async runtime token
9325f80e16SEugene Zhulenev ///       %value = <async value> : !async.value<T> // create async value
9425f80e16SEugene Zhulenev ///       %id = async.coro.id                      // create a coroutine id
9525f80e16SEugene Zhulenev ///       %hdl = async.coro.begin %id              // create a coroutine handle
9625f80e16SEugene Zhulenev ///       br ^cleanup
9725f80e16SEugene Zhulenev ///
9839957aa4SEugene Zhulenev ///     ^set_error: // this block created lazily only if needed (see code below)
9939957aa4SEugene Zhulenev ///       async.runtime.set_error %token : !async.token
10039957aa4SEugene Zhulenev ///       async.runtime.set_error %value : !async.value<T>
10139957aa4SEugene Zhulenev ///       br ^cleanup
10239957aa4SEugene Zhulenev ///
10325f80e16SEugene Zhulenev ///     ^cleanup:
10425f80e16SEugene Zhulenev ///       async.coro.free %hdl // delete the coroutine state
10525f80e16SEugene Zhulenev ///       br ^suspend
10625f80e16SEugene Zhulenev ///
10725f80e16SEugene Zhulenev ///     ^suspend:
10825f80e16SEugene Zhulenev ///       async.coro.end %hdl // marks the end of a coroutine
10925f80e16SEugene Zhulenev ///       return %token, %value : !async.token, !async.value<T>
11025f80e16SEugene Zhulenev ///   }
11125f80e16SEugene Zhulenev ///
11225f80e16SEugene Zhulenev /// The actual code for the async.execute operation body region will be inserted
11325f80e16SEugene Zhulenev /// before the entry block terminator.
11425f80e16SEugene Zhulenev ///
11525f80e16SEugene Zhulenev ///
11625f80e16SEugene Zhulenev static CoroMachinery setupCoroMachinery(FuncOp func) {
11725f80e16SEugene Zhulenev   assert(func.getBody().empty() && "Function must have empty body");
11825f80e16SEugene Zhulenev 
11925f80e16SEugene Zhulenev   MLIRContext *ctx = func.getContext();
12025f80e16SEugene Zhulenev   Block *entryBlock = func.addEntryBlock();
12125f80e16SEugene Zhulenev 
12225f80e16SEugene Zhulenev   auto builder = ImplicitLocOpBuilder::atBlockBegin(func->getLoc(), entryBlock);
12325f80e16SEugene Zhulenev 
12425f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
12525f80e16SEugene Zhulenev   // Allocate async token/values that we will return from a ramp function.
12625f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
12725f80e16SEugene Zhulenev   auto retToken = builder.create<RuntimeCreateOp>(TokenType::get(ctx)).result();
12825f80e16SEugene Zhulenev 
12925f80e16SEugene Zhulenev   llvm::SmallVector<Value, 4> retValues;
13025f80e16SEugene Zhulenev   for (auto resType : func.getCallableResults().drop_front())
13125f80e16SEugene Zhulenev     retValues.emplace_back(builder.create<RuntimeCreateOp>(resType).result());
13225f80e16SEugene Zhulenev 
13325f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
13425f80e16SEugene Zhulenev   // Initialize coroutine: get coroutine id and coroutine handle.
13525f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
13625f80e16SEugene Zhulenev   auto coroIdOp = builder.create<CoroIdOp>(CoroIdType::get(ctx));
13725f80e16SEugene Zhulenev   auto coroHdlOp =
13825f80e16SEugene Zhulenev       builder.create<CoroBeginOp>(CoroHandleType::get(ctx), coroIdOp.id());
13925f80e16SEugene Zhulenev 
14025f80e16SEugene Zhulenev   Block *cleanupBlock = func.addBlock();
14125f80e16SEugene Zhulenev   Block *suspendBlock = func.addBlock();
14225f80e16SEugene Zhulenev 
14325f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
14425f80e16SEugene Zhulenev   // Coroutine cleanup block: deallocate coroutine frame, free the memory.
14525f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
14625f80e16SEugene Zhulenev   builder.setInsertionPointToStart(cleanupBlock);
14725f80e16SEugene Zhulenev   builder.create<CoroFreeOp>(coroIdOp.id(), coroHdlOp.handle());
14825f80e16SEugene Zhulenev 
14925f80e16SEugene Zhulenev   // Branch into the suspend block.
15025f80e16SEugene Zhulenev   builder.create<BranchOp>(suspendBlock);
15125f80e16SEugene Zhulenev 
15225f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
15325f80e16SEugene Zhulenev   // Coroutine suspend block: mark the end of a coroutine and return allocated
15425f80e16SEugene Zhulenev   // async token.
15525f80e16SEugene Zhulenev   // ------------------------------------------------------------------------ //
15625f80e16SEugene Zhulenev   builder.setInsertionPointToStart(suspendBlock);
15725f80e16SEugene Zhulenev 
15825f80e16SEugene Zhulenev   // Mark the end of a coroutine: async.coro.end
15925f80e16SEugene Zhulenev   builder.create<CoroEndOp>(coroHdlOp.handle());
16025f80e16SEugene Zhulenev 
16125f80e16SEugene Zhulenev   // Return created `async.token` and `async.values` from the suspend block.
16225f80e16SEugene Zhulenev   // This will be the return value of a coroutine ramp function.
16325f80e16SEugene Zhulenev   SmallVector<Value, 4> ret{retToken};
16425f80e16SEugene Zhulenev   ret.insert(ret.end(), retValues.begin(), retValues.end());
16525f80e16SEugene Zhulenev   builder.create<ReturnOp>(ret);
16625f80e16SEugene Zhulenev 
16725f80e16SEugene Zhulenev   // Branch from the entry block to the cleanup block to create a valid CFG.
16825f80e16SEugene Zhulenev   builder.setInsertionPointToEnd(entryBlock);
16925f80e16SEugene Zhulenev   builder.create<BranchOp>(cleanupBlock);
17025f80e16SEugene Zhulenev 
17125f80e16SEugene Zhulenev   // `async.await` op lowering will create resume blocks for async
17225f80e16SEugene Zhulenev   // continuations, and will conditionally branch to cleanup or suspend blocks.
17325f80e16SEugene Zhulenev 
17425f80e16SEugene Zhulenev   CoroMachinery machinery;
17539957aa4SEugene Zhulenev   machinery.func = func;
17625f80e16SEugene Zhulenev   machinery.asyncToken = retToken;
17725f80e16SEugene Zhulenev   machinery.returnValues = retValues;
17825f80e16SEugene Zhulenev   machinery.coroHandle = coroHdlOp.handle();
17939957aa4SEugene Zhulenev   machinery.setError = nullptr; // created lazily only if needed
18025f80e16SEugene Zhulenev   machinery.cleanup = cleanupBlock;
18125f80e16SEugene Zhulenev   machinery.suspend = suspendBlock;
18225f80e16SEugene Zhulenev   return machinery;
18325f80e16SEugene Zhulenev }
18425f80e16SEugene Zhulenev 
18539957aa4SEugene Zhulenev // Lazily creates `set_error` block only if it is required for lowering to the
18639957aa4SEugene Zhulenev // runtime operations (see for example lowering of assert operation).
18739957aa4SEugene Zhulenev static Block *setupSetErrorBlock(CoroMachinery &coro) {
18839957aa4SEugene Zhulenev   if (coro.setError)
18939957aa4SEugene Zhulenev     return coro.setError;
19039957aa4SEugene Zhulenev 
19139957aa4SEugene Zhulenev   coro.setError = coro.func.addBlock();
19239957aa4SEugene Zhulenev   coro.setError->moveBefore(coro.cleanup);
19339957aa4SEugene Zhulenev 
19439957aa4SEugene Zhulenev   auto builder =
19539957aa4SEugene Zhulenev       ImplicitLocOpBuilder::atBlockBegin(coro.func->getLoc(), coro.setError);
19639957aa4SEugene Zhulenev 
19739957aa4SEugene Zhulenev   // Coroutine set_error block: set error on token and all returned values.
19839957aa4SEugene Zhulenev   builder.create<RuntimeSetErrorOp>(coro.asyncToken);
19939957aa4SEugene Zhulenev   for (Value retValue : coro.returnValues)
20039957aa4SEugene Zhulenev     builder.create<RuntimeSetErrorOp>(retValue);
20139957aa4SEugene Zhulenev 
20239957aa4SEugene Zhulenev   // Branch into the cleanup block.
20339957aa4SEugene Zhulenev   builder.create<BranchOp>(coro.cleanup);
20439957aa4SEugene Zhulenev 
20539957aa4SEugene Zhulenev   return coro.setError;
20639957aa4SEugene Zhulenev }
20739957aa4SEugene Zhulenev 
20825f80e16SEugene Zhulenev /// Outline the body region attached to the `async.execute` op into a standalone
20925f80e16SEugene Zhulenev /// function.
21025f80e16SEugene Zhulenev ///
21125f80e16SEugene Zhulenev /// Note that this is not reversible transformation.
21225f80e16SEugene Zhulenev static std::pair<FuncOp, CoroMachinery>
21325f80e16SEugene Zhulenev outlineExecuteOp(SymbolTable &symbolTable, ExecuteOp execute) {
21425f80e16SEugene Zhulenev   ModuleOp module = execute->getParentOfType<ModuleOp>();
21525f80e16SEugene Zhulenev 
21625f80e16SEugene Zhulenev   MLIRContext *ctx = module.getContext();
21725f80e16SEugene Zhulenev   Location loc = execute.getLoc();
21825f80e16SEugene Zhulenev 
21925f80e16SEugene Zhulenev   // Collect all outlined function inputs.
2204efb7754SRiver Riddle   SetVector<mlir::Value> functionInputs(execute.dependencies().begin(),
22125f80e16SEugene Zhulenev                                         execute.dependencies().end());
22225f80e16SEugene Zhulenev   functionInputs.insert(execute.operands().begin(), execute.operands().end());
22325f80e16SEugene Zhulenev   getUsedValuesDefinedAbove(execute.body(), functionInputs);
22425f80e16SEugene Zhulenev 
22525f80e16SEugene Zhulenev   // Collect types for the outlined function inputs and outputs.
22625f80e16SEugene Zhulenev   auto typesRange = llvm::map_range(
22725f80e16SEugene Zhulenev       functionInputs, [](Value value) { return value.getType(); });
22825f80e16SEugene Zhulenev   SmallVector<Type, 4> inputTypes(typesRange.begin(), typesRange.end());
22925f80e16SEugene Zhulenev   auto outputTypes = execute.getResultTypes();
23025f80e16SEugene Zhulenev 
23125f80e16SEugene Zhulenev   auto funcType = FunctionType::get(ctx, inputTypes, outputTypes);
23225f80e16SEugene Zhulenev   auto funcAttrs = ArrayRef<NamedAttribute>();
23325f80e16SEugene Zhulenev 
23425f80e16SEugene Zhulenev   // TODO: Derive outlined function name from the parent FuncOp (support
23525f80e16SEugene Zhulenev   // multiple nested async.execute operations).
23625f80e16SEugene Zhulenev   FuncOp func = FuncOp::create(loc, kAsyncFnPrefix, funcType, funcAttrs);
237973ddb7dSMehdi Amini   symbolTable.insert(func);
23825f80e16SEugene Zhulenev 
23925f80e16SEugene Zhulenev   SymbolTable::setSymbolVisibility(func, SymbolTable::Visibility::Private);
24025f80e16SEugene Zhulenev 
24125f80e16SEugene Zhulenev   // Prepare a function for coroutine lowering by adding entry/cleanup/suspend
24225f80e16SEugene Zhulenev   // blocks, adding async.coro operations and setting up control flow.
24325f80e16SEugene Zhulenev   CoroMachinery coro = setupCoroMachinery(func);
24425f80e16SEugene Zhulenev 
24525f80e16SEugene Zhulenev   // Suspend async function at the end of an entry block, and resume it using
24625f80e16SEugene Zhulenev   // Async resume operation (execution will be resumed in a thread managed by
24725f80e16SEugene Zhulenev   // the async runtime).
24825f80e16SEugene Zhulenev   Block *entryBlock = &func.getBlocks().front();
24925f80e16SEugene Zhulenev   auto builder = ImplicitLocOpBuilder::atBlockTerminator(loc, entryBlock);
25025f80e16SEugene Zhulenev 
25125f80e16SEugene Zhulenev   // Save the coroutine state: async.coro.save
25225f80e16SEugene Zhulenev   auto coroSaveOp =
25325f80e16SEugene Zhulenev       builder.create<CoroSaveOp>(CoroStateType::get(ctx), coro.coroHandle);
25425f80e16SEugene Zhulenev 
25525f80e16SEugene Zhulenev   // Pass coroutine to the runtime to be resumed on a runtime managed thread.
25625f80e16SEugene Zhulenev   builder.create<RuntimeResumeOp>(coro.coroHandle);
25725f80e16SEugene Zhulenev 
25825f80e16SEugene Zhulenev   // Split the entry block before the terminator (branch to suspend block).
25925f80e16SEugene Zhulenev   auto *terminatorOp = entryBlock->getTerminator();
26025f80e16SEugene Zhulenev   Block *suspended = terminatorOp->getBlock();
26125f80e16SEugene Zhulenev   Block *resume = suspended->splitBlock(terminatorOp);
26225f80e16SEugene Zhulenev 
26325f80e16SEugene Zhulenev   // Add async.coro.suspend as a suspended block terminator.
26425f80e16SEugene Zhulenev   builder.setInsertionPointToEnd(suspended);
26525f80e16SEugene Zhulenev   builder.create<CoroSuspendOp>(coroSaveOp.state(), coro.suspend, resume,
26625f80e16SEugene Zhulenev                                 coro.cleanup);
26725f80e16SEugene Zhulenev 
26825f80e16SEugene Zhulenev   size_t numDependencies = execute.dependencies().size();
26925f80e16SEugene Zhulenev   size_t numOperands = execute.operands().size();
27025f80e16SEugene Zhulenev 
27125f80e16SEugene Zhulenev   // Await on all dependencies before starting to execute the body region.
27225f80e16SEugene Zhulenev   builder.setInsertionPointToStart(resume);
27325f80e16SEugene Zhulenev   for (size_t i = 0; i < numDependencies; ++i)
27425f80e16SEugene Zhulenev     builder.create<AwaitOp>(func.getArgument(i));
27525f80e16SEugene Zhulenev 
27625f80e16SEugene Zhulenev   // Await on all async value operands and unwrap the payload.
27725f80e16SEugene Zhulenev   SmallVector<Value, 4> unwrappedOperands(numOperands);
27825f80e16SEugene Zhulenev   for (size_t i = 0; i < numOperands; ++i) {
27925f80e16SEugene Zhulenev     Value operand = func.getArgument(numDependencies + i);
28025f80e16SEugene Zhulenev     unwrappedOperands[i] = builder.create<AwaitOp>(loc, operand).result();
28125f80e16SEugene Zhulenev   }
28225f80e16SEugene Zhulenev 
28325f80e16SEugene Zhulenev   // Map from function inputs defined above the execute op to the function
28425f80e16SEugene Zhulenev   // arguments.
28525f80e16SEugene Zhulenev   BlockAndValueMapping valueMapping;
28625f80e16SEugene Zhulenev   valueMapping.map(functionInputs, func.getArguments());
28725f80e16SEugene Zhulenev   valueMapping.map(execute.body().getArguments(), unwrappedOperands);
28825f80e16SEugene Zhulenev 
28925f80e16SEugene Zhulenev   // Clone all operations from the execute operation body into the outlined
29025f80e16SEugene Zhulenev   // function body.
29125f80e16SEugene Zhulenev   for (Operation &op : execute.body().getOps())
29225f80e16SEugene Zhulenev     builder.clone(op, valueMapping);
29325f80e16SEugene Zhulenev 
29425f80e16SEugene Zhulenev   // Replace the original `async.execute` with a call to outlined function.
29525f80e16SEugene Zhulenev   ImplicitLocOpBuilder callBuilder(loc, execute);
29625f80e16SEugene Zhulenev   auto callOutlinedFunc = callBuilder.create<CallOp>(
29725f80e16SEugene Zhulenev       func.getName(), execute.getResultTypes(), functionInputs.getArrayRef());
29825f80e16SEugene Zhulenev   execute.replaceAllUsesWith(callOutlinedFunc.getResults());
29925f80e16SEugene Zhulenev   execute.erase();
30025f80e16SEugene Zhulenev 
30125f80e16SEugene Zhulenev   return {func, coro};
30225f80e16SEugene Zhulenev }
30325f80e16SEugene Zhulenev 
30425f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
30525f80e16SEugene Zhulenev // Convert async.create_group operation to async.runtime.create
30625f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
30725f80e16SEugene Zhulenev 
30825f80e16SEugene Zhulenev namespace {
30925f80e16SEugene Zhulenev class CreateGroupOpLowering : public OpConversionPattern<CreateGroupOp> {
31025f80e16SEugene Zhulenev public:
31125f80e16SEugene Zhulenev   using OpConversionPattern::OpConversionPattern;
31225f80e16SEugene Zhulenev 
31325f80e16SEugene Zhulenev   LogicalResult
31425f80e16SEugene Zhulenev   matchAndRewrite(CreateGroupOp op, ArrayRef<Value> operands,
31525f80e16SEugene Zhulenev                   ConversionPatternRewriter &rewriter) const override {
31625f80e16SEugene Zhulenev     rewriter.replaceOpWithNewOp<RuntimeCreateOp>(
31725f80e16SEugene Zhulenev         op, GroupType::get(op->getContext()));
31825f80e16SEugene Zhulenev     return success();
31925f80e16SEugene Zhulenev   }
32025f80e16SEugene Zhulenev };
32125f80e16SEugene Zhulenev } // namespace
32225f80e16SEugene Zhulenev 
32325f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
32425f80e16SEugene Zhulenev // Convert async.add_to_group operation to async.runtime.add_to_group.
32525f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
32625f80e16SEugene Zhulenev 
32725f80e16SEugene Zhulenev namespace {
32825f80e16SEugene Zhulenev class AddToGroupOpLowering : public OpConversionPattern<AddToGroupOp> {
32925f80e16SEugene Zhulenev public:
33025f80e16SEugene Zhulenev   using OpConversionPattern::OpConversionPattern;
33125f80e16SEugene Zhulenev 
33225f80e16SEugene Zhulenev   LogicalResult
33325f80e16SEugene Zhulenev   matchAndRewrite(AddToGroupOp op, ArrayRef<Value> operands,
33425f80e16SEugene Zhulenev                   ConversionPatternRewriter &rewriter) const override {
33525f80e16SEugene Zhulenev     rewriter.replaceOpWithNewOp<RuntimeAddToGroupOp>(
33625f80e16SEugene Zhulenev         op, rewriter.getIndexType(), operands);
33725f80e16SEugene Zhulenev     return success();
33825f80e16SEugene Zhulenev   }
33925f80e16SEugene Zhulenev };
34025f80e16SEugene Zhulenev } // namespace
34125f80e16SEugene Zhulenev 
34225f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
34325f80e16SEugene Zhulenev // Convert async.await and async.await_all operations to the async.runtime.await
34425f80e16SEugene Zhulenev // or async.runtime.await_and_resume operations.
34525f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
34625f80e16SEugene Zhulenev 
34725f80e16SEugene Zhulenev namespace {
34825f80e16SEugene Zhulenev template <typename AwaitType, typename AwaitableType>
34925f80e16SEugene Zhulenev class AwaitOpLoweringBase : public OpConversionPattern<AwaitType> {
35025f80e16SEugene Zhulenev   using AwaitAdaptor = typename AwaitType::Adaptor;
35125f80e16SEugene Zhulenev 
35225f80e16SEugene Zhulenev public:
35339957aa4SEugene Zhulenev   AwaitOpLoweringBase(MLIRContext *ctx,
35439957aa4SEugene Zhulenev                       llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions)
35525f80e16SEugene Zhulenev       : OpConversionPattern<AwaitType>(ctx),
35625f80e16SEugene Zhulenev         outlinedFunctions(outlinedFunctions) {}
35725f80e16SEugene Zhulenev 
35825f80e16SEugene Zhulenev   LogicalResult
35925f80e16SEugene Zhulenev   matchAndRewrite(AwaitType op, ArrayRef<Value> operands,
36025f80e16SEugene Zhulenev                   ConversionPatternRewriter &rewriter) const override {
36125f80e16SEugene Zhulenev     // We can only await on one the `AwaitableType` (for `await` it can be
36225f80e16SEugene Zhulenev     // a `token` or a `value`, for `await_all` it must be a `group`).
36325f80e16SEugene Zhulenev     if (!op.operand().getType().template isa<AwaitableType>())
36425f80e16SEugene Zhulenev       return rewriter.notifyMatchFailure(op, "unsupported awaitable type");
36525f80e16SEugene Zhulenev 
36625f80e16SEugene Zhulenev     // Check if await operation is inside the outlined coroutine function.
36725f80e16SEugene Zhulenev     auto func = op->template getParentOfType<FuncOp>();
36825f80e16SEugene Zhulenev     auto outlined = outlinedFunctions.find(func);
36925f80e16SEugene Zhulenev     const bool isInCoroutine = outlined != outlinedFunctions.end();
37025f80e16SEugene Zhulenev 
37125f80e16SEugene Zhulenev     Location loc = op->getLoc();
37225f80e16SEugene Zhulenev     Value operand = AwaitAdaptor(operands).operand();
37325f80e16SEugene Zhulenev 
37425f80e16SEugene Zhulenev     // Inside regular functions we use the blocking wait operation to wait for
37525f80e16SEugene Zhulenev     // the async object (token, value or group) to become available.
37625f80e16SEugene Zhulenev     if (!isInCoroutine)
37725f80e16SEugene Zhulenev       rewriter.create<RuntimeAwaitOp>(loc, operand);
37825f80e16SEugene Zhulenev 
37925f80e16SEugene Zhulenev     // Inside the coroutine we convert await operation into coroutine suspension
38025f80e16SEugene Zhulenev     // point, and resume execution asynchronously.
38125f80e16SEugene Zhulenev     if (isInCoroutine) {
38239957aa4SEugene Zhulenev       CoroMachinery &coro = outlined->getSecond();
38325f80e16SEugene Zhulenev       Block *suspended = op->getBlock();
38425f80e16SEugene Zhulenev 
38525f80e16SEugene Zhulenev       ImplicitLocOpBuilder builder(loc, op, rewriter.getListener());
38625f80e16SEugene Zhulenev       MLIRContext *ctx = op->getContext();
38725f80e16SEugene Zhulenev 
38825f80e16SEugene Zhulenev       // Save the coroutine state and resume on a runtime managed thread when
38925f80e16SEugene Zhulenev       // the operand becomes available.
39025f80e16SEugene Zhulenev       auto coroSaveOp =
39125f80e16SEugene Zhulenev           builder.create<CoroSaveOp>(CoroStateType::get(ctx), coro.coroHandle);
39225f80e16SEugene Zhulenev       builder.create<RuntimeAwaitAndResumeOp>(operand, coro.coroHandle);
39325f80e16SEugene Zhulenev 
39425f80e16SEugene Zhulenev       // Split the entry block before the await operation.
39525f80e16SEugene Zhulenev       Block *resume = rewriter.splitBlock(suspended, Block::iterator(op));
39625f80e16SEugene Zhulenev 
39725f80e16SEugene Zhulenev       // Add async.coro.suspend as a suspended block terminator.
39825f80e16SEugene Zhulenev       builder.setInsertionPointToEnd(suspended);
39925f80e16SEugene Zhulenev       builder.create<CoroSuspendOp>(coroSaveOp.state(), coro.suspend, resume,
40025f80e16SEugene Zhulenev                                     coro.cleanup);
40125f80e16SEugene Zhulenev 
40239957aa4SEugene Zhulenev       // Split the resume block into error checking and continuation.
40339957aa4SEugene Zhulenev       Block *continuation = rewriter.splitBlock(resume, Block::iterator(op));
40439957aa4SEugene Zhulenev 
40539957aa4SEugene Zhulenev       // Check if the awaited value is in the error state.
40639957aa4SEugene Zhulenev       builder.setInsertionPointToStart(resume);
407*d8c84d2aSEugene Zhulenev       auto isError =
408*d8c84d2aSEugene Zhulenev           builder.create<RuntimeIsErrorOp>(loc, rewriter.getI1Type(), operand);
40939957aa4SEugene Zhulenev       builder.create<CondBranchOp>(isError,
41039957aa4SEugene Zhulenev                                    /*trueDest=*/setupSetErrorBlock(coro),
41139957aa4SEugene Zhulenev                                    /*trueArgs=*/ArrayRef<Value>(),
41239957aa4SEugene Zhulenev                                    /*falseDest=*/continuation,
41339957aa4SEugene Zhulenev                                    /*falseArgs=*/ArrayRef<Value>());
41439957aa4SEugene Zhulenev 
41539957aa4SEugene Zhulenev       // Make sure that replacement value will be constructed in the
41639957aa4SEugene Zhulenev       // continuation block.
41739957aa4SEugene Zhulenev       rewriter.setInsertionPointToStart(continuation);
41839957aa4SEugene Zhulenev     }
41925f80e16SEugene Zhulenev 
42025f80e16SEugene Zhulenev     // Erase or replace the await operation with the new value.
42125f80e16SEugene Zhulenev     if (Value replaceWith = getReplacementValue(op, operand, rewriter))
42225f80e16SEugene Zhulenev       rewriter.replaceOp(op, replaceWith);
42325f80e16SEugene Zhulenev     else
42425f80e16SEugene Zhulenev       rewriter.eraseOp(op);
42525f80e16SEugene Zhulenev 
42625f80e16SEugene Zhulenev     return success();
42725f80e16SEugene Zhulenev   }
42825f80e16SEugene Zhulenev 
42925f80e16SEugene Zhulenev   virtual Value getReplacementValue(AwaitType op, Value operand,
43025f80e16SEugene Zhulenev                                     ConversionPatternRewriter &rewriter) const {
43125f80e16SEugene Zhulenev     return Value();
43225f80e16SEugene Zhulenev   }
43325f80e16SEugene Zhulenev 
43425f80e16SEugene Zhulenev private:
43539957aa4SEugene Zhulenev   llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions;
43625f80e16SEugene Zhulenev };
43725f80e16SEugene Zhulenev 
43825f80e16SEugene Zhulenev /// Lowering for `async.await` with a token operand.
43925f80e16SEugene Zhulenev class AwaitTokenOpLowering : public AwaitOpLoweringBase<AwaitOp, TokenType> {
44025f80e16SEugene Zhulenev   using Base = AwaitOpLoweringBase<AwaitOp, TokenType>;
44125f80e16SEugene Zhulenev 
44225f80e16SEugene Zhulenev public:
44325f80e16SEugene Zhulenev   using Base::Base;
44425f80e16SEugene Zhulenev };
44525f80e16SEugene Zhulenev 
44625f80e16SEugene Zhulenev /// Lowering for `async.await` with a value operand.
44725f80e16SEugene Zhulenev class AwaitValueOpLowering : public AwaitOpLoweringBase<AwaitOp, ValueType> {
44825f80e16SEugene Zhulenev   using Base = AwaitOpLoweringBase<AwaitOp, ValueType>;
44925f80e16SEugene Zhulenev 
45025f80e16SEugene Zhulenev public:
45125f80e16SEugene Zhulenev   using Base::Base;
45225f80e16SEugene Zhulenev 
45325f80e16SEugene Zhulenev   Value
45425f80e16SEugene Zhulenev   getReplacementValue(AwaitOp op, Value operand,
45525f80e16SEugene Zhulenev                       ConversionPatternRewriter &rewriter) const override {
45625f80e16SEugene Zhulenev     // Load from the async value storage.
45725f80e16SEugene Zhulenev     auto valueType = operand.getType().cast<ValueType>().getValueType();
45825f80e16SEugene Zhulenev     return rewriter.create<RuntimeLoadOp>(op->getLoc(), valueType, operand);
45925f80e16SEugene Zhulenev   }
46025f80e16SEugene Zhulenev };
46125f80e16SEugene Zhulenev 
46225f80e16SEugene Zhulenev /// Lowering for `async.await_all` operation.
46325f80e16SEugene Zhulenev class AwaitAllOpLowering : public AwaitOpLoweringBase<AwaitAllOp, GroupType> {
46425f80e16SEugene Zhulenev   using Base = AwaitOpLoweringBase<AwaitAllOp, GroupType>;
46525f80e16SEugene Zhulenev 
46625f80e16SEugene Zhulenev public:
46725f80e16SEugene Zhulenev   using Base::Base;
46825f80e16SEugene Zhulenev };
46925f80e16SEugene Zhulenev 
47025f80e16SEugene Zhulenev } // namespace
47125f80e16SEugene Zhulenev 
47225f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
47325f80e16SEugene Zhulenev // Convert async.yield operation to async.runtime operations.
47425f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
47525f80e16SEugene Zhulenev 
47625f80e16SEugene Zhulenev class YieldOpLowering : public OpConversionPattern<async::YieldOp> {
47725f80e16SEugene Zhulenev public:
47825f80e16SEugene Zhulenev   YieldOpLowering(
47925f80e16SEugene Zhulenev       MLIRContext *ctx,
48025f80e16SEugene Zhulenev       const llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions)
48125f80e16SEugene Zhulenev       : OpConversionPattern<async::YieldOp>(ctx),
48225f80e16SEugene Zhulenev         outlinedFunctions(outlinedFunctions) {}
48325f80e16SEugene Zhulenev 
48425f80e16SEugene Zhulenev   LogicalResult
48525f80e16SEugene Zhulenev   matchAndRewrite(async::YieldOp op, ArrayRef<Value> operands,
48625f80e16SEugene Zhulenev                   ConversionPatternRewriter &rewriter) const override {
48739957aa4SEugene Zhulenev     // Check if yield operation is inside the async coroutine function.
48825f80e16SEugene Zhulenev     auto func = op->template getParentOfType<FuncOp>();
48925f80e16SEugene Zhulenev     auto outlined = outlinedFunctions.find(func);
49025f80e16SEugene Zhulenev     if (outlined == outlinedFunctions.end())
49125f80e16SEugene Zhulenev       return rewriter.notifyMatchFailure(
49239957aa4SEugene Zhulenev           op, "operation is not inside the async coroutine function");
49325f80e16SEugene Zhulenev 
49425f80e16SEugene Zhulenev     Location loc = op->getLoc();
49525f80e16SEugene Zhulenev     const CoroMachinery &coro = outlined->getSecond();
49625f80e16SEugene Zhulenev 
49725f80e16SEugene Zhulenev     // Store yielded values into the async values storage and switch async
49825f80e16SEugene Zhulenev     // values state to available.
49925f80e16SEugene Zhulenev     for (auto tuple : llvm::zip(operands, coro.returnValues)) {
50025f80e16SEugene Zhulenev       Value yieldValue = std::get<0>(tuple);
50125f80e16SEugene Zhulenev       Value asyncValue = std::get<1>(tuple);
50225f80e16SEugene Zhulenev       rewriter.create<RuntimeStoreOp>(loc, yieldValue, asyncValue);
50325f80e16SEugene Zhulenev       rewriter.create<RuntimeSetAvailableOp>(loc, asyncValue);
50425f80e16SEugene Zhulenev     }
50525f80e16SEugene Zhulenev 
50625f80e16SEugene Zhulenev     // Switch the coroutine completion token to available state.
50725f80e16SEugene Zhulenev     rewriter.replaceOpWithNewOp<RuntimeSetAvailableOp>(op, coro.asyncToken);
50825f80e16SEugene Zhulenev 
50925f80e16SEugene Zhulenev     return success();
51025f80e16SEugene Zhulenev   }
51125f80e16SEugene Zhulenev 
51225f80e16SEugene Zhulenev private:
51325f80e16SEugene Zhulenev   const llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions;
51425f80e16SEugene Zhulenev };
51525f80e16SEugene Zhulenev 
51625f80e16SEugene Zhulenev //===----------------------------------------------------------------------===//
51739957aa4SEugene Zhulenev // Convert std.assert operation to cond_br into `set_error` block.
51839957aa4SEugene Zhulenev //===----------------------------------------------------------------------===//
51939957aa4SEugene Zhulenev 
52039957aa4SEugene Zhulenev class AssertOpLowering : public OpConversionPattern<AssertOp> {
52139957aa4SEugene Zhulenev public:
52239957aa4SEugene Zhulenev   AssertOpLowering(MLIRContext *ctx,
52339957aa4SEugene Zhulenev                    llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions)
52439957aa4SEugene Zhulenev       : OpConversionPattern<AssertOp>(ctx),
52539957aa4SEugene Zhulenev         outlinedFunctions(outlinedFunctions) {}
52639957aa4SEugene Zhulenev 
52739957aa4SEugene Zhulenev   LogicalResult
52839957aa4SEugene Zhulenev   matchAndRewrite(AssertOp op, ArrayRef<Value> operands,
52939957aa4SEugene Zhulenev                   ConversionPatternRewriter &rewriter) const override {
53039957aa4SEugene Zhulenev     // Check if assert operation is inside the async coroutine function.
53139957aa4SEugene Zhulenev     auto func = op->template getParentOfType<FuncOp>();
53239957aa4SEugene Zhulenev     auto outlined = outlinedFunctions.find(func);
53339957aa4SEugene Zhulenev     if (outlined == outlinedFunctions.end())
53439957aa4SEugene Zhulenev       return rewriter.notifyMatchFailure(
53539957aa4SEugene Zhulenev           op, "operation is not inside the async coroutine function");
53639957aa4SEugene Zhulenev 
53739957aa4SEugene Zhulenev     Location loc = op->getLoc();
53839957aa4SEugene Zhulenev     CoroMachinery &coro = outlined->getSecond();
53939957aa4SEugene Zhulenev 
54039957aa4SEugene Zhulenev     Block *cont = rewriter.splitBlock(op->getBlock(), Block::iterator(op));
54139957aa4SEugene Zhulenev     rewriter.setInsertionPointToEnd(cont->getPrevNode());
54239957aa4SEugene Zhulenev     rewriter.create<CondBranchOp>(loc, AssertOpAdaptor(operands).arg(),
54339957aa4SEugene Zhulenev                                   /*trueDest=*/cont,
54439957aa4SEugene Zhulenev                                   /*trueArgs=*/ArrayRef<Value>(),
54539957aa4SEugene Zhulenev                                   /*falseDest=*/setupSetErrorBlock(coro),
54639957aa4SEugene Zhulenev                                   /*falseArgs=*/ArrayRef<Value>());
54739957aa4SEugene Zhulenev     rewriter.eraseOp(op);
54839957aa4SEugene Zhulenev 
54939957aa4SEugene Zhulenev     return success();
55039957aa4SEugene Zhulenev   }
55139957aa4SEugene Zhulenev 
55239957aa4SEugene Zhulenev private:
55339957aa4SEugene Zhulenev   llvm::DenseMap<FuncOp, CoroMachinery> &outlinedFunctions;
55439957aa4SEugene Zhulenev };
55539957aa4SEugene Zhulenev 
55639957aa4SEugene Zhulenev //===----------------------------------------------------------------------===//
55725f80e16SEugene Zhulenev 
55825f80e16SEugene Zhulenev void AsyncToAsyncRuntimePass::runOnOperation() {
55925f80e16SEugene Zhulenev   ModuleOp module = getOperation();
56025f80e16SEugene Zhulenev   SymbolTable symbolTable(module);
56125f80e16SEugene Zhulenev 
56225f80e16SEugene Zhulenev   // Outline all `async.execute` body regions into async functions (coroutines).
56325f80e16SEugene Zhulenev   llvm::DenseMap<FuncOp, CoroMachinery> outlinedFunctions;
56425f80e16SEugene Zhulenev 
56525f80e16SEugene Zhulenev   module.walk([&](ExecuteOp execute) {
56625f80e16SEugene Zhulenev     outlinedFunctions.insert(outlineExecuteOp(symbolTable, execute));
56725f80e16SEugene Zhulenev   });
56825f80e16SEugene Zhulenev 
56925f80e16SEugene Zhulenev   LLVM_DEBUG({
57025f80e16SEugene Zhulenev     llvm::dbgs() << "Outlined " << outlinedFunctions.size()
57125f80e16SEugene Zhulenev                  << " functions built from async.execute operations\n";
57225f80e16SEugene Zhulenev   });
57325f80e16SEugene Zhulenev 
57425f80e16SEugene Zhulenev   // Lower async operations to async.runtime operations.
57525f80e16SEugene Zhulenev   MLIRContext *ctx = module->getContext();
576dc4e913bSChris Lattner   RewritePatternSet asyncPatterns(ctx);
57725f80e16SEugene Zhulenev 
57825f80e16SEugene Zhulenev   // Async lowering does not use type converter because it must preserve all
57925f80e16SEugene Zhulenev   // types for async.runtime operations.
580dc4e913bSChris Lattner   asyncPatterns.add<CreateGroupOpLowering, AddToGroupOpLowering>(ctx);
581dc4e913bSChris Lattner   asyncPatterns.add<AwaitTokenOpLowering, AwaitValueOpLowering,
58225f80e16SEugene Zhulenev                     AwaitAllOpLowering, YieldOpLowering>(ctx,
58325f80e16SEugene Zhulenev                                                          outlinedFunctions);
58425f80e16SEugene Zhulenev 
58539957aa4SEugene Zhulenev   // Lower assertions to conditional branches into error blocks.
58639957aa4SEugene Zhulenev   asyncPatterns.add<AssertOpLowering>(ctx, outlinedFunctions);
58739957aa4SEugene Zhulenev 
58825f80e16SEugene Zhulenev   // All high level async operations must be lowered to the runtime operations.
58925f80e16SEugene Zhulenev   ConversionTarget runtimeTarget(*ctx);
59025f80e16SEugene Zhulenev   runtimeTarget.addLegalDialect<AsyncDialect>();
59125f80e16SEugene Zhulenev   runtimeTarget.addIllegalOp<CreateGroupOp, AddToGroupOp>();
59225f80e16SEugene Zhulenev   runtimeTarget.addIllegalOp<ExecuteOp, AwaitOp, AwaitAllOp, async::YieldOp>();
59325f80e16SEugene Zhulenev 
59439957aa4SEugene Zhulenev   // Assertions must be converted to runtime errors.
59539957aa4SEugene Zhulenev   runtimeTarget.addIllegalOp<AssertOp>();
59639957aa4SEugene Zhulenev   runtimeTarget.addLegalOp<CondBranchOp>();
59739957aa4SEugene Zhulenev 
59825f80e16SEugene Zhulenev   if (failed(applyPartialConversion(module, runtimeTarget,
59925f80e16SEugene Zhulenev                                     std::move(asyncPatterns)))) {
60025f80e16SEugene Zhulenev     signalPassFailure();
60125f80e16SEugene Zhulenev     return;
60225f80e16SEugene Zhulenev   }
60325f80e16SEugene Zhulenev }
60425f80e16SEugene Zhulenev 
60525f80e16SEugene Zhulenev std::unique_ptr<OperationPass<ModuleOp>> mlir::createAsyncToAsyncRuntimePass() {
60625f80e16SEugene Zhulenev   return std::make_unique<AsyncToAsyncRuntimePass>();
60725f80e16SEugene Zhulenev }
608