|
| 1 | +//===-- ThreadLocalLowering.h - 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 2025 Leaning Technologies |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include <unordered_set> |
| 13 | +#include "llvm/Cheerp/LowerGlobalDestructors.h" |
| 14 | +#include "llvm/IR/Constants.h" |
| 15 | +#include "llvm/IR/Module.h" |
| 16 | +#include "llvm/Transforms/Utils/LowerGlobalDtors.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | + |
| 20 | +namespace cheerp |
| 21 | +{ |
| 22 | + |
| 23 | +void LowerGlobalDestructorsPass::filterGenericJSDestructors(Module& M) |
| 24 | +{ |
| 25 | + GlobalVariable* var = M.getGlobalVariable("llvm.global_dtors"); |
| 26 | + if (!var || !var->hasInitializer()) |
| 27 | + return; |
| 28 | + ConstantArray* initList = dyn_cast<ConstantArray>(var->getInitializer()); |
| 29 | + if (!initList) |
| 30 | + return; |
| 31 | + |
| 32 | + GlobalValue::LinkageTypes linkageTypes = var->getLinkage(); |
| 33 | + SmallVector<Constant*, 8> validDestructors; |
| 34 | + Type* elementType = nullptr; |
| 35 | + |
| 36 | + // Loop over the global destructors, put all the ones tagged asmjs into a new list. |
| 37 | + for (Value* O: initList->operands()) |
| 38 | + { |
| 39 | + ConstantStruct* CS = dyn_cast<ConstantStruct>(O); |
| 40 | + if (!CS) |
| 41 | + continue; |
| 42 | + |
| 43 | + Constant* destructor = CS->getOperand(1); |
| 44 | + if (destructor->isNullValue()) |
| 45 | + break; |
| 46 | + |
| 47 | + elementType = O->getType(); |
| 48 | + Function* destructorFunc = dyn_cast<Function>(destructor); |
| 49 | + assert(destructorFunc); |
| 50 | + if (destructorFunc->getSection() == "asmjs") |
| 51 | + validDestructors.push_back(cast<Constant>(O)); |
| 52 | + } |
| 53 | + |
| 54 | + var->eraseFromParent(); |
| 55 | + uint32_t numElements = validDestructors.size(); |
| 56 | + // If there are no valid destructors, don't create a new global. |
| 57 | + if (numElements == 0) |
| 58 | + return; |
| 59 | + |
| 60 | + ArrayType* arrayType = ArrayType::get(elementType, numElements); |
| 61 | + Constant* newInitList = ConstantArray::get(arrayType, validDestructors); |
| 62 | + new GlobalVariable(M, arrayType, true, linkageTypes, newInitList, "llvm.global_dtors"); |
| 63 | +} |
| 64 | + |
| 65 | +PreservedAnalyses LowerGlobalDestructorsPass::run(Module& M, ModuleAnalysisManager& MAM) |
| 66 | +{ |
| 67 | + ModulePassManager MPM; |
| 68 | + MPM.addPass(LowerGlobalDtorsPass()); |
| 69 | + |
| 70 | + // Collect all currently existing functions in a set. |
| 71 | + std::unordered_set<Function*> functionsBeforePass; |
| 72 | + for (Function& F: M.getFunctionList()) |
| 73 | + functionsBeforePass.insert(&F); |
| 74 | + |
| 75 | + // Remove the destructors that aren't tagged asmjs. |
| 76 | + filterGenericJSDestructors(M); |
| 77 | + |
| 78 | + // Run the LowerGlobalDtorsPass. |
| 79 | + PreservedAnalyses PA = MPM.run(M, MAM); |
| 80 | + |
| 81 | + // The functions that weren't in the list before are the new functions |
| 82 | + // created by the LowerGlobalDtorsPass. Tag them asmjs. |
| 83 | + for (Function& F: M.getFunctionList()) |
| 84 | + { |
| 85 | + if (!functionsBeforePass.count(&F)) |
| 86 | + F.setSection("asmjs"); |
| 87 | + } |
| 88 | + |
| 89 | + return PA; |
| 90 | +} |
| 91 | + |
| 92 | +} |
0 commit comments