- Notifications
You must be signed in to change notification settings - Fork 15.2k
[llvm-rtdyld] Precalculate required memory upfront #72254
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
Open
sjoerdmeijer wants to merge 1 commit into llvm:main Choose a base branch from sjoerdmeijer:rtdyld2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+80 −13
Conversation
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 changes the behaviour of llvm-rtdyld to calculate and allocate all required memory for objects and its data/code section upfront as opposed to doing this on-demand. Allocation of the memory upfront avoids fragmentation of the memory, which is a workaround for relocations getting out of range as explained and discussed here: https://discourse.llvm.org/t/problems-with-code-model-large-and-relocations/70511
You can test this locally with the following command:git-clang-format --diff da843aa09f4cb5caab1cf0c802f2d203ada84c54 8442c0fd63c42558534f9b33f4f6efdc8305aca7 -- llvm/include/llvm/ExecutionEngine/RuntimeDyld.h llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h llvm/tools/llvm-rtdyld/llvm-rtdyld.cppView the diff from clang-format here.diff --git a/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h b/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h index 423715453b..c715ec8884 100644 --- a/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h +++ b/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h @@ -196,8 +196,9 @@ public: /// TODO Error precalculateMemorySize(const object::ObjectFile &Obj, - uint64_t &CodeSize, Align &CodeAlign, uint64_t &RODataSize, Align - &RODataAlign, uint64_t &RWDataSize, Align &RWDataAlign); + uint64_t &CodeSize, Align &CodeAlign, + uint64_t &RODataSize, Align &RODataAlign, + uint64_t &RWDataSize, Align &RWDataAlign); /// Add the referenced object file to the list of objects to be loaded and /// relocated. diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 83cafa2d4e..f95acafe9b 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -1345,18 +1345,19 @@ createRuntimeDyldMachO( return Dyld; } -Error RuntimeDyld::precalculateMemorySize( - const ObjectFile &Obj, uint64_t &CodeSize, Align &CodeAlign, - uint64_t &RODataSize, Align &RODataAlign, uint64_t &RWDataSize, - Align &RWDataAlign) { +Error RuntimeDyld::precalculateMemorySize(const ObjectFile &Obj, + uint64_t &CodeSize, Align &CodeAlign, + uint64_t &RODataSize, + Align &RODataAlign, + uint64_t &RWDataSize, + Align &RWDataAlign) { if (!Dyld) { if (!Obj.isELF()) report_fatal_error("Incompatible object format!"); - Dyld = - createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()), - MemMgr, Resolver, ProcessAllSections, - std::move(NotifyStubEmitted)); + Dyld = createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()), + MemMgr, Resolver, ProcessAllSections, + std::move(NotifyStubEmitted)); } if (!Dyld->isCompatibleFile(Obj)) report_fatal_error("Incompatible object format!"); @@ -1366,7 +1367,6 @@ Error RuntimeDyld::precalculateMemorySize( return Err; } - std::unique_ptr<RuntimeDyld::LoadedObjectInfo> RuntimeDyld::loadObject(const ObjectFile &Obj) { if (!Dyld) { diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index 75caf01955..285de95698 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -428,9 +428,6 @@ protected: unsigned computeSectionStubBufSize(const ObjectFile &Obj, const SectionRef &Section); - - - // Implementation of the generic part of the loadObject algorithm. Expected<ObjSectionToIDMap> loadObjectImpl(const object::ObjectFile &Obj); @@ -460,14 +457,13 @@ public: virtual ~RuntimeDyldImpl(); -// Compute an upper bound of the memory that is required to load all + // Compute an upper bound of the memory that is required to load all // sections Error computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize, Align &CodeAlign, uint64_t &RODataSize, Align &RODataAlign, uint64_t &RWDataSize, Align &RWDataAlign); - void setProcessAllSections(bool ProcessAllSections) { this->ProcessAllSections = ProcessAllSections; } diff --git a/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp index a89c9ce486..fc7a654c11 100644 --- a/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -556,7 +556,6 @@ static int executeInput() { uint64_t TotalRODataSize = 0; uint64_t TotalRWDataSize = 0; - // If we don't have any input files, read from stdin. if (!InputFileList.size()) { InputFileList.push_back("-"); @@ -590,8 +589,9 @@ static int executeInput() { uint64_t CodeSize, RODataSize, RWDataSize; Align CodeAlign, RODataAlign, RWDataAlign; - Error Err = Dyld.precalculateMemorySize(Obj, CodeSize, CodeAlign, - RODataSize, RODataAlign, RWDataSize, RWDataAlign); + Error Err = + Dyld.precalculateMemorySize(Obj, CodeSize, CodeAlign, RODataSize, + RODataAlign, RWDataSize, RWDataAlign); if (Err) ErrorAndExit("Can't compute total size"); @@ -600,7 +600,7 @@ static int executeInput() { TotalRWDataSize += RWDataSize; } - if (!PreallocMemory) + if (!PreallocMemory) PreallocMemory = TotalCodeSize + TotalRODataSize + TotalRWDataSize; doPreallocation(MemMgr); |
Collaborator Author
| This depends on #71409 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
This changes the behaviour of llvm-rtdyld to calculate and allocate all required memory for objects and its data/code section upfront as opposed to doing this on-demand.
Allocation of the memory upfront avoids fragmentation of the memory, which is a workaround for relocations getting out of range as explained and discussed here:
https://discourse.llvm.org/t/llvm-rtdyld-aarch64-abi-relocation-restrictions/74616