This is an archive of the discontinued LLVM Phabricator instance.

[Clang] New attribute optsize
Needs ReviewPublic

Authored by xbolva00 on Jun 11 2022, 2:42 AM.

Details

Summary

Clang exposes attribute 'minsize' which can be used to perform optimizations to reduce code size as much as possible. Like optimization flag -Oz.

This patch exposes attribute 'optsize' which can be used to perform optimizations to reduce code size and at the same time the impact on performance stays minimal. Like optimization flag -Os.

Diff Detail

Event Timeline

xbolva00 created this revision.Jun 11 2022, 2:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2022, 2:42 AM
xbolva00 requested review of this revision.Jun 11 2022, 2:42 AM
xbolva00 edited the summary of this revision. (Show Details)
aaron.ballman added inline comments.Jun 14 2022, 7:47 AM
clang/include/clang/Basic/Attr.td
2263

I would like to hold off on this until we have more agreement on https://reviews.llvm.org/D126984 because I would like clang::optsize to be expressed as one of the spellings of the attribute proposed there instead of introducing a separate semantic attribute for it. Then we can also shift clang::optnone as well. (Though I'm guessing we may have to figure out something for the documentation so that we don't cram all of the docs into one place -- that leaks an implementation detail.)

xbolva00 added inline comments.Jun 14 2022, 8:03 AM
clang/include/clang/Basic/Attr.td
2263

Do you plan to rework also minsize that way? and others.. ?

Documentation is gonna be a huge pain, yes.

In my opinion this is an useless blocker.

aaron.ballman added inline comments.Jun 14 2022, 8:23 AM
clang/include/clang/Basic/Attr.td
2263

Do you plan to rework also minsize that way? and others.. ?

Eventually, yes, but I wasn't going to try to nerd snipe you into doing them (or doing them all at once) as part of this work.

Documentation is gonna be a huge pain, yes.

Yup, but it's something we should be solving anyway (we already have other attributes in similar circumstances and we've mostly just punted by saying the attributes all do similar enough things that they should be documented under one group).

In my opinion this is an useless blocker.

I'm willing to be flexible, but we have > 400 semantic attributes already and the support matrix for their combinations is pretty bad. Keeping just with the optimization attributes, optnone is a warning when misapplied, but minsize is an error. You've duplicated the logic of minsize here with your patch (including identical documentation with no hope of a user figuring out which attribute to use or how they're different), and there's no consideration for what happens when both attributes are used yet there is for combining with optnone. Etc.

So I don't see this as a useless blocker at all. I intend it as an attempt to rein in our existing maintenance burden in an area that we're now talking about extending even further. If you have other ideas on how to ensure we stop making the support matrix for the attribute combinations worse, I'm definitely willing to consider alternative approaches.

xbolva00 added inline comments.Jun 14 2022, 8:54 AM
clang/include/clang/Basic/Attr.td
2263

Understood.

including identical documentation

Not so true :) As mentioned, optsize reduces size but still promises +- same performance. Minsize does not care about performance at all.

I'm willing to be flexible, but we have > 400 semantic attributes already and the support matrix for their combinations is pretty bad.

I agree. This sounds a stronger motivation than optimise attribute/pragma.

If you have other ideas on how to ensure we stop making the support matrix for the attribute combinations worse, I'm definitely willing to consider alternative approaches.

I like your OptimiseAttr idea (but yeah, a lot of work).

RFC at Discourse would be great as well (or maybe I am blind...)

aaron.ballman added inline comments.Jun 14 2022, 11:12 AM
clang/include/clang/Basic/Attr.td
2263

Not so true :) As mentioned, optsize reduces size but still promises +- same performance. Minsize does not care about performance at all.

:: reading skills intensify :: you're right, sorry for that!

It sounds like D126984 may be temporarily abandoned in favor of an even easier approach for #pragma optimize, which also throws a wrinkle in my "please use that other functionality once it lands" idea. The underlying concerns that led to me suggesting that still remain.

Do you have a strong need for optsize right now or was it more of a "since I'm in the area, let's complete the set" kind of feature? (I'm trying to see how interested you are in continuing the work there to get us to a common semantic attribute interface for these.)

xbolva00 added inline comments.Jun 14 2022, 11:52 AM
clang/include/clang/Basic/Attr.td
2263

No problem :)


I have no strong need. I read some time ago on stackoverflow that someone wanted to optimise function for size and for gcc they recommended optimise attribute and for clang minsize. (Well, minsize could be a bit stronger) Then I found out that optsize is missing.

And when D126984 came up, I was recommending minsize as alternative for optimise(-Oz) and then I again realized that optsize is missing, so no good solution for somebody who wants -Os.

If you create something new, I have no problem to "port" this patch to your new interface/architecture.

aaron.ballman added inline comments.Jun 15 2022, 5:07 AM
clang/include/clang/Basic/Attr.td
2263

Okay, thanks for the info! I don't have the time to do the refactoring work myself currently, though.

In the interests of not blocking useful work: if that refactoring doesn't take place in the "near future" (let's say ~6 months?) then it's fine to proceed with this patch as-is?

xbolva00 added inline comments.Jun 15 2022, 6:06 AM
clang/include/clang/Basic/Attr.td
2263

Yes, no problem