Avoid iterator invalidation when recursively computing pattern depth.

computeDepth calls itself recursively, which may insert into minPatternDepth. minPatternDepth is a DenseMap, which invalidates iterators on insertion, so this may lead to asan failures.

PiperOrigin-RevId: 270374203
This commit is contained in:
River Riddle 2019-09-20 16:29:44 -07:00 committed by A. Unique TensorFlower
parent 2797517ecf
commit 91125d33ed
1 changed files with 9 additions and 6 deletions

View File

@ -933,15 +933,15 @@ void OperationLegalizer::computeLegalizationGraphBenefit() {
// If a mapping for this operation does not exist, then this operation
// is always legal. Return 0 as the depth for a directly legal operation.
auto opPatternsIt = legalizerPatterns.find(op);
if (opPatternsIt == legalizerPatterns.end())
if (opPatternsIt == legalizerPatterns.end() || opPatternsIt->second.empty())
return 0u;
auto &minDepth = minPatternDepth[op];
if (opPatternsIt->second.empty())
return minDepth;
// Initialize the depth to the maximum value.
minDepth = std::numeric_limits<unsigned>::max();
unsigned minDepth = std::numeric_limits<unsigned>::max();
// Record this initial depth in case we encounter this op again when
// recursively computing the depth.
minPatternDepth.try_emplace(op, minDepth);
// Compute the depth for each pattern used to legalize this operation.
SmallVector<std::pair<RewritePattern *, unsigned>, 4> patternsByDepth;
@ -956,6 +956,9 @@ void OperationLegalizer::computeLegalizationGraphBenefit() {
minDepth = std::min(minDepth, depth);
}
// Update the pattern depth.
minPatternDepth[op] = minDepth;
// If the operation only has one legalization pattern, there is no need to
// sort them.
if (patternsByDepth.size() == 1)