This is an archive of the discontinued LLVM Phabricator instance.

[NFC] Refactor llvm-nm symbol comparing and split sorting
ClosedPublic

Authored by DiggerLin on Feb 4 2022, 11:46 AM.

Details

Summary
  1. added a helper function isSymbolDefined().
  2. Split out sorting code
  3. refactor symbol comparing function

Diff Detail

Event Timeline

DiggerLin created this revision.Feb 4 2022, 11:46 AM
DiggerLin requested review of this revision.Feb 4 2022, 11:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2022, 11:46 AM
DiggerLin retitled this revision from refactor the llvm-nm to refactor the llvm-nm of symbol sorting.Feb 4 2022, 12:45 PM
DiggerLin updated this revision to Diff 406132.Feb 4 2022, 5:33 PM

Mostly looks good

llvm/tools/llvm-nm/llvm-nm.cpp
226–231
230
246

Is operator> used? If used, can the use site be refactored to only use operator<?

DiggerLin marked 3 inline comments as done.Feb 5 2022, 4:33 PM
DiggerLin added inline comments.
llvm/tools/llvm-nm/llvm-nm.cpp
226–231

thanks

230

thanks

246

yes, the operator> is used to change

llvm::sort(SymbolList, [=](const NMSymbol &A, const NMSymbol &B) -> bool {
       return Cmp(B, A);
     });

---->

llvm::stable_sort(SymbolList, std::greater<NMSymbol>());

there is Jame's comment as
https://reviews.llvm.org/D112735#inline-1137095

DiggerLin updated this revision to Diff 406219.Feb 5 2022, 4:35 PM
DiggerLin marked 3 inline comments as done.

Mostly looks fine to me.

Change the title/commit summary to "[NFC] Refactor llvm-nm symbol sorting" (will need the additional patch mentioned inline).

llvm/tools/llvm-nm/llvm-nm.cpp
233–234

Since the NMSymbol class has no private members, do you need the friend operator declarations?

246

Just noting that if @MaskRay prefers using the lambda and avoid the operator>, I'm okay with that.

655–657

I'm okay with this switch to stable_sort, but if I'm not mistaken, this means that under some cases, it might be a subtle behaviour change (in a good way) that could result in people seeing "identical" symbols in different orders before and after this patch.

To keep this patch as a pure refactor, I suggest you create a separate patch that handles this behaviour change, by simply swapping sort to stable_sort. Then, you can add the [NFC] tag to this patch.

DiggerLin retitled this revision from refactor the llvm-nm of symbol sorting to [NFC] refactor the llvm-nm of symbol sorting.Feb 7 2022, 5:59 AM
DiggerLin marked 2 inline comments as done.Feb 7 2022, 6:01 AM
DiggerLin added inline comments.
llvm/tools/llvm-nm/llvm-nm.cpp
233–234

thanks , good catch.

DiggerLin updated this revision to Diff 406418.Feb 7 2022, 6:02 AM
DiggerLin marked an inline comment as done.

Change the title/commit summary to "[NFC] Refactor llvm-nm symbol sorting" (will need the additional patch mentioned inline).

Patch title still doesn't match the suggested correction.

DiggerLin retitled this revision from [NFC] refactor the llvm-nm of symbol sorting to [NFC] Refactor llvm-nm symbol sorting (will need the additional patch mentioned inline).Feb 7 2022, 8:02 AM
DiggerLin retitled this revision from [NFC] Refactor llvm-nm symbol sorting (will need the additional patch mentioned inline) to [NFC] Refactor llvm-nm symbol sorting .
DiggerLin added a comment.EditedFeb 7 2022, 8:08 AM

Change the title/commit summary to "[NFC] Refactor llvm-nm symbol sorting" (will need the additional patch mentioned inline).

Patch title still doesn't match the suggested correction.

Change the title/commit summary to "[NFC] Refactor llvm-nm symbol sorting" (will need the additional patch mentioned inline).

Patch title still doesn't match the suggested correction.

  I do not think we need to additional patch for it. using std::sort  is OK for export symbol list,  " export symbol list" do not need to change std::stable_sort (if the two symbol is equal at symbol name and visibility, and we will remove the duplication symbol finally). 
and for other functionality, the code is already using the std::sort , do we need to change it?
DiggerLin edited the summary of this revision. (Show Details)Feb 7 2022, 8:45 AM
DiggerLin retitled this revision from [NFC] Refactor llvm-nm symbol sorting to [NFC] Refactor llvm-nm symbol comparing and split sorting .
DiggerLin edited the summary of this revision. (Show Details)
jhenderson accepted this revision.Feb 8 2022, 12:24 AM

I'm good with this, but please wait for @MaskRay too.

This revision is now accepted and ready to land.Feb 8 2022, 12:24 AM
MaskRay accepted this revision.Feb 8 2022, 12:28 AM

Thanks!

llvm/tools/llvm-nm/llvm-nm.cpp
246

OK, operator> is fine since there is a std::greater<NMSymbol>().

655

Optional: this can be written as llvm::sort(SymbolList, std::greater<>()); to reduce boilerplate.

MaskRay added inline comments.Feb 8 2022, 12:36 AM
llvm/tools/llvm-nm/llvm-nm.cpp
246

Actually, I think it'd be better to do:

llvm::sort(Symbols);
if (ReverseSort)
  std::reverse(Symbols.begin(), Symbols.end());

NMSymbol is not trivially copyable, so llvm::sort expands to std::sort.
Having two instantiations (less and greater) increase the code size too much.
std::reverse can decrease the code size.

This revision was landed with ongoing or failed builds.Feb 8 2022, 7:57 AM
This revision was automatically updated to reflect the committed changes.
DiggerLin marked 3 inline comments as done.Feb 8 2022, 7:58 AM
DiggerLin added inline comments.
llvm/tools/llvm-nm/llvm-nm.cpp
246

yes, it can reduce the code a little, to use the
But there is one sort, one reverse maybe less efficient than one std::greater<NMSymbol>(). sort.

655

thanks, change as suggestion when landed