Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 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,11 @@ 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() {
return verifyConstantRangeAttr(getOperation(), getRange());
}
}];

}
Expand Down
26 changes: 26 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,32 @@ static void nvvmInferResultRanges(Operation *op, Value result,
}
}

/// Verify the range attribute satisfies LLVM ConstantRange constructor
/// requirements for NVVM SpecialRangeableRegisterOp.
static LogicalResult
verifyConstantRangeAttr(Operation *op,
std::optional<LLVM::ConstantRangeAttr> rangeAttr) {
if (!rangeAttr)
return success();

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

// Check LLVM ConstantRange constructor condition
if (lower == upper && !lower.isMaxValue() && !lower.isMinValue()) {
unsigned bitWidth = lower.getBitWidth();
llvm::APInt minVal = llvm::APInt::getMinValue(bitWidth);
llvm::APInt maxVal = llvm::APInt::getMaxValue(bitWidth);
return op->emitOpError(
"invalid range attribute: Lower == Upper, but they aren't min (")
<< llvm::toString(minVal, 10, false) << ") or max ("
<< llvm::toString(maxVal, 10, false)
<< ") value! This is an invalid constant range.";
}

return success();
}

static llvm::Value *getAsPackedI32(llvm::Value *arg,
llvm::IRBuilderBase &builder) {
return builder.CreateBitCast(arg,
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: Lower == Upper, but they aren't min (0) or max (4294967295) value! This is an invalid constant range.}}
%0 = nvvm.read.ptx.sreg.warpsize range <i32, 32, 32> : i32
return
}
4 changes: 4 additions & 0 deletions mlir/test/Target/LLVMIR/nvvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ llvm.func @nvvm_special_regs() -> i32 {
%74 = nvvm.read.ptx.sreg.lanemask.ge : i32
//CHECK: call i32 @llvm.nvvm.read.ptx.sreg.lanemask.gt
%75 = nvvm.read.ptx.sreg.lanemask.gt : i32
// CHECK: %76 = call range(i32 0, 0) i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%76 = nvvm.read.ptx.sreg.tid.x range <i32, 0, 0> : i32
// CHECK: %77 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%77 = nvvm.read.ptx.sreg.tid.x range <i32, 4294967295, 4294967295> : i32
llvm.return %1 : i32
}

Expand Down