Skip to content

[mlir][GPU][transform] Add gpu_to_rocdl conversion pattern #146962

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

Merged
merged 2 commits into from
Jul 7, 2025
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
14 changes: 14 additions & 0 deletions mlir/include/mlir/Dialect/GPU/TransformOps/GPUTransformOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def ApplyGPUSubgroupReduceToNVVMConversionPatternsOp : Op<Transform_Dialect,
let assemblyFormat = "attr-dict";
}

def ApplyGPUToROCDLConversionPatternsOp : Op<Transform_Dialect,
"apply_conversion_patterns.gpu.gpu_to_rocdl",
[DeclareOpInterfaceMethods<ConversionPatternDescriptorOpInterface,
["verifyTypeConverter"]>]> {
let description = [{
Collects patterns that convert GPU dialect ops to ROCDL dialect ops. These
patterns require an "LLVMTypeConverter".
}];
let arguments = (ins StrAttr:$chipset);
let assemblyFormat = [{
`chipset` `=` $chipset attr-dict
}];
}

//===----------------------------------------------------------------------===//
// Apply...PatternsOp
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Dialect/GPU/TransformOps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ add_mlir_dialect_library(MLIRGPUTransformOps
# ConversionPatterns
MLIRNVGPUToNVVM
MLIRGPUToNVVMTransforms
MLIRGPUToROCDLTransforms
)
38 changes: 38 additions & 0 deletions mlir/lib/Dialect/GPU/TransformOps/GPUTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
#include "mlir/Conversion/GPUToNVVM/GPUToNVVMPass.h"
#include "mlir/Conversion/GPUToROCDL/GPUToROCDLPass.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Dialect/AMDGPU/IR/AMDGPUDialect.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/GPU/TransformOps/Utils.h"
#include "mlir/Dialect/GPU/Transforms/Passes.h"
#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/DeviceMappingInterface.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand All @@ -40,6 +42,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/InterleavedRange.h"
#include "llvm/Support/LogicalResult.h"
#include <type_traits>

using namespace mlir;
Expand Down Expand Up @@ -127,6 +130,41 @@ LogicalResult transform::ApplyGPUSubgroupReduceToNVVMConversionPatternsOp::
return success();
}

void transform::ApplyGPUToROCDLConversionPatternsOp::populatePatterns(
TypeConverter &typeConverter, RewritePatternSet &patterns) {
auto &llvmTypeConverter = static_cast<LLVMTypeConverter &>(typeConverter);
populateGpuMemorySpaceAttributeConversions(
llvmTypeConverter, [](AddressSpace space) {
switch (space) {
case AddressSpace::Global:
return ROCDL::ROCDLDialect::kGlobalMemoryAddressSpace;
case AddressSpace::Workgroup:
return ROCDL::ROCDLDialect::kSharedMemoryAddressSpace;
case AddressSpace::Private:
return ROCDL::ROCDLDialect::kPrivateMemoryAddressSpace;
}
llvm_unreachable("unknown address space enum value");
});
FailureOr<amdgpu::Chipset> maybeChipset =
amdgpu::Chipset::parse(getChipset());
assert(llvm::succeeded(maybeChipset) && "expected valid chipset");
populateGpuToROCDLConversionPatterns(
llvmTypeConverter, patterns, mlir::gpu::amd::Runtime::HIP, *maybeChipset);
}

LogicalResult
transform::ApplyGPUToROCDLConversionPatternsOp::verifyTypeConverter(
transform::TypeConverterBuilderOpInterface builder) {
FailureOr<amdgpu::Chipset> maybeChipset =
amdgpu::Chipset::parse(getChipset());
if (failed(maybeChipset)) {
return emitOpError("Invalid chipset name: " + getChipset());
}
if (builder.getTypeConverterType() != "LLVMTypeConverter")
return emitOpError("expected LLVMTypeConverter");
return success();
}

//===----------------------------------------------------------------------===//
// Apply...PatternsOp
//===----------------------------------------------------------------------===//s
Expand Down
2 changes: 2 additions & 0 deletions utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5505,13 +5505,15 @@ cc_library(
":GPUDialect",
":GPUToGPURuntimeTransforms",
":GPUToNVVMTransforms",
":GPUToROCDLTransforms",
":GPUTransformOpsIncGen",
":GPUTransforms",
":IR",
":LLVMCommonConversion",
":MemRefDialect",
":NVGPUDialect",
":NVVMDialect",
":ROCDLDialect",
":SCFDialect",
":Support",
":TransformDialect",
Expand Down
Loading