Skip to content

Commit f5f5926

Browse files
committed
[MLIR] FlatAffineValueConstraints: Fix bug in mergeSymbolIds
This patch fixes a bug in implementation `mergeSymbolIds` where symbol identifiers were not unique after merging them. Asserts for checking uniqueness before and after the merge are also added. The asserts checking uniqueness after the merge fail without the fix on existing test cases. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D111958
1 parent 2ae67c9 commit f5f5926

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

mlir/include/mlir/Analysis/AffineStructures.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,9 @@ class FlatAffineValueConstraints : public FlatAffineConstraints {
884884
}
885885

886886
/// Merge and align symbols of `this` and `other` such that both get union of
887-
/// of symbols that are unique. Symbols with Value as `None` are considered
888-
/// to be inequal to all other symbols.
887+
/// of symbols that are unique. Symbols in `this` and `other` should be
888+
/// unique. Symbols with Value as `None` are considered to be inequal to all
889+
/// other symbols.
889890
void mergeSymbolIds(FlatAffineValueConstraints &other);
890891

891892
protected:

mlir/lib/Analysis/AffineStructures.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,46 @@ bool FlatAffineValueConstraints::areIdsAlignedWithOther(
448448
return areIdsAligned(*this, other);
449449
}
450450

451-
/// Checks if the SSA values associated with `cst`'s identifiers are unique.
452-
static bool LLVM_ATTRIBUTE_UNUSED
453-
areIdsUnique(const FlatAffineValueConstraints &cst) {
451+
/// Checks if the SSA values associated with `cst`'s identifiers in range
452+
/// [start, end) are unique.
453+
static bool LLVM_ATTRIBUTE_UNUSED areIdsUnique(
454+
const FlatAffineValueConstraints &cst, unsigned start, unsigned end) {
455+
456+
assert(start <= cst.getNumIds() && "Start position out of bounds");
457+
assert(end <= cst.getNumIds() && "End position out of bounds");
458+
459+
if (start >= end)
460+
return true;
461+
454462
SmallPtrSet<Value, 8> uniqueIds;
455-
for (auto val : cst.getMaybeValues()) {
463+
ArrayRef<Optional<Value>> maybeValues = cst.getMaybeValues();
464+
for (Optional<Value> val : maybeValues) {
456465
if (val.hasValue() && !uniqueIds.insert(val.getValue()).second)
457466
return false;
458467
}
459468
return true;
460469
}
461470

471+
/// Checks if the SSA values associated with `cst`'s identifiers are unique.
472+
static bool LLVM_ATTRIBUTE_UNUSED
473+
areIdsUnique(const FlatAffineConstraints &cst) {
474+
return areIdsUnique(cst, 0, cst.getNumIds());
475+
}
476+
477+
/// Checks if the SSA values associated with `cst`'s identifiers of kind `kind`
478+
/// are unique.
479+
static bool LLVM_ATTRIBUTE_UNUSED areIdsUnique(
480+
const FlatAffineValueConstraints &cst, FlatAffineConstraints::IdKind kind) {
481+
482+
if (kind == FlatAffineConstraints::IdKind::Dimension)
483+
return areIdsUnique(cst, 0, cst.getNumDimIds());
484+
if (kind == FlatAffineConstraints::IdKind::Symbol)
485+
return areIdsUnique(cst, cst.getNumDimIds(), cst.getNumDimAndSymbolIds());
486+
if (kind == FlatAffineConstraints::IdKind::Local)
487+
return areIdsUnique(cst, cst.getNumDimAndSymbolIds(), cst.getNumIds());
488+
llvm_unreachable("Unexpected IdKind");
489+
}
490+
462491
/// Merge and align the identifiers of A and B starting at 'offset', so that
463492
/// both constraint systems get the union of the contained identifiers that is
464493
/// dimension-wise and symbol-wise unique; both constraint systems are updated
@@ -592,10 +621,15 @@ static void turnSymbolIntoDim(FlatAffineValueConstraints *cst, Value id) {
592621
}
593622

594623
/// Merge and align symbols of `this` and `other` such that both get union of
595-
/// of symbols that are unique. Symbols with Value as `None` are considered
596-
/// to be inequal to all other symbols.
624+
/// of symbols that are unique. Symbols in `this` and `other` should be
625+
/// unique. Symbols with Value as `None` are considered to be inequal to all
626+
/// other symbols.
597627
void FlatAffineValueConstraints::mergeSymbolIds(
598628
FlatAffineValueConstraints &other) {
629+
630+
assert(areIdsUnique(*this, IdKind::Symbol) && "Symbol ids are not unique");
631+
assert(areIdsUnique(other, IdKind::Symbol) && "Symbol ids are not unique");
632+
599633
SmallVector<Value, 4> aSymValues;
600634
getValues(getNumDimIds(), getNumDimAndSymbolIds(), &aSymValues);
601635

@@ -606,7 +640,7 @@ void FlatAffineValueConstraints::mergeSymbolIds(
606640
// If the id is a symbol in `other`, then align it, otherwise assume that
607641
// it is a new symbol
608642
if (other.findId(aSymValue, &loc) && loc >= other.getNumDimIds() &&
609-
loc < getNumDimAndSymbolIds())
643+
loc < other.getNumDimAndSymbolIds())
610644
other.swapId(s, loc);
611645
else
612646
other.insertSymbolId(s - other.getNumDimIds(), aSymValue);
@@ -621,6 +655,8 @@ void FlatAffineValueConstraints::mergeSymbolIds(
621655

622656
assert(getNumSymbolIds() == other.getNumSymbolIds() &&
623657
"expected same number of symbols");
658+
assert(areIdsUnique(*this, IdKind::Symbol) && "Symbol ids are not unique");
659+
assert(areIdsUnique(other, IdKind::Symbol) && "Symbol ids are not unique");
624660
}
625661

626662
// Changes all symbol identifiers which are loop IVs to dim identifiers.

0 commit comments

Comments
 (0)