-
Notifications
You must be signed in to change notification settings - Fork 58
Optimize rule-matching by reordering the OutSection-Rule-Sections loop #820
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
parth-07
wants to merge
1
commit into
qualcomm:main
Choose a base branch
from
parth-07:OptimizeRuleMatching
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -264,7 +264,6 @@ void ObjectBuilder::doPluginIterateSections(eld::InputFile *Obj, | |||
| } | ||||
|
|
||||
| void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) { | ||||
| std::unordered_map<Section *, bool> RetrySections; | ||||
| bool IsPartialLink = (ThisConfig.codeGenType() == LinkerConfig::Object); | ||||
| bool IsGnuCompatible = | ||||
| (ThisConfig.options().getScriptOption() == GeneralOptions::MatchGNU); | ||||
|
|
@@ -273,82 +272,78 @@ void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) { | |||
| SectionMap &SectionMap = ThisModule.getScript().sectionMap(); | ||||
| ObjectFile *ObjFile = llvm::dyn_cast<ObjectFile>(Obj); | ||||
| std::vector<Section *> Sections = getInputSectionsForRuleMatching(ObjFile); | ||||
| // For all output sections. | ||||
| for (auto *Out : SectionMap) { | ||||
| // For each input rule. | ||||
| for (auto *In : *Out) { | ||||
| auto Start = std::chrono::system_clock::now(); | ||||
| // For each input section. | ||||
| for (Section *Section : Sections) { | ||||
| ELFSection *ELFSect = llvm::dyn_cast<eld::ELFSection>(Section); | ||||
| // For each input section. | ||||
| for (Section *Sect : Sections) { | ||||
| ELFSection *ELFSect = llvm::dyn_cast<eld::ELFSection>(Sect); | ||||
|
|
||||
| bool IsRetrySect = false; | ||||
| bool IsRetrySect = false; | ||||
|
|
||||
| // Skip sections with merge strings and if there is no linker scripts | ||||
| // provided. | ||||
| if (IsPartialLink && (ELFSect && ELFSect->isMergeStr()) && | ||||
| !LinkerScriptHasSectionsCommand) | ||||
| continue; | ||||
| InputFile *Input = Obj; | ||||
| bool IsCommonSection = false; | ||||
| if (CommonELFSection *CommonSection = | ||||
| llvm::dyn_cast<CommonELFSection>(Sect)) { | ||||
| Input = CommonSection->getOrigin(); | ||||
| IsCommonSection = true; | ||||
| } | ||||
| if (Sect->getOldInputFile()) | ||||
| Input = Sect->getOldInputFile(); | ||||
| std::string const &PInputFile = | ||||
| Input->getInput()->getResolvedPath().native(); | ||||
| std::string const &Name = Input->getInput()->getName(); | ||||
| bool IsArchive = Input->isArchive() || | ||||
| llvm::dyn_cast<eld::ArchiveMemberInput>(Input->getInput()); | ||||
| // Hash of all the required things for Match. | ||||
| uint64_t InputFileHash = Input->getInput()->getResolvedPathHash(); | ||||
| uint64_t NameHash = Input->getInput()->getArchiveMemberNameHash(); | ||||
| std::string SectName = Sect->name().str(); | ||||
| uint64_t InputSectionHash = Sect->sectionNameHash(); | ||||
| if (ELFSection *ELFSect = llvm::dyn_cast<ELFSection>(Sect)) { | ||||
| if (auto OptRThisSectionName = | ||||
| ObjFile->getRuleMatchingSectName(ELFSect->getIndex())) { | ||||
| SectName = OptRThisSectionName.value(); | ||||
| InputSectionHash = llvm::hash_combine(SectName); | ||||
| } | ||||
| } | ||||
| // For all output sections. | ||||
| for (auto *Out : SectionMap) { | ||||
| if (ELFSect) { | ||||
| // If the rule needs to match on permissions, skip if the rule doesnot | ||||
| // satisfy. | ||||
| switch (Out->prolog().constraint()) { | ||||
| case OutputSectDesc::NO_CONSTRAINT: | ||||
| break; | ||||
| case OutputSectDesc::ONLY_IF_RO: | ||||
| if (ELFSect->isWritable()) | ||||
| continue; | ||||
| break; | ||||
| case OutputSectDesc::ONLY_IF_RW: | ||||
| if (!ELFSect->isWritable()) | ||||
| continue; | ||||
| break; | ||||
| } | ||||
| } | ||||
| bool shouldSkipMatch = false; | ||||
| // For each input rule. | ||||
| for (auto *In : *Out) { | ||||
| // If the section has an already assigned output section, skip. | ||||
| // If the section needs to be retried, we may need to revisit the | ||||
| // section to match the best rule. | ||||
| if (Section->getOutputSection()) { | ||||
| if (!RetrySections.size() || | ||||
| (RetrySections.find(Section) == RetrySections.end())) { | ||||
| continue; | ||||
| } | ||||
| IsRetrySect = true; | ||||
| } | ||||
|
|
||||
| if (ELFSect) { | ||||
| // If the rule needs to match on permissions, skip if the rule doesnot | ||||
| // satisfy. | ||||
| switch (Out->prolog().constraint()) { | ||||
| case OutputSectDesc::NO_CONSTRAINT: | ||||
| break; | ||||
| case OutputSectDesc::ONLY_IF_RO: | ||||
| if (ELFSect->isWritable()) | ||||
| continue; | ||||
| break; | ||||
| case OutputSectDesc::ONLY_IF_RW: | ||||
| if (!ELFSect->isWritable()) | ||||
| continue; | ||||
| break; | ||||
| } | ||||
| } | ||||
| // Skip sections with merge strings and if there is no linker scripts | ||||
| // provided. | ||||
| if (IsPartialLink && (ELFSect && ELFSect->isMergeStr()) && | ||||
| !LinkerScriptHasSectionsCommand) | ||||
| continue; | ||||
| InputFile *Input = Obj; | ||||
| bool IsCommonSection = false; | ||||
| if (CommonELFSection *CommonSection = | ||||
| llvm::dyn_cast<CommonELFSection>(Section)) { | ||||
| Input = CommonSection->getOrigin(); | ||||
| IsCommonSection = true; | ||||
| } | ||||
| if (Section->getOldInputFile()) | ||||
| Input = Section->getOldInputFile(); | ||||
| std::string const &PInputFile = | ||||
| Input->getInput()->getResolvedPath().native(); | ||||
| std::string const &Name = Input->getInput()->getName(); | ||||
| bool IsArchive = | ||||
| Input->isArchive() || | ||||
| llvm::dyn_cast<eld::ArchiveMemberInput>(Input->getInput()); | ||||
| // Hash of all the required things for Match. | ||||
| uint64_t InputFileHash = Input->getInput()->getResolvedPathHash(); | ||||
| uint64_t NameHash = Input->getInput()->getArchiveMemberNameHash(); | ||||
| std::string SectName = Section->name().str(); | ||||
| uint64_t InputSectionHash = Section->sectionNameHash(); | ||||
| if (ELFSection *ELFSect = llvm::dyn_cast<ELFSection>(Section)) { | ||||
| if (auto OptRThisSectionName = | ||||
| ObjFile->getRuleMatchingSectName(ELFSect->getIndex())) { | ||||
| SectName = OptRThisSectionName.value(); | ||||
| InputSectionHash = llvm::hash_combine(SectName); | ||||
| } | ||||
| if (Sect->getOutputSection() && !IsRetrySect) { | ||||
| shouldSkipMatch = true; | ||||
| break; | ||||
| } | ||||
| // auto Start = std::chrono::system_clock::now(); | ||||
| if (SectionMap.matched(*In, Input, PInputFile, SectName, IsArchive, | ||||
| Name, InputSectionHash, InputFileHash, NameHash, | ||||
| IsGnuCompatible, IsCommonSection)) { | ||||
| In->incMatchCount(); | ||||
| Section->setOutputSection(Out); | ||||
| Section->setMatchedLinkerScriptRule(In); | ||||
| Sect->setOutputSection(Out); | ||||
| Sect->setMatchedLinkerScriptRule(In); | ||||
| // FIXME: Shouldn't we set ELFSect to LDFileFormat::Discard? | ||||
| if (ELFSect && Out->isDiscard()) { | ||||
| ELFSect->setKind(LDFileFormat::Ignore); | ||||
|
|
@@ -359,15 +354,17 @@ void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) { | |||
| << ObjFile->getInput()->decoratedPath(); | ||||
| } | ||||
| if (IsRetrySect && !In->isSpecial()) | ||||
| RetrySections.erase(Section); | ||||
| IsRetrySect = false; | ||||
| // Retry the match until the closest match is found. | ||||
| if (In->isSpecial()) | ||||
| RetrySections.insert(std::make_pair(Section, true)); | ||||
| IsRetrySect = true; | ||||
| } // end match | ||||
| } // end each input section | ||||
| In->addMatchTime(std::chrono::system_clock::now() - Start); | ||||
| } // end each rule | ||||
| } // end each output section | ||||
| // RuleMatchTimes[In] += std::chrono::system_clock::now() - Start; | ||||
| } // end each rule | ||||
| if (shouldSkipMatch) | ||||
| break; | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need |
||||
| } // end each output section | ||||
| } // end each input section | ||||
| } | ||||
|
|
||||
| bool ObjectBuilder::initializePluginsAndProcess( | ||||
|
|
||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Section->getOutputSection() && !IsRetrySect)check needs to stay within the input-section-description loop (for (auto *In : *Out)). Otherwise, we will keep traversing input section descriptions even though the section has already matched an input section description.