diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -842,54 +842,57 @@ llvm::SaveAndRestore argumentIDSaver(nextArgumentID); llvm::SaveAndRestore conflictIDSaver(nextConflictID); - // The context includes nextValueID, nextArgumentID, nextConflictID and scoped - // HashTable. - using hashTableScopeTy = llvm::ScopedHashTable::ScopeTy; - // A namingContext carries the information inherits from parent region. - using namingContext = - std::tuple; - // Allocator for hashTableScopeTy + // The naming context includes `nextValueID`, `nextArgumentID`, + // `nextConflictID` and `usedNames` scoped HashTable. This information is + // carried from the parent region. + using UsedNamesScopeTy = llvm::ScopedHashTable::ScopeTy; + using NamingContext = + std::tuple; + + // Allocator for UsedNamesScopeTy llvm::BumpPtrAllocator allocator; - SmallVector nameContext; + // Add a scope for the top level operation. + auto *topLevelNamesScope = + new (allocator.Allocate()) UsedNamesScopeTy(usedNames); + + SmallVector nameContext; for (Region ®ion : op->getRegions()) nameContext.push_back(std::make_tuple(®ion, nextValueID, nextArgumentID, - nextConflictID, nullptr)); + nextConflictID, topLevelNamesScope)); numberValuesInOp(*op, interfaces); while (!nameContext.empty()) { Region *region; - hashTableScopeTy *parentScope; + UsedNamesScopeTy *parentScope; std::tie(region, nextValueID, nextArgumentID, nextConflictID, parentScope) = nameContext.pop_back_val(); // When we switch from one subtree to another, pop the scopes(needless) // until the parent scope. while (usedNames.getCurScope() != parentScope) { - usedNames.getCurScope()->~hashTableScopeTy(); + usedNames.getCurScope()->~UsedNamesScopeTy(); assert((usedNames.getCurScope() != nullptr || parentScope == nullptr) && "top level parentScope must be a nullptr"); } // Add a scope for the current region. - auto *curNamesScope = allocator.Allocate(); - new (curNamesScope) hashTableScopeTy(usedNames); + auto *curNamesScope = new (allocator.Allocate()) + UsedNamesScopeTy(usedNames); numberValuesInRegion(*region, interfaces); - for (Block &block : *region) { - for (Operation &op : block) - for (Region ®ion : op.getRegions()) - nameContext.push_back(std::make_tuple(®ion, nextValueID, - nextArgumentID, nextConflictID, - curNamesScope)); - } + for (Operation &op : region->getOps()) + for (Region ®ion : op.getRegions()) + nameContext.push_back(std::make_tuple(®ion, nextValueID, + nextArgumentID, nextConflictID, + curNamesScope)); } // Manually remove all the scopes. while (usedNames.getCurScope() != nullptr) - usedNames.getCurScope()->~hashTableScopeTy(); + usedNames.getCurScope()->~UsedNamesScopeTy(); } void SSANameState::printValueID(Value value, bool printResultNo,