-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][nvgpu] Add nvgpu.rcp
OP
#100965
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
[mlir][nvgpu] Add nvgpu.rcp
OP
#100965
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-nvgpu Author: None (Observer007) ChangesThis new op is a simple wrapper op of Full diff: https://github.com/llvm/llvm-project/pull/100965.diff 3 Files Affected:
diff --git a/mlir/include/mlir/Dialect/NVGPU/IR/NVGPU.td b/mlir/include/mlir/Dialect/NVGPU/IR/NVGPU.td
index dda8f31e688fe..3501c5af16d8c 100644
--- a/mlir/include/mlir/Dialect/NVGPU/IR/NVGPU.td
+++ b/mlir/include/mlir/Dialect/NVGPU/IR/NVGPU.td
@@ -20,6 +20,7 @@
#ifndef NVGPU
#define NVGPU
+include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/OpBase.td"
@@ -802,4 +803,16 @@ def NVGPU_WarpgroupMmaInitAccumulatorOp : NVGPU_Op<"warpgroup.mma.init.accumulat
let hasVerifier = 1;
}
+def NVGPU_RcpApproxOp : NVGPU_Op<"rcp_approx", [
+ Pure, SameOperandsAndResultType
+]> {
+ let summary = "A wrapper of nvvm rcp.approx.ftz.f";
+ let description = [{
+ F32 vector reciprocal calculation using `nvvm.rcp.approx.ftz.f`.
+ The input and output are both F32 vector with same shape.
+ }];
+ let arguments = (ins VectorOf<[F32]>:$in);
+ let results = (outs VectorOf<[F32]>:$out);
+ let assemblyFormat = "$in attr-dict `:` type($out)";
+}
#endif // NVGPU
diff --git a/mlir/lib/Conversion/NVGPUToNVVM/NVGPUToNVVM.cpp b/mlir/lib/Conversion/NVGPUToNVVM/NVGPUToNVVM.cpp
index 11d29754aa760..b7ea4aaeb7d8c 100644
--- a/mlir/lib/Conversion/NVGPUToNVVM/NVGPUToNVVM.cpp
+++ b/mlir/lib/Conversion/NVGPUToNVVM/NVGPUToNVVM.cpp
@@ -11,6 +11,7 @@
#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
+#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
@@ -1666,6 +1667,41 @@ struct NVGPUTmaPrefetchOpLowering
}
};
+struct NVGPURcpApproxOpLowering
+ : public ConvertOpToLLVMPattern<nvgpu::RcpApproxOp> {
+ using ConvertOpToLLVMPattern<nvgpu::RcpApproxOp>::ConvertOpToLLVMPattern;
+ LogicalResult
+ matchAndRewrite(nvgpu::RcpApproxOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ ImplicitLocOpBuilder b(op->getLoc(), rewriter);
+ auto i64Ty = b.getI64Type();
+ auto f32Ty = b.getF32Type();
+ VectorType inTy = op.getIn().getType();
+ // apply rcp.approx.ftz.f on each element in vector.
+ auto convert1DVec = [&](Type llvm1DVectorTy, Value inVec) {
+ Value ret1DVec = b.create<LLVM::UndefOp>(llvm1DVectorTy);
+ int numElems = llvm::cast<VectorType>(llvm1DVectorTy).getNumElements();
+ for (int i = 0; i < numElems; i++) {
+ Value idx = b.create<LLVM::ConstantOp>(i64Ty, b.getI64IntegerAttr(i));
+ Value elem = b.create<LLVM::ExtractElementOp>(inVec, idx);
+ Value dst = b.create<NVVM::RcpApproxFtzF32Op>(f32Ty, elem);
+ ret1DVec = b.create<LLVM::InsertElementOp>(ret1DVec, dst, idx);
+ }
+ return ret1DVec;
+ };
+ if (inTy.getRank() == 1) {
+ rewriter.replaceOp(op, convert1DVec(inTy, adaptor.getIn()));
+ return success();
+ }
+ return LLVM::detail::handleMultidimensionalVectors(
+ op.getOperation(), adaptor.getOperands(), *(this->getTypeConverter()),
+ [&](Type llvm1DVectorTy, ValueRange operands) -> Value {
+ OpAdaptor adaptor(operands);
+ return convert1DVec(llvm1DVectorTy, adaptor.getIn());
+ },
+ rewriter);
+ }
+};
} // namespace
void mlir::populateNVGPUToNVVMConversionPatterns(LLVMTypeConverter &converter,
@@ -1688,5 +1724,5 @@ void mlir::populateNVGPUToNVVMConversionPatterns(LLVMTypeConverter &converter,
NVGPUWarpgroupMmaInitAccumulatorOpLowering, // nvgpu.warpgroup.mma.init.accumulator
MmaSyncOptoNVVM, MmaLdMatrixOpToNVVM, NVGPUAsyncCopyLowering,
NVGPUAsyncCreateGroupLowering, NVGPUAsyncWaitLowering,
- NVGPUMmaSparseSyncLowering>(converter);
+ NVGPUMmaSparseSyncLowering, NVGPURcpApproxOpLowering>(converter);
}
diff --git a/mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir b/mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir
index 86a552c03a473..95cd7a0892b6c 100644
--- a/mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir
+++ b/mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir
@@ -1339,3 +1339,19 @@ module attributes {transform.with_named_sequence} {
transform.yield
}
}
+
+// CHECK-LABEL: @rcp_approx_ftz_f32
+// CHECK-SAME: %[[IN:.*]]: vector<32x16xf32>
+func.func @rcp_approx_ftz_f32(
+ %in: vector<32x16xf32>) {
+ // CHECK: %[[IN_LLVM:.*]] = builtin.unrealized_conversion_cast %[[IN]] : vector<32x16xf32> to !llvm.array<32 x vector<16xf32>>
+ // CHECK: %[[IN1DVEC:.*]] = llvm.extractvalue %[[IN_LLVM]][0] : !llvm.array<32 x vector<16xf32>>
+ // CHECK: %[[OUT1DVEC:.*]] = llvm.mlir.undef : vector<16xf32>
+ // CHECK: %[[IDX_0:.+]] = llvm.mlir.constant(0 : i64) : i64
+ // CHECK: %[[ELEM_0:.*]] = llvm.extractelement %[[IN1DVEC]][%[[IDX_0]] : i64]
+ // CHECK: %[[ELEM_RCP0:.*]] = nvvm.rcp.approx.ftz.f %[[ELEM_0]] : f32
+ // CHECK: llvm.insertelement %[[ELEM_RCP0]], %[[OUT1DVEC]][%[[IDX_0]] : i64] : vector<16xf32>
+ // CHECK-COUNT-511: nvvm.rcp.approx.ftz.f
+ %out = nvgpu.rcp_approx %in : vector<32x16xf32>
+ 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.
In general it looks great. Thanks for adding this support in NVGPU dialect. I left some comments
We typically name the titles with a prefix of [mlir] then the dialect [nvgpu]. We can change the title [mlir][nvgpu] Add nvgpu.rcp OP
@@ -20,6 +20,7 @@ | |||
#ifndef NVGPU | |||
#define NVGPU | |||
|
|||
include "mlir/Interfaces/InferTypeOpInterface.td" |
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.
Nit : In this dialect, we do not infer types. It is clearer to see the input and output directly in the IR. Otherwise, one would need to read TableGen or, even more challenging, the C++ implementation.
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.
I want to check the input/output type of rcp
are equal with SameOperandsAndResultType
. So you suggested we'd better change to use verifier?
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.
My point was about inferring the result type. When someone sees an IR like %out = nvgpu.rcp_approx %in : vector<32x16xf32>
, the return type is unclear, making the IR unreadable. To understand the return type, one would need to look at the TableGen definition of the operation or read the C++ implementation.
// Current OP, unclear what is %out
%out = nvgpu.rcp_approx %in : vector<32x16xf32>
// This is my proposal, it's clear what is %out
%out = nvgpu.rcp_approx %in : vector<32x16xf32> -> vector<32x16xf32>
You could use PredOpTrait
or SameOperandsAndResultType
, or handle it in the verifier. Does SameOperandsAndResultType
forces to infer result type?
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.
Ok, got it. Let me update the assembly format.
You could use PredOpTrait or SameOperandsAndResultType, or handle it in the verifier. Does SameOperandsAndResultType forces to infer result type?
Yes, SameOperandsAndResultType
relies on InferTypeOpInterface
and it forces to create build function with InferReturnTypes
.
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.
What about PredOpTrait
?
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.
Thanks @joker-eph and @grypp , both of formats are good to me. Since most ops of nvgpu dialect have explicit result types, I personally think it is fine to follow this style.
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.
Consistency is important, so that's reasonable.
That said it's not a matter of majority: we can also see the nvgpu dialect as "not using the common upstream practices" and in need of an upgrade.
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.
Recovered original style. Please help check if you have other questions about this pr, thanks a lot. :-)
%out = nvgpu.rcp_approx %in {rounding=approx, ftz}: vector<32x16xf32>
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.
That said it's not a matter of majority: we can also see the nvgpu dialect as "not using the common upstream practices" and in need of an upgrade.
We can upgrade it, but what's the guideline with inferring result types? It makes sense when input and output types same, like with arith
. Otherwise, imho, it turns IR obscure and unreadable.
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.
It makes sense when input and output types same, like with arith.
I thought this is the case we're talking about here? The example provided above was %out = nvgpu.rcp_approx %in : vector<32x16xf32> -> vector<32x16xf32>
For the generality, it is slightly more subtle, for example what about %res = memref.load %ptr[%idx] : memref<10xf32>
?
nvgpu.rcp
op
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.
Thanks, looks better
✅ With the latest revision this PR passed the C/C++ code formatter. |
@Observer007 you need to run clang-format |
@Observer007 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This PR introduces a new OP for reciprocal calculation for
vector
types usingnvvm.rcp
OPs. Currently, it supports only f32 types