-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[MLIR][XeGPU] Support order attribute and add pattern for vector.transpose in WgToSg Pass #165307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbpatel
wants to merge
14
commits into
llvm:main
Choose a base branch
from
nbpatel:xegpu_wg_sg_transpose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+387
−286
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2124bbf
Add pattern for transpose
nbpatel 245a723
Merge branch 'main' into xegpu_wg_sg_transpose
nbpatel 88ef6c9
Add pattern for 2D transpose
nbpatel cd34514
Merge branch 'main' into xegpu_wg_sg_transpose
nbpatel f69e709
support nD order
nbpatel fb29a2f
Add 1:N test case
nbpatel 10643dc
Merge branch 'main' into xegpu_wg_sg_transpose
nbpatel 205fdfd
Clean up tests
nbpatel fe2ee14
Clang-format
nbpatel c25cc3e
Merge branch 'main' into xegpu_wg_sg_transpose
nbpatel c7124c1
Fix test
nbpatel d628a22
Address feedback
nbpatel 44a8a88
Update comment
nbpatel 77df356
Merge branch 'main' into xegpu_wg_sg_transpose
nbpatel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,27 +280,82 @@ LayoutAttr::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError, | |
| FailureOr<SmallVector<Value>> | ||
| LayoutAttr::delinearizeId(OpBuilder &builder, Location loc, Value linearId) { | ||
|
|
||
| // TODO: handle order attribute | ||
| auto hasDefaultOrder = [&]() { | ||
| DenseI32ArrayAttr order = getOrder(); | ||
| return !order || isIdentityPermutation(llvm::to_vector_of<int64_t>( | ||
| llvm::reverse(order.asArrayRef()))); | ||
| }; | ||
| if (!hasDefaultOrder()) | ||
| return mlir::emitError(loc, "order attribute is currently not supported."); | ||
| SmallVector<int64_t> layout; | ||
| SmallVector<int64_t> sgLayoutInt; | ||
| if (isForWorkgroup()) { | ||
| layout = getEffectiveSgLayoutAsInt(); | ||
| sgLayoutInt = getEffectiveSgLayoutAsInt(); | ||
| } else if (isForSubgroup()) { | ||
| layout = getEffectiveLaneLayoutAsInt(); | ||
| sgLayoutInt = getEffectiveLaneLayoutAsInt(); | ||
| } else { | ||
| return failure(); | ||
| } | ||
| auto dims = llvm::map_to_vector(layout, [&](int64_t d) -> Value { | ||
| return builder.createOrFold<arith::ConstantIndexOp>(loc, d); | ||
| }); | ||
|
|
||
| return affine::delinearizeIndex(builder, loc, linearId, dims); | ||
| DenseI32ArrayAttr orderAttr = getOrder(); | ||
|
|
||
| // Handle order attribute | ||
| SmallVector<int64_t> order; | ||
| if (orderAttr && !orderAttr.empty()) { | ||
| order = llvm::to_vector( | ||
| llvm::map_range(orderAttr.asArrayRef(), | ||
| [](int32_t idx) { return static_cast<int64_t>(idx); })); | ||
| } else { | ||
| // Default order: [1, 0] for 2D (row-major), [2, 1, 0] for 3D, etc. | ||
| order = llvm::to_vector( | ||
| llvm::reverse(llvm::seq<int64_t>(0, sgLayoutInt.size()))); | ||
| } | ||
|
|
||
| if (order.size() != sgLayoutInt.size()) { | ||
| return failure(); | ||
| } | ||
|
|
||
| SmallVector<Value> result(sgLayoutInt.size()); | ||
| Value remaining = linearId; | ||
|
|
||
| /// Process dimensions in the order they appear in the order array | ||
| /// The first dimension in order is the fastest-changing | ||
| /// | ||
| /// Example walkthrough for linearId=22, sgLayout=[2,4,4], order=[2,1,0]: | ||
| /// | ||
| /// Initial: remaining=22, dimIdx = order[i], dimSize = sgLayout[dimIdx], | ||
| /// result=[?,?,?] | ||
| /// | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider add comment: dimIdx = order[i], dimSize = sgLayout[dimIdx] |
||
| /// i=0 (process columns, dimIdx=2, dimSize=4): | ||
| /// result[2] = 22 % 4 = 2 (column coordinate) | ||
| /// remaining = 22 / 4 = 5 (5 complete groups of 4 columns processed) | ||
| /// | ||
| /// i=1 (process rows, dimIdx=1, dimSize=4): | ||
| /// result[1] = 5 % 4 = 1 (row coordinate) | ||
| /// remaining = 5 / 4 = 1 (1 complete group of 4 rows processed) | ||
| /// | ||
| /// i=2 (process layers, dimIdx=0, dimSize=2): | ||
| /// result[0] = 1 % 2 = 1 (layer coordinate) | ||
| /// (no remaining update - last iteration) | ||
| /// | ||
| /// Final result: [1,1,2] = Layer 1, Row 1, Column 2 | ||
| for (size_t i = 0; i < order.size(); ++i) { | ||
| int64_t dimIdx = order[i]; | ||
| int64_t dimSize = sgLayoutInt[dimIdx]; | ||
|
|
||
| Value dimSizeVal = | ||
| builder.createOrFold<arith::ConstantIndexOp>(loc, dimSize); | ||
nbpatel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Extract the coordinate for this dimension using modulo operation | ||
| /// This gives us "how far within this dimension" we are | ||
| /// e.g., linearId=22, dimSize=4: 22 % 4 = 2 (we're at position 2 within | ||
| /// this dimension) | ||
| result[dimIdx] = | ||
| builder.createOrFold<index::RemUOp>(loc, remaining, dimSizeVal); | ||
|
|
||
| /// Update remaining for the next dimension by removing what we've already | ||
| /// processed. Division tells us "how many complete groups of this dimension | ||
| /// we've gone through" e.g., linearId=22, dimSize=4: 22 / 4 = 5 (we've | ||
| /// completed 5 groups of 4) Skip this for the last iteration since there's | ||
| /// no next dimension to process | ||
| if (i < order.size() - 1) { | ||
| remaining = | ||
| builder.createOrFold<index::DivUOp>(loc, remaining, dimSizeVal); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// Implements DistributeLayoutAttr::computeDistributedCoords to generate | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,32 @@ | ||
| // RUN: mlir-opt --test-xegpu-layout-interface --cse -split-input-file %s | FileCheck %s | ||
|
|
||
| //CHECk: #map = affine_map<()[s0] -> (s0 floordiv 8)> | ||
| gpu.module @test { | ||
| gpu.func @slice_attr() -> vector<128xindex> { | ||
| //CHECK: [[sgId:%.+]] = gpu.subgroup_id : index | ||
| //CHECK: [[IDY:%.+]] = affine.apply #map()[[[sgId]]] | ||
| //CHECK: [[c32:%.+]] = arith.constant 32 : index | ||
| //CHECK: [[LOCALY:%.+]] = index.mul [[IDY]], [[c32]] | ||
| //CHECK: [[c128:%.+]] = arith.constant 128 : index | ||
| //CHECK: [[MODY:%.+]] = index.remu [[LOCALY]], [[c128]] | ||
| //CHECK: [[BASE:%.+]] = vector.step : vector<32xindex> | ||
| //CHECK: [[CAST:%.+]] = vector.broadcast [[MODY]] : index to vector<32xindex> | ||
| //CHECK: [[ADD:%.+]] = arith.addi [[BASE]], [[CAST]] : vector<32xindex> | ||
| // CHECK-DAG: %[[SGID:.*]] = gpu.subgroup_id : index | ||
| // CHECK-DAG: %[[DIVU:.*]] = index.divu %[[SGID]], %[[C8:.*]] | ||
| // CHECK-DAG: %[[REMU:.*]] = index.remu %[[DIVU]], %[[C4:.*]] | ||
| // CHECK-DAG: %[[MUL:.*]] = index.mul %[[REMU]], %[[C32:.*]] | ||
| // CHECK-DAG: %[[MOD:.*]] = index.remu %[[MUL]], %[[C128:.*]] | ||
| // CHECK-DAG: %[[BASE:.*]] = vector.step : vector<32xindex> | ||
| // CHECK-DAG: %[[CAST:.*]] = vector.broadcast %[[MOD]] : index to vector<32xindex> | ||
| // CHECK-DAG: %[[ADD:.*]] = arith.addi %[[BASE]], %[[CAST]] : vector<32xindex> | ||
| %step = vector.step {layout_result_0 = #xegpu.slice<#xegpu.layout<sg_layout = [4, 8], sg_data = [32, 32]>, dims = [1]>}: vector<128xindex> | ||
| gpu.return %step : vector<128xindex> | ||
| } | ||
|
|
||
| gpu.func @nested_slice_attr() -> vector<128xindex> { | ||
| //CHECK: [[sgId:%.+]] = gpu.subgroup_id : index | ||
| //CHECK: [[IDY:%.+]] = affine.apply #map()[[[sgId]]] | ||
| //CHECK: [[c32:%.+]] = arith.constant 32 : index | ||
| //CHECK: [[LOCALY:%.+]] = index.mul [[IDY]], [[c32]] | ||
| //CHECK: [[c128:%.+]] = arith.constant 128 : index | ||
| //CHECK: [[MODY:%.+]] = index.remu [[LOCALY]], [[c128]] | ||
| //CHECK: [[BASE:%.+]] = vector.step : vector<32xindex> | ||
| //CHECK: [[CAST:%.+]] = vector.broadcast [[MODY]] : index to vector<32xindex> | ||
| //CHECK: [[ADD:%.+]] = arith.addi [[BASE]], [[CAST]] : vector<32xindex> | ||
| // CHECK-DAG: %[[SGID:.*]] = gpu.subgroup_id : index | ||
| // CHECK-DAG: %[[DIVU1:.*]] = index.divu %[[SGID]], %[[C1:.*]] | ||
| // CHECK-DAG: %[[DIVU2:.*]] = index.divu %[[DIVU1]], %[[C8:.*]] | ||
| // CHECK-DAG: %[[REMU:.*]] = index.remu %[[DIVU2]], %[[C4:.*]] | ||
| // CHECK-DAG: %[[MUL:.*]] = index.mul %[[REMU]], %[[C32:.*]] | ||
| // CHECK-DAG: %[[MOD:.*]] = index.remu %[[MUL]], %[[C128:.*]] | ||
| // CHECK-DAG: %[[BASE:.*]] = vector.step : vector<32xindex> | ||
| // CHECK-DAG: %[[CAST:.*]] = vector.broadcast %[[MOD]] : index to vector<32xindex> | ||
| // CHECK-DAG: %[[ADD:.*]] = arith.addi %[[BASE]], %[[CAST]] : vector<32xindex> | ||
| %0 = vector.step {layout_result_0 = #xegpu.slice<#xegpu.slice<#xegpu.layout<sg_layout = [4, 8, 1], sg_data = [32, 32, 1]>, dims = [2]>, dims = [1]>} : vector<128xindex> | ||
| gpu.return %0 : vector<128xindex> | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.