This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add asserts that we don't increase codesize during pseudo expansion
ClosedPublic

Authored by reames on Jan 27 2023, 11:16 AM.

Details

Summary

We currently assume that pseudo expansion doesn't increase the distance between a branch and it's label. This patch adds some asserts to catch violations of this property in pseudo expansion.

I chose to only do the assertion at the function level as we have to scan the whole function for size changes (since expansion can create multiple blocks). We could cache individual block sizes, and thus make the check cheap enough to do after every expansion, but that requires more complex code.

It also looks like we have some shared code structure here. If reviewers want, I'm happy to factor out a base class for pseudo expansion, and add the asserts there. I'm not sure this is worth doing, but will happily defer to reviewer preference.

Diff Detail

Event Timeline

reames created this revision.Jan 27 2023, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 27 2023, 11:16 AM
reames requested review of this revision.Jan 27 2023, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 27 2023, 11:16 AM

s/macro/pseudo/? I got very confused until I read the diff and thought you were talking about assembly macros.

This is just checking that the Size of the pseudo is a true upper bound, right?

reames retitled this revision from [RISCV] Add asserts that we don't increase codesize during macro expansion to [RISCV] Add asserts that we don't increase codesize during pseudo expansion.Jan 27 2023, 11:26 AM
reames edited the summary of this revision. (Show Details)

s/macro/pseudo/? I got very confused until I read the diff and thought you were talking about assembly macros.

This is just checking that the Size of the pseudo is a true upper bound, right?

Yes to both. Sorry for the confusion, fixed the patch description.

craig.topper added inline comments.Jan 27 2023, 12:18 PM
llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp
81

Will this warn in release builds? Should we put #ifndef NDEBUG around the loops?

81

Warn for unused variables I mean.

reames planned changes to this revision.Jan 27 2023, 12:20 PM
reames added inline comments.
llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp
81

It might since the variable is repeatedly increment, but your point is solid and deserves a rework regardless.

Overall seems a reasonable implementation, despite the limitations pointed out in the patch description.

BTW, a little while ago while debugging some branch relaxation issues I noticed that we were overestimating the size of some instructions and thus relaxing when it wasn't needed. I made a mental note to look into that when I could. It will be useful to have these additional checks if we start tightening loose upper bounds.

reames updated this revision to Diff 493343.Jan 30 2023, 10:08 AM

Address review comment re: non-assert builds.

craig.topper added inline comments.Jan 30 2023, 5:57 PM
llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp
286

Is any compiler smart enough to see that this is an unused method in a class in an anonymous namespace in release builds?

reames updated this revision to Diff 493613.Jan 31 2023, 7:35 AM

ifdef helper function

asb accepted this revision.Jan 31 2023, 7:37 AM

LGTM.

This revision is now accepted and ready to land.Jan 31 2023, 7:37 AM
This revision was landed with ongoing or failed builds.Jan 31 2023, 7:50 AM
This revision was automatically updated to reflect the committed changes.