Skip to content

Move some helpers from alias_analysis to allocation_utils#4556

Merged
wujingyue merged 1 commit intomainfrom
wjy/refactor
Jun 2, 2025
Merged

Move some helpers from alias_analysis to allocation_utils#4556
wujingyue merged 1 commit intomainfrom
wjy/refactor

Conversation

@wujingyue
Copy link
Collaborator

For #4554

@wujingyue wujingyue requested a review from Priya2698 June 1, 2025 22:18
@wujingyue
Copy link
Collaborator Author

!test

@github-actions
Copy link

github-actions bot commented Jun 1, 2025

Description

  • Moved layout mapping and compliance functions to allocation_utils.cpp

  • Updated alias_analysis.cpp to remove redundant functions

  • Added necessary includes in allocation_utils.cpp and allocation_utils.h


Changes walkthrough 📝

Relevant files
Refactoring
alias_analysis.cpp
Remove redundant layout functions                                               

csrc/alias_analysis.cpp

  • Removed mapInLayoutToOutRoot function
  • Removed contiguityIsCompliant and isCompliantWith functions
  • Removed unnecessary include of logical_domain_map.h
  • +0/-76   
    Enhancement
    allocation_utils.cpp
    Add layout mapping and compliance functions                           

    csrc/ir/allocation_utils.cpp

  • Added contiguityIsCompliant function
  • Added isCompliantWith function
  • Added mapInLayoutToOutRoot function
  • Added necessary includes for ir/utils.h and logical_domain_map.h
  • +62/-0   
    allocation_utils.h
    Declare layout functions                                                                 

    csrc/ir/allocation_utils.h

  • Added declarations for isCompliantWith and mapInLayoutToOutRoot
    functions
  • +20/-0   

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🧪 No relevant tests
    ⚡ Recommended focus areas for review

    Missing Code

    The functions mapInLayoutToOutRoot and isCompliantWith have been moved from alias_analysis.cpp to allocation_utils.cpp, but there are no corresponding updates or additions in the tests to validate these changes.

          Layout&& layout);
    
      EmptyAllocationAs empty_allocation_as_;
      AliasAnalysisResult& analysis_;
    };
    
    bool okToRelayout(
        const TensorView* tv,
        const Layout& new_layout,
        const EmptyAllocationAs empty_allocation_as) {
      if (empty_allocation_as == EmptyAllocationAs::kUndetermined &&
          !tv->hasAllocation()) {
        return true;
      }
    
      std::optional<Layout> old_layout = canonicalizeLayout(tv);
    Code Duplication

    The functions mapInLayoutToOutRoot and isCompliantWith are being moved from alias_analysis.cpp to allocation_utils.cpp, but they are being added back into allocation_utils.cpp without removing them from alias_analysis.cpp. This could lead to code duplication and maintenance issues.

    namespace {
    bool contiguityIsCompliant(
        const std::optional<bool>& actual,
        const std::optional<bool>& required) {
      if (actual == true && required == false) {
        return true;
      }
      return actual == required;
    }
    } // namespace
    
    bool isCompliantWith(const Layout& layout, const Layout& required) {
      if (layout.allocation_domain != required.allocation_domain) {
        // This can be relaxed by allowing broadcast dimensions to be ordered
        // differently.
        return false;
      }
    
      for (const auto i : arange(layout.size())) {
        if (!contiguityIsCompliant(layout.contiguity[i], required.contiguity[i])) {
          return false;
        }
      }
      return true;
    }
    
    std::optional<Layout> mapInLayoutToOutRoot(
        const std::optional<Layout>& in_layout,
        TensorView* in,
        TensorView* out) {
      if (!in_layout.has_value()) {
        return std::nullopt;
      }
    
      if (!ir_utils::computePermutation(
               in->getLogicalDomain(), in_layout->allocation_domain)
               .has_value()) {
        // Give up when `in`'s allocation domain is not an logical permutation. As
        // an extension, we could map in_alloc to in_logical and apply the inverse
        // mapping to out_root.
        return std::nullopt;
      }
    
      std::unordered_map<IterDomain*, IterDomain*> in_logical_to_out_root =
          PairwiseLogicalDomainMap(in, out).mapProducerToConsumer();
    
      Layout out_layout;
      for (auto&& [in_alloc_id, contiguity] :
           zip(in_layout->allocation_domain, in_layout->contiguity)) {
        IterDomain* out_root_id = getOrDefault(in_logical_to_out_root, in_alloc_id);
        if (out_root_id == nullptr) {
          // This can happen when in_alloc_id is of type reduction or squeezed out.
          continue;
        }
        out_layout.allocation_domain.push_back(out_root_id);
        out_layout.contiguity.push_back(contiguity);
      }
      return out_layout;
    }
    Missing Documentation

    The newly added functions isCompliantWith and mapInLayoutToOutRoot in allocation_utils.h lack detailed documentation. It is important to provide clear documentation for new functions to ensure they are used correctly.

    // Returns whether `layout` is compliant with `required`. This is
    // uni-directional. For example, `contiguity=[t,t]` is compliant with
    // `contiguity=[f,f]` but not vice versa.
    bool isCompliantWith(const Layout& layout, const Layout& required);
    
    // A helper function used to compute the perferred output layout. It computes
    // the mapping from `in_logical` to `out_root` and applies that mapping to
    // `preferred_in_layout`. For many ops, this function returns a good initial
    // preferred output layout for aliasing because it tries to preserve the input
    // layout. An op (e.g. ViewOp and SliceOp) that transforms root to logical
    // using expressions will have to modify this initial layout so its allocation
    // domain will be a function of its logical domain.
    //
    // Returns `nullopt` if computation fails, so the caller can handle things
    // conservatively.
    std::optional<Layout> mapInLayoutToOutRoot(

    @wujingyue wujingyue merged commit f3c0937 into main Jun 2, 2025
    52 of 53 checks passed
    @wujingyue wujingyue deleted the wjy/refactor branch June 2, 2025 23:30
    nsarka pushed a commit to nsarka/Fuser that referenced this pull request Jul 28, 2025
    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