Understanding __builtin_-functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding __builtin_-functions

Kristóf Umann via cfe-dev

Hey Clang-Experts,

 

I’m currently working with Clang-CL based on the LLVM 8.0.0 (is that the right term? Or is it just Clang 8.0.0?) and targeting Windows with 64bit.

In my code I used the function “__builtin_signbitd64” which I picked up from the GCC list of builtin functions.

 

In my understanding, builtin functions are not really functions, but instead they get replaced with assembly instructions, like intrinsic functions. However, when I printed the resulting assembly code I got:

 

#DEBUG_VALUE: signbit:_X <- $xmm6

#DEBUG_VALUE: preSqrt <- $xmm6

.cv_inline_site_id 70 within 58 inlined_at 6 588 0

.cv_loc     70 7 312 0              # C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt\corecrt_math.h:312:0

movapd      xmm0, xmm6

call  __builtin_signbitd64

xorpd xmm0, xmm0

test  eax, eax

 

And later an undefined reference to said “__builtin_signbitd64” function. Why? How do builtin functions work then? What is there purpose?

 

Kind greetings

Björn

Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Understanding __builtin_-functions

Kristóf Umann via cfe-dev

> In my code I used the function “__builtin_signbitd64” which I picked up from the GCC list of builtin functions.

 

As a rule, Clang does not view the GCC list of builtins as a set of API requirements.  Clang generally views builtins as a convenient implementation detail.  Because builtins use a function-like syntax, Clang identifies builtins by name.  If the name doesn't match the list of known builtins, even if the name starts with "__builtin_", Clang will treat it as a normal function.

 

Intrinsics (e.g. those defined by Intel) are the user-facing API.  They might or might not use builtins in their implementations; over the years I have seen Clang remove many builtins as the optimizers and code generators became better able to select the optimal instruction sequences that previously required builtins.  If there's an intrinsic that does what you want, it should be the stable API.

--paulr

 

From: cfe-dev [mailto:[hidden email]] On Behalf Of Gaier, Bjoern via cfe-dev
Sent: Friday, August 30, 2019 6:12 AM
To: 'Clang Dev'
Subject: [cfe-dev] Understanding __builtin_-functions

 

Hey Clang-Experts,

 

I’m currently working with Clang-CL based on the LLVM 8.0.0 (is that the right term? Or is it just Clang 8.0.0?) and targeting Windows with 64bit.

In my code I used the function “__builtin_signbitd64” which I picked up from the GCC list of builtin functions.

 

In my understanding, builtin functions are not really functions, but instead they get replaced with assembly instructions, like intrinsic functions. However, when I printed the resulting assembly code I got:

 

#DEBUG_VALUE: signbit:_X <- $xmm6

#DEBUG_VALUE: preSqrt <- $xmm6

.cv_inline_site_id 70 within 58 inlined_at 6 588 0

.cv_loc     70 7 312 0              # C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt\corecrt_math.h:312:0

movapd      xmm0, xmm6

call  __builtin_signbitd64

xorpd xmm0, xmm0

test  eax, eax

 

And later an undefined reference to said “__builtin_signbitd64” function. Why? How do builtin functions work then? What is there purpose?

 

Kind greetings

Björn

Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Understanding __builtin_-functions

Kristóf Umann via cfe-dev

Many thanks for that answer! Now I understand better!

 

From: [hidden email] <[hidden email]>
Sent: Freitag, 30. August 2019 16:33
To: Gaier, Bjoern <[hidden email]>
Cc: [hidden email]
Subject: RE: Understanding __builtin_-functions

 

> In my code I used the function “__builtin_signbitd64” which I picked up from the GCC list of builtin functions.

 

As a rule, Clang does not view the GCC list of builtins as a set of API requirements.  Clang generally views builtins as a convenient implementation detail.  Because builtins use a function-like syntax, Clang identifies builtins by name.  If the name doesn't match the list of known builtins, even if the name starts with "__builtin_", Clang will treat it as a normal function.

 

Intrinsics (e.g. those defined by Intel) are the user-facing API.  They might or might not use builtins in their implementations; over the years I have seen Clang remove many builtins as the optimizers and code generators became better able to select the optimal instruction sequences that previously required builtins.  If there's an intrinsic that does what you want, it should be the stable API.

--paulr

 

From: cfe-dev [[hidden email]] On Behalf Of Gaier, Bjoern via cfe-dev
Sent: Friday, August 30, 2019 6:12 AM
To: 'Clang Dev'
Subject: [cfe-dev] Understanding __builtin_-functions

 

Hey Clang-Experts,

 

I’m currently working with Clang-CL based on the LLVM 8.0.0 (is that the right term? Or is it just Clang 8.0.0?) and targeting Windows with 64bit.

In my code I used the function “__builtin_signbitd64” which I picked up from the GCC list of builtin functions.

 

In my understanding, builtin functions are not really functions, but instead they get replaced with assembly instructions, like intrinsic functions. However, when I printed the resulting assembly code I got:

 

#DEBUG_VALUE: signbit:_X <- $xmm6

#DEBUG_VALUE: preSqrt <- $xmm6

.cv_inline_site_id 70 within 58 inlined_at 6 588 0

.cv_loc     70 7 312 0              # C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt\corecrt_math.h:312:0

movapd      xmm0, xmm6

call  __builtin_signbitd64

xorpd xmm0, xmm0

test  eax, eax

 

And later an undefined reference to said “__builtin_signbitd64” function. Why? How do builtin functions work then? What is there purpose?

 

Kind greetings

Björn

Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika

Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev