Skip to content

Conversation

amanasifkhalid
Copy link
Contributor

Allow Compiler::gtComplexityExceeds et al to compute the complexity of a tree as something other than the count of its nodes by relying on a callback function. We don't diverge from this definition of complexity anywhere at the moment, though this means callers can compute additional information, like the total node count for a block or loop, without needing out parameters by simply including such computations in the callback. I was motivated to do this by a want for additional insight into the optimization potential of loop inversion candidates, like whether a loop might also benefit from cloning because it contains bounds checks. Such information can be included in the callback to the complexity check.

In the near future, we might want to diverge from the number of nodes in a code segment as our definition of complexity and employ something more sophisticated. These changes should make that easy to try out.

@Copilot Copilot AI review requested due to automatic review settings July 24, 2025 22:26
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jul 24, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the complexity checking system in the JIT compiler to use callback functions instead of hardcoded node counting. The change allows for more flexible complexity computations while maintaining the current behavior of counting tree nodes as the default complexity metric.

Key changes:

  • Converted gtComplexityExceeds and related functions to use template parameters with callback functions
  • Moved complexity checking implementations from .cpp files to header files as templates
  • Updated all call sites to provide lambda functions that maintain existing node-counting behavior

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/coreclr/jit/compiler.h Updated function signatures to use template parameters for complexity callbacks
src/coreclr/jit/compiler.hpp Added template implementations of complexity checking functions
src/coreclr/jit/block.h Updated ComplexityExceeds method signatures to use template parameters
src/coreclr/jit/block.cpp Removed concrete implementations that were moved to header as templates
src/coreclr/jit/gentree.cpp Removed original gtComplexityExceeds implementation
src/coreclr/jit/optimizer.cpp Updated call site and removed optLoopComplexityExceeds implementation
src/coreclr/jit/objectalloc.cpp Updated call site with lambda callback
src/coreclr/jit/morph.cpp Updated call sites with lambda callbacks
src/coreclr/jit/loopcloning.cpp Updated call site with lambda callback
src/coreclr/jit/forwardsub.cpp Updated call sites with lambda callbacks
src/coreclr/jit/rangecheckcloning.cpp Updated call site with lambda callback that also updates external counter
Comments suppressed due to low confidence (1)

src/coreclr/jit/rangecheckcloning.cpp:447

  • [nitpick] The lambda parameter name 'tree' is inconsistent with other similar lambdas in the codebase that use 'node' as the parameter name.
            auto     countNode = [&actual](GenTree* tree) -> unsigned {

Copy link
Contributor

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

@amanasifkhalid
Copy link
Contributor Author

@dotnet/jit-contrib PTAL. No diffs. Thanks!

@amanasifkhalid amanasifkhalid enabled auto-merge (squash) July 25, 2025 15:01
@amanasifkhalid
Copy link
Contributor Author

/ba-g #118064

@amanasifkhalid amanasifkhalid merged commit e831ce8 into dotnet:main Jul 25, 2025
108 of 110 checks passed
@amanasifkhalid amanasifkhalid deleted the complexity-exceeds-helpers branch July 25, 2025 17:21
@github-actions github-actions bot locked and limited conversation to collaborators Aug 25, 2025
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