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

JIT: Produce correctly typed IR in TLS helper expansion #108836

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Oct 14, 2024

The TLS helper expansion would produce ADD(long, int) trees like

▌  STORE_LCL_VAR long   V01 rat0
└──▌  ADD       long
   ├──▌  ADD       long
   │  ├──▌  LCL_VAR   long   V02 rat1
   │  └──▌  CNS_INT   int    72 $41
   └──▌  CNS_INT   long   192

which is not legal IR. Fix that by inserting a cast. Also do some more aggressive folding to get rid of these casts in the normal cases, and to fold some constant arithmetic in other cases.

Noticed this while investigating #108830.

The TLS helper expansion would produce `ADD(long, int)` trees like
```
▌  STORE_LCL_VAR long   V01 rat0
└──▌  ADD       long
   ├──▌  ADD       long
   │  ├──▌  LCL_VAR   long   V02 rat1
   │  └──▌  CNS_INT   int    72 $41
   └──▌  CNS_INT   long   192
```

which is not legal IR. Fix that by inserting a cast. Also do some more
aggressive folding to get rid of these casts in the normal cases, and to
fold some constant arithmeticf in other cases.
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 14, 2024
@jakobbotsch jakobbotsch marked this pull request as ready for review October 14, 2024 14:34
@jakobbotsch
Copy link
Member Author

cc @dotnet/jit-contrib PTAL @kunalspathak

Diffs

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

Added a question.

I did verify the difference between my local disasm vs. what jit-diff is showing. The reason I didn't see diff locally was because on a standalone app, the tlsindex value was low that was getting removed because we were able to use an addressing mode to load it. However, in jit-diffs, we get higher tls index value which cannot be expressed using addressing mode and hence couldn't be eliminated.

@@ -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
Member

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.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

LGTM

@jakobbotsch jakobbotsch merged commit c5b9a02 into dotnet:main Oct 15, 2024
104 of 107 checks passed
@jakobbotsch jakobbotsch deleted the tls-fix-typing branch October 15, 2024 17:47
@github-actions github-actions bot locked and limited conversation to collaborators Nov 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants