Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class NVVM_PureSpecialRangeableRegisterOp<string mnemonic, list<Trait> traits =
let assemblyFormat = "(`range` $range^)? attr-dict `:` type($res)";
let llvmBuilder = baseLlvmBuilder # setRangeRetAttrCode # baseLlvmBuilderCoda;
let mlirBuilder = baseMlirBuilder # importRangeRetAttrCode # baseMlirBuilderCoda;
let hasVerifier = 1;

// Backwards-compatibility builder for an unspecified range.
let builders = [
Expand All @@ -279,6 +280,23 @@ class NVVM_PureSpecialRangeableRegisterOp<string mnemonic, list<Trait> traits =
SetIntRangeFn setResultRanges) {
nvvmInferResultRanges(getOperation(), getResult(), argRanges, setResultRanges);
}

// Verify the range attribute satisfies LLVM ConstantRange constructor requirements.
::llvm::LogicalResult $cppClass::verify() {
auto rangeAttr = getRange();
if (!rangeAttr)
return ::mlir::success(); // No range specified, validation passes

const ::llvm::APInt &lower = rangeAttr->getLower();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have never verified the range attribute if I'm not mistaken.

I think we can do that for the other ops as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other ops that I missed that have a range attribute? This bug was initially targeting nvvm.read.ptx.sreg.tid.x but the fix applies to all the special registers.

const ::llvm::APInt &upper = rangeAttr->getUpper();

// Check LLVM ConstantRange constructor condition
if (!(lower != upper || (lower.isMaxValue() || lower.isMinValue()))) {
return emitOpError("invalid range attribute: range must be a valid constant range");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add more information on what the criteria is and also print the lower and upper values.

}

return ::mlir::success();
}
}];

}
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Target/LLVMIR/nvvmir-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,13 @@ llvm.func @clusterlaunchcontrol_query_cancel_get_first_cta_id_invalid_return_typ
%res = nvvm.clusterlaunchcontrol.query.cancel query = get_first_cta_id_x, %try_cancel_response : i1
llvm.return
}


// -----

// Test for range validation - invalid range where lower == upper but not at extremes
func.func @invalid_range_equal_bounds() {
// expected-error @below {{invalid range attribute: range must be a valid constant range}}
%0 = nvvm.read.ptx.sreg.warpsize range <i32, 32, 32> : i32
return
}