This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][MC] Relax conditional branches to unresolved symbols
ClosedPublic

Authored by jobnoorman on Jul 11 2023, 6:21 AM.

Details

Summary

D108961 introduced relaxation for out-of-range conditional branches.
However, relaxation was only performed when the branch target could be
resolved. I believe this has two undesired consequences:

  • b<cc> ... foo, where foo is undefined, would not be relaxed although there is no guarantee the offset to foo will fit;
  • Conditional branches are never relaxed with -mattr=+relax because MC considers fixups where shouldForceRelocation returns true (which will be the case with +relax) to be unresolved.

Note that binutils performs conditional branch relaxation in both cases.

This patch proposes to perform conditional branch relaxation even when
the target cannot be resolved.

Note on llvm/test/MC/RISCV/long-conditional-jump.s: I've removed the
.p2align because this causes alignment nops to be inserted for the
+relax tests. This in turn causes all the branch targets to change
compared to the non-+relax tests. Since +relax shouldn't change
these offsets, I found this confusing and hence chose to remove the
alignment.

Depends on D155953

Diff Detail

Event Timeline

jobnoorman created this revision.Jul 11 2023, 6:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 6:21 AM
jobnoorman requested review of this revision.Jul 11 2023, 6:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 6:21 AM
asb added a comment.Jul 13 2023, 8:51 AM

Just to note that @jobnoorman and I spent some time discussing this earlier in the week and I was pretty convinced the current behaviour isn't what is desired (especially for the second bullet point in the patch description). In D108961, @reames made a good argument for supporting this relaxation from the perspective of improving toolchain resilience (in particular, where we fail to accurately estimate the upper bounds of of instruction sizes). I think that applies here just as well.

Flagging as putting the code review aspect aside, it would be useful to figure out early if people feel strongly that the intent of this patch is wrong.

reames accepted this revision.Jul 20 2023, 10:26 AM

This makes perfect sense to me. LGTM, but please wait a few days before landing in case anyone else wants to chime in.

This revision is now accepted and ready to land.Jul 20 2023, 10:26 AM
MaskRay accepted this revision.Jul 21 2023, 8:53 PM

LGTM.

asb accepted this revision.Jul 24 2023, 8:04 AM

LGTM.

This revision was landed with ongoing or failed builds.Jul 28 2023, 1:56 AM
This revision was automatically updated to reflect the committed changes.

Should we back port this to llvm-17 branch cut?

Should we back port this to llvm-17 branch cut?

The previous behavior has been quite a while and I don't know who is actively requiring this behavior, so probably not a best candidate for release/17.x.