Skip to content

Commit 0889552

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Refactor IL construction to compound
In preparation of having `Union` besides `Struct`, renames all mentions of `Struct` to `Compound`. Bug: #38491 tools/test.py ffi ffi_2 TEST=tests/ffi(_2)/(.*)by_value_(*.)_test.dart Change-Id: I47124a95d67c5afc3da9b5f5c08d0be47908f2e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194423 Reviewed-by: Aske Simon Christensen <[email protected]>
1 parent bde9578 commit 0889552

File tree

4 files changed

+64
-61
lines changed

4 files changed

+64
-61
lines changed

runtime/vm/compiler/ffi/marshaller.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ Location CallMarshaller::LocInFfiCall(intptr_t def_index_global) const {
390390
}
391391

392392
bool CallMarshaller::PassTypedData() const {
393-
return IsStruct(compiler::ffi::kResultIndex);
393+
return IsCompound(compiler::ffi::kResultIndex);
394394
}
395395

396396
intptr_t CallMarshaller::TypedDataSizeInBytes() const {

runtime/vm/compiler/ffi/marshaller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class BaseMarshaller : public ZoneAllocated {
110110
kFfiHandleCid;
111111
}
112112

113-
bool IsStruct(intptr_t arg_index) const {
113+
bool IsCompound(intptr_t arg_index) const {
114114
const auto& type = AbstractType::Handle(zone_, CType(arg_index));
115115
const bool predefined = IsFfiTypeClassId(type.type_class_id());
116116
return !predefined;

runtime/vm/compiler/frontend/kernel_to_il.cc

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,10 +3776,11 @@ Fragment FlowGraphBuilder::BitCast(Representation from, Representation to) {
37763776
return Fragment(instr);
37773777
}
37783778

3779-
Fragment FlowGraphBuilder::WrapTypedDataBaseInStruct(
3780-
const AbstractType& struct_type) {
3781-
const auto& struct_sub_class = Class::ZoneHandle(Z, struct_type.type_class());
3782-
struct_sub_class.EnsureIsFinalized(thread_);
3779+
Fragment FlowGraphBuilder::WrapTypedDataBaseInCompound(
3780+
const AbstractType& compound_type) {
3781+
const auto& compound_sub_class =
3782+
Class::ZoneHandle(Z, compound_type.type_class());
3783+
compound_sub_class.EnsureIsFinalized(thread_);
37833784
const auto& lib_ffi = Library::Handle(Z, Library::FfiLibrary());
37843785
const auto& compound_class =
37853786
Class::Handle(Z, lib_ffi.LookupClassAllowPrivate(Symbols::Compound()));
@@ -3790,16 +3791,16 @@ Fragment FlowGraphBuilder::WrapTypedDataBaseInStruct(
37903791

37913792
Fragment body;
37923793
LocalVariable* typed_data = MakeTemporary("typed_data_base");
3793-
body += AllocateObject(TokenPosition::kNoSource, struct_sub_class, 0);
3794-
body += LoadLocal(MakeTemporary("struct")); // Duplicate Struct.
3794+
body += AllocateObject(TokenPosition::kNoSource, compound_sub_class, 0);
3795+
body += LoadLocal(MakeTemporary("compound")); // Duplicate Struct or Union.
37953796
body += LoadLocal(typed_data);
37963797
body += StoreInstanceField(compound_typed_data_base,
37973798
StoreInstanceFieldInstr::Kind::kInitializing);
37983799
body += DropTempsPreserveTop(1); // Drop TypedData.
37993800
return body;
38003801
}
38013802

3802-
Fragment FlowGraphBuilder::LoadTypedDataBaseFromStruct() {
3803+
Fragment FlowGraphBuilder::LoadTypedDataBaseFromCompound() {
38033804
const auto& lib_ffi = Library::Handle(Z, Library::FfiLibrary());
38043805
const auto& compound_class =
38053806
Class::Handle(Z, lib_ffi.LookupClassAllowPrivate(Symbols::Compound()));
@@ -3813,15 +3814,15 @@ Fragment FlowGraphBuilder::LoadTypedDataBaseFromStruct() {
38133814
return body;
38143815
}
38153816

3816-
Fragment FlowGraphBuilder::CopyFromStructToStack(
3817+
Fragment FlowGraphBuilder::CopyFromCompoundToStack(
38173818
LocalVariable* variable,
38183819
const GrowableArray<Representation>& representations) {
38193820
Fragment body;
38203821
const intptr_t num_defs = representations.length();
38213822
int offset_in_bytes = 0;
38223823
for (intptr_t i = 0; i < num_defs; i++) {
38233824
body += LoadLocal(variable);
3824-
body += LoadTypedDataBaseFromStruct();
3825+
body += LoadTypedDataBaseFromCompound();
38253826
body += LoadUntagged(compiler::target::Pointer::data_field_offset());
38263827
body += IntConstant(offset_in_bytes);
38273828
const Representation representation = representations[i];
@@ -3959,7 +3960,7 @@ Fragment FlowGraphBuilder::CopyFromUnboxedAddressToTypedDataBase(
39593960
return body;
39603961
}
39613962

3962-
Fragment FlowGraphBuilder::FfiCallConvertStructArgumentToNative(
3963+
Fragment FlowGraphBuilder::FfiCallConvertCompoundArgumentToNative(
39633964
LocalVariable* variable,
39643965
const compiler::ffi::BaseMarshaller& marshaller,
39653966
intptr_t arg_index) {
@@ -3970,29 +3971,29 @@ Fragment FlowGraphBuilder::FfiCallConvertStructArgumentToNative(
39703971
// separate definitions into the FFI call.
39713972
GrowableArray<Representation> representations;
39723973
marshaller.RepsInFfiCall(arg_index, &representations);
3973-
body += CopyFromStructToStack(variable, representations);
3974+
body += CopyFromCompoundToStack(variable, representations);
39743975
} else {
39753976
ASSERT(native_loc.IsPointerToMemory());
39763977
// Only load the typed data, do copying in the FFI call machine code.
39773978
body += LoadLocal(variable); // User-defined struct.
3978-
body += LoadTypedDataBaseFromStruct();
3979+
body += LoadTypedDataBaseFromCompound();
39793980
}
39803981
return body;
39813982
}
39823983

3983-
Fragment FlowGraphBuilder::FfiCallConvertStructReturnToDart(
3984+
Fragment FlowGraphBuilder::FfiCallConvertCompoundReturnToDart(
39843985
const compiler::ffi::BaseMarshaller& marshaller,
39853986
intptr_t arg_index) {
39863987
Fragment body;
39873988
// The typed data is allocated before the FFI call, and is populated in
39883989
// machine code. So, here, it only has to be wrapped in the struct class.
3989-
const auto& struct_type =
3990+
const auto& compound_type =
39903991
AbstractType::Handle(Z, marshaller.CType(arg_index));
3991-
body += WrapTypedDataBaseInStruct(struct_type);
3992+
body += WrapTypedDataBaseInCompound(compound_type);
39923993
return body;
39933994
}
39943995

3995-
Fragment FlowGraphBuilder::FfiCallbackConvertStructArgumentToDart(
3996+
Fragment FlowGraphBuilder::FfiCallbackConvertCompoundArgumentToDart(
39963997
const compiler::ffi::BaseMarshaller& marshaller,
39973998
intptr_t arg_index,
39983999
ZoneGrowableArray<LocalVariable*>* definitions) {
@@ -4012,39 +4013,39 @@ Fragment FlowGraphBuilder::FfiCallbackConvertStructArgumentToDart(
40124013
} else {
40134014
ASSERT(marshaller.Location(arg_index).IsPointerToMemory());
40144015
// Allocate a TypedData and copy contents pointed to by an address into it.
4015-
LocalVariable* address_of_struct = MakeTemporary("address_of_struct");
4016+
LocalVariable* address_of_compound = MakeTemporary("address_of_compound");
40164017
body += IntConstant(length_in_bytes);
40174018
body +=
40184019
AllocateTypedData(TokenPosition::kNoSource, kTypedDataUint8ArrayCid);
40194020
LocalVariable* typed_data_base = MakeTemporary("typed_data_base");
4020-
body += LoadLocal(address_of_struct);
4021+
body += LoadLocal(address_of_compound);
40214022
body += LoadLocal(typed_data_base);
40224023
body += CopyFromUnboxedAddressToTypedDataBase(length_in_bytes);
4023-
body += DropTempsPreserveTop(1); // address_of_struct.
4024+
body += DropTempsPreserveTop(1); // address_of_compound.
40244025
}
4025-
// Wrap typed data in struct class.
4026-
const auto& struct_type =
4026+
// Wrap typed data in compound class.
4027+
const auto& compound_type =
40274028
AbstractType::Handle(Z, marshaller.CType(arg_index));
4028-
body += WrapTypedDataBaseInStruct(struct_type);
4029+
body += WrapTypedDataBaseInCompound(compound_type);
40294030
return body;
40304031
}
40314032

4032-
Fragment FlowGraphBuilder::FfiCallbackConvertStructReturnToNative(
4033+
Fragment FlowGraphBuilder::FfiCallbackConvertCompoundReturnToNative(
40334034
const compiler::ffi::CallbackMarshaller& marshaller,
40344035
intptr_t arg_index) {
40354036
Fragment body;
40364037
const auto& native_loc = marshaller.Location(arg_index);
40374038
if (native_loc.IsMultiple()) {
40384039
// We pass in typed data to native return instruction, and do the copying
40394040
// in machine code.
4040-
body += LoadTypedDataBaseFromStruct();
4041+
body += LoadTypedDataBaseFromCompound();
40414042
} else {
40424043
ASSERT(native_loc.IsPointerToMemory());
40434044
// We copy the data into the right location in IL.
40444045
const intptr_t length_in_bytes =
40454046
marshaller.Location(arg_index).payload_type().SizeInBytes();
40464047

4047-
body += LoadTypedDataBaseFromStruct();
4048+
body += LoadTypedDataBaseFromCompound();
40484049
LocalVariable* typed_data_base = MakeTemporary("typed_data_base");
40494050

40504051
auto* pointer_to_return =
@@ -4065,7 +4066,7 @@ Fragment FlowGraphBuilder::FfiCallbackConvertStructReturnToNative(
40654066
Fragment FlowGraphBuilder::FfiConvertPrimitiveToDart(
40664067
const compiler::ffi::BaseMarshaller& marshaller,
40674068
intptr_t arg_index) {
4068-
ASSERT(!marshaller.IsStruct(arg_index));
4069+
ASSERT(!marshaller.IsCompound(arg_index));
40694070

40704071
Fragment body;
40714072
if (marshaller.IsPointer(arg_index)) {
@@ -4093,7 +4094,7 @@ Fragment FlowGraphBuilder::FfiConvertPrimitiveToNative(
40934094
const compiler::ffi::BaseMarshaller& marshaller,
40944095
intptr_t arg_index,
40954096
LocalVariable* api_local_scope) {
4096-
ASSERT(!marshaller.IsStruct(arg_index));
4097+
ASSERT(!marshaller.IsCompound(arg_index));
40974098

40984099
Fragment body;
40994100
if (marshaller.IsPointer(arg_index)) {
@@ -4195,8 +4196,8 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiNative(const Function& function) {
41954196

41964197
// Unbox and push the arguments.
41974198
for (intptr_t i = 0; i < marshaller.num_args(); i++) {
4198-
if (marshaller.IsStruct(i)) {
4199-
body += FfiCallConvertStructArgumentToNative(
4199+
if (marshaller.IsCompound(i)) {
4200+
body += FfiCallConvertCompoundArgumentToNative(
42004201
parsed_function_->ParameterVariable(kFirstArgumentParameterOffset +
42014202
i),
42024203
marshaller, i);
@@ -4246,9 +4247,9 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiNative(const Function& function) {
42464247
body += Drop();
42474248
}
42484249

4249-
if (marshaller.IsStruct(compiler::ffi::kResultIndex)) {
4250-
body += FfiCallConvertStructReturnToDart(marshaller,
4251-
compiler::ffi::kResultIndex);
4250+
if (marshaller.IsCompound(compiler::ffi::kResultIndex)) {
4251+
body += FfiCallConvertCompoundReturnToDart(marshaller,
4252+
compiler::ffi::kResultIndex);
42524253
} else {
42534254
body += FfiConvertPrimitiveToDart(marshaller, compiler::ffi::kResultIndex);
42544255
}
@@ -4322,8 +4323,8 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiCallback(const Function& function) {
43224323
defs->Add(def);
43234324
}
43244325

4325-
if (marshaller.IsStruct(i)) {
4326-
body += FfiCallbackConvertStructArgumentToDart(marshaller, i, defs);
4326+
if (marshaller.IsCompound(i)) {
4327+
body += FfiCallbackConvertCompoundArgumentToDart(marshaller, i, defs);
43274328
} else {
43284329
body += FfiConvertPrimitiveToDart(marshaller, i);
43294330
}
@@ -4346,9 +4347,9 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiCallback(const Function& function) {
43464347
String::ZoneHandle(Z, marshaller.function_name()));
43474348
}
43484349

4349-
if (marshaller.IsStruct(compiler::ffi::kResultIndex)) {
4350-
body += FfiCallbackConvertStructReturnToNative(marshaller,
4351-
compiler::ffi::kResultIndex);
4350+
if (marshaller.IsCompound(compiler::ffi::kResultIndex)) {
4351+
body += FfiCallbackConvertCompoundReturnToNative(
4352+
marshaller, compiler::ffi::kResultIndex);
43524353
} else {
43534354
body += FfiConvertPrimitiveToNative(marshaller, compiler::ffi::kResultIndex,
43544355
/*api_local_scope=*/nullptr);
@@ -4379,7 +4380,7 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiCallback(const Function& function) {
43794380
FfiConvertPrimitiveToNative(marshaller, compiler::ffi::kResultIndex,
43804381
/*api_local_scope=*/nullptr);
43814382

4382-
} else if (marshaller.IsStruct(compiler::ffi::kResultIndex)) {
4383+
} else if (marshaller.IsCompound(compiler::ffi::kResultIndex)) {
43834384
ASSERT(function.FfiCallbackExceptionalReturn() == Object::null());
43844385
// Manufacture empty result.
43854386
const intptr_t size =
@@ -4390,9 +4391,9 @@ FlowGraph* FlowGraphBuilder::BuildGraphOfFfiCallback(const Function& function) {
43904391
catch_body += IntConstant(size);
43914392
catch_body +=
43924393
AllocateTypedData(TokenPosition::kNoSource, kTypedDataUint8ArrayCid);
4393-
catch_body += WrapTypedDataBaseInStruct(
4394+
catch_body += WrapTypedDataBaseInCompound(
43944395
AbstractType::Handle(Z, marshaller.CType(compiler::ffi::kResultIndex)));
4395-
catch_body += FfiCallbackConvertStructReturnToNative(
4396+
catch_body += FfiCallbackConvertCompoundReturnToNative(
43964397
marshaller, compiler::ffi::kResultIndex);
43974398

43984399
} else {

runtime/vm/compiler/frontend/kernel_to_il.h

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -293,44 +293,46 @@ class FlowGraphBuilder : public BaseFlowGraphBuilder {
293293
intptr_t arg_index);
294294

295295
// We pass in `variable` instead of on top of the stack so that we can have
296-
// multiple consecutive calls that keep only struct parts on the stack with
297-
// no struct parts in between.
298-
Fragment FfiCallConvertStructArgumentToNative(
296+
// multiple consecutive calls that keep only compound parts on the stack with
297+
// no compound parts in between.
298+
Fragment FfiCallConvertCompoundArgumentToNative(
299299
LocalVariable* variable,
300300
const compiler::ffi::BaseMarshaller& marshaller,
301301
intptr_t arg_index);
302302

303-
Fragment FfiCallConvertStructReturnToDart(
303+
Fragment FfiCallConvertCompoundReturnToDart(
304304
const compiler::ffi::BaseMarshaller& marshaller,
305305
intptr_t arg_index);
306306

307307
// We pass in multiple `definitions`, which are also expected to be the top
308-
// of the stack. This eases storing each definition in the resulting struct.
309-
Fragment FfiCallbackConvertStructArgumentToDart(
308+
// of the stack. This eases storing each definition in the resulting struct
309+
// or union.
310+
Fragment FfiCallbackConvertCompoundArgumentToDart(
310311
const compiler::ffi::BaseMarshaller& marshaller,
311312
intptr_t arg_index,
312313
ZoneGrowableArray<LocalVariable*>* definitions);
313314

314-
Fragment FfiCallbackConvertStructReturnToNative(
315+
Fragment FfiCallbackConvertCompoundReturnToNative(
315316
const compiler::ffi::CallbackMarshaller& marshaller,
316317
intptr_t arg_index);
317318

318-
// Wraps a TypedDataBase from the stack and wraps it in a subclass of Struct.
319-
Fragment WrapTypedDataBaseInStruct(const AbstractType& struct_type);
319+
// Wraps a TypedDataBase from the stack and wraps it in a subclass of
320+
// _Compound.
321+
Fragment WrapTypedDataBaseInCompound(const AbstractType& compound_type);
320322

321-
// Loads the _typedDataBase field from a subclass of Struct.
322-
Fragment LoadTypedDataBaseFromStruct();
323+
// Loads the _typedDataBase field from a subclass of _Compound.
324+
Fragment LoadTypedDataBaseFromCompound();
323325

324-
// Breaks up a subclass of Struct in multiple definitions and puts them on
326+
// Breaks up a subclass of _Compound in multiple definitions and puts them on
325327
// the stack.
326328
//
327-
// Takes in the Struct as a local `variable` so that can be anywhere on the
328-
// stack and this function can be called multiple times to leave only the
329-
// results of this function on the stack without any Structs in between.
329+
// Takes in the _Compound as a local `variable` so that can be anywhere on
330+
// the stack and this function can be called multiple times to leave only the
331+
// results of this function on the stack without any _Compounds in between.
330332
//
331-
// The struct contents are heterogeneous, so pass in `representations` to
332-
// know what representation to load.
333-
Fragment CopyFromStructToStack(
333+
// The compound contents are heterogeneous, so pass in
334+
// `representations` to know what representation to load.
335+
Fragment CopyFromCompoundToStack(
334336
LocalVariable* variable,
335337
const GrowableArray<Representation>& representations);
336338

@@ -340,7 +342,7 @@ class FlowGraphBuilder : public BaseFlowGraphBuilder {
340342
//
341343
// Leaves TypedData on stack.
342344
//
343-
// The struct contents are heterogeneous, so pass in `representations` to
345+
// The compound contents are heterogeneous, so pass in `representations` to
344346
// know what representation to load.
345347
Fragment PopFromStackToTypedDataBase(
346348
ZoneGrowableArray<LocalVariable*>* definitions,

0 commit comments

Comments
 (0)