This is an archive of the discontinued LLVM Phabricator instance.

[clang] fix generation of .debug_aranges with LTO (resubmit)
AbandonedPublic

Authored by azat on Sep 14 2022, 9:58 AM.

Details

Reviewers
None
Summary

Right now in case of LTO the section is not emited:

$ cat test.c
void __attribute__((optnone)) bar()
{
}
void __attribute__((optnone)) foo()
{
        bar();
}
int main()
{
        foo();
}

$ clang -flto=thin -gdwarf-aranges -g -O3 test.c
$ eu-readelf -waranges a.out  | fgrep -c -e foo -e bar
0

$ clang -gdwarf-aranges -g -O3 test.c
$ eu-readelf -waranges a.out  | fgrep -c -e foo -e bar
2

Fix this by passing explicitly --plugin-opt=-generate-arange-section.

But as mentioned by @dblaikie:

the better solution would be to encode this in IR metadata
(DICompileUnit) if it's reasonable to respect this option on a per-CU
basis (which it probably is, though that'd be a bit of backend work) -
or to set it as IR global metadata (like how the DWARF version is
encoded - probably using the same IR linker merging strategy, of
choosing the highest value (so if any module has aranges, then a linked
module has aranges too - even for the CUs that had it turned off) if
it's really not feasible to support on a per-CU basis) but probably
OK-enough, given that aranges is hopefully on the way out & not worth
all the work of the deeper fix

This is the resubmit, previous submission [1], got reverted, due to test
failures, that had been addressed in [2] and [3].

[1]: https://reviews.llvm.org/D133092
[3]: https://reviews.llvm.org/D133841
[4]: https://reviews.llvm.org/D133847

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
Suggested-by: OCHyams <orlando.hyams@sony.com>

Diff Detail

Event Timeline

azat created this revision.Sep 14 2022, 9:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2022, 9:58 AM
Herald added a subscriber: inglorion. · View Herald Transcript
azat requested review of this revision.Sep 14 2022, 9:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2022, 9:58 AM

I think @MaskRay was saying this doesn't actually work & different flags are needed to pass to lld - have you tested this end-to-end/with a real compilation, does that work? (are there other cases that already use -mllvm to pass flags down to lld?)

azat added a comment.Sep 14 2022, 1:38 PM

I think @MaskRay was saying this doesn't actually work & different flags are needed to pass to lld

I thought that @MaskRay was talking about more generic approach that will not require if for the linker name, and indeed --plugin-opt=-generate-arange-section is better, and both gold and bfd support it!

have you tested this end-to-end/with a real compilation, does that work? (are there other cases that already use -mllvm to pass flags down to lld?)

Sure:

$ cat test.c
void __attribute__((optnone)) bar()
{
}
void __attribute__((optnone)) foo()
{
        bar();
}
int main()
{
        foo();
}
$ clang -c -flto=thin -fuse-ld=lld -gdwarf-aranges -g -O3 test.c
$ clang -flto=thin -fuse-ld=lld -gdwarf-aranges -g -O3 test.c -### # to get full command
$ /usr/bin/ld.lld ... -mllvm -generate-arange-section
$ eu-readelf -waranges a.out  | grep -F -c -e foo -e bar
2
azat updated this revision to Diff 460212.Sep 14 2022, 1:51 PM

Use --plugin-opt=-generate-arange-section instead.

azat edited the summary of this revision. (Show Details)Sep 14 2022, 1:53 PM
MaskRay added inline comments.Sep 14 2022, 2:23 PM
clang/test/Driver/debug-options-aranges.c
1 ↗(On Diff #460212)

The message is outdated now. This just checks how -gdwarf-aranges ias passed to the compiler and linker.

3 ↗(On Diff #460212)

--target=x86_64

6 ↗(On Diff #460212)

delete empty //

azat updated this revision to Diff 460302.Sep 14 2022, 11:35 PM

Add a real check of .debug_aranges in the test

azat updated this revision to Diff 460304.Sep 14 2022, 11:39 PM

Test cleanup

azat marked an inline comment as done.Sep 14 2022, 11:40 PM

The message is outdated now.

Actually even before it does not check that .debug_aranges had been emitted.

This just checks how -gdwarf-aranges ias passed to the compiler and linker.

I've improved the test (though maybe Address Range Header match requires regex?)

clang/test/Driver/debug-options-aranges.c
3 ↗(On Diff #460212)

error: 'x86_64': unable to pass LLVM bit-code files to linker

azat updated this revision to Diff 460321.Sep 15 2022, 1:22 AM

Fix clang-format and tiny test refactoring

probinson added inline comments.
clang/test/Driver/debug-options-aranges.c
7 ↗(On Diff #460321)

I don't see any FileCheck commands that use prefix GARANGE.

dblaikie added inline comments.Sep 15 2022, 11:54 AM
clang/test/Driver/debug-options-aranges.c
15 ↗(On Diff #460321)

Generally Clang tests don't test the LLVM behavior - this test should probably just check how -gdwarf-aranges is passed to the linker. Other linker-level tests will test that, given that flag, the linker does the right thing.

azat updated this revision to Diff 460475.Sep 15 2022, 12:08 PM

Update the test

azat marked 2 inline comments as done.Sep 15 2022, 12:08 PM
azat added inline comments.
clang/test/Driver/debug-options-aranges.c
7 ↗(On Diff #460321)

Indeed, that was a leftover. Removed.

15 ↗(On Diff #460321)

Okay, removed.

azat marked 4 inline comments as done.Sep 15 2022, 12:09 PM
azat edited the summary of this revision. (Show Details)Sep 18 2022, 11:34 PM