Skip to content

[crossgen2] Use Alignment to calculate class Offset - #38962

Merged
davidwrighton merged 5 commits into
dotnet:masterfrom
t-mustafin:issue-38822
Jul 15, 2020
Merged

davidwrighton merged 5 commits into
dotnet:masterfrom
t-mustafin:issue-38822

Conversation

@t-mustafin

Copy link
Copy Markdown
Contributor

ComputeAutoFieldLayout() function ignores calculated fieldSizeAndAlignment.Alignment and uses context.Target.LayoutPointerSize instead. It produces a problems if fieldSizeAndAlignment.Alignment > context.Target.LayoutPointerSize on arm for example.
I suggest to use maximum of that values to calculate offset.
Issue #38822

t-mustafin and others added 4 commits June 8, 2020 13:11
Use alignment to calculate class fields offsets.

Fixes dotnet#38822

Signed-off-by: Timur <t.mustafin@partner.samsung.com>
@t-mustafin t-mustafin changed the title Issue 38822 [crossgen2] Use Alignment to calculate class Offset Jul 8, 2020
@t-mustafin

Copy link
Copy Markdown
Contributor Author

@t-mustafin

Copy link
Copy Markdown
Contributor Author

cc @MichalStrehovsky

@MichalStrehovsky

MichalStrehovsky commented Jul 9, 2020 •

Copy link
Copy Markdown
Member

This method mostly tries to match

VOID MethodTableBuilder::PlaceInstanceFields(MethodTable ** pByValueClassCache)

in the VM. It feels like what we actually miss are #if FEATURE_64BIT_ALIGNMENT such as this one:

#ifdef FEATURE_64BIT_ALIGNMENT
if (GetHalfBakedClass()->IsAlign8Candidate()) {
minAlign = 8;
}
else
#endif // FEATURE_64BIT_ALIGNMENT

There's a couple other things under FEATURE_64BIT_ALIGNMENT that are not present in the crossgen2 code.

I'll let @davidwrighton take the "does this exactly match what the CoreCLR VM is doing?" parts since my knowledge of the CoreCLR type system is fairly limited.

@davidwrighton

Copy link
Copy Markdown
Member

I've written a more comprehensive layout test (See https://github.com/davidwrighton/runtime/tree/layout_testing ) That test isn't ready to merge, as the amount of C# in the test is unreasonably large, but I believe it should be a good way to see that most of these cases are handled properly. I plan to run that test on arm32 tomorrow, or Monday, with this fix.

@davidwrighton davidwrighton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unable to run the layout test to completion on the arm32 hardware I have available. I repeatedly saw crashes while attempting to run the JIT_GetRuntimeType helper. However, my analysis of the situation indicates that @MichalStrehovsky is correct, handling of the FEATURE_64BIT_ALIGNMENT behavior in crossgen2 is what needs to be matched. The particular fix here looks to be correct, and appears to match up with this snippet of the type loader in coreclr.

#ifdef FEATURE_64BIT_ALIGNMENT
if (pByValueMT->RequiresAlign8())
dwCumulativeInstanceFieldPos = (DWORD)ALIGN_UP(dwCumulativeInstanceFieldPos, 8);
else
#endif // FEATURE_64BIT_ALIGNMENT
dwCumulativeInstanceFieldPos = (DWORD)ALIGN_UP(dwCumulativeInstanceFieldPos, TARGET_POINTER_SIZE);
#endif // !(!defined(TARGET_64BIT) && (DATA_ALIGNMENT > 4))

My analysis of the rest of the behavior of the runtime indicates that this is at least close to correct, but as I was unable to run the test suites, I could not confirm further details. If you fix the vairable use issue I mentioned here, I will approve this fix as it is strictly more correct than the previous state, but I do not have confidence that all issues have been found.

else
{
cumulativeInstanceFieldPos = LayoutInt.AlignUp(cumulativeInstanceFieldPos, context.Target.LayoutPointerSize, context.Target);
largestAlignmentRequired = LayoutInt.Max(fieldSizeAndAlignment.Alignment, context.Target.LayoutPointerSize);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value doesn't represent the largetAlignment required, so please don't re-use the variable here. Define a new variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I defined new variable AlignmentRequired.

Signed-off-by: Timur <t.mustafin@partner.samsung.com>

@davidwrighton davidwrighton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said earlier, I'm not entirely convinced this fixes everything, but I think its a strict improvement over the current state.

@davidwrighton
davidwrighton merged commit 055c3fc into dotnet:master Jul 15, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@t-mustafin
t-mustafin deleted the issue-38822 branch June 16, 2023 17:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants