Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 18 additions & 14 deletions mlir/lib/Dialect/GPU/Utils/DistributionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/IR/Value.h"
#include "llvm/ADT/DenseMap.h"

#include <numeric>

Expand Down Expand Up @@ -57,26 +58,29 @@ WarpDistributionPattern::moveRegionToNewWarpOpAndAppendReturns(
warpOp.getResultTypes().end());
auto yield = cast<gpu::YieldOp>(
warpOp.getBodyRegion().getBlocks().begin()->getTerminator());
llvm::SmallSetVector<Value, 32> yieldValues(yield.getOperands().begin(),
yield.getOperands().end());
SmallVector<Value> yieldValues(yield.getOperands().begin(),
yield.getOperands().end());
llvm::SmallDenseMap<Value, unsigned> indexLookup;
// Record the value -> first index mapping for faster lookup.
for (auto [i, v] : llvm::enumerate(yieldValues)) {
if (!indexLookup.count(v))
indexLookup[v] = i;
}

for (auto [value, type] : llvm::zip_equal(newYieldedValues, newReturnTypes)) {
if (yieldValues.insert(value)) {
// If the value already exists in the yield, don't create a new output.
if (indexLookup.count(value)) {
indices.push_back(indexLookup[value]);
} else {
// If the value is new, add it to the yield and to the types.
yieldValues.push_back(value);
types.push_back(type);
indices.push_back(yieldValues.size() - 1);
} else {
// If the value already exit the region don't create a new output.
for (auto [idx, yieldOperand] :
llvm::enumerate(yieldValues.getArrayRef())) {
if (yieldOperand == value) {
indices.push_back(idx);
break;
}
}
}
}
yieldValues.insert_range(newYieldedValues);

WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndReplaceReturns(
rewriter, warpOp, yieldValues.getArrayRef(), types);
rewriter, warpOp, yieldValues, types);
rewriter.replaceOp(warpOp,
newWarpOp.getResults().take_front(warpOp.getNumResults()));
return newWarpOp;
Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/Vector/vector-warp-distribute.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1803,3 +1803,24 @@ func.func @warp_propagate_nd_write(%laneid: index, %dest: memref<4x1024xf32>) {
// CHECK-DIST-AND-PROP: %[[IDS:.+]]:2 = affine.delinearize_index %{{.*}} into (4, 8) : index, index
// CHECK-DIST-AND-PROP: %[[INNER_ID:.+]] = affine.apply #map()[%[[IDS]]#1]
// CHECK-DIST-AND-PROP: vector.transfer_write %[[W]], %{{.*}}[%[[IDS]]#0, %[[INNER_ID]]] {{.*}} : vector<1x128xf32>

// -----
func.func @warp_propagate_duplicated_operands_in_yield(%laneid: index) {
%r:3 = gpu.warp_execute_on_lane_0(%laneid)[32] -> (vector<1xf32>, vector<1xf32>, vector<1xf32>) {
%0 = "some_def"() : () -> (vector<32xf32>)
%1 = "some_other_def"() : () -> (vector<32xf32>)
%2 = math.exp %1 : vector<32xf32>
gpu.yield %2, %0, %0 : vector<32xf32>, vector<32xf32>, vector<32xf32>
}
"some_use"(%r#0) : (vector<1xf32>) -> ()
return
}

// CHECK-PROP-LABEL : func.func @warp_propagate_duplicated_operands_in_yield(
// CHECK-PROP : %[[W:.*]] = gpu.warp_execute_on_lane_0(%{{.*}})[32] -> (vector<1xf32>) {
// CHECK-PROP : %{{.*}} = "some_def"() : () -> vector<32xf32>
// CHECK-PROP : %[[T3:.*]] = "some_other_def"() : () -> vector<32xf32>
// CHECK-PROP : gpu.yield %[[T3]] : vector<32xf32>
// CHECK-PROP : }
// CHECK-PROP : %[T1:.*] = math.exp %[[W]] : vector<1xf32>
// CHECK-PROP : "some_use"(%[[T1]]) : (vector<1xf32>) -> ()
Loading