-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[MLIR][ROCDL] Add math.clampf -> rocdl.fmed3 conversion #163259
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
krzysz00
merged 10 commits into
llvm:main
from
keshavvinayak01:users/keshavvinayak01/clampf-med3-math-to-rocdl
Oct 14, 2025
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f5cf021
[MLIR][ROCDL] Added math.clampf -> rocdl.fmed3 conversion
keshavvinayak01 92bcb55
Removed incorrect formatting
keshavvinayak01 49b08f9
Corrected pass option
keshavvinayak01 636ef8d
Addressed Comments by Krzysztof:
keshavvinayak01 767c0ac
Set chipset default value to empty
keshavvinayak01 5b197b7
Pattern should only apply to f16/f32 types; added reject lit for bf16
keshavvinayak01 f25ec27
Formatting lit test
keshavvinayak01 9b50b9d
Moved GFX9+ condition to within addChipsetDependentPatterns
keshavvinayak01 a9f2eff
Merge branch 'main' into users/keshavvinayak01/clampf-med3-math-to-rocdl
keshavvinayak01 61af07c
Added valid default value for chipset to pass
keshavvinayak01 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
#include "mlir/Conversion/GPUCommon/GPUCommonPass.h" | ||
#include "mlir/Conversion/LLVMCommon/LoweringOptions.h" | ||
#include "mlir/Conversion/LLVMCommon/TypeConverter.h" | ||
#include "mlir/Conversion/LLVMCommon/VectorPattern.h" | ||
#include "mlir/Dialect/AMDGPU/Utils/Chipset.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h" | ||
|
@@ -42,8 +44,65 @@ static void populateOpPatterns(const LLVMTypeConverter &converter, | |
f32ApproxFunc, f16Func); | ||
} | ||
|
||
struct ClampFOpConversion final | ||
: public ConvertOpToLLVMPattern<math::ClampFOp> { | ||
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; | ||
ClampFOpConversion(const LLVMTypeConverter &converter, | ||
amdgpu::Chipset chipset) | ||
: ConvertOpToLLVMPattern<math::ClampFOp>(converter), chipset(chipset) {} | ||
|
||
LogicalResult | ||
matchAndRewrite(math::ClampFOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
// Only f16 and f32 types are supported by fmed3 | ||
Type opTy = op.getType(); | ||
auto resultType = getTypeConverter()->convertType(opTy); | ||
|
||
if (auto vectorType = dyn_cast<VectorType>(opTy)) { | ||
opTy = vectorType.getElementType(); | ||
} | ||
|
||
if (!opTy.isF16() && !opTy.isF32()) { | ||
keshavvinayak01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return rewriter.notifyMatchFailure( | ||
op, "fmed3 only supports f16 and f32 types"); | ||
} | ||
|
||
// Handle multi-dimensional vectors (converted to LLVM arrays) | ||
if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(resultType)) { | ||
// Handle multi-dimensional vectors (converted to LLVM arrays) | ||
return LLVM::detail::handleMultidimensionalVectors( | ||
op.getOperation(), adaptor.getOperands(), *getTypeConverter(), | ||
[&](Type llvm1DVectorTy, ValueRange operands) -> Value { | ||
typename math::ClampFOp::Adaptor adaptor(operands); | ||
return rewriter.create<ROCDL::FMed3Op>( | ||
keshavvinayak01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
op.getLoc(), llvm1DVectorTy, adaptor.getValue(), | ||
adaptor.getMin(), adaptor.getMax()); | ||
}, | ||
rewriter); | ||
} | ||
|
||
// Handle 1D vectors and scalars directly | ||
rewriter.replaceOpWithNewOp<ROCDL::FMed3Op>(op, op.getType(), op.getValue(), | ||
op.getMin(), op.getMax()); | ||
return success(); | ||
} | ||
|
||
amdgpu::Chipset chipset; | ||
}; | ||
|
||
void addChipsetDependentPatterns(const LLVMTypeConverter &converter, | ||
keshavvinayak01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
RewritePatternSet &patterns, | ||
amdgpu::Chipset chipset) { | ||
|
||
// V_MED3_F16/F32 only exists in gfx9+ architectures | ||
if (chipset.majorVersion >= 9) { | ||
patterns.add<ClampFOpConversion>(converter, chipset); | ||
} | ||
} | ||
|
||
void mlir::populateMathToROCDLConversionPatterns( | ||
const LLVMTypeConverter &converter, RewritePatternSet &patterns) { | ||
const LLVMTypeConverter &converter, RewritePatternSet &patterns, | ||
amdgpu::Chipset chipset) { | ||
// Handled by mathToLLVM: math::AbsIOp | ||
// Handled by mathToLLVM: math::AbsFOp | ||
// Handled by mathToLLVM: math::CopySignOp | ||
|
@@ -118,15 +177,17 @@ void mlir::populateMathToROCDLConversionPatterns( | |
// worth creating a separate pass for it. | ||
populateOpPatterns<arith::RemFOp>(converter, patterns, "__ocml_fmod_f32", | ||
"__ocml_fmod_f64", "__ocml_fmod_f16"); | ||
|
||
addChipsetDependentPatterns(converter, patterns, chipset); | ||
} | ||
|
||
namespace { | ||
struct ConvertMathToROCDLPass | ||
: public impl::ConvertMathToROCDLBase<ConvertMathToROCDLPass> { | ||
ConvertMathToROCDLPass() = default; | ||
struct ConvertMathToROCDLPass final | ||
: impl::ConvertMathToROCDLBase<ConvertMathToROCDLPass> { | ||
using impl::ConvertMathToROCDLBase< | ||
ConvertMathToROCDLPass>::ConvertMathToROCDLBase; | ||
|
||
void runOnOperation() override; | ||
}; | ||
} // namespace | ||
|
||
void ConvertMathToROCDLPass::runOnOperation() { | ||
auto m = getOperation(); | ||
|
@@ -135,10 +196,20 @@ void ConvertMathToROCDLPass::runOnOperation() { | |
RewritePatternSet patterns(&getContext()); | ||
LowerToLLVMOptions options(ctx, DataLayout(m)); | ||
LLVMTypeConverter converter(ctx, options); | ||
populateMathToROCDLConversionPatterns(converter, patterns); | ||
|
||
// Only populate chipset-dependent patterns if chipset is specified | ||
if (!chipset.empty()) { | ||
FailureOr<amdgpu::Chipset> maybeChipset = amdgpu::Chipset::parse(chipset); | ||
if (failed(maybeChipset)) { | ||
return signalPassFailure(); | ||
} | ||
populateMathToROCDLConversionPatterns(converter, patterns, *maybeChipset); | ||
} | ||
Comment on lines
+201
to
+207
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. This is wrong, the call to 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. |
||
|
||
ConversionTarget target(getContext()); | ||
target.addLegalDialect<BuiltinDialect, func::FuncDialect, | ||
vector::VectorDialect, LLVM::LLVMDialect>(); | ||
target | ||
.addLegalDialect<BuiltinDialect, func::FuncDialect, vector::VectorDialect, | ||
LLVM::LLVMDialect, ROCDL::ROCDLDialect>(); | ||
target.addIllegalOp<LLVM::CosOp, LLVM::ExpOp, LLVM::Exp2Op, LLVM::FAbsOp, | ||
LLVM::FCeilOp, LLVM::FFloorOp, LLVM::FRemOp, LLVM::LogOp, | ||
LLVM::Log10Op, LLVM::Log2Op, LLVM::PowOp, LLVM::SinOp, | ||
|
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.