Support for gcc vector attributes, SIMD builtins

Brad Roberts braddr at puremagic.com
Sun Feb 6 14:18:29 PST 2011


On 2/6/2011 4:15 AM, Iain Buclaw wrote:
> == Quote from Mike Farnsworth (mike.farnsworth at gmail.com)'s article
>> On 02/01/2011 10:38 AM, Iain Buclaw wrote:
>>> I haven't given it much thought on how internal representation could be, but I'd
>>> lean on using unions in D code for usage in the language. As its probably most
>>> portable.
>>>
>>> For example, one of the older 'hello vectors' I know of:
>>>
>>> import std.c.stdio;
>>>
>>> pragma(set_attribute, __v4sf, vector_size(16));
>>> typedef float __v4sf;
>>>
>>> union f4vector
>>> {
>>>     __v4sf v;
>>>     float[4] f;
>>> }
>>>
>>> int main()
>>> {
>>>     f4vector a, b, c;
>>>
>>>     a.f = [1, 2, 3, 4];
>>>     b.f = [5, 6, 7, 8];
>>>
>>>     c.v = a.v + b.v;
>>>     printf("%f, %f, %f, %f\n", c.f[0], c.f[1], c.f[2], c.f[3]);
>>>
>>>     return 0;
>>> }
>> I've been giving this a serious try, and while the above works, I can't
>> get any __builtin_... functions to actually work.  I've added support
>> for the VECTOR_TYPE tree code in gcc_type_to_d_type(tree) function (in
>> d_builtins2.cc):
>>         case VECTOR_TYPE:
>>         {
>>             tree baseType = TREE_TYPE(t);
>>             d = gcc_type_to_d_type(baseType, printstuff);
>>             if (d)
>>                 return d;
>>             break;
>>         }
>> This allows it to succeed in interpreting the SSE-related builtins in
>> gcc_type_to_d_type(tree).  Note that all it does is grab the base vector
>> element type and convert that to a D type so as not to confuse the
>> frontend; this way it matches the typedef for __v4sf, so as long as we
>> use the union we won't lose data before we can pass it to a builtin.
> 
> Try:
>         case VECTOR_TYPE:
>         {
>             tree basetype = TYPE_DEBUG_REPRESENTATION_TYPE(t);
>             assert(TREE_CODE(basetype) == RECORD_TYPE);
>             basetype = TREE_TYPE(TYPE_FIELDS(basetype));
>             d = gcc_type_to_d_type(basetype);
>             if (d)
>             {
>                 d->ctype = t;
>                 return d;
>             }
>             break;
>         }
> 
> That makes them static arrays, so you needn't require a whacky union to use vector
> functions.
> 
>   float[4] a = [1,2,3,4], b = [5,6,7,8], c;
>   c = __builtin_ia32_addps(a,b);
> 
> 
> 
> Secondly, __builtin_ia32_addps requires SSE turned on. Compile with -msse
> 
> 
> Regards

I'd be happy to have gcc finding vectorization opportunities, but there's no need to add this sort of thing to the
language.  This already has a hook to call a library function:

float[4] a = [1,2,3,4], b = [5,6,7,8], c;
c[] = a[] + b[];




More information about the D.gnu mailing list