|
| 1 | +//===--- LLVMEmitAsyncEntryReturnMetadata.cpp - Async function metadata ---===// |
| 2 | +// |
| 3 | + |
| 4 | +#include "swift/LLVMPasses/Passes.h" |
| 5 | +#include "llvm/Pass.h" |
| 6 | +#include "llvm/IR/Constants.h" |
| 7 | +#include "llvm/Transforms/Utils/ModuleUtils.h" |
| 8 | + |
| 9 | +using namespace llvm; |
| 10 | +using namespace swift; |
| 11 | + |
| 12 | +#define DEBUG_TYPE "swift-async-return" |
| 13 | + |
| 14 | +PreservedAnalyses AsyncEntryReturnMetadataPass::run(Module &M, |
| 15 | + ModuleAnalysisManager &AM) { |
| 16 | + bool changed = false; |
| 17 | + |
| 18 | + SmallVector<llvm::Function *, 16> asyncEntries; |
| 19 | + SmallVector<llvm::Function *, 16> asyncReturns; |
| 20 | + for (auto &F : M) { |
| 21 | + if (F.isDeclaration()) |
| 22 | + continue; |
| 23 | + |
| 24 | + if (F.hasFnAttribute("async_entry")) |
| 25 | + asyncEntries.push_back(&F); |
| 26 | + if (F.hasFnAttribute("async_ret")) |
| 27 | + asyncReturns.push_back(&F); |
| 28 | + } |
| 29 | + |
| 30 | + auto &ctxt = M.getContext(); |
| 31 | + auto int32Ty = llvm::Type::getInt32Ty(ctxt); |
| 32 | + auto sizeTy = M.getDataLayout().getIntPtrType(ctxt, /*addrspace*/ 0); |
| 33 | + |
| 34 | + auto addSection = [&] (const char * sectionName, const char *globalName, |
| 35 | + SmallVectorImpl<llvm::Function *> & entries) { |
| 36 | + if (entries.empty()) |
| 37 | + return; |
| 38 | + |
| 39 | + auto intArrayTy = llvm::ArrayType::get(int32Ty, entries.size()); |
| 40 | + auto global = |
| 41 | + new llvm::GlobalVariable(M, intArrayTy, true, |
| 42 | + llvm::GlobalValue::InternalLinkage, |
| 43 | + nullptr, /*init*/ globalName, |
| 44 | + nullptr, /*insertBefore*/ |
| 45 | + llvm::GlobalValue::NotThreadLocal, |
| 46 | + 0/*address space*/); |
| 47 | + global->setAlignment(Align(4)); |
| 48 | + global->setSection(sectionName); |
| 49 | + size_t index = 0; |
| 50 | + SmallVector<llvm::Constant*, 16> offsets; |
| 51 | + for (auto *fn : entries) { |
| 52 | + llvm::Constant *indices[] = { llvm::ConstantInt::get(int32Ty, 0), |
| 53 | + llvm::ConstantInt::get(int32Ty, index)}; |
| 54 | + ++index; |
| 55 | + |
| 56 | + llvm::Constant *base = llvm::ConstantExpr::getInBoundsGetElementPtr( |
| 57 | + intArrayTy, global, indices); |
| 58 | + base = llvm::ConstantExpr::getPtrToInt(base, sizeTy); |
| 59 | + auto *target = llvm::ConstantExpr::getPtrToInt(fn, sizeTy); |
| 60 | + llvm::Constant *offset = llvm::ConstantExpr::getSub(target, base); |
| 61 | + |
| 62 | + if (sizeTy != int32Ty) { |
| 63 | + offset = llvm::ConstantExpr::getTrunc(offset, int32Ty); |
| 64 | + } |
| 65 | + offsets.push_back(offset); |
| 66 | + } |
| 67 | + auto constant = llvm::ConstantArray::get(intArrayTy, offsets); |
| 68 | + global->setInitializer(constant); |
| 69 | + appendToUsed(M, global); |
| 70 | + |
| 71 | + llvm::GlobalVariable::SanitizerMetadata Meta; |
| 72 | + Meta.IsDynInit = false; |
| 73 | + Meta.NoAddress = true; |
| 74 | + global->setSanitizerMetadata(Meta); |
| 75 | + |
| 76 | + changed = true; |
| 77 | + }; |
| 78 | + |
| 79 | + addSection("__TEXT,__swift_as_entry, coalesced, no_dead_strip", |
| 80 | + "__swift_async_entry_functlets", |
| 81 | + asyncEntries); |
| 82 | + addSection("__TEXT,__swift_as_ret, coalesced, no_dead_strip", |
| 83 | + "__swift_async_ret_functlets", |
| 84 | + asyncReturns); |
| 85 | + |
| 86 | + if (!changed) |
| 87 | + return PreservedAnalyses::all(); |
| 88 | + |
| 89 | + return PreservedAnalyses::none(); |
| 90 | +} |
0 commit comments