Skip to content
Merged
Changes from all commits
Commits
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
28 changes: 22 additions & 6 deletions src/coreclr/jit/helperexpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ bool Compiler::fgExpandThreadLocalAccessForCall(BasicBlock** pBlock, Statement*
tlsValueDef = gtNewStoreLclVarNode(tlsLclNum, tlsValue);
GenTree* tlsLclValueUse = gtNewLclVarNode(tlsLclNum);
GenTree* typeThreadStaticBlockIndexValue = call->gtArgs.GetArgByIndex(0)->GetNode();
assert(genActualType(typeThreadStaticBlockIndexValue) == TYP_INT);

if (helper == CORINFO_HELP_GETDYNAMIC_NONGCTHREADSTATIC_BASE_NOCTOR_OPTIMIZED2)
{
Expand All @@ -1007,12 +1008,19 @@ bool Compiler::fgExpandThreadLocalAccessForCall(BasicBlock** pBlock, Statement*
// use(tlsRoot);
// ...

GenTree* typeThreadStaticBlockIndexValue = call->gtArgs.GetArgByIndex(0)->GetNode();
GenTree* threadStaticBase =
gtNewOperNode(GT_ADD, TYP_I_IMPL,
gtNewOperNode(GT_ADD, TYP_I_IMPL, gtCloneExpr(tlsLclValueUse),
gtCloneExpr(typeThreadStaticBlockIndexValue)),
typeThreadStaticBlockIndexValue = gtCloneExpr(typeThreadStaticBlockIndexValue);
#ifdef TARGET_64BIT
typeThreadStaticBlockIndexValue = gtNewCastNode(TYP_I_IMPL, typeThreadStaticBlockIndexValue, true, TYP_I_IMPL);
// Usually a constant, try to fold
typeThreadStaticBlockIndexValue = gtFoldExpr(typeThreadStaticBlockIndexValue);
#endif

GenTree* offset =
gtNewOperNode(GT_ADD, TYP_I_IMPL, typeThreadStaticBlockIndexValue,
gtNewIconNode(threadStaticBlocksInfo.offsetOfBaseOfThreadLocalData, TYP_I_IMPL));
offset = gtFoldExpr(offset);

GenTree* threadStaticBase = gtNewOperNode(GT_ADD, TYP_I_IMPL, gtCloneExpr(tlsLclValueUse), offset);
GenTree* tlsStaticBaseStoreLcl = gtNewStoreLclVarNode(threadStaticBlockLclNum, threadStaticBase);

BasicBlock* tlsBaseComputeBB = fgNewBBFromTreeAfter(BBJ_ALWAYS, prevBb, tlsValueDef, debugInfo, true);
Expand Down Expand Up @@ -1060,6 +1068,14 @@ bool Compiler::fgExpandThreadLocalAccessForCall(BasicBlock** pBlock, Statement*
// Create tree to "threadStaticBlockValue = threadStaticBlockBase[typeIndex]"
typeThreadStaticBlockIndexValue = gtNewOperNode(GT_MUL, TYP_INT, gtCloneExpr(typeThreadStaticBlockIndexValue),
gtNewIconNode(TARGET_POINTER_SIZE, TYP_INT));
// Usually a constant, try to fold
typeThreadStaticBlockIndexValue = gtFoldExpr(typeThreadStaticBlockIndexValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be or do we need to fold expression before casting it as well?

#ifdef TARGET_X64
    typeThreadStaticBlockIndexValue = gtNewCastNode...
#endif
    typeThreadStaticBlockIndexValue = gtFoldExpr(...

Copy link
Member Author

Choose a reason for hiding this comment

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

We need to fold it before as well since gtFoldExpr is not recursive.

#ifdef TARGET_64BIT
typeThreadStaticBlockIndexValue = gtNewCastNode(TYP_I_IMPL, typeThreadStaticBlockIndexValue, true, TYP_I_IMPL);

// Usually a constant, try to fold
typeThreadStaticBlockIndexValue = gtFoldExpr(typeThreadStaticBlockIndexValue);
#endif
GenTree* typeThreadStaticBlockRef =
gtNewOperNode(GT_ADD, TYP_BYREF, threadStaticBlocksValue, typeThreadStaticBlockIndexValue);
GenTree* typeThreadStaticBlockValue = gtNewIndir(TYP_BYREF, typeThreadStaticBlockRef, GTF_IND_NONFAULTING);
Expand Down Expand Up @@ -1416,7 +1432,7 @@ bool Compiler::fgExpandStaticInitForCall(BasicBlock** pBlock, Statement* stmt, G
}

// Don't fold ADD(CNS1, CNS2) here since the result won't be reloc-friendly for AOT
GenTree* offsetNode = gtNewOperNode(GT_ADD, TYP_I_IMPL, baseAddr, gtNewIconNode(isInitOffset));
GenTree* offsetNode = gtNewOperNode(GT_ADD, TYP_I_IMPL, baseAddr, gtNewIconNode(isInitOffset, TYP_I_IMPL));
isInitedActualValueNode = gtNewIndir(TYP_I_IMPL, offsetNode, GTF_IND_NONFAULTING | GTF_IND_VOLATILE);

// 0 means "initialized" on NativeAOT
Expand Down
Loading