[Mlir-commits] [mlir] [mlir] Allow trailing digit for alias in AsmPrinter (PR #127993)

Hongren Zheng llvmlistbot at llvm.org
Wed Mar 5 23:20:28 PST 2025


================
@@ -1073,6 +1073,37 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
   return name;
 }
 
+unsigned AliasInitializer::uniqueAliasNameIndex(
+    StringRef alias, llvm::StringMap<unsigned> &nameCounts,
+    llvm::StringSet<llvm::BumpPtrAllocator &> &usedAliases) {
+  // Get nameIndex that will not generate conflicting name.
+  unsigned nameIndex = 0;
+  if (!usedAliases.count(alias)) {
+    usedAliases.insert(alias);
+  } else {
+    // Otherwise, we had a conflict - probe until we find a unique name.
+    SmallString<64> probeAlias(alias);
+    // alias with trailing digit will be printed as _N
+    if (isdigit(alias.back()))
+      probeAlias.push_back('_');
+    // nameCounts start from 1 because 0 is not printed in SymbolAlias.
+    if (nameCounts[probeAlias] == 0)
+      nameCounts[probeAlias] = 1;
+    // This is guaranteed to terminate (and usually in a single iteration)
+    // because it generates new names by incrementing nameCounts.
+    while (true) {
+      nameIndex = nameCounts[probeAlias]++;
+      probeAlias += llvm::utostr(nameIndex);
+      if (!usedAliases.count(probeAlias)) {
+        usedAliases.insert(probeAlias);
+        break;
+      }
+      probeAlias.resize(alias.size() + isdigit(alias.back()) ? 1 : 0);
----------------
ZenithalHourlyRate wrote:

This is part of the loop logic: every time the detection failed, `probeAlias` need to reset to `alias` and re-add the trailing `llvm::utostr(nameIndex)`

See also https://github.com/llvm/llvm-project/blob/87976ca45f4fa983ef92bf8f43a54472ec354e00/mlir/lib/IR/AsmPrinter.cpp#L1771-L1785

https://github.com/llvm/llvm-project/pull/127993


More information about the Mlir-commits mailing list