This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Accept zicsr and zifencei command line options
ClosedPublic

Authored by reames on Feb 13 2023, 2:41 PM.

Details

Summary

This change adds the definition of the two extensions, but does not either a) make any instruction conditional on them or b) enabled the extensions by default. (The *instructions* do remain enabled by default per ISA version 2.0 which is our current default.)

This is meant to be a building block towards something like https://reviews.llvm.org/D141666, and in the meantime, address one of the most surprising of the current user experience warts. The current behavior of rejecting the extensions at the command line despite emitting code which appears to use them is surprising to anyone not deeply versed in the details of this situation.

Between versions 2.0 and 2.1 of the base I specification, a backwards incompatible change was made to remove selected instructions and CSRs from the base ISA. These instructions were grouped into a set of new extensions (these), but were no longer required by the base ISA. This change is described in “Preface to Document Version 20190608-Base-Ratified” from the specification document.

As LLVM currently implements only version 2.0 of the base specification, accepting these extensions at the command line introduces a configuration which doesn't actually match any spec version. It's a pretty harmless variant since the 2.0 extension definitions, to my knowledge, exactly match the text from the 2.0 I text before they were moved into standalone extensions in 2.1 of I. (The version numbering in that sentence is a tad confusing to say the least. Hopefully I got it right.)

It is worth noting that we already have numerous examples of accepting extensions in the march string which didn't exist in version of the spec document corresponding to our current base I version, so this doesn't set any new precedent.

Diff Detail

Event Timeline

reames created this revision.Feb 13 2023, 2:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 13 2023, 2:41 PM
reames requested review of this revision.Feb 13 2023, 2:41 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 13 2023, 2:41 PM

I'm fine with this approach but I think they should be filtered out of the ELF attributes, maybe also preprocessor macros? That is, treating them as a redundant no-op in the driver rather than like a true extension.

@jrtc27 Not sure if this changes your take, but I realized the variant being introduced is maybe much less interesting than I'd first thought. We generally make no effort to make sure an extension was defined in the spec version corresponding to our base revision. Given that, we have a bunch of cases where we allow I2.0 + some random extension. Given that, this one stops looking all that interesting. It doesn't actually set much precedent - because we already did that, a long time ago.

If you agree with that framing, I'll rework the description.

@jrtc27 Not sure if this changes your take, but I realized the variant being introduced is maybe much less interesting than I'd first thought. We generally make no effort to make sure an extension was defined in the spec version corresponding to our base revision. Given that, we have a bunch of cases where we allow I2.0 + some random extension. Given that, this one stops looking all that interesting. It doesn't actually set much precedent - because we already did that, a long time ago.

If you agree with that framing, I'll rework the description.

Hm, do we allow M + Zmmul? If so then I guess I can get behind that view.

@jrtc27 Not sure if this changes your take, but I realized the variant being introduced is maybe much less interesting than I'd first thought. We generally make no effort to make sure an extension was defined in the spec version corresponding to our base revision. Given that, we have a bunch of cases where we allow I2.0 + some random extension. Given that, this one stops looking all that interesting. It doesn't actually set much precedent - because we already did that, a long time ago.

If you agree with that framing, I'll rework the description.

Hm, do we allow M + Zmmul? If so then I guess I can get behind that view.

$ ~/llvm-dev/build/bin/clang -target riscv64 -march=rv64g_zmmul vector_add_i32.c -S
$ cat vector_add_i32.s 
	.text
	.attribute	4, 16
	.attribute	5, "rv64i2p0_m2p0_a2p0_f2p0_d2p0_zmmul1p0"

vector_add_i32.c is a random C file; contents are uninteresting.

jrtc27 accepted this revision.Feb 13 2023, 5:22 PM

Personally happy with the concept then, seems consistent and overall helpful, just some nits

llvm/lib/Target/RISCV/RISCVFeatures.td
83–86

s/ifence/fence.i/ (or Instruction Fence if you want words) for both these

llvm/test/CodeGen/RISCV/attributes.ll
105–106
155–156
This revision is now accepted and ready to land.Feb 13 2023, 5:22 PM
asb added a comment.Feb 14 2023, 7:06 AM

Thanks Philip. I started looking at this too, and agree that something like this is a good next step. I've been looking more closely at the GCC behaviour and noticed a couple of things that might be of interest:

  • -march=rv64gc results in`rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0`, but -march=rv64imafdc results in rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0
  • -march=rv64i2p0_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0 results in rv64i2p0_m2p0_a2p1_f2p2_d2p2_c2p0 (i.e. the zicsr and zifencei are silently dropped)

The strategy proposed in this patch seems sensible to me, unless someone can see a good reason I'm missing to try to go more down the route gcc took and to rewrite the -march string?

I'm looking at the case now of LLD erroring out when linking a .o specifying I2.1, which I think was unintentional and a regression from 15.0.x to 16.0.x. Similar with llvm-objdump refusing to try disassembling a file with I2.1, which is probably more user hostile than we'd like.

reames updated this revision to Diff 497328.Feb 14 2023, 7:46 AM
reames edited the summary of this revision. (Show Details)

Add docs, and rework description.

reames updated this revision to Diff 497336.Feb 14 2023, 8:02 AM

Address @jrtc27's comments.

We seem to have rough consensus here, but I'm going to wait roughly 24 hours before landing to give others time to comment.

asb added a comment.Feb 14 2023, 11:48 AM

I've posted RFC: Resolving issues related to extension versioning in RISC-V which discusses some related issues (I think it complements this patch, rather than anything being blocking or suggesting a contradictory approach).

This was discussed at today's RISCV sync-up call. We have a large conversion pending on the topic of isa compatibility checking, but there was a consensus that this was reasonable and could move forward. I'm going to be landing this change in the near future.

This revision was landed with ongoing or failed builds.Feb 16 2023, 10:41 AM
This revision was automatically updated to reflect the committed changes.
asb added a comment.Feb 16 2023, 10:41 AM

LGTM, and the summary of the discussion in the sync-up call matches my understanding.