-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Description
We have discovered an unexpected behavior of the runtime regarding layout of a struct in memory with .NET CLR.
In the following example, the struct BoolAndExplicitStruct layout will be promoted to an auto-layout because of the presence of the field public bool Bool and public ExplicitLayoutStruct ExplicitLayout together:
[StructLayout(LayoutKind.Explicit)]
struct ExplicitLayoutStruct {
[FieldOffset(0)] public byte B00;
}
struct RegularStruct
{
public int Field;
}
struct BoolAndExplicitStruct
{
// bool AND a field with an explicit layout struct
// change the layout of this struct to Auto instead of sequential
public bool Bool;
public ExplicitLayoutStruct ExplicitLayout;
public int Int32;
public RegularStruct RegularStruct;
public long Int64;
}
// Field Int64: starts at offset 0
// Field Int32: starts at offset 8
// Field Bool: starts at offset 12
// Field ExplicitLayout: starts at offset 16
// Field RegularStruct: starts at offset 24This is causing lots of trouble on the Burst compiler on our side, because as we are using these structs in native code, we expect the same sequential+explicit layout than .NET, but we were not expecting an implicit auto-layout promotion in this case (We haven't implemented today the auto-layout because we fear its implementation dependent behavior)
It seems to be related around this function CheckIfDisqualifiedFromManagedSequential or the caller of this function, but haven't identified exactly in the code when this flip occurs.
Do you know the reason of this auto-layout promotion? Could we revert that behavior? (We will be happy to make a PR)
Configuration
Happens on all OS, all CPU and all .NET version (including .NET framework) but not on Mono (because Mono doesn't have auto-layout promotion afaik)
Regression?
Nope.