Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use core.simd? #595

Closed
jkm opened this issue Apr 7, 2014 · 10 comments
Closed

How to use core.simd? #595

jkm opened this issue Apr 7, 2014 · 10 comments

Comments

@jkm
Copy link

jkm commented Apr 7, 2014

I'm trying out latest alpha release (LDC 0.13.0 alpha 2) from https://github.com/ldc-developers/ldc/releases on Linux x86_64. I'd like to compare ldc to dmd but the simple program

void main(string[] args)                                                        
{                                                                               
    import core.simd;                                                           
    float4 a = 1, b = 2;                                                        
    float4 c = __simd(XMM.ADDSS, a, b);                                         
}

does not compile

test.d(5): Error: undefined identifier __simd
test.d(5): Error: undefined identifier XMM

I suppose I must be doing something fundamentally wrong since I found posts where core.simd works with ldc. Please enlighten me.

@redstar
Copy link
Member

redstar commented Apr 8, 2014

Quite easy: the instructions from core.simd are not supported by LDC. Instead, you additionally import ldc.simd. Then you have 3 new functions which operates on vectors. shufflevector, extractelement and insertelement (and some other stuff). Other functions are not available at the D level - they are produced by the different optimization passes of LLVM.
The advantage of this approach is that the D code is CPU-independent. The disadvantage is that it is easy to write sub-optimal code (e.g. if the target CPU does not support vector instructions).

@jkm
Copy link
Author

jkm commented Apr 8, 2014

I like that vectorization is left to the compiler but at least for dmd is was decided that it is suboptimal (see http://dlang.org/simd.html). I suppose you are going to add more functions to ldc.simd. Is there a way (in ldc) to detect for which CPU the code is being compiled?

@redstar
Copy link
Member

redstar commented Apr 8, 2014

No, the DMD way of doing vector instruction will not be added to LDC. It is more or less impossible to implement this in LLVM. (See it the positive way: LDC can generate avx or altivec instructions if available...)

There is no way to detect the CPU during compilation. You have to specify the cpu on the command line (e.g. -mcpu=krait). You can then also define an identifier for the cpu if you need it.

@jkm
Copy link
Author

jkm commented Apr 8, 2014

What do you mean by

define an identifier for the cpu if you need it

?
How do I do that?

@redstar
Copy link
Member

redstar commented Apr 8, 2014

E.g. ldc2 -mcpu=corei7 -d-version=COREI7.
In D you can then use version(COREI7) { ... }

@dnadlinger
Copy link
Member

You can, however, also use a large portion of the GCC SIMD builtins. Some elementary insertions/shuffles are not offered as builtins though (because LLVM does not expose them). For those, use ldc.simd.

@jkm
Copy link
Author

jkm commented Apr 8, 2014

How do use GCC SIMD builtins? Can you give an example?

@dnadlinger
Copy link
Member

Just import ldc.gccbuiltins_x86 (or whatever platform you are targeting), then use the various builtins like __builtin_ia32_ptestz128 as usual.

@redstar
Copy link
Member

redstar commented Apr 14, 2014

I close this issue because there is nothing to fix. If needed the discussion can continue in the forum (http://forum.dlang.org/group/digitalmars.D.ldc).

@redstar redstar closed this as completed Apr 14, 2014
@dnadlinger
Copy link
Member

We should probably offer a tutorial or a few examples of how to write SIMD code with LDC. Maybe @jerro can point us to a few public examples of his that could be linked on a wiki page or such.

redstar pushed a commit that referenced this issue Sep 27, 2014
prevent HashTab manipulation during opApply iteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants