Skip to content

Commit

Permalink
[mono][llvm] Fix alignment of local vars (#106313)
Browse files Browse the repository at this point in the history
* [mono][llvm] Fix alignment of local vars

klass->min_align specifies the alignment required by the class fields. For example a class/struct containing an int64 will require a minimum alignment of 8 byte for its storage, while a a class/struct containing an int8 will require a 1 byte alignment. `build_named_alloca` is used to allocate storage for a local var of a certain type so using `klass->min_align` is incorrect. We should use `mono_type_size` instead, as this is used throughout the runtime for this purpose. A var of object type should have the alignment `sizeof (gpointer)` since it is a pointer, completely unrelated to the class field layout.

* Re-enable tests
  • Loading branch information
BrzVlad committed Aug 13, 2024
1 parent 5049a48 commit 7812a92
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ public static void Value_ThrownException_DoesntCreateValue_PublicationOnly()
Assert.False(lazy.IsValueCreated);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_SimpleRefTypes()
{
Expand Down Expand Up @@ -417,7 +416,6 @@ public static void EnsureInitialized_SimpleRefTypes()
Assert.Equal(strTemplate, d);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_SimpleRefTypes_Invalid()
{
Expand All @@ -430,7 +428,6 @@ public static void EnsureInitialized_SimpleRefTypes_Invalid()
Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized(ref ndc));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_ComplexRefTypes()
{
Expand Down Expand Up @@ -487,7 +484,6 @@ public static void EnsureInitialized_ComplexRefTypes()
Assert.Null(LazyInitializer.EnsureInitialized(ref e, ref einit, ref elock, () => { initCount++; return null; }));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_ComplexRefTypes_Invalid()
{
Expand All @@ -498,7 +494,6 @@ public static void EnsureInitialized_ComplexRefTypes_Invalid()
Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized(ref ndc, ref ndcInit, ref ndcLock));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void LazyInitializerComplexValueTypes()
{
Expand Down Expand Up @@ -553,7 +548,6 @@ public static void Ctor_Value_ValueType()
VerifyLazy(lazyObject, 123, hasValue: true, isValueCreated: true);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Uninitialized()
{
Expand All @@ -565,7 +559,6 @@ public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Unini
Assert.NotNull(syncLock);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Initialized()
{
Expand All @@ -577,7 +570,6 @@ public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Initi
Assert.Null(syncLock);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/105251", TestPlatforms.tvOS)]
[Fact]
public static void EnsureInitializer_FuncInitializationWithoutTrackingBool_Null()
{
Expand Down
6 changes: 3 additions & 3 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2978,10 +2978,10 @@ build_named_alloca (EmitContext *ctx, MonoType *t, char const *name)

g_assert (!mini_is_gsharedvt_variable_type (t));

if (mini_class_is_simd (ctx->cfg, k))
align = mono_class_value_size (k, NULL);
if (mini_class_is_simd (ctx->cfg, k) && !m_type_is_byref (t))
align = mono_class_value_size (k, NULL); // FIXME mono_type_size should report correct alignment
else
align = mono_class_min_align (k);
mono_type_size (t, &align);

/* Sometimes align is not a power of 2 */
while (mono_is_power_of_two (align) == -1)
Expand Down

0 comments on commit 7812a92

Please sign in to comment.