Skip to content

Commit

Permalink
[vm/ffi] Rewrite Pointer.elementAt calls in CFE
Browse files Browse the repository at this point in the history
This rewrites `elementAt` calls in the CFE to skip the runtime entry
for `sizeOf` when the type argument is constant.

The runtime entry is still used when the type argument is generic.
Forcing the type argument to be constant and removing the runtime entry
will be done in follow up CLs.

Bug: #44621
Bug: #38721

TEST=tests/ffi/data_test.dart

Change-Id: I480db43e7c115c24bd45f0ddab0cfea7eb8cfa58
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/182263
Reviewed-by: Clement Skau <[email protected]>
  • Loading branch information
dcharkes authored and [email protected] committed Feb 2, 2021
1 parent 64bf734 commit 1fff52a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions pkg/vm/lib/transformations/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class FfiTransformer extends Transformer {
final Field pragmaOptions;
final Procedure listElementAt;
final Procedure numAddition;
final Procedure numMultiplication;

final Library ffiLibrary;
final Class allocatorClass;
Expand Down Expand Up @@ -267,6 +268,7 @@ class FfiTransformer extends Transformer {
pragmaOptions = coreTypes.pragmaOptions,
listElementAt = coreTypes.index.getMember('dart:core', 'List', '[]'),
numAddition = coreTypes.index.getMember('dart:core', 'num', '+'),
numMultiplication = coreTypes.index.getMember('dart:core', 'num', '*'),
ffiLibrary = index.getLibrary('dart:ffi'),
allocatorClass = index.getClass('dart:ffi', 'Allocator'),
nativeFunctionClass = index.getClass('dart:ffi', 'NativeFunction'),
Expand Down
27 changes: 16 additions & 11 deletions pkg/vm/lib/transformations/ffi_use_sites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import 'ffi.dart'
FfiTransformerData,
NativeType,
FfiTransformer,
optimizedTypes,
nativeTypeSizes,
WORD_SIZE,
UNKNOWN,
Expand Down Expand Up @@ -432,20 +431,26 @@ class _FfiUseSiteTransformer extends FfiTransformer {

_warningNativeTypeValid(nativeType, node);

// TODO(http://dartbug.com/38721): Change this to an error.
if (nativeType is TypeParameterType) {
// Do not rewire generic invocations.
return node;
}
final Class nativeClass = (nativeType as InterfaceType).classNode;
final NativeType nt = getType(nativeClass);
if (optimizedTypes.contains(nt)) {
final typeArguments = [
if (nt == NativeType.kPointer) _pointerTypeGetTypeArg(nativeType)
];
return StaticInvocation(
elementAtMethods[nt],
Arguments([node.receiver, node.arguments.positional[0]],
types: typeArguments));

Expression inlineSizeOf = _inlineSizeOf(nativeType);
if (inlineSizeOf != null) {
// Generates `receiver.offsetBy(inlineSizeOfExpression)`.
return MethodInvocation(
node.receiver,
offsetByMethod.name,
Arguments([
MethodInvocation(
node.arguments.positional.single,
numMultiplication.name,
Arguments([inlineSizeOf]),
numMultiplication)
], types: node.arguments.types),
offsetByMethod);
}
}
} on _FfiStaticTypeError {
Expand Down

0 comments on commit 1fff52a

Please sign in to comment.