aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Verifier.cpp
diff options
context:
space:
mode:
authorDurgadoss R <durgadossr@nvidia.com>2025-05-23 15:45:59 +0530
committerGitHub <noreply@github.com>2025-05-23 15:45:59 +0530
commitb6c9c7633abfe783244bf19b0290d82678b2c419 (patch)
tree31df737effe0aaba62ff32b5ada4ead9ad7358de /llvm/lib/IR/Verifier.cpp
parentbae8e1f99e0b245ec31912e29b4c80e823f635c6 (diff)
downloadllvm-b6c9c7633abfe783244bf19b0290d82678b2c419.zip
llvm-b6c9c7633abfe783244bf19b0290d82678b2c419.tar.gz
llvm-b6c9c7633abfe783244bf19b0290d82678b2c419.tar.bz2
[Verifier] Add checks for range attribute on ImmArg (#140522)
This patch implements verifier checks for range attributes on ImmArg. This enables validation of the range of ImmArg operands in intrinsics, when the intrinsic definition includes the range information. Signed-off-by: Durgadoss R <durgadossr@nvidia.com>
Diffstat (limited to 'llvm/lib/IR/Verifier.cpp')
-rw-r--r--llvm/lib/IR/Verifier.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 73b4274..fdb4dda 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1987,8 +1987,12 @@ void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
V);
if (Attrs.hasAttribute(Attribute::ImmArg)) {
- Check(Attrs.getNumAttributes() == 1,
- "Attribute 'immarg' is incompatible with other attributes", V);
+ unsigned AttrCount =
+ Attrs.getNumAttributes() - Attrs.hasAttribute(Attribute::Range);
+ Check(AttrCount == 1,
+ "Attribute 'immarg' is incompatible with other attributes except the "
+ "'range' attribute",
+ V);
}
// Check for mutually incompatible attributes. Only inreg is compatible with
@@ -3680,6 +3684,20 @@ void Verifier::visitCallBase(CallBase &Call) {
Value *ArgVal = Call.getArgOperand(i);
Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
"immarg operand has non-immediate parameter", ArgVal, Call);
+
+ // If the imm-arg is an integer and also has a range attached,
+ // check if the given value is within the range.
+ if (Call.paramHasAttr(i, Attribute::Range)) {
+ if (auto *CI = dyn_cast<ConstantInt>(ArgVal)) {
+ const ConstantRange &CR =
+ Call.getParamAttr(i, Attribute::Range).getValueAsConstantRange();
+ Check(CR.contains(CI->getValue()),
+ "immarg value " + Twine(CI->getValue().getSExtValue()) +
+ " out of range [" + Twine(CR.getLower().getSExtValue()) +
+ ", " + Twine(CR.getUpper().getSExtValue()) + ")",
+ Call);
+ }
+ }
}
if (Call.paramHasAttr(i, Attribute::Preallocated)) {