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

[vm/ffi] Support SSE & NEON packed data #37470

Open
dcharkes opened this issue Jul 9, 2019 · 2 comments
Open

[vm/ffi] Support SSE & NEON packed data #37470

dcharkes opened this issue Jul 9, 2019 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@dcharkes
Copy link
Contributor

dcharkes commented Jul 9, 2019

Currently, the FFI supports int8-int64, uint8-uint32, intptr, double, and float as native types. These are supported by having them available in the API and handling their size and alignment correctly in structs and trampolines.

However, the full ABI's also include packed data for SSE, with __m128 etc. We could add those to the FFI.

@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi labels Jul 9, 2019
@dcharkes
Copy link
Contributor Author

Issue specific for bit fields: #38954.

@dcharkes dcharkes changed the title [vm/ffi] Support packed data and other uncommon primitive data types. [vm/ffi] Support SSE packed data and other uncommon primitive data types. Oct 25, 2019
@dcharkes dcharkes changed the title [vm/ffi] Support SSE packed data and other uncommon primitive data types. [vm/ffi] Support SSE packed data Oct 25, 2019
@dcharkes
Copy link
Contributor Author

dcharkes commented Oct 25, 2019

Data Types

Types taken from Intel:

Packed integers:

  • __m128i
  • __m256i
  • __m512i

Packed floats:

  • __m64
  • __m128
  • __m256
  • __m512

Packed doubles:

  • __m128d
  • __m256d
  • __m512d

Calling Conventions

A packed float return value is passed in multiple XMM registers (in Clang 9):

typedef float __m512 __attribute__((__vector_size__(64), __aligned__(64)));

__m512 bla() {
    __m512 x;
    x[1] = 1.0f;
    return x;              // this return
}

int foo() {
    float z = bla()[1];
    return (z);
}
        movaps  xmm0, xmmword ptr [rsp]
        movaps  xmm1, xmmword ptr [rsp + 16]
        movaps  xmm2, xmmword ptr [rsp + 32]
        movaps  xmm3, xmmword ptr [rsp + 48]
        mov     rsp, rbp
        pop     rbp
        ret

This might have some overlap with how structs-by-value are passed (#36730).

Native Memory Access

Memory access could be provided by regarding these as arrays.

API Design

We need Dart type to represent these types in function signatures for calling conventions. We can add extension methods for [] and []= on Pointers to those types for loading and storing data.

/// __m64
FloatX2 extends NativeType {}

extension FloatX2Pointer on Pointer<Float> {
  external double operator [](int index);

  external void operator []=(int index, double value);
}

(Naming up for debate, cc @lrhn.)

This design should work together with inline arrays (#35763):

typedef float __m128 __attribute__((__vector_size__(16), __aligned__(16)));

struct MyStruct {
  __m128 arr[4];
};
class MyStruct extends Struct {
  @InlineArray(4)
  Pointer<FloatX4> arr;
}

main() {
  MyStruct m = //...
  m.arr.elementAt(2)[2] = 1.0;
}

cc @mkustermann @lrhn

Edit:

Passing SSE data structures by value to functions requires stronger stack alignment in System V. (I haven't looked at other ABIs yet.)

If parameters of type __m256 are required to be passed on the stack, the stack
pointer must be aligned on a 0 mod 32 byte boundary at the time of the call.
If parameters of type __m512 are required to be passed on the stack, the stack
pointer must be aligned on a 0 mod 64 byte boundary at the time of the call.

source: https://github.com/hjl-tools/x86-psABI/wiki/intel386-psABI-1.1.pdf

@dcharkes dcharkes changed the title [vm/ffi] Support SSE packed data [vm/ffi] Support SSE & NEON packed data Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

1 participant