Skip to content

Fold null checks against known non-null values - #109164

Merged
EgorBo merged 11 commits into
dotnet:mainfrom
MichalPetryka:patch-43
Feb 19, 2025
Merged

Fold null checks against known non-null values#109164
EgorBo merged 11 commits into
dotnet:mainfrom
MichalPetryka:patch-43

Conversation

@MichalPetryka

Copy link
Copy Markdown
Contributor

Found in #108579.

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

Copy link
Copy Markdown
Contributor

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

@MichalPetryka

Copy link
Copy Markdown
ContributorAuthor

@MihuBot

@MichalPetryka

Copy link
Copy Markdown
ContributorAuthor

@MihuBot -dependsOn 108579

Comment threadsrc/coreclr/jit/gentree.cpp Outdated

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

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
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)?

@MichalPetrykaMichalPetrykaOct 24, 2024

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

@MihuBot -dependsOn 109715

@MichalPetryka

MichalPetryka commented Nov 22, 2024

Copy link
Copy Markdown
ContributorAuthor

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

@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
Copy Markdown
ContributorAuthor

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

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
Copy Markdown
ContributorAuthor

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.

return (addr->gtFlags & GTF_ARR_ADDR_NONNULL) == 0;

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

Copy link
Copy Markdown
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()

@MichalPetrykaMichalPetrykaDec 16, 2024

Copy link
Copy Markdown
ContributorAuthor

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.

@JulieLeeMSFT

Copy link
Copy Markdown
Member

@EgorBo, please review this PR.

@EgorBo
EgorBo merged commit f1b8d5a into dotnet:mainFeb 19, 2025
sirntar pushed a commit to sirntar/runtime that referenced this pull request Feb 19, 2025
grendello added a commit to grendello/runtime that referenced this pull request Feb 19, 2025
* main: (27 commits)
Fold null checks against known non-null values (dotnet#109164)
JIT: Always track the context for late devirt (dotnet#112396)
JIT: array allocation fixes (dotnet#112676)
[H/3] Fix test closing connection too fast (dotnet#112691)
Fix LINQ handling of iterator.Take(...).Last(...) (dotnet#112680)
[browser][MT] move wasm MT CI legs to extra-platforms (dotnet#112690)
JIT: Don't use `Compiler::compFloatingPointUsed` to check if FP kills are needed (dotnet#112668)
[LoongArch64] Fix a typo within PR#112166. (dotnet#112672)
Fix new EH hang on DebugBreak (dotnet#112640)
Use encode callback instead of renting a buffer to write to in DSAKeyFormatHelper
Move some links to other doc (dotnet#112574)
Reflection-based XmlSerializer - Deserialize empty collections and allow for sub-types in collection items. (dotnet#111723)
JIT: Use `fgCalledCount` for OSR method entry weight (dotnet#112662)
Use Avx10.2 Instructions in Floating Point Conversions (dotnet#111775)
Expose StressLog via CDAC and port StressLogAnalyzer to managed code (dotnet#104999)
JIT: Use linear block order for MinOpts in LSRA (dotnet#108147)
Update dependencies from https://github.com/dotnet/arcade build 20250213.2 (dotnet#112625)
JIT: Clean up and optimize call arg lowering (dotnet#112639)
Update dependencies from https://github.com/dotnet/emsdk build 20250217.1 (dotnet#112645)
JIT: Support `FIELD_LIST` for returns (dotnet#112308)
...
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Mar 22, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIcommunity-contributionIndicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@MichalPetryka@jkotas@JulieLeeMSFT@EgorBo