Skip to content

[Relax] Enhance unique block name generation with numeric suffixes#18554

Merged
tlopex merged 2 commits intoapache:mainfrom
guan404ming:unique-block-name-generation
Dec 10, 2025
Merged

[Relax] Enhance unique block name generation with numeric suffixes#18554
tlopex merged 2 commits intoapache:mainfrom
guan404ming:unique-block-name-generation

Conversation

@guan404ming
Copy link
Copy Markdown
Member

Why

Resolve todo in fuse_tir.cc by enhancing unique block name generation with numeric suffixes

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @guan404ming, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the block name deduplication process within the relax.transform.FuseTIR pass. The core improvement lies in the GetUniqueName utility, which now intelligently increments numeric suffixes in block names (e.g., name1 becomes name2 instead of name10), leading to more logical and predictable naming conventions for generated blocks. This resolves a known issue and enhances the robustness of the fusion pass.

Highlights

  • Improved Unique Name Generation: The GetUniqueName function in BlockNameDeduplicator has been updated to correctly handle numeric suffixes when generating unique block names, ensuring that names like "name1" are followed by "name2" instead of "name10".
  • Resolution of TODO: This change directly addresses and resolves a long-standing TODO comment in fuse_tir.cc that highlighted the need for proper numeric suffix detection during block renaming.
  • New Test Case: A dedicated test, test_block_name_numeric_suffix_deduplication, has been added to tests/python/relax/test_transform_fuse_tir.py to validate the new naming logic and ensure its correctness.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the unique block name generation in fuse_tir.cc to correctly handle numeric suffixes, resolving a TODO item. The implementation is sound and is accompanied by a new test case that verifies the improved behavior. My review includes a suggestion to further improve the robustness and readability of the C++ code by handling potential integer overflows and exceptions, and by clarifying the logic for generating new names.

Comment on lines 359 to 395
ffi::String GetUniqueName(const ffi::String& prefix) {
ffi::String unique_prefix = prefix;
auto it = name_count_.find(prefix);
while (name_count_.count(unique_prefix)) {
unique_prefix = prefix + "_" + std::to_string(++it->second);
std::string str_prefix = std::string(prefix);

// Find where the trailing digits start
size_t base_len = str_prefix.length();
while (base_len > 0 && std::isdigit(str_prefix[base_len - 1])) {
--base_len;
}

std::string base_name;
int start_num = 0;

if (base_len < str_prefix.length()) {
base_name = str_prefix.substr(0, base_len);
start_num = std::stoi(str_prefix.substr(base_len));
} else {
base_name = str_prefix;
}

// Check if the original name is available
ffi::String candidate = prefix;
if (!name_count_.count(candidate)) {
name_count_[candidate] = 0;
return candidate;
}

// Generate unique name by incrementing the numeric suffix
int counter = (start_num > 0) ? start_num + 1 : 1;
while (true) {
candidate = ffi::String(base_name + std::to_string(counter));
if (!name_count_.count(candidate)) {
name_count_[candidate] = 0;
return candidate;
}
++counter;
}
name_count_[unique_prefix] = 0;
return unique_prefix;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The new implementation is a great improvement and correctly handles numeric suffixes. However, there are a few areas where it could be made more robust and readable:

  1. Integer Overflow: int counter and start_num could overflow if a block name has a very large numeric suffix, leading to undefined behavior. Using long long and std::stoll would be safer.
  2. Unhandled Exception: std::stoi can throw std::out_of_range if the numeric suffix is too large to fit in an int, which would crash the program. This should be handled, for example with a try-catch block.
  3. Confusing Logic: The logic for initializing the counter, int counter = (start_num > 0) ? start_num + 1 : 1;, is a bit subtle. While correct, its intent is not immediately obvious. A more explicit check would improve readability and maintainability.

Here is a suggested refactoring that addresses these points:

  ffi::String GetUniqueName(const ffi::String& prefix) {
    std::string str_prefix = std::string(prefix);

    // Find where the trailing digits start
    size_t base_len = str_prefix.length();
    while (base_len > 0 && std::isdigit(str_prefix[base_len - 1])) {
      --base_len;
    }

    std::string base_name;
    long long start_num = 0;
    bool has_suffix = base_len < str_prefix.length();

    if (has_suffix) {
      base_name = str_prefix.substr(0, base_len);
      try {
        start_num = std::stoll(str_prefix.substr(base_len));
      } catch (const std::out_of_range&) {
        // Fallback: if the number is too large, treat the whole string as a base name.
        has_suffix = false;
        base_name = str_prefix;
      }
    } else {
      base_name = str_prefix;
    }

    // Check if the original name is available
    ffi::String candidate = prefix;
    if (!name_count_.count(candidate)) {
      name_count_[candidate] = 0;
      return candidate;
    }

    // Generate unique name by incrementing the numeric suffix
    long long counter = has_suffix ? start_num + 1 : 1;
    while (true) {
      candidate = ffi::String(base_name + std::to_string(counter));
      if (!name_count_.count(candidate)) {
        name_count_[candidate] = 0;
        return candidate;
      }
      ++counter;
      ICHECK_GT(counter, 0) << "Counter overflow when generating unique block name for prefix: "
                            << prefix;
    }
  }

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.

I think this review makes sense especially the second. Could you modify the code like that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make sense to me as well, I've updated it.

@guan404ming guan404ming marked this pull request as ready for review December 6, 2025 06:17
@guan404ming
Copy link
Copy Markdown
Member Author

cc @tlopex @mshr-h

Comment on lines 359 to 395
ffi::String GetUniqueName(const ffi::String& prefix) {
ffi::String unique_prefix = prefix;
auto it = name_count_.find(prefix);
while (name_count_.count(unique_prefix)) {
unique_prefix = prefix + "_" + std::to_string(++it->second);
std::string str_prefix = std::string(prefix);

// Find where the trailing digits start
size_t base_len = str_prefix.length();
while (base_len > 0 && std::isdigit(str_prefix[base_len - 1])) {
--base_len;
}

std::string base_name;
int start_num = 0;

if (base_len < str_prefix.length()) {
base_name = str_prefix.substr(0, base_len);
start_num = std::stoi(str_prefix.substr(base_len));
} else {
base_name = str_prefix;
}

// Check if the original name is available
ffi::String candidate = prefix;
if (!name_count_.count(candidate)) {
name_count_[candidate] = 0;
return candidate;
}

// Generate unique name by incrementing the numeric suffix
int counter = (start_num > 0) ? start_num + 1 : 1;
while (true) {
candidate = ffi::String(base_name + std::to_string(counter));
if (!name_count_.count(candidate)) {
name_count_[candidate] = 0;
return candidate;
}
++counter;
}
name_count_[unique_prefix] = 0;
return unique_prefix;
}
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.

I think this review makes sense especially the second. Could you modify the code like that?

@guan404ming guan404ming requested a review from tlopex December 9, 2025 06:35
Copy link
Copy Markdown
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

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

LGTM Thanks!

@tlopex tlopex merged commit 85a8770 into apache:main Dec 10, 2025
13 checks passed
@guan404ming guan404ming deleted the unique-block-name-generation branch December 10, 2025 10:26
@guan404ming
Copy link
Copy Markdown
Member Author

Thanks!

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