|
| 1 | +//===-- ThreadLocalLowering.cpp - Cheerp helper -------------------------===// |
| 2 | +// |
| 3 | +// Cheerp: The C++ compiler for the Web |
| 4 | +// |
| 5 | +// This file is distributed under the Apache License v2.0 with LLVM Exceptions. |
| 6 | +// See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +// Copyright 2024-2025 Leaning Technologies |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "llvm/Cheerp/ThreadLocalLowering.h" |
| 13 | +#include "llvm/Cheerp/GlobalDepsAnalyzer.h" |
| 14 | +#include "llvm/Cheerp/LinearMemoryHelper.h" |
| 15 | +#include "llvm/Cheerp/InvokeWrapping.h" |
| 16 | +#include "llvm/Cheerp/Registerize.h" |
| 17 | +#include "llvm/IR/Instruction.h" |
| 18 | +#include "llvm/IR/IntrinsicInst.h" |
| 19 | +#include "llvm/IR/IRBuilder.h" |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | + |
| 23 | +namespace cheerp |
| 24 | +{ |
| 25 | + |
| 26 | +static Function* getOrCreateThreadLocalWrapper(Module* M, GlobalDepsAnalyzer& GDA) |
| 27 | +{ |
| 28 | + Type* i8Ty = IntegerType::getInt8Ty(M->getContext()); |
| 29 | + Type* i8PtrTy = PointerType::get(i8Ty, 0); |
| 30 | + Type* i32Ty = IntegerType::getInt32Ty(M->getContext()); |
| 31 | + Type* argTy[] = {i32Ty}; |
| 32 | + FunctionType* fTy = FunctionType::get(i8PtrTy,ArrayRef<Type*>(argTy, 1), false); |
| 33 | + Function* wrapper = cast<Function>(M->getOrInsertFunction("__getThreadLocalAddress", fTy).getCallee()); |
| 34 | + if (!wrapper->empty()) |
| 35 | + return wrapper; |
| 36 | + |
| 37 | + BasicBlock* entry = BasicBlock::Create(M->getContext(),"entry", wrapper); |
| 38 | + IRBuilder<> Builder(entry); |
| 39 | + // Get the thread local address. |
| 40 | + Function* threadPointerIntrinsic = Intrinsic::getDeclaration(M, Intrinsic::cheerp_get_thread_pointer); |
| 41 | + Value* threadPointer = Builder.CreateCall(threadPointerIntrinsic); |
| 42 | + // Add the offset argument |
| 43 | + Value* offset = wrapper->getArg(0); |
| 44 | + Value* address = Builder.CreateAdd(threadPointer, offset); |
| 45 | + // Bitcast to a pointer |
| 46 | + address = Builder.CreateIntToPtr(address, i8PtrTy); |
| 47 | + Builder.CreateRet(address); |
| 48 | + |
| 49 | + wrapper->setSection("asmjs"); |
| 50 | + GDA.insertAsmJSExport(wrapper); |
| 51 | + return wrapper; |
| 52 | +} |
| 53 | + |
| 54 | +bool replaceThreadLocalIntrinsicWithFunction(Function& F, GlobalDepsAnalyzer& GDA) |
| 55 | +{ |
| 56 | + Module* M = F.getParent(); |
| 57 | + bool changed = false; |
| 58 | + SmallVector<Instruction*, 8> deleteList; |
| 59 | + |
| 60 | + for (BasicBlock& BB: F) |
| 61 | + { |
| 62 | + for (Instruction& I: BB) |
| 63 | + { |
| 64 | + if (isa<IntrinsicInst>(I)) |
| 65 | + { |
| 66 | + IntrinsicInst& II = cast<IntrinsicInst>(I); |
| 67 | + Intrinsic::ID intrId = II.getIntrinsicID(); |
| 68 | + if (intrId == Intrinsic::threadlocal_address) |
| 69 | + { |
| 70 | + IRBuilder<> Builder(&II); |
| 71 | + // Replace call to intrinsic with function |
| 72 | + // 1. Use an intrinsic that will be the offset for the threadlocal. |
| 73 | + Type* argTy[] = {II.getOperand(0)->getType()}; |
| 74 | + Function* offsetIntrinsic = Intrinsic::getDeclaration(M, Intrinsic::cheerp_get_threadlocal_offset, argTy); |
| 75 | + Value* offset = Builder.CreateCall(offsetIntrinsic, II.getOperand(0)); |
| 76 | + // 2. Pass this offset to the wasm function that will calculate the address from the thread pointer. |
| 77 | + Function* newFunc = getOrCreateThreadLocalWrapper(M, GDA); |
| 78 | + Value* newCall = Builder.CreateCall(newFunc, offset); |
| 79 | + // 3. Bitcast return code from this function to required type. |
| 80 | + Type* origType = II.getType(); |
| 81 | + if (origType != newCall->getType()) |
| 82 | + newCall = Builder.CreateBitCast(newCall, origType); |
| 83 | + I.replaceAllUsesWith(newCall); |
| 84 | + deleteList.push_back(&I); |
| 85 | + changed = true; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + for (Instruction* I: deleteList) |
| 91 | + I->eraseFromParent(); |
| 92 | + return changed; |
| 93 | +} |
| 94 | + |
| 95 | +PreservedAnalyses ThreadLocalLoweringInnerPass::run(Function& F, FunctionAnalysisManager& FAM) |
| 96 | +{ |
| 97 | + if (F.getSection() == "asmjs") |
| 98 | + return PreservedAnalyses::all(); |
| 99 | + |
| 100 | + // Find calls to threadlocal.address intrinsic, replace with calls to function. |
| 101 | + bool changed = replaceThreadLocalIntrinsicWithFunction(F, GDA); |
| 102 | + if (!changed) |
| 103 | + return PreservedAnalyses::all(); |
| 104 | + PreservedAnalyses PA; |
| 105 | + PA.preserve<PointerAnalysis>(); |
| 106 | + PA.preserve<RegisterizeAnalysis>(); |
| 107 | + PA.preserve<GlobalDepsAnalysis>(); |
| 108 | + PA.preserve<DominatorTreeAnalysis>(); |
| 109 | + PA.preserve<InvokeWrappingAnalysis>(); |
| 110 | + return PA; |
| 111 | +} |
| 112 | + |
| 113 | +PreservedAnalyses ThreadLocalLoweringPass::run(Module& M, ModuleAnalysisManager& MAM) |
| 114 | +{ |
| 115 | + FunctionPassManager FPM; |
| 116 | + |
| 117 | + GlobalDepsAnalyzer& GDA = MAM.getResult<GlobalDepsAnalysis>(M); |
| 118 | + FPM.addPass(ThreadLocalLoweringInnerPass(GDA)); |
| 119 | + |
| 120 | + ModulePassManager MPM; |
| 121 | + |
| 122 | + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 123 | + PreservedAnalyses PA = MPM.run(M, MAM); |
| 124 | + return PA; |
| 125 | +} |
| 126 | + |
| 127 | +} |
0 commit comments