Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Wasm: Align classes and arrays with 64 bit types at 8 byte #8255

Merged
merged 7 commits into from
Aug 13, 2020

Conversation

yowl
Copy link
Contributor

@yowl yowl commented Aug 5, 2020

Closes #8250

This PR enables the FEATURE_64BIT_ALIGNMENT feature for Wasm and implements some of the RhpNew... functions to align arrays and classes at 8 bytes. This will move Wasm closer to enabling threads (although may have to wait for Wasm Exceptions for this as the current emscripten exception handling is not thread safe - this Wasm feature is currently behind a flag in v8).

May also improve ARM32 support, but not tried that.

#define GC_ALLOC_FINALIZE 0x1 // TODO: Defined in gc.h
#define GC_ALLOC_FINALIZE 0x1 // TODO: Defined in gc.h
#define GC_ALLOC_ALIGN8_BIAS 0x4 // TODO: Defined in gc.h
#define GC_ALLOC_ALIGN8 0x8 // TODO: Defined in gc.h
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried including gcinterface.h but that seems to have other dependencies

@@ -180,36 +181,161 @@ COOP_PINVOKE_HELPER(String *, RhNewString, (EEType * pArrayEEType, int numElemen

#endif
#if defined(USE_PORTABLE_HELPERS)
struct RawEEType // TODO: defined in common.h
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a copy from Bootstrap/common.h. Could have used EEType also...

// dummy object for aligning next allocation to 8 that supports Methodtable GetBaseSize (12),HasComponentSize (false)
static struct RawEEType dummy12ByteEEType = { 0, 0, 12, NULL, 0, 0, 0 };

#if defined(HOST_ARM) || defined(HOST_WASM)
COOP_PINVOKE_HELPER(Object *, RhpNewFinalizableAlign8, (EEType* pEEType))
{
Object * pObject = nullptr;
/* TODO */ ASSERT_UNCONDITIONALLY("NYI");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't looked at this one, but if its trivial I could fill it out.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this can be pretty simple - similar to RhpNewFastMisalign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I've done it, not committed though as I'm having a hard time getting a test. The only types that I can make have alignment.AsInt == 8 at

alignment = target.GetObjectAlignment(alignment);
are structs so can't put finalizers on those.

I thought a class with a long would be enough

    class FinalizableClass
    {
        long l; // requires 8 alignment 
        ~FinalizableClass()
        {
            l = 1;
            if(l == 1) finalizeCalled = true;
        }
    }

Copy link
Member

Choose a reason for hiding this comment

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

Good point. You are right - this is de-facto unreachable. It may be worth it to add a comment about it.

#ifdef FEATURE_CONSERVATIVE_GC
&& (GCConfig::GetConservativeGC() || interior <= heap_segment_allocated(seg))
if ( interior >= heap_segment_allocated(seg))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same as merged in dotnet/runtime#39905 . If this complicates merging, I can leave this PR as WIP.


#ifdef HOST_ARM
// dummy object for aligning next allocation to 8 that supports Methodtable GetBaseSize (12),HasComponentSize (false)
Copy link
Member

Choose a reason for hiding this comment

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

This should use the existing g_pFreeObjectEEType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, removed in favour of g_pFreeObjectEEType

@@ -286,8 +287,7 @@ public LayoutInt GetObjectAlignment(LayoutInt fieldAlignment)
case TargetArchitecture.ARM64:
return new LayoutInt(8);
case TargetArchitecture.X86:
case TargetArchitecture.Wasm32:
return new LayoutInt(4);
return new LayoutInt(Math.Max(4, fieldAlignment.AsInt));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Think this is old and is causing the CI test failures. I'll try reverting.

@@ -663,7 +663,6 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
while (minAlign.AsInt < cumulativeInstanceFieldPos.AsInt)
minAlign = new LayoutInt(minAlign.AsInt * 2);
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Unnecessary WS change

@@ -180,36 +181,150 @@ COOP_PINVOKE_HELPER(String *, RhNewString, (EEType * pArrayEEType, int numElemen

#endif
#if defined(USE_PORTABLE_HELPERS)
#if defined(HOST_ARM) || defined(HOST_WASM)
Copy link
Member

Choose a reason for hiding this comment

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

Use FEATURE_64BIT_ALIGNMENT here?

Object * pObject = nullptr;
/* TODO */ ASSERT_UNCONDITIONALLY("NYI");
size_t size = pEEType->get_BaseSize();
size = (size + 7) & ~7;
Copy link
Member

Choose a reason for hiding this comment

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

This should not be needed. I do not see it in the asm version either.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@jkotas jkotas merged commit 3b8af4f into dotnet:master Aug 13, 2020
@yowl yowl deleted the wasm-8bytealign branch August 13, 2020 21:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wasm: Alignment required for atomics
2 participants