diff --git a/mlir/include/mlir/IR/AffineMap.h b/mlir/include/mlir/IR/AffineMap.h --- a/mlir/include/mlir/IR/AffineMap.h +++ b/mlir/include/mlir/IR/AffineMap.h @@ -243,19 +243,19 @@ /// Returns a new AffineMap with the same number of dims and symbols and one /// less result at `pos`, dropped. - AffineMap dropResult(int64_t pos) { - auto exprs = llvm::to_vector<4>(getResults()); - exprs.erase(exprs.begin() + pos); - return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext()); - } + AffineMap dropResult(int64_t pos) { return dropResults({pos}); } // Returns a new AffineMap with the same number of dims and symbols, but all // positions in `positions` dropped from results. AffineMap dropResults(ArrayRef positions) { - AffineMap resultMap = *this; - for (int64_t pos : positions) - resultMap = resultMap.dropResult(pos); - return resultMap; + SmallVector reverse_sorted_positions = llvm::to_vector(positions); + std::sort(reverse_sorted_positions.rbegin(), + reverse_sorted_positions.rend()); + + auto exprs = llvm::to_vector<4>(getResults()); + for (int64_t pos : reverse_sorted_positions) + exprs.erase(exprs.begin() + pos); + return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext()); } /// Returns a new AffineMap with the same number of dims and symbols and an