This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][builtins] Factor out some common bit manipulations
Needs ReviewPublic

Authored by atrosinenko on Aug 20 2020, 8:52 AM.

Details

Summary

This patch incapsulates some commonly used hacks in a (hopefully) UB-free manner.

Some existing LibCall implementations contain constants like this one:

const di_int MIN = (di_int)1 << ((int)(sizeof(di_int) * CHAR_BIT) - 1);

UBSan complains on such expressions and that seems to be true positive.

This patch is expected to be NFC except for eliminating some UB. It consists of the following parts:

  • implementing helpers in int_types.h
  • replacing some typedefs with #defines so these type alises can be handled by the above machinery
  • replacing some constants with these common helper invocations
    • this is expected to be unable to cause any performance regressions as these are actually compile-time constants
  • fixing a couple of UBs in __neg?i2 LibCalls

Diff Detail

Event Timeline

atrosinenko created this revision.Aug 20 2020, 8:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 20 2020, 8:52 AM
Herald added subscribers: Restricted Project, dexonsmith, dberris. · View Herald Transcript
atrosinenko requested review of this revision.Aug 20 2020, 8:52 AM
lebedev.ri resigned from this revision.Aug 20 2020, 8:53 AM
MaskRay added inline comments.Aug 24 2020, 11:27 AM
compiler-rt/lib/builtins/int_mulo_impl.inc
22

AFAIK LLVM_USE_SANITIZER does not instrument compiler-rt. libclang_rt.builtins- is at the bottom of the library layering. How did you enable sanitizers?

Explicilty mention that UBSan have to be manually configured in trap-on-error mode to operate of builtins library.

atrosinenko added inline comments.Aug 24 2020, 1:28 PM
compiler-rt/lib/builtins/int_mulo_impl.inc
22

AFAIK LLVM_USE_SANITIZER does not instrument compiler-rt. libclang_rt.builtins- is at the bottom of the library layering. How did you enable sanitizers?

No, I have not managed to force CMake to enable UBSan on builtins. I just passed -fsanitize=undefined -fsanitize-undefined-trap-on-error cflags explicitly. The second option is exactly because I cannot link UBSan runtime libraries here but can enable -ggdb3 and look where sanitizer triggered.

So, this hack works for this particular setup, so I can manually run such checks at least manually at least on clang, etc.