-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[mlir][gpu] Add subgroup_broadcast op
#152808
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,6 +171,38 @@ struct GPUSubgroupSizeOpToROCDL : ConvertOpToLLVMPattern<gpu::SubgroupSizeOp> { | |
| const amdgpu::Chipset chipset; | ||
| }; | ||
|
|
||
| static bool isSupportedReadLaneType(Type type) { | ||
| // read(first)lane also supports some vector types, but limit it for scalars | ||
| // for now. | ||
| return type.isInteger(16) || type.isInteger(32) || type.isInteger(64) || | ||
| isa<Float16Type, BFloat16Type, Float32Type, Float64Type, | ||
| LLVM::LLVMPointerType>(type); | ||
| } | ||
|
|
||
| struct GPUSubgroupBroadcastOpToROCDL | ||
| : public ConvertOpToLLVMPattern<gpu::SubgroupBroadcastOp> { | ||
| using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; | ||
|
|
||
| LogicalResult | ||
| matchAndRewrite(gpu::SubgroupBroadcastOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| Value src = adaptor.getSrc(); | ||
| if (!isSupportedReadLaneType(src.getType())) | ||
| return rewriter.notifyMatchFailure(op, "unsupported readlane type"); | ||
|
|
||
| if (adaptor.getBroadcastType() == gpu::BroadcastType::specific_lane) { | ||
| rewriter.replaceOpWithNewOp<ROCDL::ReadlaneOp>(op, src.getType(), src, | ||
| adaptor.getLane()); | ||
| } else { // first_active_lane or any_lane | ||
| // any_lane is lowered to readfirstlane too, to force value into scalar | ||
| // register. | ||
| rewriter.replaceOpWithNewOp<ROCDL::ReadfirstlaneOp>(op, src.getType(), | ||
|
||
| src); | ||
| } | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct GPUShuffleOpLowering : public ConvertOpToLLVMPattern<gpu::ShuffleOp> { | ||
| using ConvertOpToLLVMPattern<gpu::ShuffleOp>::ConvertOpToLLVMPattern; | ||
|
|
||
|
|
@@ -463,7 +495,8 @@ void mlir::populateGpuToROCDLConversionPatterns( | |
| // TODO: Add alignment for workgroup memory | ||
| patterns.add<GPUDynamicSharedMemoryOpLowering>(converter); | ||
|
|
||
| patterns.add<GPUShuffleOpLowering, GPULaneIdOpToROCDL>(converter); | ||
| patterns.add<GPUShuffleOpLowering, GPULaneIdOpToROCDL, | ||
| GPUSubgroupBroadcastOpToROCDL>(converter); | ||
| patterns.add<GPUSubgroupSizeOpToROCDL>(converter, chipset); | ||
|
|
||
| populateMathToROCDLConversionPatterns(converter, patterns); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // RUN: mlir-opt %s --loop-invariant-code-motion | FileCheck %s | ||
|
|
||
| func.func private @side_effect(%arg0 : f32, %arg1 : f32, %arg2 : f32) | ||
|
|
||
| // CHECK-LABEL: func @broadcast_hoisting | ||
| // CHECK-SAME: (%[[ARG:.*]]: f32, %[[IDX:.*]]: i32, {{.*}}: index) | ||
| func.func @broadcast_hoisting(%arg0 : f32, %arg1 : i32, %arg2 : index) { | ||
| %c0 = arith.constant 0 : index | ||
| %c1 = arith.constant 1 : index | ||
| // `any_lane` and `specific_lane` can be speculated across the control flow, but | ||
| // `first_active_lane` cannot as active lanes can change. | ||
| // CHECK: %[[V1:.*]] = gpu.subgroup_broadcast %[[ARG]], any_lane : f32 | ||
| // CHECK: %[[V2:.*]] = gpu.subgroup_broadcast %[[ARG]], specific_lane %[[IDX]] : f32 | ||
| // CHECK: scf.for | ||
| // CHECK: %[[V0:.*]] = gpu.subgroup_broadcast %[[ARG]], first_active_lane : f32 | ||
| // CHECK: func.call @side_effect(%[[V0]], %[[V1]], %[[V2]]) | ||
| scf.for %i = %c0 to %arg2 step %c1 { | ||
| %0 = gpu.subgroup_broadcast %arg0, first_active_lane : f32 | ||
| %1 = gpu.subgroup_broadcast %arg0, any_lane : f32 | ||
| %2 = gpu.subgroup_broadcast %arg0, specific_lane %arg1 : f32 | ||
| func.call @side_effect(%0, %1, %2) : (f32, f32, f32) -> () | ||
| } | ||
| func.return | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to make sure we don't need type mangling or duplication here - ex. I don't know if readfirstlane or readlane of <4 x i32> or <32 x i8> works natively
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
limited lowering to scalar types for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can always decompose vectors to the largest supported scalars, similar to subgroup_reduce