diff --git a/mlir/lib/Analysis/FlatLinearValueConstraints.cpp b/mlir/lib/Analysis/FlatLinearValueConstraints.cpp --- a/mlir/lib/Analysis/FlatLinearValueConstraints.cpp +++ b/mlir/lib/Analysis/FlatLinearValueConstraints.cpp @@ -1054,37 +1054,46 @@ } /// Merge and align symbols of `this` and `other` such that both get union of -/// of symbols that are unique. Symbols in `this` and `other` should be -/// unique. Symbols with Value as `None` are considered to be inequal to all -/// other symbols. +/// symbols that are unique. The symbols will be ordered with the symbols from +/// `this` appearing first, then the symbols from `other`. Symbols in `this` and +/// `other` should be unique. Symbols with Value as `None` are considered to be +/// inequal to all other symbols. void FlatLinearValueConstraints::mergeSymbolVars( FlatLinearValueConstraints &other) { assert(areVarsUnique(*this, VarKind::Symbol) && "Symbol vars are not unique"); assert(areVarsUnique(other, VarKind::Symbol) && "Symbol vars are not unique"); - SmallVector aSymValues; - getValues(getNumDimVars(), getNumDimAndSymbolVars(), &aSymValues); + auto maybeSymValues = getMaybeValues(presburger::VarKind::Symbol); // Merge symbols: merge symbols into `other` first from `this`. unsigned s = other.getNumDimVars(); - for (Value aSymValue : aSymValues) { - unsigned loc; - // If the var is a symbol in `other`, then align it, otherwise assume that - // it is a new symbol - if (other.findVar(aSymValue, &loc) && loc >= other.getNumDimVars() && - loc < other.getNumDimAndSymbolVars()) - other.swapVar(s, loc); - else - other.insertSymbolVar(s - other.getNumDimVars(), aSymValue); + for (std::optional maybeValue : maybeSymValues) { + if (maybeValue.has_value()) { + unsigned loc; + // If the var is a symbol in `other`, then align it, otherwise assume that + // it is a new symbol + if (other.findVar(*maybeValue, &loc) && loc >= other.getNumDimVars() && + loc < other.getNumDimAndSymbolVars()) + other.swapVar(s, loc); + else + other.insertSymbolVar(s - other.getNumDimVars(), *maybeValue); + } else + // if the symbol has no Value attached, we have to add it as a new symbol + // to `other` + other.insertSymbolVar(s - other.getNumDimVars()); s++; } // Symbols that are in other, but not in this, are added at the end. for (unsigned t = other.getNumDimVars() + getNumSymbolVars(), e = other.getNumDimAndSymbolVars(); - t < e; t++) - insertSymbolVar(getNumSymbolVars(), other.getValue(t)); + t < e; t++) { + if (other.hasValue(t)) + insertSymbolVar(getNumSymbolVars(), other.getValue(t)); + else + insertSymbolVar(getNumSymbolVars()); + } assert(getNumSymbolVars() == other.getNumSymbolVars() && "expected same number of symbols");