Skip to content

Commit 8174f84

Browse files
committed
[MLIR][NVVM] Add support for dp4a instructions
This change adds the `dp4a` Op to the NVVM dialect to perform four-way byte dot product-accumulate operation. For more information, see PTX ISA: https://docs.nvidia.com/cuda/parallel-thread-execution/#integer-arithmetic-instructions-dp4a
1 parent 20d6375 commit 8174f84

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td

+47
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,53 @@ def NVVM_Tcgen05StOp : NVVM_Op<"tcgen05.st"> {
34443444
let hasVerifier = 1;
34453445
}
34463446

3447+
//===----------------------------------------------------------------------===//
3448+
// NVVM dp4a Op
3449+
//===----------------------------------------------------------------------===//
3450+
3451+
def NVVM_Dp4aOp : NVVM_Op<"dp4a"> {
3452+
let summary = "Four-way byte dot product-accumulate instruction.";
3453+
let description = [{
3454+
Performs a four-way byte dot-product which is accumulated in a 32-bit
3455+
result.
3456+
Operand `a` and `b` are vectors of 4 bytes between which the dot product is
3457+
computed.
3458+
By default, the byte inputs are zero-extended to 32-bit before the dot
3459+
product is computed. The `a_siext` and `b_siext` unit attributes can be
3460+
used to mention that the individual byte inputs in the corresponding
3461+
operand are signed and need to be sign-extended instead.
3462+
Operand `c` is a 32-bit integer to which the result is accumulated. It is
3463+
treated as holding a signed integer if any of `a` or `b` are to be
3464+
sign-extended.
3465+
3466+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#integer-arithmetic-instructions-dp4a)
3467+
}];
3468+
3469+
let arguments = (ins
3470+
VectorOfLengthAndType<[4], [I8]>:$a,
3471+
VectorOfLengthAndType<[4], [I8]>:$b,
3472+
I32:$c,
3473+
DefaultValuedAttr<UnitAttr, "false">:$a_siext,
3474+
DefaultValuedAttr<UnitAttr, "false">:$b_siext
3475+
);
3476+
3477+
let results = (outs I32:$res);
3478+
3479+
let assemblyFormat = "$a `,` $b `,` $c attr-dict `:` type($a) `,` type($b)";
3480+
3481+
let extraClassDeclaration = [{
3482+
static llvm::Intrinsic::ID getIntrinsicID(bool a_siext, bool b_siext);
3483+
llvm::Value* getPackedArg(llvm::Value* arg, llvm::IRBuilderBase& builder);
3484+
}];
3485+
3486+
string llvmBuilder = [{
3487+
llvm::Intrinsic::ID id = NVVM::Dp4aOp::getIntrinsicID($a_siext, $b_siext);
3488+
llvm::Value* argA = op.getPackedArg($a, builder);
3489+
llvm::Value* argB = op.getPackedArg($b, builder);
3490+
$res = createIntrinsicCall(builder, id, {argA, argB, $c});
3491+
}];
3492+
}
3493+
34473494
//===----------------------------------------------------------------------===//
34483495
// NVVM target attribute.
34493496
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/AsmParser/Parser.h"
3434
#include "llvm/IR/Attributes.h"
3535
#include "llvm/IR/Function.h"
36+
#include "llvm/IR/IRBuilder.h"
3637
#include "llvm/IR/IntrinsicsNVPTX.h"
3738
#include "llvm/IR/Type.h"
3839
#include "llvm/Support/Casting.h"
@@ -1203,6 +1204,12 @@ LogicalResult NVVM::VoteSyncOp::verify() {
12031204
return success();
12041205
}
12051206

1207+
llvm::Value *NVVM::Dp4aOp::getPackedArg(llvm::Value *arg,
1208+
llvm::IRBuilderBase &builder) {
1209+
return builder.CreateBitCast(arg,
1210+
llvm::Type::getInt32Ty(builder.getContext()));
1211+
}
1212+
12061213
//===----------------------------------------------------------------------===//
12071214
// getIntrinsicID/getIntrinsicIDAndArgs methods
12081215
//===----------------------------------------------------------------------===//
@@ -1590,6 +1597,14 @@ static void nvvmInferResultRanges(Operation *op, Value result,
15901597
}
15911598
}
15921599

1600+
#define GET_DP4A_ID(a_sign, is_b_siext) \
1601+
is_b_siext ? llvm::Intrinsic::nvvm_idp4a_##a_sign##_s \
1602+
: llvm::Intrinsic::nvvm_idp4a_##a_sign##_u
1603+
1604+
llvm::Intrinsic::ID Dp4aOp::getIntrinsicID(bool a_siext, bool b_siext) {
1605+
return a_siext ? GET_DP4A_ID(s, b_siext) : GET_DP4A_ID(u, b_siext);
1606+
}
1607+
15931608
//===----------------------------------------------------------------------===//
15941609
// NVVMDialect initialization, type parsing, and registration.
15951610
//===----------------------------------------------------------------------===//

mlir/test/Dialect/LLVMIR/nvvm.mlir

+9
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ func.func @st_bulk(%addr_gen: !llvm.ptr, %addr_shared: !llvm.ptr<3>, %size: i64)
578578
return
579579
}
580580

581+
// CHECK-LABEL: @dp4a
582+
func.func @dp4a(%a: i32, %a_vec: vector<4xi8>, %b: i32, %b_vec: vector<4xi8>, %c: i32) {
583+
// CHECK: nvvm.dp4a %{{.*}}, %{{.*}}, %{{.*}} : vector<4xi8>, vector<4xi8>
584+
%1 = nvvm.dp4a %a_vec, %b_vec, %c: vector<4xi8>, vector<4xi8>
585+
// CHECK: nvvm.dp4a %{{.*}}, %{{.*}}, %{{.*}} {a_siext, b_siext} : vector<4xi8>, vector<4xi8>
586+
%3 = nvvm.dp4a %a_vec, %b_vec, %c {a_siext, b_siext}: vector<4xi8>, vector<4xi8>
587+
return
588+
}
589+
581590
// -----
582591

583592
// Just check these don't emit errors.

mlir/test/Target/LLVMIR/nvvmir.mlir

+22
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,25 @@ llvm.func @nvvm_st_bulk(%addr_gen: !llvm.ptr, %addr_shared: !llvm.ptr<3>, %size:
844844
nvvm.st.bulk %addr_shared, size = %size, init = 0: !llvm.ptr<3>
845845
llvm.return
846846
}
847+
848+
// -----
849+
// CHECK-LABEL: @nvvm_dp4a
850+
llvm.func @nvvm_dp4a(%a: vector<4xi8>, %b: vector<4xi8>, %c: i32) {
851+
// CHECK: %[[a_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
852+
// CHECK: %[[b_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
853+
// CHECK: call i32 @llvm.nvvm.idp4a.u.u(i32 %[[a_cast]], i32 %[[b_cast]], i32 %{{.*}})
854+
%0 = nvvm.dp4a %a, %b, %c: vector<4xi8>, vector<4xi8>
855+
// CHECK: %[[a_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
856+
// CHECK: %[[b_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
857+
// CHECK: call i32 @llvm.nvvm.idp4a.s.u(i32 %[[a_cast]], i32 %[[b_cast]], i32 %{{.*}})
858+
%1 = nvvm.dp4a %a, %b, %c {a_siext}: vector<4xi8>, vector<4xi8>
859+
// CHECK: %[[a_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
860+
// CHECK: %[[b_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
861+
// CHECK: call i32 @llvm.nvvm.idp4a.u.s(i32 %[[a_cast]], i32 %[[b_cast]], i32 %{{.*}})
862+
%2 = nvvm.dp4a %a, %b, %c {b_siext}: vector<4xi8>, vector<4xi8>
863+
// CHECK: %[[a_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
864+
// CHECK: %[[b_cast:.*]] = bitcast <4 x i8> %{{.*}} to i32
865+
// CHECK: call i32 @llvm.nvvm.idp4a.s.s(i32 %[[a_cast]], i32 %[[b_cast]], i32 %{{.*}})
866+
%3 = nvvm.dp4a %a, %b, %c {a_siext, b_siext}: vector<4xi8>, vector<4xi8>
867+
llvm.return
868+
}

0 commit comments

Comments
 (0)