This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Don't allow X0 to be used for 'r' constraint in inline assembly
ClosedPublic

Authored by craig.topper on Jul 7 2023, 1:43 PM.

Details

Summary

Some instructions treat x0 as a special encoding rather than as a
value of 0. Since we don't parse the inline assembly to know what
the instruction is, chooser the safest option of never using x0.

Fixes #63747.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 7 2023, 1:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2023, 1:43 PM
craig.topper requested review of this revision.Jul 7 2023, 1:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2023, 1:43 PM
jrtc27 added a comment.Jul 7 2023, 1:46 PM

Does __asm__("%z0" :: "r"(0)); still work and give x0?

Does __asm__("%z0" :: "r"(0)); still work and give x0?

That doesn't work in gcc https://godbolt.org/z/x3vPWao1d

jrtc27 added a comment.Jul 7 2023, 1:59 PM

Does __asm__("%z0" :: "r"(0)); still work and give x0?

That doesn't work in gcc https://godbolt.org/z/x3vPWao1d

Huh, only for "i" then I guess. Bit sad but oh well.

asb accepted this revision.Jul 9 2023, 9:40 AM

It feels like the "better" solution would be to introduce a new constraint for GPRs other than X0. But I guess there's not a huge advantage, and we've got the precedent of GCC's behaviour so this is probably the best we can do. LGTM.

This revision is now accepted and ready to land.Jul 9 2023, 9:40 AM

GCC is using rJ and %z<N> for such case, but I found https://github.com/riscv-non-isa/riscv-c-api-doc/blob/master/riscv-c-api.md#constraints-on-operands-of-inline-assembly-statements isn't document that, seems like we should add more NOTE to mention that.

Few more info about r in GCC, r is special operand constraint, it must be point to some register class, no symbolic operand or immediate is allowed, it's kind of hard code logic in generic code.

This revision was landed with ongoing or failed builds.Jul 10 2023, 1:30 PM
This revision was automatically updated to reflect the committed changes.