New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 47280 - [feature request] -fbuiltin-*
Summary: [feature request] -fbuiltin-*
Status: NEW
Alias: None
Product: clang
Classification: Unclassified
Component: C (show other bugs)
Version: unspecified
Hardware: PC All
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-21 16:55 PDT by Nick Desaulniers
Modified: 2020-08-21 17:23 PDT (History)
9 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Desaulniers 2020-08-21 16:55:03 PDT
From this thread on LKML: https://lore.kernel.org/lkml/76071c24-ec6f-7f7a-4172-082bd574d581@zytor.com/

It was noted that for hosted environments that don't quite have a full libc implementation, that they can lean on -fno-builtin-* flags in Clang to avoid libcall optimizations where LLVM would emit calls to * which the environment did not provide definitions of (though this differs from GCC's behavior, see these three posts from Arvind that are a really fantastic analysis:
1. https://lore.kernel.org/lkml/20200818214146.GA3196105@rani.riverdale.lan/
2. https://lore.kernel.org/lkml/20200820175617.GA604994@rani.riverdale.lan/
3. https://lore.kernel.org/lkml/20200821172935.GA1411923@rani.riverdale.lan/
).

Kernel developer H. Peter Anvin points out it would be helpful for embedded environments that would use -ffreestanding to opt out of all such libcall optimizations, to be able to opt back in (rather than opting out via -fno-builtin-*) to certain libcall optimizations when the otherwise freestanding implementation provided such symbols with matching semantics (ie. the Linux kernel).

It was suggested that listing supported functions in the environment via #pragma's (maybe in a header for codebase-wide inclusion) might be helpful.  In particular, I think it would slot in nicely in Clang to provide -fbuiltin-* support (there's rules in tablegen to generate support for -f-* and -fno-* from one rule).  So `-ffreestanding -fbuiltin-strcpy` would be "disable all libcall optimizations except related to transforming other libcalls into strcpy, denoting the target supported no other builtins than strcpy (and the always required mem* family).

It's not an immediate need, more so a curiosity that it's easy to opt out of libcall optimizations, but not necessarily opt in.
Comment 1 Eli Friedman 2020-08-21 17:23:20 PDT
Another suggestion from that thread which seemed potentially viable was adding a "-fno-synthesize-libc-calls" flag: basically, assume calls to known functions have the libc semantics, but don't transform those calls to refer to other libc functions.  The end result is a bit weird semantically: conventionally, whether libc usable is sort of a binary choice. But it would follow the longstanding kernel preference for "do what I mean" semantics without requiring the user to actually list out the relevant functions.