[Mlir-commits] [mlir] [mlir] Allow trailing digit for alias in AsmPrinter (PR #127993)
River Riddle
llvmlistbot at llvm.org
Wed Mar 5 23:15:41 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);
----------------
River707 wrote:
Can you hoist this size computation out of the loop?
https://github.com/llvm/llvm-project/pull/127993
More information about the Mlir-commits
mailing list