This is an archive of the discontinued LLVM Phabricator instance.

Align stubs for external and common global variables to pointer size.
ClosedPublic

Authored by rahulchaudhry on Mar 23 2018, 3:24 PM.

Details

Summary

This patch fixes PR36885: clang++ generates unaligned stub symbol
holding a pointer.

Diff Detail

Event Timeline

rahulchaudhry created this revision.Mar 23 2018, 3:24 PM
mgrang added a subscriber: mgrang.

Can you please add a unit test?

Can you please add a unit test?

There is a minimized testcase in the bug report: https://bugs.llvm.org/show_bug.cgi?id=36885
I'm not sure which repo the test should be added to (llvm or clang?).
Should the test be generating asm or ll file and check for alignment directive on the stub?
It'll be useful if you can point me to an existing test that does something similar for regular (non-stub) symbols.
Any guidance will be appreciated. This is my first llvm change.

You can add the test in llvm as an ll file. You can generate the ll as:

clang++ a.cpp -emit-llvm -S -o a.ll

Take a look at llvm/test/Object/ARM/symbol-addr.ll as an example to get started.
See also:
https://llvm.org/docs/CommandGuide/llc.html
https://llvm.org/docs/TestingGuide.html

Added a unit test.

You can add the test in llvm as an ll file.

Done. Please take another look.

mgrang added inline comments.Mar 27 2018, 5:27 PM
test/Object/X86/align-stub.ll
1 ↗(On Diff #140024)

Please follow 80 char limit.
See: https://llvm.org/docs/CodingStandards.html#source-code-width

Also -relocation-model=pic instead of --relocation-model=pic.

2 ↗(On Diff #140024)

Remove the extraneous space after RUN:

80-char line length limit, and use '-relocation-model=pic' instead of '--relocation-model=pic'.

rahulchaudhry marked an inline comment as done.Mar 27 2018, 5:46 PM
rahulchaudhry added inline comments.
test/Object/X86/align-stub.ll
2 ↗(On Diff #140024)

It's a continuation line now, so should the space stay, or would you like me to remove it?

espindola added inline comments.Mar 28 2018, 10:11 PM
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1341

Do you need to align in every loop iteration?

test/Object/X86/align-stub.ll
2 ↗(On Diff #140026)

Please don't use -filetype=obj. You can check for the align directive in the assembly.

Moved EmitAlignment() out of the loop.

Remove '-filetype=obj' from test, checking for
alignment directive in assembly output instead.

rahulchaudhry marked 3 inline comments as done.Mar 29 2018, 12:46 PM

Updated the comment and CHECK directives to match each other.

espindola added inline comments.Mar 29 2018, 1:59 PM
test/Object/X86/align-stub.ll
12 ↗(On Diff #140318)

Code change LGTM, but can't you trim the test a bit? For example, why do you need two comdats to test a change on the exception table output?

rahulchaudhry added inline comments.Mar 29 2018, 2:39 PM
test/Object/X86/align-stub.ll
12 ↗(On Diff #140318)

The test file was generated by running "clang++ -fPIE -emit-llvm -S " on the testcase in PR36885.
I removed a few lines of header and footer as it didn't matter.
I don't know llvm well enough to be comfortable editing this file by hand, or to write one from scratch.

Frankly, it's not the greatest idea that we're adding compiler generated output as tests, instead of the original source which is far more compact and readable.

espindola added inline comments.Mar 29 2018, 2:45 PM
test/Object/X86/align-stub.ll
12 ↗(On Diff #140318)

The fix is to reduce the .ll file :-)

Simply remove everything you can as long the new file still shows the results you want.

You can check https://llvm.org/docs/LangRef.html if you have any questions.

You should also use named temporaries. You can do that by passing the .ll to "opt -instnamer". This will give you a testcase that is much simpler to reduce.

Minimized test case.

rahulchaudhry added inline comments.Mar 29 2018, 4:15 PM
test/Object/X86/align-stub.ll
12 ↗(On Diff #140318)

Probably not what you expected, but I've minimized the testcase aggressively :)

espindola accepted this revision.Mar 29 2018, 5:18 PM

Awesome :-)

LGTM.

test/CodeGen/X86/catch.ll
8

Nit, the p2align is in another line, no? Could you use CHECK-NEXT?

This revision is now accepted and ready to land.Mar 29 2018, 5:18 PM

Use CHECK-NEXT to match p2align.

rahulchaudhry marked an inline comment as done.Mar 30 2018, 9:00 AM

Thanks for the review.
If everything looks good, can you land it as well.
This is my first change. I don't have write permissions to the repo.

test/CodeGen/X86/catch.ll
8

There were multiple .data directives in the original test, and I was trying to match the one preceding p2align.
In this case, there's only one .data, so simple CHECK/CHECK-NEXT sequence works fine.