This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Implement __rndr, __rndrrs intrinsics
ClosedPublic

Authored by stelios-arm on Mar 9 2021, 8:01 AM.

Details

Summary

This patch implements the __rndr and __rndrrs intrinsics to provide access to the random number instructions introduced in Armv8.5-A. They are only defined for the AArch64 execution state and are available when __ARM_FEATURE_RNG is defined.

These intrinsics store the random number in their pointer argument and return a status code if the generation succeeded. The difference between __rndr and __rndrrs, is that the latter intrinsic reseeds the random number generator.

The instructions write the NZCV flags indicating the success of the operation that we can then read with a CSET.

[1] https://developer.arm.com/docs/101028/latest/data-processing-intrinsics
[2] https://bugs.llvm.org/show_bug.cgi?id=47838

Diff Detail

Event Timeline

stelios-arm created this revision.Mar 9 2021, 8:01 AM
stelios-arm requested review of this revision.Mar 9 2021, 8:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 9 2021, 8:01 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
SjoerdMeijer added inline comments.Mar 9 2021, 8:23 AM
clang/lib/Basic/Targets/AArch64.cpp
363

Where/when is HasRandGen set?

clang/test/Preprocessor/init-aarch64.c
28 ↗(On Diff #329347)

Why are we expecting this here? Are we not only expecting this for v8.5?

We also need a negative test and CHECK-NOT of this where we don't expect this.

llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

Do all MRS instructions do this?

llvm/lib/Target/AArch64/AArch64InstrInfo.td
1276

Nit: can be on the same line.

dmgreen added inline comments.Mar 9 2021, 8:34 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

No, but some do and it's not obvious which ones do and don't. I think it should be reasonable to always def NZCV here, even if they are just dead. It should be very rare that it would be beneficial for NZCV to be live across a MRS instruction.

llvm/lib/Target/AArch64/AArch64InstrInfo.td
1272

These are usually put somewhere else in the aarch64 backend, and it might be worth keeping it with the others.

1276

I always prefer Pat's to have input and output lines separate, for what it's worth. It makes them more easily readable.

SjoerdMeijer added inline comments.Mar 9 2021, 9:09 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

True, but since creating another definition is cheap, what would the cons be of:

class MRS_NZCV : MRSI {
  ..
  let Defs = [NZCV];
}

The way I look at it is that the description would be more accurate?

dmgreen added inline comments.Mar 9 2021, 10:02 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
16019

Can you make sure EQ is correct here? I think it might be backwards.

llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

I believe that would have an over-lapping definition with the existing MRS instruction?

It would need to be a pseudo I think, which would eventually be turned into a MSR proper late enough on the pipeline for it to be valid (or the other way around, the no-nzcv version gets turned into a the nzcv version to keep the verifier happy).

It could also be a optional def, but those are only used in the arm backend and I would not recommend using them anywhere else. I would probably suggest just setting MRS as a NZCV setting instruction, unless we find a reason to need to implement it differently.

SjoerdMeijer added inline comments.Mar 9 2021, 10:41 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

I believe that would have an over-lapping definition with the existing MRS instruction?

Ah yeah, that might be true.

It would need to be a pseudo I think

How much work is adding a pseudo for this case? My first reaction would be just trying to model this correctly, then we potentially don't have to worry again later about this.

@SjoerdMeijer @dmgreen Thanks for your reviews, I will be looking into this.

SjoerdMeijer added inline comments.Mar 10 2021, 2:42 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

I just wanted to add that I don't have too strong opinions on this, but what I suggested seemed more the correct, even though the consequence of setting NCZV for all MRS is minimal. So I will leave this one up to you @dmgreen and @stelios-arm .

stelios-arm marked 4 inline comments as done.Mar 10 2021, 4:26 PM
stelios-arm added inline comments.
clang/lib/Basic/Targets/AArch64.cpp
363

Oh, I forgot to set it, I am going to address this in the new revision.

clang/test/Preprocessor/init-aarch64.c
28 ↗(On Diff #329347)

Correct this shouldn't be here. I am going to address this in a new revision.

llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

I will talk with @dmgreen and if it is decided that it is needed to change, I will address it in a future revision. Please note that the next revision of patch, will not address this comment (temporarily).

llvm/lib/Target/AArch64/AArch64InstrInfo.td
1276

The styling for Pat's is not consistent across the file, some are in one line and others have the input and output lines separate. I also prefer the latter for readability.

stelios-arm marked 2 inline comments as done.

Addressed the comments made by @SjoerdMeijer and @dmgreen.

dmgreen added inline comments.Mar 11 2021, 12:48 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

Hmm. I feel like adding the pseudo is more trouble than it is worth - they do not come for free. With the extra lowering and adding things like scheduling info, etc. It feels simpler to me to set the flags, even if that's not always exactly what the instruction would do.

SjoerdMeijer added inline comments.Mar 11 2021, 1:08 AM
llvm/lib/Target/AArch64/AArch64InstrFormats.td
1495

Okidoki, then we at least need some comments here explaining this all.

stelios-arm marked an inline comment as done.
  1. Addressed the comment made by @SjoerdMeijer and added the comment for the MRS instruction.
  2. Added the +rang feature in arm_acle.c

Rephrased a comment.

SjoerdMeijer added inline comments.Mar 12 2021, 2:30 AM
clang/lib/Basic/Targets/AArch64.cpp
364

This needs a clang preprocessor test: both a check for its presence and absence.
Have a look/grep where these others predefined macros are tested.

clang/test/CodeGen/arm_acle.c
1760

Not sure if I am surprised that this works.... This is (re)using tag AArch64-v8_3 and for the other tests that use this and don't have RNG set, I was expecting FileCheck to complain about this, not sure if I am missing something though.

llvm/lib/Target/AArch64/AArch64InstrInfo.td
610–612

Nit: for this comment to be informative, please expand it, or just remove it if there's not much to say about it.

stelios-arm marked an inline comment as done.
  1. Removed a redundant comment
  2. Removed the changes made in the test/CodeGen/arm_acle.c, since the test is disabled.
  3. Added a clang preprocessor test to check the presence and absence of the __ARM_FEATURE_RNG macro.
stelios-arm added inline comments.Mar 12 2021, 7:04 AM
clang/test/CodeGen/arm_acle.c
1760

It turns out that this test is disabled (https://github.com/llvm/llvm-project/commit/6fcd4e080f09c9765d6e0ea03b1da91669c8509a). I am going to remove the changes in this file.

SjoerdMeijer added inline comments.Mar 15 2021, 2:23 AM
clang/test/CodeGen/arm_acle.c
1760

But this seems to be a useful test to have. Are we testing this elsewhere now?

stelios-arm added inline comments.Mar 15 2021, 2:28 AM
clang/test/CodeGen/arm_acle.c
1760

@dmgreen created a patch to re-enable this test (https://reviews.llvm.org/D98510). I am going to add the tests again.

Added tests in arm_acle.c.

SjoerdMeijer accepted this revision.Mar 15 2021, 7:00 AM

Thanks, nice one, LGTM.

llvm/test/CodeGen/AArch64/rand.ll
2

Nit: we don't need -mcpu=neoverse-v1, so just remove it?

This revision is now accepted and ready to land.Mar 15 2021, 7:00 AM
stelios-arm updated this revision to Diff 330670.EditedMar 15 2021, 8:26 AM

Changed the rand.ll llc arguments to the ones that are only relevant.

stelios-arm marked an inline comment as done.Mar 15 2021, 8:26 AM

Typo fix for __ARM_FEATURE_RNG macro check in aarch64-target-features.c.

This revision was landed with ongoing or failed builds.Mar 15 2021, 10:52 AM
This revision was automatically updated to reflect the committed changes.