-
Notifications
You must be signed in to change notification settings - Fork 29
Call destructors #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Call destructors #309
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===-- Cheerp/LowerGlobalDestructors.h - Cheerp helper -------------------===// | ||
| // | ||
| // Cheerp: The C++ compiler for the Web | ||
| // | ||
| // This file is distributed under the Apache License v2.0 with LLVM Exceptions. | ||
| // See LICENSE.TXT for details. | ||
| // | ||
| // Copyright 2025 Leaning Technologies | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef CHEERP_LOWER_GLOBAL_DESTRUCTORS_H | ||
| #define CHEERP_LOWER_GLOBAL_DESTRUCTORS_H | ||
|
|
||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| namespace cheerp{ | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| class LowerGlobalDestructorsPass: public PassInfoMixin<LowerGlobalDestructorsPass> { | ||
| private: | ||
| void filterGenericJSDestructors(Module& M); | ||
| public: | ||
| PreservedAnalyses run(Module& M, ModuleAnalysisManager& MAM); | ||
| static bool isRequired() { return true;} | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //===-- ThreadLocalLowering.h - Cheerp helper -------------------------===// | ||
| // | ||
| // Cheerp: The C++ compiler for the Web | ||
| // | ||
| // This file is distributed under the Apache License v2.0 with LLVM Exceptions. | ||
| // See LICENSE.TXT for details. | ||
| // | ||
| // Copyright 2025 Leaning Technologies | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <unordered_set> | ||
| #include "llvm/Cheerp/LowerGlobalDestructors.h" | ||
| #include "llvm/IR/Constants.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/Transforms/Utils/LowerGlobalDtors.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| namespace cheerp | ||
| { | ||
|
|
||
| void LowerGlobalDestructorsPass::filterGenericJSDestructors(Module& M) | ||
| { | ||
| GlobalVariable* var = M.getGlobalVariable("llvm.global_dtors"); | ||
| if (!var || !var->hasInitializer()) | ||
| return; | ||
| ConstantArray* initList = dyn_cast<ConstantArray>(var->getInitializer()); | ||
| if (!initList) | ||
| return; | ||
|
|
||
| GlobalValue::LinkageTypes linkageTypes = var->getLinkage(); | ||
| SmallVector<Constant*, 8> validDestructors; | ||
| Type* elementType = nullptr; | ||
|
|
||
| // Loop over the global destructors, put all the ones tagged asmjs into a new list. | ||
| for (Value* O: initList->operands()) | ||
| { | ||
| ConstantStruct* CS = dyn_cast<ConstantStruct>(O); | ||
| if (!CS) | ||
| continue; | ||
|
|
||
| Constant* destructor = CS->getOperand(1); | ||
| if (destructor->isNullValue()) | ||
| break; | ||
|
|
||
| elementType = O->getType(); | ||
| Function* destructorFunc = dyn_cast<Function>(destructor); | ||
| assert(destructorFunc); | ||
| if (destructorFunc->getSection() == "asmjs") | ||
| validDestructors.push_back(cast<Constant>(O)); | ||
| } | ||
|
|
||
| var->eraseFromParent(); | ||
| uint32_t numElements = validDestructors.size(); | ||
| // If there are no valid destructors, don't create a new global. | ||
| if (numElements == 0) | ||
| return; | ||
|
|
||
| ArrayType* arrayType = ArrayType::get(elementType, numElements); | ||
| Constant* newInitList = ConstantArray::get(arrayType, validDestructors); | ||
| new GlobalVariable(M, arrayType, true, linkageTypes, newInitList, "llvm.global_dtors"); | ||
| } | ||
|
|
||
| PreservedAnalyses LowerGlobalDestructorsPass::run(Module& M, ModuleAnalysisManager& MAM) | ||
| { | ||
| ModulePassManager MPM; | ||
| MPM.addPass(LowerGlobalDtorsPass()); | ||
|
|
||
| // Collect all currently existing functions in a set. | ||
| std::unordered_set<Function*> functionsBeforePass; | ||
| for (Function& F: M.getFunctionList()) | ||
| functionsBeforePass.insert(&F); | ||
|
|
||
| // Remove the destructors that aren't tagged asmjs. | ||
| filterGenericJSDestructors(M); | ||
|
|
||
| // Run the LowerGlobalDtorsPass. | ||
| PreservedAnalyses PA = MPM.run(M, MAM); | ||
|
|
||
| // The functions that weren't in the list before are the new functions | ||
| // created by the LowerGlobalDtorsPass. Tag them asmjs. | ||
| for (Function& F: M.getFunctionList()) | ||
| { | ||
| if (!functionsBeforePass.count(&F)) | ||
| F.setSection("asmjs"); | ||
| } | ||
|
|
||
| return PA; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.