Skip to content

ARM64 altjit uses wrong return type for a wrapped Vector128. #38980

Open

Description

@sandreenko

For a test:

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static Vector128Wrapped TestReturnVector128WrappedPromoted()
    {
        var a = new Vector128Wrapped
        {
            f = Vector128<short>.Zero
        };
        return a;
    }

arm64 native will use:

N001 (  3,  2) [000015] ------------        t15 =    LCL_FLD   simd16 V00 loc0         [+0]
                                                  *    simd16 V00.f (offs=0x00) -> V03 tmp1         
                                                  /--*  t15    simd16 
N002 (  4,  3) [000016] ------------              *  RETURN    simd16 $1c1

and generate

IN0003: 000018                    ldr     q16, [fp,#16]	// [V00 loc0]
IN0004: 00001C                    mov     v0.16b, v16.16b

when altjit will use:

N001 (  3,  2) [000015] -c-----N----        t15 =    LCL_VAR   struct<Vector128Wrapped, 16>(P) V00 loc0         
                                                  *    simd16 V00.f (offs=0x00) -> V03 tmp1          $1c0
                                                  /--*  t15    struct 
N002 (  4,  3) [000016] ------------              *  RETURN    struct $1c1

and generate:

IN0003: 000018                    ldr     x0, [fp,#16]	// [V00 loc0]
IN0004: 00001C                    ldr     x1, [fp,#24]	// [V00 loc0+0x08]

That was found in #37745.

The issue is probably in this VM code:

#ifdef TARGET_ARM64
MethodTable* pMT;
#if defined(FEATURE_HFA)
pMT = pByValueClassCache[i];
#else
pMT = pFD->LookupApproxFieldTypeHandle().AsMethodTable();
#endif
int thisElemSize = pMT->GetVectorSize();
if (thisElemSize != 0)
{
fieldHFAType = (thisElemSize == 8) ? CORINFO_HFA_ELEM_VECTOR64 : CORINFO_HFA_ELEM_VECTOR128;
}
else
#endif // TARGET_ARM64

that we run on native arm64, but have disabled for altjit.

The issue prevents us from an optimization that would fail with altjit with the current implementation.

category:correctness
theme:altjit
skill-level:intermediate
cost:medium

Activity

  1. added
    area-CodeGen-coreclr
    on Jul 9, 2020
  2. added
    untriaged
    on Jul 9, 2020
  3. sandreenko commented on Jul 9, 2020

    @sandreenko
    ContributorAuthor

    @CarolEidt , @BruceForstall could you please take a look? what do you think is the best solution here?
    I could think of a few options:

    • allow the optimization only when altjit is not used (would be hard to keep altjit in sync after);
    • support RETURN struct as x0,x1 (simd16 in a register) specifically for altjit in codegen (again, that code path won't get tested in ci);
    • somehow teach VM to give us a correct answer (not sure if possible);
    • don't use VM and write our own function that will respect altjit flag?

    cc @dotnet/jit-contrib

  4. kunalspathak commented on Jul 9, 2020

    @kunalspathak
    Contributor

    This is an interesting finding where altjit generated different code than that on target machine. I can imagine that it is difficult to keep altjit in sync, I wonder how many such cases might be there in JIT code base where code generated differs. I think it is of lesser importance because altjit is just used as convivence tool for JIT developers (at least that's what my understanding of altjit is), so it is ok to find such cases on need basis.

  5. BruceForstall commented on Jul 9, 2020

    @BruceForstall
    Contributor

    I think a simple solution in this case is to change CheckForHFA to use #ifdef TARGET_64BIT instead of #ifdef TARGET_ARM64. It looks like this is already done for MethodTable::GetVectorSize. This would work because we run the arm64 altjit against the x64 VM and the arm32 altjit against the x86 VM.

  6. sandreenko commented on Jul 9, 2020

    @sandreenko
    ContributorAuthor

    I think a simple solution in this case is to change CheckForHFA to use #ifdef TARGET_64BIT instead of #ifdef TARGET_ARM64. It looks like this is already done for MethodTable::GetVectorSize. This would work because we run the arm64 altjit against the x64 VM and the arm32 altjit against the x86 VM.

    Won't it produce incorrect result on x64 windows?

  7. BruceForstall commented on Jul 9, 2020

    @BruceForstall
    Contributor

    Those functions should never be used on x86/x64 except by the altjits. It's not under #ifdef FEATURE_HFA solely to allow the altjit to call getHFAType across the JIT-EE interface.

  8. removed
    untriaged
    on Jul 13, 2020
  9. added this to the 6.0.0 milestone on Jul 13, 2020
  10. sandreenko commented on Jul 13, 2020

    @sandreenko
    ContributorAuthor

    I have tried Bruce's suggestion, but it was not enough, arm64altjit is returning false from IsIntrinsicType for Vector128Wrapped, when arm64 returns true. I was trying to debug it further but was blocked by a strange error that I am investigating now.
    In any case, I think we don't have enough time in 5.0 for the optimization that was blocked by this issue, so the issue could wait as well (or be fixed as a bug in a later previes, without the optimization).

  11. added
    JitUntriaged
    on Oct 28, 2020
  12. removed
    JitUntriaged
    on Nov 7, 2020
  13. added
    needs-further-triage
    on Mar 23, 2021
  14. added
    Priority:3
    and removed
    needs-further-triage
    on Apr 1, 2021
  15. modified the milestones: 6.0.0, Future on Apr 1, 2021
  16. sandreenko commented on Apr 1, 2021

    @sandreenko
    ContributorAuthor

    It is altjit only, pushing to future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:3area-CodeGen-coreclr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions