Skip to content

Commit d39a26f

Browse files
committed
[MLIR][NVVM] Update redux.sync op
This change: - Updates the `redux.sync` NVVM Op input and output type constraints - Adds a verifier for the Op to prevent stack dumps and the execution of an `llvm_unreachable` in certain cases of invalid usage, and instead gracefully error out with an informative error message.
1 parent 4c46ae3 commit d39a26f

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ def ReduxKind : I32EnumAttr<"ReduxKind", "NVVM redux kind",
476476
def ReduxKindAttr : EnumAttr<NVVM_Dialect, ReduxKind, "redux_kind">;
477477

478478
def NVVM_ReduxOp :
479-
NVVM_Op<"redux.sync", [NVVMRequiresSM<80>]>,
480-
Results<(outs LLVM_Type:$res)>,
481-
Arguments<(ins LLVM_Type:$val,
479+
NVVM_Op<"redux.sync", [NVVMRequiresSM<80>, AllTypesMatch<["res", "val"]>]>,
480+
Results<(outs AnyTypeOf<[I32, F32]>:$res)>,
481+
Arguments<(ins AnyTypeOf<[I32, F32]>:$val,
482482
ReduxKindAttr:$kind,
483483
I32:$mask_and_clamp,
484484
DefaultValuedAttr<BoolAttr, "false">:$abs,
@@ -496,6 +496,8 @@ def NVVM_ReduxOp :
496496

497497
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#parallel-synchronization-and-communication-instructions-redux-sync)
498498
}];
499+
let hasVerifier = 1;
500+
499501
string llvmBuilder = [{
500502
auto intId = getReduxIntrinsicId($_resultType, $kind, $abs, $nan);
501503
$res = createIntrinsicCall(builder, intId, {$val, $mask_and_clamp});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,32 @@ LogicalResult NVVM::ClusterLaunchControlQueryCancelOp::verify() {
15631563
return success();
15641564
}
15651565

1566+
LogicalResult NVVM::ReduxOp::verify() {
1567+
mlir::Type reduxType = getType();
1568+
if (!reduxType.isF32()) {
1569+
if (getAbs())
1570+
return emitOpError("abs attribute is supported only for f32 type");
1571+
if (getNan())
1572+
return emitOpError("nan attribute is supported only for f32 type");
1573+
}
1574+
1575+
NVVM::ReduxKind kind = getKind();
1576+
switch (kind) {
1577+
case NVVM::ReduxKind::FMIN:
1578+
case NVVM::ReduxKind::FMAX:
1579+
if (!reduxType.isF32())
1580+
return emitOpError("fmin and fmax redux kind must be used with f32 type");
1581+
break;
1582+
default:
1583+
if (reduxType.isF32())
1584+
return emitOpError(
1585+
"only fmin and fmax redux kinds are supported for f32 type");
1586+
break;
1587+
}
1588+
1589+
return success();
1590+
}
1591+
15661592
/// Packs the given `field` into the `result`.
15671593
/// The `result` is 64-bits and each `field` can be 32-bits or narrower.
15681594
static llvm::Value *

mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ using mlir::LLVM::detail::createIntrinsicCall;
3636
static llvm::Intrinsic::ID getReduxIntrinsicId(llvm::Type *resultType,
3737
NVVM::ReduxKind kind,
3838
bool hasAbs, bool hasNaN) {
39-
if (!(resultType->isIntegerTy(32) || resultType->isFloatTy()))
40-
llvm_unreachable("unsupported data type for redux");
41-
4239
switch (kind) {
4340
case NVVM::ReduxKind::ADD:
4441
return llvm::Intrinsic::nvvm_redux_sync_add;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: mlir-translate -verify-diagnostics -split-input-file -mlir-to-llvmir %s
2+
3+
// -----
4+
5+
llvm.func @redux_sync_i32_with_abs(%value: i32, %offset: i32) {
6+
// expected-error@+1 {{'nvvm.redux.sync' op abs attribute is supported only for f32 type}}
7+
%res = nvvm.redux.sync add %value, %offset {abs = true}: i32 -> i32
8+
llvm.return
9+
}
10+
11+
// -----
12+
13+
llvm.func @redux_sync_i32_with_nan(%value: i32, %offset: i32) {
14+
// expected-error@+1 {{'nvvm.redux.sync' op nan attribute is supported only for f32 type}}
15+
%res = nvvm.redux.sync add %value, %offset {nan = true}: i32 -> i32
16+
llvm.return
17+
}
18+
19+
// -----
20+
21+
llvm.func @redux_sync_f32_with_invalid_kind(%value: f32, %offset: i32) {
22+
// expected-error@+1 {{'nvvm.redux.sync' op only fmin and fmax redux kinds are supported for f32 type}}
23+
%res = nvvm.redux.sync add %value, %offset: f32 -> f32
24+
llvm.return
25+
}
26+
27+
// -----
28+
29+
llvm.func @redux_sync_i32_with_invalid_kind(%value: i32, %offset: i32) {
30+
// expected-error@+1 {{'nvvm.redux.sync' op fmin and fmax redux kind must be used with f32 type}}
31+
%res = nvvm.redux.sync fmin %value, %offset: i32 -> i32
32+
llvm.return
33+
}

0 commit comments

Comments
 (0)