November 22, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On 11/22/2016 10:57 AM, tsbockman wrote: > Your test fails because you aren't actually using `core.bitop`. Intrinsics are > detected based on fully qualified names. As soon as you copy `bsf()` and `bsr()` > outside of `core.bitop`, the FQN changes and they're not intrinsics anymore. That is correct. From toir.d, this is what is detected: "_D4core5bitop3bsfFNaNbNiNfkZi" i.e. the fully mangled name. Detecting any function called "bsf" would be problematic because it would essentially add an arbitrary number of reserved words to the core language. An alternative would be to name them with a prefix like: __intrinsic_bsf but I see little advantage to that over core.bitop.bsf |
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 23/11/2016 1:46 PM, Walter Bright wrote:
> On 11/22/2016 5:31 AM, Ilya Yaroshenko wrote:
>> Please add a module (core.intrinsics ?) which will contain all DMD
>> intrinsics
>> similar to ldc.intrinsics. After each DMD release it is not clear what is
>> intrinsics and what is not. I need BSF intrinsics for Better C library
>> Mir
>> Random [1], which should work without linking with DRuntime and
>> Phobos. I can
>> use ldc.intrinsics for LDC, but have no idea about DMD. I want BSR and
>> BSF
>> instructions to be generated instead of current _software_
>> implementation in
>> core.bitop.
>>
>> ==================
>> Philosophical Questions:
>>
>> 1. Why hight level stuff like BitRange is in core.bitop, but not in
>> std.bitmanip? If it should be in core, why it is public?
>>
>> 2. Why bsf and bsr do NOT use hardware instructions anymore?
>> ==================
>>
>> Please ping me for Phobos and DRuntime PRs if they are related to math
>> and
>> numeric issues.
>
>
> The definitive list of dmd intrinsics is here:
>
> https://github.com/dlang/dmd/blob/master/src/toir.d#L349
>
> It is definitive in the sense that it is what dmd actually does, rather
> than what any documentation says it does :-)
>
> There reason there isn't a specific core.intrinsics module is that
> essentially any function in the library could be made an intrinsic by an
> implementation, and it is up to the implementation to make such choices.
> Therefore, making a function an intrinsic should not necessitate moving
> its location.
>
> The bodies of 'intrinsic' functions exist to:
>
> 1. provide a reference implementation that documents what it does
>
> 2. provide a fallback if some implementation decides to not make it an
> intrinsic. Such decisions are left up to the implementation.
>
> 3. the bodies are needed if the address of an intrinsic function is taken.
>
> If an implementation does decide to make a certain function an
> intrinsic, the function is not referenced from the object file and the
> library does not need to be linked to.
>
> bsr and bsf are dmd intrinsics. It's easy enough to verify by running
> obj2asm on a dmd generated object file, and then grepping it for bsr/bsf
> instruction mnemonics.
Most of those intrinsics are in std.math.
These things are why I failed to split std.math up.
Pretty please can we get them moved out?
|
November 22, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On 11/22/2016 3:07 PM, Johan Engelen wrote:
> On Tuesday, 22 November 2016 at 22:57:22 UTC, Iain Buclaw wrote:
>> It's a compilers job is to detect code patterns and emit suitable instructions
>> for them. :-)
>
> None of the compilers detect the body code pattern.
That is correct as far as bsf/bsr are concerned. The trouble with detecting a coding pattern is there are an endless number of ways such can be coded, and the compiler cannot detect all of them. Worse, the only way you can tell if the compiler did detect it is to look at the assembler output.
Nevertheless, the compiler still does detect some patterns, like for rol() and ror(), and uses a built-in operator for them. So that people use the detectable patterns, use the rol() template in core.bitop. The reason the compiler attempts to detect them anyway is there's a ton of code out there that has specific code written for rol/ror, and it's unlikely that people will ever rewrite it to use the templates.
You'll find that C/C++ compilers behave similarly. Burt Regehr did a blog entry on that a while back.
The compiler will also detect common forms of little/big endian byte manipulation, again see Regehr. This is pretty much standard behavior for modern compilers.
|
November 22, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On 11/22/2016 9:05 AM, Ilya Yaroshenko wrote:
> In addition, i need to be sure that an intrinsics function is always inlined
> (without -inline flag too).
If it is a supported intrinsic, it is always inlined. There'd be no purpose to it otherwise :-)
|
November 22, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 11/22/2016 8:07 AM, Steven Schveighoffer wrote:
> On 11/22/16 8:31 AM, Ilya Yaroshenko wrote:
>> 2. Why bsf and bsr do NOT use hardware instructions anymore?
> They should unless there is no hardware instruction available. I believe the
> software implementation is only a fallback when this is the case.
That's correct.
|
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Wednesday, 23 November 2016 at 01:18:09 UTC, Walter Bright wrote:
> On 11/22/2016 8:07 AM, Steven Schveighoffer wrote:
>> On 11/22/16 8:31 AM, Ilya Yaroshenko wrote:
>>> 2. Why bsf and bsr do NOT use hardware instructions anymore?
>> They should unless there is no hardware instruction available. I believe the
>> software implementation is only a fallback when this is the case.
>
> That's correct.
Thank you for the confirmation!
|
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On 2016-11-22 20:43, Ilya Yaroshenko wrote: > DMD can recognise code patterns and replace them with hardware > functions. I never seen that it can replace bodies. Why do you think it > can? Have you disassembler DMD the code with your PR? Here are all the built-in functions [1]. It's looking at the mangled name. [1] https://github.com/dlang/dmd/blob/master/src/builtin.d#L186 -- /Jacob Carlborg |
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 2016-11-23 01:46, Walter Bright wrote: > On 11/22/2016 5:31 AM, Ilya Yaroshenko wrote: >> Please add a module (core.intrinsics ?) which will contain all DMD >> intrinsics >> similar to ldc.intrinsics. After each DMD release it is not clear what is >> intrinsics and what is not. I need BSF intrinsics for Better C library >> Mir >> Random [1], which should work without linking with DRuntime and >> Phobos. I can >> use ldc.intrinsics for LDC, but have no idea about DMD. I want BSR and >> BSF >> instructions to be generated instead of current _software_ >> implementation in >> core.bitop. >> >> ================== >> Philosophical Questions: >> >> 1. Why hight level stuff like BitRange is in core.bitop, but not in >> std.bitmanip? If it should be in core, why it is public? >> >> 2. Why bsf and bsr do NOT use hardware instructions anymore? >> ================== >> >> Please ping me for Phobos and DRuntime PRs if they are related to math >> and >> numeric issues. > > > The definitive list of dmd intrinsics is here: > > https://github.com/dlang/dmd/blob/master/src/toir.d#L349 There's builtin.d [1] as well. When is which one used? [1] https://github.com/dlang/dmd/blob/master/src/builtin.d -- /Jacob Carlborg |
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 23 November 2016 at 15:29:48 UTC, Jacob Carlborg wrote:
> There's builtin.d [1] as well. When is which one used?
builtin is used for CTFE.
|
November 23, 2016 Re: !!!Please add intrinsics module for DMD DRuntime!!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Wednesday, 23 November 2016 at 00:52:15 UTC, Walter Bright wrote: > Consider the code: > > import core.bitop; > > int foo(int v) { > return core.bitop.bsf(v); > } > > Compiling: > > dmd foo.d -c > obj2asm foo.obj > > yields: > > _D5bug113fooFiZi comdat > bsf EAX,AL > ret > > Meaning the bsf() intrinsic is properly detected and used. I did some additional testing, and it seems that the bsf and bsr intrinsics are only working *sometimes* on DMD master: https://issues.dlang.org/show_bug.cgi?id=16743 |
Copyright © 1999-2021 by the D Language Foundation