This is an archive of the discontinued LLVM Phabricator instance.

Add multikey_qsort to STLExtras.h
Needs ReviewPublic

Authored by ruiu on Oct 26 2015, 2:26 PM.

Details

Reviewers
grosbach
Summary

So this is a generalized version of the function I added to
StringTableBuilder before. I'm not sure this is really an "STLExtra".
Is this the right place to add this function?

Diff Detail

Event Timeline

ruiu updated this revision to Diff 38462.Oct 26 2015, 2:26 PM
ruiu retitled this revision from to Add multikey_qsort to STLExtras.h.
ruiu updated this object.
ruiu added a reviewer: grosbach.
ruiu added a subscriber: llvm-commits.
grosbach edited edge metadata.Oct 26 2015, 2:43 PM

Not 100% sure it's the best place, either; it's just what came to mind since the function we're replacing the invocation of is also there.

include/llvm/ADT/STLExtras.h
376

Does the compiler not auto-recognize the tail call pattern here? If not, it seems like it should.

ruiu added inline comments.Oct 26 2015, 2:50 PM
include/llvm/ADT/STLExtras.h
376

Unlike Scheme we cannot assume that the tail call optimization is always in place. If a compiler does tail call optimization only in -O2 and not in -O0, this code could cause SEGV only in -O0 for some input. That would be confusing. So I think this is needed.

grosbach added inline comments.Oct 26 2015, 3:12 PM
include/llvm/ADT/STLExtras.h
376

I'm not sure I agree. -O0 vs. -O2 (or -Os) builds of the compiler (and Debug vs. Release in general) already have radically different memory usage patterns and algorithmic behavior (various +asserts stuff has n^2 algorithms, e.g.).

Is this a theoretical concern, or are you seeing crashes on real examples with a debug build of the compiler?

dblaikie added inline comments.
include/llvm/ADT/STLExtras.h
376

Eh, I think we tend not to write recursive algorithms where there aren't nice/known/low bounds, even if the compiler can optimize them away (same reason we write ++i instead of i++, etc).

That said, I wouldn't write it with goto if I could help it - I'd rather find a more natural iterative expression of the issue. (even just a simple "while (true) / return" thing I'd prefer over a raw goto, but a do/while loop seems plausible here)

ruiu added inline comments.Oct 26 2015, 3:24 PM
include/llvm/ADT/STLExtras.h
376

test/MC/COFF/section-name-encoding.s fails if this is not tail-optimized. But as long as it is tail-optimized, everything should be fine. So this is kind of theoretical.

grosbach added inline comments.Oct 26 2015, 3:24 PM
include/llvm/ADT/STLExtras.h
376

That's fair. To be clear, it's the "goto" I'm having trouble swallowing. A do/while would be totally reasonable.

silvas added a subscriber: silvas.Oct 26 2015, 3:26 PM

I'd really like to see sanity check performance numbers vs. std::sort to make sure that this function is worth it.

ruiu updated this revision to Diff 38471.Oct 26 2015, 3:33 PM
ruiu edited edge metadata.

Rewrite it with for(;;) to remove goto. (I was taught to think that a tail call is a jump as opposed to a call, so it was natural for me to use goto instead of for(;;) here, but I'm totally OK with for(;;) if this is more readable for others.)

dblaikie added inline comments.Oct 26 2015, 3:44 PM
include/llvm/ADT/STLExtras.h
351

This could become the loop condition? (while (End - Start > 1) ?)

It'd be possible to contort this so that the late return could be part of the loop condition too, but that might be awkward. When does getKey return a null pivot? (when does that trailing conditional return fire)

ruiu updated this revision to Diff 38475.Oct 26 2015, 3:57 PM
  • Updated as per David's comment.
  • Add a comment.