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

NativeAOT: Partially expand static initialization #83911

Merged
merged 40 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7100058
Expand static initialization in JIT
EgorBo Mar 24, 2023
023e2c8
Clean up
EgorBo Mar 24, 2023
6019949
Address feedback, clean up
EgorBo Mar 25, 2023
791244b
Address feedback, baby steps towards NAOT impl
EgorBo Mar 25, 2023
4f7c857
Remove test that always expects CORINFO_HELP_GETSHARED_NONGCSTATIC_BA…
EgorBo Mar 25, 2023
620bbd1
NativeAOT non-GC impl
EgorBo Mar 25, 2023
4279269
Fix NAOT
EgorBo Mar 25, 2023
7b9a259
Enable GC statics for NativeAOT
EgorBo Mar 25, 2023
a79eba7
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Mar 25, 2023
c1744bc
Fix assert
EgorBo Mar 25, 2023
141e99e
code clean up
EgorBo Mar 25, 2023
9ab6dc1
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Mar 25, 2023
0131b3c
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Mar 26, 2023
e073da4
Flip condition to "*isInited == 1"
EgorBo Mar 26, 2023
5256957
Update comments
EgorBo Mar 26, 2023
6cb5d30
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Mar 27, 2023
bc46cbd
Split getIsClassInitedFieldAddress into two functions
EgorBo Mar 27, 2023
9229e5d
Cache static base to a local
EgorBo Mar 27, 2023
95f0f7a
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Mar 27, 2023
bebf956
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Apr 3, 2023
86ccf98
Update src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoIm…
EgorBo Apr 3, 2023
96bd745
Address feedback
EgorBo Apr 3, 2023
efa8dfa
clean up
EgorBo Apr 3, 2023
c3f389b
Address feedback
EgorBo Apr 3, 2023
61bf741
Address feedback
EgorBo Apr 3, 2023
19d1484
use GTF_ICON_GLOBAL_PTR
EgorBo Apr 3, 2023
6d7ea49
Merge branch 'main' of github.com:dotnet/runtime into expand-static-init
EgorBo Apr 5, 2023
742c948
Update src/coreclr/jit/flowgraph.cpp
EgorBo Apr 5, 2023
dbb4a0c
Update src/coreclr/jit/flowgraph.cpp
EgorBo Apr 5, 2023
deb4eb6
Update src/coreclr/jit/flowgraph.cpp
EgorBo Apr 5, 2023
f0a44ba
Update src/coreclr/jit/flowgraph.cpp
EgorBo Apr 5, 2023
9b11b91
Merge branch 'expand-static-init' of github.com:EgorBo/runtime-1 into…
EgorBo Apr 5, 2023
2570f84
formatting
EgorBo Apr 5, 2023
16c75b2
Address feedback (1st round)
EgorBo Apr 5, 2023
2bbf5bf
Address feedback (round 2)
EgorBo Apr 6, 2023
ddc1731
Address feedback
EgorBo Apr 6, 2023
f65d080
Fix assert on NAOT
EgorBo Apr 6, 2023
c7bc7d8
Apply suggestions from code review
EgorBo Apr 6, 2023
6d1d18a
Update src/coreclr/jit/flowgraph.cpp
EgorBo Apr 6, 2023
cc56182
Address feedback
EgorBo Apr 6, 2023
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
12 changes: 12 additions & 0 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,18 @@ class ICorStaticInfo
void **ppIndirection
) = 0;

virtual bool getIsClassInitedFlagAddress(
CORINFO_CLASS_HANDLE cls,
CORINFO_CONST_LOOKUP* addr,
int* offset
) = 0;

virtual bool getStaticBaseAddress(
CORINFO_CLASS_HANDLE cls,
bool isGc,
CORINFO_CONST_LOOKUP* addr
) = 0;

// return the number of bytes needed by an instance of the class
virtual unsigned getClassSize (
CORINFO_CLASS_HANDLE cls
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ size_t getClassModuleIdForStatics(
CORINFO_MODULE_HANDLE* pModule,
void** ppIndirection) override;

bool getIsClassInitedFlagAddress(
CORINFO_CLASS_HANDLE cls,
CORINFO_CONST_LOOKUP* addr,
int* offset) override;

bool getStaticBaseAddress(
CORINFO_CLASS_HANDLE cls,
bool isGc,
CORINFO_CONST_LOOKUP* addr) override;

unsigned getClassSize(
CORINFO_CLASS_HANDLE cls) override;

Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
#define GUID_DEFINED
#endif // !GUID_DEFINED

constexpr GUID JITEEVersionIdentifier = { /* C1A00D6C-2B60-4511-8AD2-6DB109224E37 */
0xc1a00d6c,
0x2b60,
0x4511,
{ 0x8a, 0xd2, 0x6d, 0xb1, 0x9, 0x22, 0x4e, 0x37 }
constexpr GUID JITEEVersionIdentifier = { /* ee1c0d7d-f4fb-4900-84ab-b302f304a39a */
0xee1c0d7d,
0xf4fb,
0x4900,
{0x84, 0xab, 0xb3, 0x2, 0xf3, 0x4, 0xa3, 0x9a}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/ICorJitInfo_names_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ DEF_CLR_API(getAssemblyName)
DEF_CLR_API(LongLifetimeMalloc)
DEF_CLR_API(LongLifetimeFree)
DEF_CLR_API(getClassModuleIdForStatics)
DEF_CLR_API(getIsClassInitedFlagAddress)
DEF_CLR_API(getStaticBaseAddress)
DEF_CLR_API(getClassSize)
DEF_CLR_API(getHeapClassSize)
DEF_CLR_API(canAllocateOnStack)
Expand Down
22 changes: 22 additions & 0 deletions src/coreclr/jit/ICorJitInfo_wrapper_generated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,28 @@ size_t WrapICorJitInfo::getClassModuleIdForStatics(
return temp;
}

bool WrapICorJitInfo::getIsClassInitedFlagAddress(
CORINFO_CLASS_HANDLE cls,
CORINFO_CONST_LOOKUP* addr,
int* offset)
{
API_ENTER(getIsClassInitedFlagAddress);
bool temp = wrapHnd->getIsClassInitedFlagAddress(cls, addr, offset);
API_LEAVE(getIsClassInitedFlagAddress);
return temp;
}

bool WrapICorJitInfo::getStaticBaseAddress(
CORINFO_CLASS_HANDLE cls,
bool isGc,
CORINFO_CONST_LOOKUP* addr)
{
API_ENTER(getStaticBaseAddress);
bool temp = wrapHnd->getStaticBaseAddress(cls, isGc, addr);
API_LEAVE(getStaticBaseAddress);
return temp;
}

unsigned WrapICorJitInfo::getClassSize(
CORINFO_CLASS_HANDLE cls)
{
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5013,6 +5013,9 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
// Expand runtime lookups (an optimization but we'd better run it in tier0 too)
DoPhase(this, PHASE_EXPAND_RTLOOKUPS, &Compiler::fgExpandRuntimeLookups);

// Partially inline static initializations
DoPhase(this, PHASE_EXPAND_STATIC_INIT, &Compiler::fgExpandStaticInit);

// Insert GC Polls
DoPhase(this, PHASE_INSERT_GC_POLLS, &Compiler::fgInsertGCPolls);

Expand Down
22 changes: 22 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4537,6 +4537,8 @@ class Compiler

BasicBlock* fgNewBBafter(BBjumpKinds jumpKind, BasicBlock* block, bool extendRegion);

BasicBlock* fgNewBBFromTreeAfter(BBjumpKinds jumpKind, BasicBlock* block, GenTree* tree, DebugInfo& debugInfo, bool updateSideEffects = false);

BasicBlock* fgNewBBinRegion(BBjumpKinds jumpKind,
unsigned tryIndex,
unsigned hndIndex,
Expand Down Expand Up @@ -5300,6 +5302,7 @@ class Compiler
void SplitTreesRandomly();
void SplitTreesRemoveCommas();
PhaseStatus fgExpandRuntimeLookups();
PhaseStatus fgExpandStaticInit();
PhaseStatus fgInsertGCPolls();
BasicBlock* fgCreateGCPoll(GCPollType pollType, BasicBlock* block);

Expand Down Expand Up @@ -7023,6 +7026,7 @@ class Compiler
#define OMF_HAS_TAILCALL_SUCCESSOR 0x00001000 // Method has potential tail call in a non BBJ_RETURN block
#define OMF_HAS_MDNEWARRAY 0x00002000 // Method contains 'new' of an MD array
#define OMF_HAS_MDARRAYREF 0x00004000 // Method contains multi-dimensional intrinsic array element loads or stores.
#define OMF_HAS_STATIC_INIT 0x00008000 // Method has static initializations we might want to partially inline

// clang-format on

Expand Down Expand Up @@ -7053,6 +7057,16 @@ class Compiler
optMethodFlags |= OMF_HAS_FROZEN_OBJECTS;
}

bool doesMethodHaveStaticInit()
{
return (optMethodFlags & OMF_HAS_STATIC_INIT) != 0;
}

void setMethodHasStaticInit()
{
optMethodFlags |= OMF_HAS_STATIC_INIT;
}

bool doesMethodHaveGuardedDevirtualization() const
{
return (optMethodFlags & OMF_HAS_GUARDEDDEVIRT) != 0;
Expand Down Expand Up @@ -8046,6 +8060,14 @@ class Compiler
static CORINFO_METHOD_HANDLE eeFindHelper(unsigned helper);
static CorInfoHelpFunc eeGetHelperNum(CORINFO_METHOD_HANDLE method);

enum StaticHelperReturnValue
{
SHRV_STATIC_BASE_PTR,
SHRV_VOID,
};
static bool IsStaticHelperEligibleForExpansion(GenTree* tree,
bool* isGc = nullptr,
StaticHelperReturnValue* retValKind = nullptr);
static bool IsSharedStaticHelper(GenTree* tree);
static bool IsGcSafePoint(GenTreeCall* call);

Expand Down
51 changes: 51 additions & 0 deletions src/coreclr/jit/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,57 @@ inline CorInfoHelpFunc Compiler::eeGetHelperNum(CORINFO_METHOD_HANDLE method)
return ((CorInfoHelpFunc)(((size_t)method) >> 2));
}

//------------------------------------------------------------------------
// IsStaticHelperEligibleForExpansion: Determine whether this node is a static init
// helper eligible for late expansion
//
// Arguments:
// tree - tree node
// isGC - [OUT] whether the helper returns GCStaticBase or NonGCStaticBase
// retValKind - [OUT] describes its return value
//
// Return Value:
// Returns true if eligible for late expansion
//
inline bool Compiler::IsStaticHelperEligibleForExpansion(GenTree* tree, bool* isGc, StaticHelperReturnValue* retValKind)
{
if (!tree->IsHelperCall())
{
return false;
}

bool gc = false;
bool result = false;
StaticHelperReturnValue retVal = {};
switch (eeGetHelperNum(tree->AsCall()->gtCallMethHnd))
{
case CORINFO_HELP_READYTORUN_GCSTATIC_BASE:
case CORINFO_HELP_GETSHARED_GCSTATIC_BASE:
result = true;
gc = true;
retVal = SHRV_STATIC_BASE_PTR;
break;
case CORINFO_HELP_READYTORUN_NONGCSTATIC_BASE:
case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE:
result = true;
gc = false;
retVal = SHRV_STATIC_BASE_PTR;
break;
// TODO: other helpers
default:
break;
}
if (isGc != nullptr)
{
*isGc = gc;
}
if (retValKind != nullptr)
{
*retValKind = retVal;
}
return result;
}

// TODO-Cleanup: Replace calls to IsSharedStaticHelper with new HelperCallProperties
//

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ CompPhaseNameMacro(PHASE_OPT_UPDATE_FLOW_GRAPH, "Update flow graph opt pass
CompPhaseNameMacro(PHASE_COMPUTE_EDGE_WEIGHTS2, "Compute edge weights (2, false)",false, -1, false)
CompPhaseNameMacro(PHASE_STRESS_SPLIT_TREE, "Stress gtSplitTree", false, -1, false)
CompPhaseNameMacro(PHASE_EXPAND_RTLOOKUPS, "Expand runtime lookups", false, -1, true)
CompPhaseNameMacro(PHASE_EXPAND_STATIC_INIT, "Expand static init", false, -1, true)
CompPhaseNameMacro(PHASE_INSERT_GC_POLLS, "Insert GC Polls", false, -1, true)
CompPhaseNameMacro(PHASE_DETERMINE_FIRST_COLD_BLOCK, "Determine first cold block", false, -1, true)
CompPhaseNameMacro(PHASE_RATIONALIZE, "Rationalize IR", false, -1, false)
Expand Down
34 changes: 34 additions & 0 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6078,6 +6078,40 @@ BasicBlock* Compiler::fgNewBBafter(BBjumpKinds jumpKind, BasicBlock* block, bool
return newBlk;
}

//------------------------------------------------------------------------
// fgNewBBFromTreeAfter: Create a basic block from the given tree and insert it
// after the specified block.
//
// Arguments:
// jumpKind - jump kind for the new block.
// block - insertion point.
// tree - tree that will be wrapped into a statement and
// inserted in the new block.
// debugInfo - debug info to propagate into the new statement.
// updateSideEffects - update side effects for the whole statement.
//
// Return Value:
// The new block
//
// Notes:
// The new block will have BBF_INTERNAL flag and EH region will be extended
//
BasicBlock* Compiler::fgNewBBFromTreeAfter(
BBjumpKinds jumpKind, BasicBlock* block, GenTree* tree, DebugInfo& debugInfo, bool updateSideEffects)
{
BasicBlock* newBlock = fgNewBBafter(jumpKind, block, true);
newBlock->bbFlags |= BBF_INTERNAL;
Statement* stmt = fgNewStmtFromTree(tree, debugInfo);
fgInsertStmtAtEnd(newBlock, stmt);
newBlock->bbCodeOffs = block->bbCodeOffsEnd;
newBlock->bbCodeOffsEnd = block->bbCodeOffsEnd;
if (updateSideEffects)
{
gtUpdateStmtSideEffects(stmt);
}
return newBlock;
}

/*****************************************************************************
* Inserts basic block before existing basic block.
*
Expand Down
Loading