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] Inline multi-dimensional arrays #45023

Closed
dcharkes opened this issue Feb 16, 2021 · 6 comments
Closed

[vm/ffi] Inline multi-dimensional arrays #45023

dcharkes opened this issue Feb 16, 2021 · 6 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-ffi

Comments

@dcharkes
Copy link
Contributor

dcharkes commented Feb 16, 2021

Follow up of #35763

class Matrix4x4 extends Struct {
  @CArraySize([4,4])
  CArray<CArray<Float>> data;
}
struct Matrix4x4 {
  float data[4][4];
};

Context: https://dart-review.googlesource.com/c/sdk/+/183640/21/pkg/front_end/testcases/general_nnbd_opt_out/ffi_struct_inline_array.dart#12

Or

class MyStruct { 
  @ArraySize(8)
  Array<Int8> a;
 
  @ArraySize2(2, 4)
  Array<Array<Int8>> b;

  @ArraySizeN([2,3,4,5])
  Array<Array<Array<Array<Int8>>>> c;
}
@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-ffi labels Feb 16, 2021
@dcharkes
Copy link
Contributor Author

Or using the initializer:

class MyStruct { 
  Array<Int8> a = Array(8);
 
  Array<Array<Int8>> b = Array(2, 4);

  Array<Array<Array<Array<Int8>>>> c = Array.multi([2,3,4,5]);
}

external const Array(int firstDimension, [int dimension2, int dimension3, int dimension4, int dimension5]);
external const Array.multi(List<int> dimensions);

Credits to @lrhn

@dcharkes
Copy link
Contributor Author

dcharkes commented Feb 23, 2021

After some more discussion: keeping the annotations for consistency and enforcing const size arguments.

We use Array for the dimensions as well.

class MyStruct extends Struct {
  @Array(8)
  external Array<Uint8> inlineArray;

  @Array(2, 2, 2)
  external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;

  @Array.multi([2, 2, 2, 2, 2, 2, 2, 2])
  external Array<Array<Array<Array<Array<Array<Array<Array<Uint8>>>>>>>> eightDimensionalInlineArray;
}
/// A fixed-sized array of [T]s.
class Array<T extends NativeType> extends NativeType {
  /// Const constructor to specify [Array] dimensions in [Struct]s.
  ///
  /// ```
  /// class MyStruct extends Struct {
  ///   @Array(8)
  ///   external Array<Uint8> inlineArray;
  ///
  ///   @Array(2, 2, 2)
  ///   external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
  /// }
  /// ```
  ///
  /// Do not invoke in normal code.
  external const factory Array(int dimension1,
      [int dimension2, int dimension3, int dimension4, int dimension5]);

  /// Const constructor to specify [Array] dimensions in [Struct]s.
  ///
  /// ```
  /// class MyStruct extends Struct {
  ///   @Array.multi([2, 2, 2])
  ///   external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;
  ///
  ///   @Array.multi([2, 2, 2, 2, 2, 2, 2, 2])
  ///   external Array<Array<Array<Array<Array<Array<Array<Array<Uint8>>>>>>>> eightDimensionalInlineArray;
  /// }
  /// ```
  ///
  /// Do not invoke in normal code.
  external const factory Array.multi(List<int> dimensions);
}

@lrhn
Copy link
Member

lrhn commented Feb 26, 2021

Shouldn't

 ///   @Array.multi([2, 2, 2])
 ///   external Array<Uint8> threeDimensionalInlineArray;

be

  ///   @Array.multi([2, 2, 2])
  ///   external Array<Array<Array<Uint8>>> threeDimensionalInlineArray;

and similarly for the other examples (good luck with fitting the eight-dimensional case on one line).

@dcharkes
Copy link
Contributor Author

Good catch, updated!

@ishomam
Copy link

ishomam commented Aug 1, 2021

What if the size of the multi-dimensional arrays will be only available during runtime (during c++ function execution)?
Is there a way to handle this?

@dcharkes
Copy link
Contributor Author

dcharkes commented Aug 2, 2021

@ishomam that would be a Pointer.

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, FFI, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

3 participants