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

Fold null checks against known non-null values #109164

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

MichalPetryka
Copy link
Contributor

Found in #108579.

@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 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 23, 2024
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@MichalPetryka
Copy link
Contributor Author

@MihuBot

@MichalPetryka
Copy link
Contributor Author

@MihuBot -dependsOn 108579

src/coreclr/jit/gentree.cpp Outdated Show resolved Hide resolved

return NewMorphedIntConNode(compareResult);
GenTree* newTree = gtNewIconNode(compareResult);
if (wrapEffects)
Copy link
Member

Choose a reason for hiding this comment

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

just wrap unconditionally, gtWrapWithSideEffects won't create a COMMA if there are no side-effects

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIR we can't use the op if gtTryRemoveBoxUpstreamEffects removes the box, that's why I did the check.

Copy link
Member

Choose a reason for hiding this comment

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

not sure I understand, if it removes - does it leave a tree with a side-effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure I understand, if it removes - does it leave a tree with a side-effect?

As far as @SingleAccretion explained it to me on Discord, using the tree when gtTryRemoveBoxUpstreamEffects succeedes is nonsensical since the original tree is invalid due to the earlier box not existing anymore.

GenTree* boxSourceTree = gtTryRemoveBoxUpstreamEffects(op);
bool didOptimize = (boxSourceTree != nullptr);
// See if we can optimize away the box and related statements.
wrapEffects = (gtTryRemoveBoxUpstreamEffects(op) == nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

previous logic used to give up if box can't be removed, is it expected that the new one always folds?

Copy link
Member

Choose a reason for hiding this comment

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

btw, does it all handle boxed nullables (when boxed value is null reference)?

Copy link
Contributor Author

@MichalPetryka MichalPetryka Oct 24, 2024

Choose a reason for hiding this comment

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

is it expected that the new one always folds?

Yeah, my reasoning here was that there's no reason to avoid folding if we can't remove.

does it all handle boxed nullables (when boxed value is null reference)?

Not sure, I'd assume those wouldn't pass IsBoxedValue, otherwise I'd think the previous code would be broken.
EDIT: They wouldn't, IsBoxedValue checks GTF_BOX_VALUE which guarantees it's not null.

MichalPetryka added a commit to MichalPetryka/runtime that referenced this pull request Oct 25, 2024
@MichalPetryka
Copy link
Contributor Author

@MihuBot -dependsOn 109715

@MichalPetryka
Copy link
Contributor Author

MichalPetryka commented Nov 22, 2024

@jkoritzinsky Windows jit-format job appears to be broken, could you look into this?

@jkotas It seems that there was some Apple Clang bug that manifested when building a file I didn't touch here, do you know who'd be the right person for handling this?

2024-11-21T21:51:02.9088540Z   clang: error: unable to execute command: Abort trap: 6
2024-11-21T21:51:02.9714770Z   clang: error: clang frontend command failed due to signal (use -v to see invocation)
2024-11-21T21:51:03.0717300Z   Apple clang version 15.0.0 (clang-1500.1.0.2.5)
2024-11-21T21:51:03.1719340Z   Target: x86_64-apple-darwin22.6.0
2024-11-21T21:51:03.2721070Z   Thread model: posix
2024-11-21T21:51:03.3616560Z   InstalledDir: /Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
2024-11-21T21:51:03.5356310Z   [ 75%] Building CXX object jit/CMakeFiles/clrjit_universal_arm64_x64.dir/ifconversion.cpp.o
2024-11-21T21:51:03.6593320Z   [ 75%] Building CXX object jit/CMakeFiles/clrjit_universal_arm64_x64.dir/importer.cpp.o
2024-11-21T21:51:03.7539170Z   [ 75%] Building CXX object jit/CMakeFiles/clrjit_universal_arm64_x64.dir/importercalls.cpp.o
2024-11-21T21:51:03.8653490Z   clang: note: diagnostic msg: 
2024-11-21T21:51:03.9656260Z   ********************
2024-11-21T21:51:04.0658900Z   
2024-11-21T21:51:04.1044960Z   PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
2024-11-21T21:51:04.1394990Z   Preprocessed source(s) and associated run script(s) are located at:
2024-11-21T21:51:04.1624610Z   clang: note: diagnostic msg: /var/folders/p1/44pzfl0j2m301zzfb0fv0p380000gn/T/hashbv-f258fd.cpp
2024-11-21T21:51:04.2044370Z   clang: note: diagnostic msg: /var/folders/p1/44pzfl0j2m301zzfb0fv0p380000gn/T/hashbv-f258fd.sh
2024-11-21T21:51:04.2325410Z   clang: note: diagnostic msg: Crash backtrace is located in
2024-11-21T21:51:04.2550480Z   clang: note: diagnostic msg: /Users/runner/Library/Logs/DiagnosticReports/clang_<YYYY-MM-DD-HHMMSS>_<hostname>.crash
2024-11-21T21:51:04.2557650Z   clang: note: diagnostic msg: (choose the .crash file that corresponds to your crash)
2024-11-21T21:51:04.2563810Z   clang: note: diagnostic msg: 
2024-11-21T21:51:04.2570240Z   
2024-11-21T21:51:04.2585420Z   ********************
2024-11-21T21:51:04.2590510Z   make[3]: *** [jit/CMakeFiles/clrjit_universal_arm64_x64.dir/hashbv.cpp.o] Error 1
2024-11-21T21:51:04.2597660Z   make[3]: *** Waiting for unfinished jobs....
2024-11-21T21:51:08.5169240Z   make[2]: *** [jit/CMakeFiles/clrjit_universal_arm64_x64.dir/all] Error 2
2024-11-21T21:51:08.5182910Z   make[1]: *** [CMakeFiles/crosscomponents.dir/rule] Error 2
2024-11-21T21:51:08.5186860Z   make: *** [crosscomponents] Error 2
2024-11-21T21:51:08.5188920Z   ~/work/1/s/src/coreclr
2024-11-21T21:51:08.5228330Z ##[error]Failed to build "CoreCLR component".

@jkotas
Copy link
Member

jkotas commented Nov 22, 2024

@jkotas It seems that there was some Apple Clang bug that manifested when building a file I didn't touch here, do you know who'd be the right person for handling this?

Is it deterministic repro?

@MichalPetryka
Copy link
Contributor Author

@jkotas It seems that there was some Apple Clang bug that manifested when building a file I didn't touch here, do you know who'd be the right person for handling this?

Is it deterministic repro?

I don't see it failing on other PRs so I assume no, should I merge master again and see if it fails again?

@jkotas
Copy link
Member

jkotas commented Nov 22, 2024

I don't see it failing on other PRs so I assume no, should I merge master again and see if it fails again?

Yes. The temp files that the error messages suggest collecting are gone, so nothing will be lost by retrying.

@MichalPetryka
Copy link
Contributor Author

I don't see it failing on other PRs so I assume no, should I merge master again and see if it fails again?

Yes. The temp files that the error messages suggest collecting are gone, so nothing will be lost by retrying.

Seems it didn't happen again.

@@ -863,6 +863,9 @@ bool Compiler::fgAddrCouldBeNull(GenTree* addr)
case GT_ARR_ADDR:
return (addr->gtFlags & GTF_ARR_ADDR_NONNULL) == 0;

case GT_BOX:
return !addr->IsBoxedValue();
Copy link
Member

Choose a reason for hiding this comment

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

remind me why GT_BOX may produce null possibly? I don't understand the need in IsBoxedValue()

Copy link
Contributor Author

@MichalPetryka MichalPetryka Dec 16, 2024

Choose a reason for hiding this comment

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

IsBoxedValue checks the GTF_BOX_VALUE flag which is documented as guaranteeing that the box is not null, when we discussed this on Discord we reached the conclusion that the flag might be an old leftover that's not needed anymore and could be removed, but like I said there, I'd prefer such cleanup to be a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants