Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Conversation

@PeterSolMS
Copy link

Initial Version - JIT part.

Based on the new items in the CORINFO_FIELD_INFO struct, build a tree that calls the helper if the cctor hasn't run yet, and otherwise directly loads the statics area.

I'm not yet happy with the codegen, but it's a start...

@BruceForstall
Copy link

@dotnet/jit-contrib

@mikedn
Copy link

mikedn commented Jun 22, 2017

I'm not yet happy with the codegen

How does codegen looks?

@JosephTremoulet
Copy link

This may inhibit hoisting invariant static field loads out of loops by defeating the handshake/pattern-match between the importer and optimizer from #11465 and #11694 -- we consider all fields marked CORINFO_FLG_FIELD_INITCLASS non-hoistable except as a sub-expression of a comma whose first operand is exactly the helper call.

@karelz
Copy link
Member

karelz commented Aug 25, 2017

What is status of this PR? No update in 2 months :(

@karelz
Copy link
Member

karelz commented Aug 30, 2017

Closing for now, feel free to reopen when you're ready to work on it again ...

@karelz karelz closed this Aug 30, 2017
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this pull request Jan 11, 2022
NativeAOT currently goes through a helper call whenever we need to access static fields. For this simple program:

```csharp
class Program
{
    static int s1, s2;
    static int Main() => s1 + s2;
}
```

We generate:

```nasm
repro_Program__Main:
sub         rsp,28h
call        __GetNonGCStaticBase_repro_Program
mov         edx,dword ptr [rax]
add         edx,dword ptr [rax+4]
mov         eax,edx
add         rsp,28h
ret

__GetNonGCStaticBase_repro_Program:
lea         rax,[?__NONGCSTATICS@repro_Program@@]
ret
```

It's not terrible, but also could be better. As far as I can see, JitInterface cannot express the exact way static fields are accessed in NativeAOT. I had a previous attempt to use the existing JitInterface facilities in dotnet/corert#5131 (misusing the facilities that exist to support RVA static fields), but it didn't generate "nice" addressing modes and couldn't support GC statics (see the disassembly there).

I'm adding a way to do that with "nice" addressing modes. After this change, the above program compiles into:

```nasm
repro_Program__Main:
mov         eax,dword ptr [?__NONGCSTATICS@repro_Program@@]
lea         rdx,[?__NONGCSTATICS@repro_Program@@]
add         eax,dword ptr [rdx+4]
ret
```

There's room for improvement:
* It would be nice if we could similarly inline these lookups when we're in shared generic code
* I think the addressing mode could be more compact if RyuJIT generated a reloc with a delta on x64.
* It would be nice if we could do this if there's a static constructor. .NET Native could inline the "did static constructor already run?" checks. There was a previous attempt at that in dotnet/coreclr#12420 (and corresponding dotnet/corert#3962).
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.

6 participants