Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,31 @@ static Value vectorizeOperand(Value operand, VectorizationState &state) {
return nullptr;
}

/// Returns true if any vectorized loop IV drives more than one index.
static bool isIVMappedToMultipleIndices(
ArrayRef<Value> indices,
const DenseMap<Operation *, unsigned> &loopToVectorDim) {
for (auto &kvp : loopToVectorDim) {
AffineForOp forOp = cast<AffineForOp>(kvp.first);
// Find which indices are invariant w.r.t. this loop IV.
auto invariants =
affine::getInvariantAccesses(forOp.getInductionVar(), indices);
// Count how many vary (i.e. are not invariant).
unsigned nonInvariant = 0;
for (Value idx : indices)
if (!invariants.count(idx))
++nonInvariant;
// Bail if more than one index varies for this single loop IV.
if (nonInvariant > 1) {
LLVM_DEBUG(dbgs() << "[early‑vect] Bail out: IV "
<< forOp.getInductionVar() << " drives " << nonInvariant
<< " indices\n");
return true;
}
}
return false;
}

/// Vectorizes an affine load with the vectorization strategy in 'state' by
/// generating a 'vector.transfer_read' op with the proper permutation map
/// inferred from the indices of the load. The new 'vector.transfer_read' is
Expand Down Expand Up @@ -1217,6 +1242,9 @@ static Operation *vectorizeAffineLoad(AffineLoadOp loadOp,
indices.append(mapOperands.begin(), mapOperands.end());
}

if (isIVMappedToMultipleIndices(indices, state.vecLoopToVecDim))
return nullptr;

// Compute permutation map using the information of new vector loops.
auto permutationMap = makePermutationMap(state.builder.getInsertionBlock(),
indices, state.vecLoopToVecDim);
Expand Down Expand Up @@ -1262,6 +1290,9 @@ static Operation *vectorizeAffineStore(AffineStoreOp storeOp,
else
indices.append(mapOperands.begin(), mapOperands.end());

if (isIVMappedToMultipleIndices(indices, state.vecLoopToVecDim))
return nullptr;

// Compute permutation map using the information of new vector loops.
auto permutationMap = makePermutationMap(state.builder.getInsertionBlock(),
indices, state.vecLoopToVecDim);
Expand Down
31 changes: 31 additions & 0 deletions mlir/test/Dialect/Affine/SuperVectorize/vectorize_unsupported.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,34 @@ func.func @unparallel_loop_reduction_unsupported(%in: memref<256x512xf32>, %out:
}
return
}

// -----

#map = affine_map<(d0)[s0] -> (d0 mod s0)>
#map1 = affine_map<(d0)[s0] -> (d0 floordiv s0)>

func.func @iv_mapped_to_multiple_indices_unsupported(%arg0: index) -> memref<2x2xf32> {
%c2 = arith.constant 2 : index
%cst = arith.constant 1.0 : f32
%alloc = memref.alloc() : memref<2x2xf32>

affine.for %i = 0 to 4 {
%row = affine.apply #map1(%i)[%c2]
%col = affine.apply #map(%i)[%c2]
affine.store %cst, %alloc[%row, %col] : memref<2x2xf32>
}

return %alloc : memref<2x2xf32>
}

// CHECK: #[[$ATTR_0:.+]] = affine_map<(d0)[s0] -> (d0 floordiv s0)>
// CHECK: #[[$ATTR_1:.+]] = affine_map<(d0)[s0] -> (d0 mod s0)>

// CHECK-LABEL: func.func @iv_mapped_to_multiple_indices_unsupported(
// CHECK-SAME: %[[VAL_0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: index) -> memref<2x2xf32> {
// CHECK: %[[VAL_1:.*]] = arith.constant 2 : index
// CHECK: affine.for %[[VAL_4:.*]] = 0 to 4 {
// CHECK: %[[VAL_5:.*]] = affine.apply #[[$ATTR_0]](%[[VAL_4]]){{\[}}%[[VAL_1]]]
// CHECK: %[[VAL_6:.*]] = affine.apply #[[$ATTR_1]](%[[VAL_4]]){{\[}}%[[VAL_1]]]
// CHECK: }
// CHECK: }
Loading