Skip to content

Commit

Permalink
[vm] Add support for real unboxed floating point fields in AOT
Browse files Browse the repository at this point in the history
Non-nullable floating point fields (double, Float32x4, Float64x2)
are fully unboxed in their classes.

A bitmap for each class was added to the shared class table in order to keep
track of the pointers of the classes. Since all classes in Flutter Gallery
have less than 64 fields, the bitmap is represented by a 64 bit integer and
fields whose offset is more than 64 words are not unboxed.

The instance sizes and field offsets might change between target and host
in cross-compilation, since the number of words used to store unboxed fields
may differ.

dart-aot Xeon

  SplayLatency               -4.62%
  SplayHarderLatency         -4.17%
  NavierStokes               -2.20%
  Tracer                      8.72%
  ParticleSystemPaint         2.90%
  NBodySIMD                   8.35%
  NBody                      25.59%

With hack TFA to make doubles in Rect/Offset/Size classes in flutter non-nullable:

flutter arm-v8:

  gallery total size: -1%

  matrix_utils_transform_rect_perspective   -16.70% (less is better)
  matrix_utils_transform_rect_affine        -31.82% (less is better)
  matrix_utils_transform_point_perspective  -24.90% (less is better)
  matrix_utils_transform_point_affine)      -27.26% (less is better)
  rrect_contains_bench                      -4.719% (less is better)

Change-Id: I9ae09c9c3167d99f9efd071a92937aa51093fd1d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131824
Commit-Queue: Victor Agnez Lima <victoragnez@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Samir Jindel <sjindel@google.com>
  • Loading branch information
victoragnez authored and commit-bot@chromium.org committed Jan 30, 2020
1 parent 7248e98 commit 9eb531b
Show file tree
Hide file tree
Showing 46 changed files with 3,632 additions and 535 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=
// VMOptions=--use_compactor
// VMOptions=--use_compactor --force_evacuation

import 'dart:typed_data';

import 'package:observatory/service_io.dart';
import 'package:unittest/unittest.dart';
import 'test_helper.dart';

double getDoubleWithHeapObjectTag() {
final bd = ByteData(8);
bd.setUint64(0, 0x8000000000000001, Endian.host);
final double v = bd.getFloat64(0, Endian.host);
return v;
}

// small example from [Lenguaer & Tarjan 1979]
class R {
final double fld = getDoubleWithHeapObjectTag();
var x;
var y;
var z;
}

class A {
var x;
}

class B {
var x;
var y;
var z;
}

class C {
var x;
var y;
}

class D {
var x;
}

class E {
var x;
}

class F {
var x;
}

class G {
var x;
var y;
}

class H {
var x;
var y;
}

class I {
var x;
}

class J {
var x;
}

class K {
var x;
var y;
}

class L {
var x;
}

var r;

buildGraph() {
r = new R();
var a = new A();
var b = new B();
var c = new C();
var d = new D();
var e = new E();
var f = new F();
var g = new G();
var h = new H();
var i = new I();
var j = new J();
var k = new K();
var l = new L();

r.x = a;
r.y = b;
r.z = c;
a.x = d;
b.x = a;
b.y = d;
b.z = e;
c.x = f;
c.y = g;
d.x = l;
e.x = h;
f.x = i;
g.x = i;
g.y = j;
h.x = e;
h.y = k;
i.x = k;
j.x = i;
k.x = i;
k.y = r;
l.x = h;

expect(r.fld, getDoubleWithHeapObjectTag());
}

var tests = <IsolateTest>[
(Isolate isolate) async {
final graph = await isolate.fetchHeapSnapshot().done;

node(String className) {
return graph.objects.singleWhere((v) => v.klass.name == className);
}

expect(node('I').parent, equals(node('R')));
expect(node('K').parent, equals(node('R')));
expect(node('C').parent, equals(node('R')));
expect(node('H').parent, equals(node('R')));
expect(node('E').parent, equals(node('R')));
expect(node('A').parent, equals(node('R')));
expect(node('D').parent, equals(node('R')));
expect(node('B').parent, equals(node('R')));

expect(node('F').parent, equals(node('C')));
expect(node('G').parent, equals(node('C')));
expect(node('J').parent, equals(node('G')));
expect(node('L').parent, equals(node('D')));

expect(node('R'), isNotNull); // The field.
},
];

main(args) => runIsolateTests(args, tests, testeeBefore: buildGraph);
4 changes: 4 additions & 0 deletions runtime/platform/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ typedef simd128_value_t fpu_register_t;
#define DUAL_MAPPING_SUPPORTED 1
#endif

#if defined(DART_PRECOMPILED_RUNTIME) || defined(DART_PRECOMPILER)
#define SUPPORT_UNBOXED_INSTANCE_FIELDS
#endif

// Short form printf format specifiers
#define Pd PRIdPTR
#define Pu PRIuPTR
Expand Down
14 changes: 10 additions & 4 deletions runtime/platform/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Utils {
}

template <typename T>
static inline T Maximum(T x, T y) {
static constexpr inline T Maximum(T x, T y) {
return x > y ? x : y;
}

Expand Down Expand Up @@ -352,12 +352,18 @@ class Utils {
return bit_cast<word>(mask);
}

static uword Bit(uint32_t n) {
ASSERT(n < kBitsPerWord);
uword bit = 1;
template <typename T = uword>
static T Bit(uint32_t n) {
ASSERT(n < sizeof(T) * kBitsPerByte);
T bit = 1;
return bit << n;
}

template <typename T>
DART_FORCE_INLINE static bool TestBit(T mask, intptr_t position) {
return ((mask >> position) & 1) != 0;
}

// Decode integer in SLEB128 format from |data| and update |byte_index|.
template <typename ValueType>
static ValueType DecodeSLEB128(const uint8_t* data,
Expand Down
59 changes: 59 additions & 0 deletions runtime/tests/vm/dart/unboxed_fields_type_args_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:typed_data';
import 'dart:async';
import 'dart:isolate';

import "package:expect/expect.dart";

double getDoubleWithHeapObjectTag() {
final bd = ByteData(8);
bd.setUint64(0, 0x8000000000000001, Endian.host);
final double v = bd.getFloat64(0, Endian.host);
return v;
}

class Foo {
final String clazz = "foo";
final double x = getDoubleWithHeapObjectTag();
}

// Here we ensure to have a GC pointer and a non-GC pointer field, and then a
// type argument vector, so the offset in number of words for the type arguments
// will be different between host and target when compiling from 64-bit to
// 32-bit architectures.
class Bar<T> extends Foo {
final String clazz = "bar";
final double y = getDoubleWithHeapObjectTag();
final T value;
Bar(T val) : value = val;
}

main() async {
final receivePort = new ReceivePort();
receivePort.sendPort.send(Foo());
receivePort.sendPort.send(Bar<String>("StringBar"));
receivePort.sendPort.send(Bar<double>(4.2));
final it = StreamIterator(receivePort);

Expect.isTrue(await it.moveNext());
final foo = it.current as Foo;

Expect.isTrue(await it.moveNext());
final string_bar = it.current as Bar<String>;

Expect.isTrue(await it.moveNext());
final double_bar = it.current as Bar<double>;

Expect.equals(string_bar.value, "StringBar");
Expect.equals(string_bar.clazz, "bar");
Expect.equals(string_bar.y, getDoubleWithHeapObjectTag());
Expect.equals(string_bar.x, getDoubleWithHeapObjectTag());
Expect.equals(double_bar.value, 4.2);
Expect.equals(foo.clazz, "foo");
Expect.equals(foo.x, getDoubleWithHeapObjectTag());

await it.cancel();
}
12 changes: 6 additions & 6 deletions runtime/vm/bootstrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ static void Finish(Thread* thread) {
// Verify that closure field offsets are identical in Dart and C++.
ASSERT(fields.Length() == 6);
field ^= fields.At(0);
ASSERT(field.Offset() == Closure::instantiator_type_arguments_offset());
ASSERT(field.HostOffset() == Closure::instantiator_type_arguments_offset());
field ^= fields.At(1);
ASSERT(field.Offset() == Closure::function_type_arguments_offset());
ASSERT(field.HostOffset() == Closure::function_type_arguments_offset());
field ^= fields.At(2);
ASSERT(field.Offset() == Closure::delayed_type_arguments_offset());
ASSERT(field.HostOffset() == Closure::delayed_type_arguments_offset());
field ^= fields.At(3);
ASSERT(field.Offset() == Closure::function_offset());
ASSERT(field.HostOffset() == Closure::function_offset());
field ^= fields.At(4);
ASSERT(field.Offset() == Closure::context_offset());
ASSERT(field.HostOffset() == Closure::context_offset());
field ^= fields.At(5);
ASSERT(field.Offset() == Closure::hash_offset());
ASSERT(field.HostOffset() == Closure::hash_offset());
#endif // defined(DEBUG)

// Eagerly compile Bool class, bool constants are used from within compiler.
Expand Down
30 changes: 15 additions & 15 deletions runtime/vm/class_finalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,33 +240,33 @@ void ClassFinalizer::VerifyBootstrapClasses() {
#if defined(DEBUG)
// Basic checking.
cls = object_store->object_class();
ASSERT(Instance::InstanceSize() == cls.instance_size());
ASSERT(Instance::InstanceSize() == cls.host_instance_size());
cls = object_store->integer_implementation_class();
ASSERT(Integer::InstanceSize() == cls.instance_size());
ASSERT(Integer::InstanceSize() == cls.host_instance_size());
cls = object_store->smi_class();
ASSERT(Smi::InstanceSize() == cls.instance_size());
ASSERT(Smi::InstanceSize() == cls.host_instance_size());
cls = object_store->mint_class();
ASSERT(Mint::InstanceSize() == cls.instance_size());
ASSERT(Mint::InstanceSize() == cls.host_instance_size());
cls = object_store->one_byte_string_class();
ASSERT(OneByteString::InstanceSize() == cls.instance_size());
ASSERT(OneByteString::InstanceSize() == cls.host_instance_size());
cls = object_store->two_byte_string_class();
ASSERT(TwoByteString::InstanceSize() == cls.instance_size());
ASSERT(TwoByteString::InstanceSize() == cls.host_instance_size());
cls = object_store->external_one_byte_string_class();
ASSERT(ExternalOneByteString::InstanceSize() == cls.instance_size());
ASSERT(ExternalOneByteString::InstanceSize() == cls.host_instance_size());
cls = object_store->external_two_byte_string_class();
ASSERT(ExternalTwoByteString::InstanceSize() == cls.instance_size());
ASSERT(ExternalTwoByteString::InstanceSize() == cls.host_instance_size());
cls = object_store->double_class();
ASSERT(Double::InstanceSize() == cls.instance_size());
ASSERT(Double::InstanceSize() == cls.host_instance_size());
cls = object_store->bool_class();
ASSERT(Bool::InstanceSize() == cls.instance_size());
ASSERT(Bool::InstanceSize() == cls.host_instance_size());
cls = object_store->array_class();
ASSERT(Array::InstanceSize() == cls.instance_size());
ASSERT(Array::InstanceSize() == cls.host_instance_size());
cls = object_store->immutable_array_class();
ASSERT(ImmutableArray::InstanceSize() == cls.instance_size());
ASSERT(ImmutableArray::InstanceSize() == cls.host_instance_size());
cls = object_store->weak_property_class();
ASSERT(WeakProperty::InstanceSize() == cls.instance_size());
ASSERT(WeakProperty::InstanceSize() == cls.host_instance_size());
cls = object_store->linked_hash_map_class();
ASSERT(LinkedHashMap::InstanceSize() == cls.instance_size());
ASSERT(LinkedHashMap::InstanceSize() == cls.host_instance_size());
#endif // defined(DEBUG)

// Remember the currently pending classes.
Expand Down Expand Up @@ -1358,7 +1358,7 @@ void ClassFinalizer::VerifyImplicitFieldOffsets() {
fields_array ^= cls.fields();
ASSERT(fields_array.Length() == ByteBuffer::NumberOfFields());
field ^= fields_array.At(0);
ASSERT(field.Offset() == ByteBuffer::data_offset());
ASSERT(field.HostOffset() == ByteBuffer::data_offset());
name ^= field.name();
expected_name ^= String::New("_data");
ASSERT(String::EqualsIgnoringPrivateKey(name, expected_name));
Expand Down
Loading

0 comments on commit 9eb531b

Please sign in to comment.