Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 8-byte alignment for 12-byte structs on ARM32 #56375

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,11 +1147,13 @@ void Compiler::lvaInitUserArgs(InitVarDscInfo* varDscInfo, unsigned skipArgs, un
assert((doubleAlignMask & RBM_ARG_REGS) == doubleAlignMask);
if (doubleAlignMask != RBM_NONE && doubleAlignMask != RBM_ARG_REGS)
{
// doubleAlignMask can only be 0011 and/or 1100 as 'double aligned types' can
// begin at r0 or r2.
assert(doubleAlignMask == 0x3 || doubleAlignMask == 0xC /* || 0xF is if'ed out */);
// 'double aligned types' can begin only at r0 or r2 and we always expect at least two registers to be used
// Note that in rare cases, we can have double-aligned structs of 12 bytes (if specified explicitly with
// attributes)
assert((doubleAlignMask == 0b0011) || (doubleAlignMask == 0b1100) ||
(doubleAlignMask == 0b0111) /* || 0b1111 is if'ed out */);

// Now if doubleAlignMask is 0011 i.e., {r0,r1} and we prespill r2 or r3
// Now if doubleAlignMask is xyz1 i.e., the struct starts in r0, and we prespill r2 or r3
// but not both, then the stack would be misaligned for r0. So spill both
// r2 and r3.
//
Expand All @@ -1160,7 +1162,10 @@ void Compiler::lvaInitUserArgs(InitVarDscInfo* varDscInfo, unsigned skipArgs, un
// ; -8 r1 r1
// ; -c r0 r0 <-- misaligned.
// ; callee saved regs
if (doubleAlignMask == 0x3 && doubleAlignMask != codeGen->regSet.rsMaskPreSpillRegArg)
bool startsAtR0 = (doubleAlignMask & 1) == 1;
bool r2XorR3 = ((codeGen->regSet.rsMaskPreSpillRegArg & RBM_R2) == 0) !=
((codeGen->regSet.rsMaskPreSpillRegArg & RBM_R3) == 0);
if (startsAtR0 && r2XorR3)
{
codeGen->regSet.rsMaskPreSpillAlign =
(~codeGen->regSet.rsMaskPreSpillRegArg & ~doubleAlignMask) & RBM_ARG_REGS;
Expand Down
24 changes: 24 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_42723/Runtime_42723.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

class Runtime_42723
{
static int Main()
{
return Test(new S { X = 17, Y = 83 });
}

// On ARM32 we were asserting for a 8-byte aligned 12-byte struct as a parameter
[MethodImpl(MethodImplOptions.NoInlining)]
static int Test(S s) => (int)(s.X + s.Y);

[StructLayout(LayoutKind.Sequential, Size = 12)]
struct S
{
public double X;
public float Y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>