Files
swift-mirror/lib/LLVMPasses/LLVMEmitAsyncEntryReturnMetadata.cpp
Arnold Schwaighofer eaf90dff38 IRGen: Add metadata for async funclets denoting frame entry and frame exists
Adds sections `__TEXT,__swift_as_entry`, and `__TEXT,__swift_as_ret` that
contain relative pointers to async functlets modelling async function entries,
and function returns, respectively.

Emission of the sections can be trigger with the frontend option
`-Xfrontend -enable-async-frame-push-pop-metadata`.

This is done by:

* IRGen adding a `async_entry` function attribute to async functions.
* LLVM's coroutine splitting identifying continuation funclets that
  model the return from an async function call by adding the function
  attribute `async_ret`.  (see #llvm-project/pull/9204)
* An LLVM pass that keys off these two function attribute and emits the
  metadata into the above mention sections.

rdar://134460666
2024-09-03 08:44:16 -07:00

91 lines
2.9 KiB
C++

//===--- LLVMEmitAsyncEntryReturnMetadata.cpp - Async function metadata ---===//
//
#include "swift/LLVMPasses/Passes.h"
#include "llvm/Pass.h"
#include "llvm/IR/Constants.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
using namespace llvm;
using namespace swift;
#define DEBUG_TYPE "swift-async-return"
PreservedAnalyses AsyncEntryReturnMetadataPass::run(Module &M,
ModuleAnalysisManager &AM) {
bool changed = false;
SmallVector<llvm::Function *, 16> asyncEntries;
SmallVector<llvm::Function *, 16> asyncReturns;
for (auto &F : M) {
if (F.isDeclaration())
continue;
if (F.hasFnAttribute("async_entry"))
asyncEntries.push_back(&F);
if (F.hasFnAttribute("async_ret"))
asyncReturns.push_back(&F);
}
auto &ctxt = M.getContext();
auto int32Ty = llvm::Type::getInt32Ty(ctxt);
auto sizeTy = M.getDataLayout().getIntPtrType(ctxt, /*addrspace*/ 0);
auto addSection = [&] (const char * sectionName, const char *globalName,
SmallVectorImpl<llvm::Function *> & entries) {
if (entries.empty())
return;
auto intArrayTy = llvm::ArrayType::get(int32Ty, entries.size());
auto global =
new llvm::GlobalVariable(M, intArrayTy, true,
llvm::GlobalValue::InternalLinkage,
nullptr, /*init*/ globalName,
nullptr, /*insertBefore*/
llvm::GlobalValue::NotThreadLocal,
0/*address space*/);
global->setAlignment(Align(4));
global->setSection(sectionName);
size_t index = 0;
SmallVector<llvm::Constant*, 16> offsets;
for (auto *fn : entries) {
llvm::Constant *indices[] = { llvm::ConstantInt::get(int32Ty, 0),
llvm::ConstantInt::get(int32Ty, index)};
++index;
llvm::Constant *base = llvm::ConstantExpr::getInBoundsGetElementPtr(
intArrayTy, global, indices);
base = llvm::ConstantExpr::getPtrToInt(base, sizeTy);
auto *target = llvm::ConstantExpr::getPtrToInt(fn, sizeTy);
llvm::Constant *offset = llvm::ConstantExpr::getSub(target, base);
if (sizeTy != int32Ty) {
offset = llvm::ConstantExpr::getTrunc(offset, int32Ty);
}
offsets.push_back(offset);
}
auto constant = llvm::ConstantArray::get(intArrayTy, offsets);
global->setInitializer(constant);
appendToUsed(M, global);
llvm::GlobalVariable::SanitizerMetadata Meta;
Meta.IsDynInit = false;
Meta.NoAddress = true;
global->setSanitizerMetadata(Meta);
changed = true;
};
addSection("__TEXT,__swift_as_entry, coalesced, no_dead_strip",
"__swift_async_entry_functlets",
asyncEntries);
addSection("__TEXT,__swift_as_ret, coalesced, no_dead_strip",
"__swift_async_ret_functlets",
asyncReturns);
if (!changed)
return PreservedAnalyses::all();
return PreservedAnalyses::none();
}