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 @@ -555,18 +555,40 @@ llvm::ScopedHashTable usedNames; llvm::BumpPtrAllocator usedNameAllocator; + // If testing that SSA IDs are being used in tests directly. +#ifdef MLIR_TEST_SSA_ID_SEQUENCE_ASSUMED + static constexpr unsigned initID() { + // This could be more random seed, but given the intention is just to + // ensure 0 is not assumged, should suffice. + return __LINE__ + 42; + } + + static unsigned incrementID(unsigned &id) { + unsigned int ij = id; + // Use MINSTD to increment (period 2^31-1). + id = (16807l * ij) % 2147483647; + return ij; + }; +#else + static constexpr unsigned initID() { return 0; } + + static unsigned incrementID(unsigned &id) { return id++; } +#endif + /// This is the next value ID to assign in numbering. - unsigned nextValueID = 0; + unsigned nextValueID; /// This is the next ID to assign to a region entry block argument. - unsigned nextArgumentID = 0; + unsigned nextArgumentID; /// This is the next ID to assign when a name conflict is detected. - unsigned nextConflictID = 0; + unsigned nextConflictID; }; } // end anonymous namespace SSANameState::SSANameState( Operation *op, - DialectInterfaceCollection &interfaces) { + DialectInterfaceCollection &interfaces) + : nextValueID(initID()), nextArgumentID(initID()), + nextConflictID(initID()) { llvm::ScopedHashTable::ScopeTy usedNamesScope(usedNames); numberValuesInOp(*op, interfaces); @@ -704,7 +726,7 @@ continue; if (isEntryBlock) { specialNameBuffer.resize(strlen("arg")); - specialName << nextArgumentID++; + specialName << incrementID(nextArgumentID); } setValueName(arg, specialName.str()); } @@ -740,7 +762,7 @@ // If the first result wasn't numbered, give it a default number. if (valueIDs.try_emplace(resultBegin, nextValueID).second) - ++nextValueID; + incrementID(nextValueID); // If this operation has multiple result groups, mark it. if (resultGroups.size() != 1) { @@ -790,7 +812,7 @@ void SSANameState::setValueName(Value value, StringRef name) { // If the name is empty, the value uses the default numbering. if (name.empty()) { - valueIDs[value] = nextValueID++; + valueIDs[value] = incrementID(nextValueID); return; } @@ -846,7 +868,7 @@ probeName.push_back('_'); while (true) { probeName.resize(name.size() + 1); - probeName += llvm::utostr(nextConflictID++); + probeName += llvm::utostr(incrementID(nextConflictID)); if (!usedNames.count(probeName)) { name = StringRef(probeName).copy(usedNameAllocator); break;