www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How use ldc pragmas?

reply james.p.leblanc <james.p.leblanc gmail.com> writes:
D-ers,

After experimenting with ldc's autovectorization of avx code, it 
appears there may
be counter-intuitiveness to the autovectorization (especially for 
complex numbers).
(My comment may be wrong, so any corrections are quite welcome).

Based on this, I wanted to investigate the use of ldc's pragma 
capabilities
(as found in ldc2/import/gccbuiltins_x86.di).

However, getting the ball rolling is a bit difficult, as I have 
not found a
simple example showing the whole process.  What I have just now 
is:

```d
// my example.d
import std.stdio;
import ldc.intrinsics;

void main()
{
    writeln("hello");

    float x;

    pragma(LDC_intrinsic, "llvm.sqrt.32")
    float sqrt(float);
}
```
I try to compile the above using:

```bash
ldc2 -mattr=+avx2 myexample.d -H 
~/ldc2/import/ldc/gccbuiltins_x86.di
```

But receive the following:
```console
ldcint.d(11): Error: unrecognized `pragma(LDC_intrinsic)`
```

I get the feeling that I am really "lost in the woods" here, 
probably
missing important aspects.

Are there any pointers this forum might have to help here?
(A very simple and complete example would be greatly appreciated!)

Thanks!
James
Oct 01 2021
next sibling parent reply max haughton <maxhaton gmail.com> writes:
On Friday, 1 October 2021 at 19:23:06 UTC, james.p.leblanc wrote:
 D-ers,

 After experimenting with ldc's autovectorization of avx code, 
 it appears there may
 be counter-intuitiveness to the autovectorization (especially 
 for complex numbers).
 (My comment may be wrong, so any corrections are quite welcome).

 [...]
Is it sqrt.32 or sqrt.f32? Try the latter, LLVM docs seem to agree.
Oct 01 2021
parent reply james.p.leblanc <james.p.leblanc gmail.com> writes:
On Friday, 1 October 2021 at 19:58:30 UTC, max haughton wrote:
 On Friday, 1 October 2021 at 19:23:06 UTC, james.p.leblanc
 Is it sqrt.32 or sqrt.f32? Try the latter, LLVM docs seem to 
 agree.
Hello Max, Thanks for the correction... unfortunately, even after changing the "32", to "f32", **I receive the same error**. So there is something of a bigger nature that **I am still doing wrong.** I should have noted earlier that I am just trying to mirror the docs found at: https://wiki.dlang.org/LDC-specific_language_changes ```d // provide square root intrinsics pragma(LDC_intrinsic, "llvm.sqrt.f32") float sqrt(float); pragma(LDC_intrinsic, "llvm.sqrt.f64") double sqrt(double); pragma(LDC_intrinsic, "llvm.sqrt.f80") real sqrt(real); // x86 only ``` Any additional hints for ldc instrinsics are greatly appreciated. James
Oct 01 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 1 October 2021 at 20:14:01 UTC, james.p.leblanc wrote:
 On Friday, 1 October 2021 at 19:58:30 UTC, max haughton wrote:
 [...]
 [...]
Hello Max, Thanks for the correction... unfortunately, even after changing the "32", to "f32", **I receive the same error**. So there is something of a bigger nature that **I am still doing wrong.** I should have noted earlier that I am just trying to mirror the docs found at: https://wiki.dlang.org/LDC-specific_language_changes ```d // provide square root intrinsics pragma(LDC_intrinsic, "llvm.sqrt.f32") float sqrt(float); pragma(LDC_intrinsic, "llvm.sqrt.f64") double sqrt(double); pragma(LDC_intrinsic, "llvm.sqrt.f80") real sqrt(real); // x86 only ``` Any additional hints for ldc instrinsics are greatly appreciated. James
Take a look at my post ;)
Oct 01 2021
parent james.p.leblanc <james.p.leblanc gmail.com> writes:
On Friday, 1 October 2021 at 20:19:39 UTC, Imperatorn wrote:


 Take a look at my post ;)
YES! === Thanks to both Imperatorn, and Max for their helpful and quick responses. The problem is SOLVED! I didn't realize that the pragma had to be placed outside of the main() ... but it seems that I should have known this! (Tomorrow, I shall dive into the deeper waters with avx2.) Sometimes, getting a simple example to build from can be the hardest part, eh? Thanks Again, James
Oct 01 2021
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 1 October 2021 at 19:23:06 UTC, james.p.leblanc wrote:
 D-ers,

 After experimenting with ldc's autovectorization of avx code, 
 it appears there may
 be counter-intuitiveness to the autovectorization (especially 
 for complex numbers).
 (My comment may be wrong, so any corrections are quite welcome).

 [...]
```d import std.stdio; import ldc.intrinsics; pragma(LDC_intrinsic, "llvm.sqrt.f32") float sqrt(float); void main() { writeln("hello"); float x = 42.42; writeln(sqrt(x)); } ```
Oct 01 2021
prev sibling parent james.p.leblanc <james.p.leblanc gmail.com> writes:
On Friday, 1 October 2021 at 19:23:06 UTC, james.p.leblanc wrote:
 D-ers,
Update from myself to myself (and any others who might use the bash command from my origin posting),
 I try to compile the above using:
```bash ldc2 -mattr=+avx2 myexample.d -H ~/ldc2/import/ldc/gccbuiltins_x86.di ``` Remove the "-H ..." this should not be used (based on an earlier misunderstanding of mine). Instead, simply use: ```bash ldc2 -mattr=+avx2 myexample.d ``` Cheers, jpl
Oct 02 2021