This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][MC] Add CLI option to disable branch relaxation
ClosedPublic

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

Details

Summary

D154958 enables branch relaxation for unresolved symbols. This has an
interesting consequence for some LLD tests: branch relocations are
tested by using branches to undefined symbols and defining them, with
different values, on the LLD command line. These tests broke and there
doesn't seem to be an easy workaround: as far as I can tell, there is no
way to convince llvm-mc to emit a branch relocation to an undefined
symbol without branch relaxation kicking in.

This patch proposes to add a flag, --riscv-disable-branch-relaxation,
to do just that. The main purpose for this flag is for testing but it
might be seen as a first step to some kind of "strict" or WYSIWYG mode
(i.e., what you give to the assembler is exactly what comes out). The
need for this has been mentioned in, for example, D108961. However, I
suspect there will be a lot of discussion around what exactly such a
strict mode would look like. Therefore, I gated this feature behind a
CLI flag instead of adding a new target feature.

Diff Detail

Event Timeline

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

I'm supportive of adding a targeted flag to make it easier to write these tests, with the idea that a more public facing flag might later be introduced that alters a full "strict" mode.

I think it might make sense to pass cl::Hidden to DisableBranchRelaxation so this doesn't become a documented feature.

llvm/test/MC/RISCV/long-jump-disable-relax.s
3

Nit: I think we normally use the -single-hyphen-prefix spelling for llvm-mc opts.

MaskRay added inline comments.Jul 21 2023, 8:56 AM
llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
32

Drop false.

I think positive option names are more common. Perhaps -riscv-asm-relax-branch=0 since asm and linker relaxation are quite different and the current name can lure users to think the other way.

MaskRay added inline comments.Jul 21 2023, 8:48 PM
llvm/test/MC/RISCV/long-jump-disable-relax.s
18

Also add tests that the target symbol is defined.

Address reviewer comments:

  • Make option cl::Hidden
  • Rename option to -riscv-asm-relax-branches
  • Add tests for branches to defined symbols

The tests revealed some issues for the case where symbols are out of range for
compressed jumps and branches: no errors were reported because it was impossible
to trigger this (due to relaxation). This update also adds to necessary error
reporting.

jobnoorman marked 3 inline comments as done.Jul 24 2023, 12:16 AM
asb accepted this revision.Jul 24 2023, 7:56 AM

LGTM, thanks.

This revision is now accepted and ready to land.Jul 24 2023, 7:56 AM
MaskRay accepted this revision.Jul 24 2023, 12:10 PM
MaskRay added inline comments.Jul 27 2023, 9:41 PM
llvm/test/MC/RISCV/long-jump-disable-relax.s
2

Give a file-level comment?

45

[[@LINE]] is deprecated lit syntax. Use [[#@LINE]] for new tests.

Address @MaskRay's comments

jobnoorman marked 2 inline comments as done.Jul 28 2023, 1:41 AM
This revision was landed with ongoing or failed builds.Jul 28 2023, 1:42 AM
This revision was automatically updated to reflect the committed changes.