This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][builtins] Do not assume int to be at least 32 bit wide
AbandonedPublic

Authored by atrosinenko on Aug 19 2020, 8:45 AM.

Details

Summary

This patch drops implicit assumptions on int/unsigned types being at least 32 bit wide. This is not always true on 16 bit targets such as MSP430.

This patch contains the following changes:

  • rep_clz(a) and src_rep_t_clz(a) for 64-bit rep_t from soft-float utility headers are moved to int_lib.h as a common clzdi(a) implementation (signedness was changed to match one of __builtin_clz series of intrinsics). The original functions was turned into inline proxy functions that merely adapt the signedness
  • __floatsi[st]f and __floatunsi[st]f3 are updated to accept a signed/unsigned integer argument of a specific width
  • similar changes were made for some unit tests
  • __udivmoddi4 switched to ctzsi macro defined in int_lib.h instead of __builtin_ctz
  • the *vfp LibCall versions were NOT touched

Diff Detail

Event Timeline

atrosinenko created this revision.Aug 19 2020, 8:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2020, 8:45 AM
Herald added subscribers: Restricted Project, dberris. · View Herald Transcript
atrosinenko requested review of this revision.Aug 19 2020, 8:45 AM

On one hand, this clzdi() implementation should be completely harmless when fallback is implemented via clzsi(). On the other hand, it may possibly be completely removed. This code snippet mimics one already existing for clzsi in int_types.h and for currently supported targets it can probably be even as simple as this (provided we can use C99):

#if ULLONG_MAX == 0xFFFFFFFFFFFFFFFF
#define clzdi __builtin_clzll
#define ctzdi __builtin_ctzll
#else
#error could not determine appropriate clzdi macro for this system
#endif
compiler-rt/lib/builtins/int_lib.h
149

@scanon

This function was copied from fp_lib.h almost unchanged, so it can be used from fp_extend.h (the version from that file is almost identical except it does not use the properly-sized clzsi() macro). But is this manual fallback implementation useful nowadays? As I can see, the implementation in fp_lib.h remained almost untouched since 2010 when this file was uploaded via rGb1fdde1d01a3: Adding soft-float comparisons, addition, subtraction, multiplication and…. Now, __builtin_clzll is used in many places of compiler-rt/builtins library for seemingly the same purpose.

atrosinenko abandoned this revision.Aug 25 2020, 9:16 AM

Replaced this by D86546: [compiler-rt][builtins] Use explicitly-sized integer types for LibCalls and D86547: [compiler-rt][builtins] Use c[tl]zsi macro instead of __builtin_c[tl]z. These two patches include everything except for factoring out src_rep_t_clz() and rep_clz() to clzdi() for now.