This is an archive of the discontinued LLVM Phabricator instance.

[X86] Emulate _rdrand64_step with two rdrand32 if it is 32bit
ClosedPublic

Authored by yubing on Aug 18 2022, 8:30 AM.

Diff Detail

Event Timeline

yubing created this revision.Aug 18 2022, 8:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2022, 8:30 AM
Herald added a subscriber: pengfei. · View Herald Transcript
yubing requested review of this revision.Aug 18 2022, 8:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2022, 8:30 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
craig.topper added inline comments.Aug 18 2022, 8:40 AM
clang/lib/Headers/immintrin.h
297

Should & be &&?

297

Can we avoid the pointer cast here? Use two unsigned ints and manually concatenate them to a 64-bit value.

RKSimon added inline comments.Aug 18 2022, 8:50 AM
clang/lib/Headers/immintrin.h
297

+1

unsigned int lo, hi;
if (__builtin_ia32_rdrand32_step(&lo) &&
    __builtin_ia32_rdrand32_step(&hi)) {
  *p = ((unsigned long)hi << 32) | lo;
  return 1;
}
clang/test/CodeGen/X86/rdrand-builtins.c
2

X86 not X32 :)

yubing updated this revision to Diff 453865.Aug 18 2022, 7:34 PM

Address comments

yubing updated this revision to Diff 453866.Aug 18 2022, 7:35 PM

fix a small issue

RKSimon added inline comments.Aug 19 2022, 6:11 AM
clang/test/CodeGen/X86/rdrand-builtins.c
19–20

why do you still need the #if-else-endif?

craig.topper added inline comments.Aug 19 2022, 9:59 AM
clang/lib/Headers/immintrin.h
296

variable names in intrinsic headers must start with 2 underscores.

yubing updated this revision to Diff 454356.Aug 21 2022, 6:45 PM

address simon's comments

craig.topper added inline comments.Aug 21 2022, 7:00 PM
clang/lib/Headers/immintrin.h
296

What about this comment?

yubing updated this revision to Diff 454357.Aug 21 2022, 7:06 PM

address craig's comments

RKSimon added inline comments.Aug 22 2022, 6:23 AM
clang/lib/Headers/immintrin.h
297

Are there any sideeffects that we might encounter by not always performing both __builtin_ia32_rdrand32_step calls?

unsigned int __lo, __hi;
int __res_lo = __builtin_ia32_rdrand32_step(&__lo);
int __res_hi = __builtin_ia32_rdrand32_step(&__hi);
if (__res_lo && __res_hi) {
  *__p = ((unsigned long long)__hi << 32) | (unsigned long long)__lo;
  return 1;
} else {
  *__p = 0;
  return 0;
}
yubing added inline comments.Aug 22 2022, 6:45 AM
clang/lib/Headers/immintrin.h
297

however, if the first rdrand32 failed, then we don't need to execute the second one.

RKSimon added inline comments.Aug 22 2022, 7:07 AM
clang/lib/Headers/immintrin.h
297

I understand that - but given randomizers are often used for sensitive applications (crypto) - my question was whether not always calling this twice was going to affect things.

yubing updated this revision to Diff 454738.Aug 23 2022, 1:36 AM

Execute the second rdrand32 despite of whether the first one fail or not

RKSimon accepted this revision.Aug 23 2022, 1:59 AM

LGTM - cheers

This revision is now accepted and ready to land.Aug 23 2022, 1:59 AM
This revision was landed with ongoing or failed builds.Aug 23 2022, 6:29 PM
This revision was automatically updated to reflect the committed changes.
yubing reopened this revision.Aug 23 2022, 7:12 PM
This revision is now accepted and ready to land.Aug 23 2022, 7:12 PM
yubing updated this revision to Diff 455041.Aug 23 2022, 7:12 PM

address sign-conversion issue

This revision was landed with ongoing or failed builds.Aug 23 2022, 7:23 PM
This revision was automatically updated to reflect the committed changes.