Skip to content

Replace optional<unique_ptr> with unique_ptr for heuristics#5998

Open
wujingyue wants to merge 3 commits intomainfrom
wjy/heur
Open

Replace optional<unique_ptr> with unique_ptr for heuristics#5998
wujingyue wants to merge 3 commits intomainfrom
wjy/heur

Conversation

@wujingyue
Copy link
Collaborator

Summary: std::optional<std::unique_ptr<T>> is redundant since unique_ptr can represent "no value" via nullptr.

Changes:

  • getMaybeHeuristicsFor / getMaybeHeuristicParams now return std::unique_ptr<T> (nullptr when unavailable).
  • Call sites: .has_value()!= nullptr, removed valueOrError for heuristics path.
  • FusionKernelRuntime constructor uses NVF_ERROR(heuristics_ != nullptr) after getMaybeHeuristicsFor.

Files: fusion_kernel_runtime.{h,cpp}, fusion_segmenter.{h,cpp}, fusion_executor_cache.cpp, benchmarks (heuristic_*.cpp, shape_inference.cpp).

Made with Cursor

wujingyue and others added 3 commits February 21, 2026 07:50
- getMaybeHeuristicsFor / getMaybeHeuristicParams return unique_ptr (nullptr = no value)
- Update call sites: .has_value() -> != nullptr, remove valueOrError for heuristics
- Use NVF_ERROR for heuristics_ check in FusionKernelRuntime

Co-authored-by: Cursor <cursoragent@cursor.com>
@wujingyue wujingyue requested review from mdavis36 and naoyam February 21, 2026 16:39
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 21, 2026

Greptile Summary

This PR refactors the heuristics API by removing the redundant std::optional<std::unique_ptr<T>> pattern and replacing it with std::unique_ptr<T>, using nullptr to represent "no value". The changes are:

  • getMaybeHeuristicsFor now returns std::unique_ptr<HeuristicParamsList> (instead of std::optional<std::unique_ptr<...>>)
  • getMaybeHeuristicParams now returns std::unique_ptr<HeuristicParams> (instead of std::optional<std::unique_ptr<...>>)
  • Call sites updated: .has_value()!= nullptr, .value() removed
  • FusionKernelRuntime constructor uses NVF_ERROR(heuristics_ != nullptr) instead of valueOrError
  • Benchmark files updated to use nullptr checks

The refactoring is straightforward and improves code clarity by eliminating the double-indirection pattern.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The refactoring is mechanical and straightforward - replacing std::optional<std::unique_ptr<T>> with std::unique_ptr<T> is a well-understood C++ pattern improvement. All call sites have been consistently updated, and the semantics remain identical (nullptr represents "no value" in both cases)
  • No files require special attention

Important Files Changed

Filename Overview
csrc/runtime/fusion_kernel_runtime.h Updated getMaybeHeuristicsFor signature to return std::unique_ptr<HeuristicParamsList> instead of std::optional<std::unique_ptr<...>>, with updated documentation
csrc/runtime/fusion_kernel_runtime.cpp Refactored to return nullptr instead of std::nullopt, replaced valueOrError with direct nullptr check at constructor callsite
csrc/fusion_segmenter.h Updated getMaybeHeuristicParams signature to return std::unique_ptr<HeuristicParams> with updated documentation
csrc/fusion_segmenter.cpp Changed return type and replaced std::nullopt with nullptr
csrc/runtime/fusion_executor_cache.cpp Simplified lambda to directly assign return value and check for nullptr instead of checking .has_value() and extracting with .value()
benchmarks/cpp/heuristic_cache.cpp Updated assertions to use != nullptr instead of .has_value()
benchmarks/cpp/heuristic_lookup.cpp Updated assertions to use != nullptr instead of .has_value()
benchmarks/cpp/shape_inference.cpp Updated assertions to use != nullptr instead of .has_value()

Last reviewed commit: 2558dc5

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link

Description

  • Replace std::optional<std::unique_ptr<T>> with std::unique_ptr<T> for heuristics

  • Update function signatures: getMaybeHeuristicsFor and getMaybeHeuristicParams now return unique_ptr directly

  • Modify call sites: .has_value() checks changed to != nullptr, removed .valueOrError() calls

  • Add null pointer validation with NVF_ERROR(heuristics_ != nullptr) in FusionKernelRuntime constructor

Changes walkthrough

Relevant files
Enhancement
heuristic_cache.cpp
Update heuristic cache benchmark null checks                         

benchmarks/cpp/heuristic_cache.cpp

  • Changed .has_value() to != nullptr checks in benchmark functions
  • Updated NVF_ERROR assertions to use null pointer comparison
  • +2/-2     
    heuristic_lookup.cpp
    Update heuristic lookup benchmark null checks                       

    benchmarks/cpp/heuristic_lookup.cpp

  • Changed .has_value() to != nullptr checks in benchmark functions
  • Updated NVF_ERROR assertions to use null pointer comparison
  • +2/-2     
    shape_inference.cpp
    Update shape inference benchmark null checks                         

    benchmarks/cpp/shape_inference.cpp

  • Changed .has_value() to != nullptr checks in shape inference functions
  • Updated NVF_ERROR assertions to use null pointer comparison
  • +2/-2     
    fusion_segmenter.cpp
    Update getMaybeHeuristicParams return type and null handling

    csrc/fusion_segmenter.cpp

  • Changed return type from std::optional> to std::unique_ptr
  • Changed return std::nullopt; to return nullptr; when scheduling
    unavailable
  • +3/-3     
    fusion_executor_cache.cpp
    Simplify kernel runtime reuse logic                                           

    csrc/runtime/fusion_executor_cache.cpp

  • Simplified kernel runtime reuse logic to use direct null pointer check
  • Removed unnecessary optional wrapping and unwrapping of heuristics
  • +2/-6     
    fusion_kernel_runtime.cpp
    Update getMaybeHeuristicParams return type and constructor validation

    csrc/runtime/fusion_kernel_runtime.cpp

  • Changed return type from std::optional> to std::unique_ptr
  • Updated constructor to use direct null pointer check instead of
    valueOrError
  • Added NVF_ERROR(heuristics_ != nullptr) validation
  • Changed return std::nullopt; to return nullptr; when heuristics
    unavailable
  • +11/-13 
    fusion_segmenter.h
    Update SegmentedGroup getMaybeHeuristicParams signature   

    csrc/fusion_segmenter.h

  • Updated function signature from std::optional> to std::unique_ptr
  • Updated documentation to reflect nullptr instead of nullopt return
  • +2/-2     
    fusion_kernel_runtime.h
    Update FusionKernelRuntime getMaybeHeuristicsFor signature

    csrc/runtime/fusion_kernel_runtime.h

  • Updated function signature from std::optional> to std::unique_ptr
  • Updated documentation to reflect nullptr instead of nullopt return
  • +3/-3     

    PR Reviewer Guide

    Here are some key observations to aid the review process:

    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review
    Logic Error in Kernel Reuse

    In the kernel runtime reuse logic, the lambda captures new_heuristics by reference but then assigns to it inside the lambda. However, the assignment new_heuristics = kernel_runtime->getMaybeHeuristicsFor(...) happens after the return statement, making it unreachable. This breaks the kernel reuse functionality as new_heuristics will never be populated with the found runtime's heuristics.

    if (!isOptionDisabled(DisableOption::KernelReuse)) {
      FUSER_PERF_SCOPE("FusionExecutorCache::getKernelRuntimeFor::reuseKRT");
      auto runtime_it = std::find_if(
          kernel_runtimes.begin(),
          kernel_runtimes.end(),
          [&args, &new_heuristics, &forced_index_type](auto& kernel_runtime) {
            new_heuristics =
                kernel_runtime->getMaybeHeuristicsFor(args, forced_index_type);
            return new_heuristics != nullptr;
          });

    Base automatically changed from wjy/optional to main February 23, 2026 18:07
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants