This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][MC]Add support for Binary MCExpr
ClosedPublic

Authored by Yunzezhu on Aug 11 2023, 2:52 AM.

Details

Summary

There is an issue: https://github.com/llvm/llvm-project/issues/64612
This issue happens because in RISCVMCCodeEmitter::getImmOpValue it only handles MCExpr kind Target and SymbolRef.
When code with format like .+ comes in, it comes with MCExpr kind Binary, the fixupkind remains fixup_riscv_invalid and reports error.

This patch make MCExpr kind Binary handled with the same way as MCExpr kind SymbolRef, so code with binary expression can get correct fixupkind and be used to generate more complex relocation.

Diff Detail

Event Timeline

Yunzezhu created this revision.Aug 11 2023, 2:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 2:52 AM
Yunzezhu requested review of this revision.Aug 11 2023, 2:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 2:52 AM
asb added a comment.Aug 14 2023, 7:30 AM

Thanks for catching this. It looks like if you do e.g. .LBB0-16 it will just underflow - do other targets do anything smarter here?

I tried to fix this issue yesterday and I used the same way as this patch. Its problem is that it can't complile case in https://github.com/llvm/llvm-project/issues/64623:

<unknown>:0: error: unsupported relocation type

Because there is no corresponding relocation type for fixup type fixup_riscv_12_i.

Thanks for catching this. It looks like if you do e.g. .LBB0-16 it will just underflow - do other targets do anything smarter here?

It seems PowerPC handles expr with binary expression opcode add only by adding an assert, so no underflow happens without sub kind binary expr. Other targets support binary expr like Hexagon and Lanai get fixup kind separately from LHS and RHS or from LHS only, and ignore such possible underflow.
I tried gcc and gcc supports sub kind binary exprs like .LBB0-16 too. I'm not sure if such sub expr will cause underflow since local symbol handles relocation only and global symbol looks have little chance of underflow.

Yunzezhu updated this revision to Diff 553334.Aug 24 2023, 7:44 PM

fixed a problem cause warning during compiling.

I tried to fix this issue yesterday and I used the same way as this patch. Its problem is that it can't complile case in https://github.com/llvm/llvm-project/issues/64623:

<unknown>:0: error: unsupported relocation type

Because there is no corresponding relocation type for fixup type fixup_riscv_12_i.

I think this is fine as this error is only generated when relaxation is enabled in which case it safer to not allow the symbol difference expression in LI. The error message could be improved though.

gentle ping

asb added a comment.Sep 5 2023, 6:14 AM

As this lacks coverage for li which was the source of the original bug report, could you please add in the tests from D158830. I think it would probably be worth capturing the current underflow behaviour too (perhaps with a FIXME? note that it would likely be better to error here). Thanks.

Yunzezhu updated this revision to Diff 555977.Sep 6 2023, 12:48 AM

Add test cases from https://reviews.llvm.org/D158830 and FIXME note.

yroux added a subscriber: yroux.Sep 18 2023, 5:13 AM

Hi,

Just a comment to point that this patch will help building the RISCV Architecture test-suite with Clang
where test macros for JAL and branch contain instructions like inst reg1, reg2, label+offset.

I was about to submit the same patch, so it LGTM ;) but I let others accept it.

asb accepted this revision.Sep 18 2023, 7:50 AM

LGTM, thanks!

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