This is an archive of the discontinued LLVM Phabricator instance.

Pass -ffunction-sections/-fdata-sections along to gold-plugin
ClosedPublic

Authored by tejohnson on Sep 15 2016, 7:59 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

tejohnson updated this revision to Diff 71591.Sep 15 2016, 7:59 PM
tejohnson retitled this revision from to Pass -ffunction-sections/-fdata-sections along to gold-plugin.
tejohnson updated this object.
tejohnson added reviewers: mehdi_amini, pcc.
tejohnson added a subscriber: cfe-commits.
pcc edited edge metadata.Sep 30 2016, 1:39 PM

Perhaps it would be better to enable -ffunction-sections/-fdata-sections unconditionally at the linker level if the linker supports it? See also PR22999.

In D24644#557774, @pcc wrote:

Perhaps it would be better to enable -ffunction-sections/-fdata-sections unconditionally at the linker level if the linker supports it? See also PR22999.

My understanding is that these are not typically the default because it makes the resulting object files larger and the link slower. OTOH, for ThinLTO we definitely benefit more from these optimizations (and from PR22999 looks like there is a compelling reason to want this for LTO as well).

However, when you say enable them "unconditionally at the linker level" what do you mean - these need to go LLVM via plugin options. Are you suggesting having them enabled unconditionally in both gold-plugin and gold? That will require changes to both llvm and binutils, and the latter will have effects for other compilers. What about -Wl,-gc-sections, isn't that also needed to make effective use of these options?

pcc added a comment.Oct 3 2016, 11:05 AM

Are you suggesting having them enabled unconditionally in both gold-plugin and gold? That will require changes to both llvm and binutils, and the latter will have effects for other compilers.

I mean in the gold plugin only. There would not need to be any changes to gold because it does not take these parameters directly.

What about -Wl,-gc-sections, isn't that also needed to make effective use of these options?

Not necessarily, PR22999 would happen with or without gc-sections.

There are two things pushing and pulling here:

a) You want to be able to pass compiler code generation options at the time we're actually doing the code generation,
b) "Traditionally" we don't pass CFLAGS to the linker.

I think I'd like to see us passing more options down at code generation time and handling it explicitly. In particular for this we don't have the knowledge at link time of what was intended. Even if we only turn it on when we see -gc-sections we won't know if the programmer wants function and data sections or just the former. Or maybe they want function sections for some reason other than gc-sections.

In short, I'm more on the a) side here in what I want :)

To go to PR22999: I think it might be reasonable for the linker during code generation to turn on function/data-sections where it isn't reasonable for us to do so in the compiler. I can come up with weird cases to break it, but those are largely module level inline assembly.

mehdi_amini edited edge metadata.Oct 5 2016, 8:28 AM

What about function attributes? Hey that's the trend :)
You could have a subset of the functions in their own sections but not all. With LTO it means that you can disable this for a single input file.

What about function attributes? Hey that's the trend :)
You could have a subset of the functions in their own sections but not all. With LTO it means that you can disable this for a single input file.

True, but we'd need data attributes too for -fdata-sections. That's the main reason I haven't migrated the options out of TargetOptions and into the IR here. Rough sketch on module merge time: We'd probably want to error on functions/data that had separate section set in one module but not in another - there are a few ways to make that not error at link time, but at that point you're really relying on weird linker side effects and it's probably not what you intended anyhow.

-eric

Ping. It seems like using attributes is not feasible at this time due to the lack of data attributes.

mehdi_amini accepted this revision.Oct 12 2016, 1:26 PM
mehdi_amini edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Oct 12 2016, 1:26 PM
This revision was automatically updated to reflect the committed changes.
fhahn added a subscriber: fhahn.Jun 27 2017, 8:08 AM

I'd like to fix PR22999 and was wondering if you think adding a function-section attribute to the IR would be a viable solution?

When doing LTO, we could add the same function-section to each function in a module in the IRLinker. @mehdi_amini did you think something like that when suggesting using attributes?

ps: Maybe it would be better to discuss this at the bug report, but this review seemed the easiest way to address the right people.

I'd like to fix PR22999 and was wondering if you think adding a function-section attribute to the IR would be a viable solution?

When doing LTO, we could add the same function-section to each function in a module in the IRLinker. @mehdi_amini did you think something like that when suggesting using attributes?

Not sure how this would work? How do you codegen half of the functions with function-section but not all? How is the backend supposed to behave if it starts with a function that isn't decorated with this attribute, then move to one that is, and finally proceed with one that isn't?

fhahn added a comment.Jun 27 2017, 9:28 AM

I'd like to fix PR22999 and was wondering if you think adding a function-section attribute to the IR would be a viable solution?

When doing LTO, we could add the same function-section to each function in a module in the IRLinker. @mehdi_amini did you think something like that when suggesting using attributes?

Not sure how this would work? How do you codegen half of the functions with function-section but not all? How is the backend supposed to behave if it starts with a function that isn't decorated with this attribute, then move to one that is, and finally proceed with one that isn't?

hm yes I guess we would have to group the functions by their function-section attribute, which isn't such a good idea probably.

@echristo you mentioned that the compiler maybe could determine if we should use function-sections during codegen, but it does not seem possible to determine the size of the code section before emitting all functions, at which point it is already too late. Maybe I am missing something?