Skip to content

Allow empty range attribute and add assert for full range #100601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2024
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
3 changes: 2 additions & 1 deletion llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,8 @@ Currently, only the following parameter attributes are defined:
- The pair ``a,b`` represents the range ``[a,b)``.
- Both ``a`` and ``b`` are constants.
- The range is allowed to wrap.
- The range should not represent the full or empty set. That is, ``a!=b``.
- The empty range is represented using ``0,0``.
- Otherwise, ``a`` and ``b`` are not allowed to be equal.

This attribute may only be applied to parameters or return values with integer
or vector of integer types.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3109,8 +3109,8 @@ bool LLParser::parseRangeAttr(AttrBuilder &B) {
if (ParseAPSInt(BitWidth, Lower) ||
parseToken(lltok::comma, "expected ','") || ParseAPSInt(BitWidth, Upper))
return true;
if (Lower == Upper)
return tokError("the range should not represent the full or empty set!");
if (Lower == Upper && !Lower.isZero())
return tokError("the range represent the empty set but limits aren't 0!");

if (parseToken(lltok::rparen, "expected ')'"))
return true;
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
const ConstantRange &CR) {
assert(Attribute::isConstantRangeAttrKind(Kind) &&
"Not a ConstantRange attribute");
assert(!CR.isFullSet() && "ConstantRange attribute must not be full");
LLVMContextImpl *pImpl = Context.pImpl;
FoldingSetNodeID ID;
ID.AddInteger(Kind);
Expand Down Expand Up @@ -2020,6 +2021,9 @@ AttrBuilder &AttrBuilder::addInAllocaAttr(Type *Ty) {

AttrBuilder &AttrBuilder::addConstantRangeAttr(Attribute::AttrKind Kind,
const ConstantRange &CR) {
if (CR.isFullSet())
return *this;

return addAttribute(Attribute::get(Ctx, Kind, CR));
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Assembler/range-attribute-invalid-range.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s

; CHECK: the range should not represent the full or empty set!
define void @range_empty(i8 range(i8 0, 0) %a) {
; CHECK: the range represent the empty set but limits aren't 0!
define void @range_empty(i8 range(i8 1, 1) %a) {
ret void
}
4 changes: 2 additions & 2 deletions llvm/test/Bitcode/attributes.ll
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ define range(i32 -1, 42) i32 @range_attribute(<4 x i32> range(i32 -1, 42) %a) {
ret i32 0
}

; CHECK: define range(i32 0, 42) i32 @range_attribute_same_range_other_bitwidth(i8 range(i8 0, 42) %a)
define range(i32 0, 42) i32 @range_attribute_same_range_other_bitwidth(i8 range(i8 0, 42) %a) {
; CHECK: define range(i32 0, 0) i32 @range_attribute_same_range_other_bitwidth(i8 range(i8 0, 42) %a)
define range(i32 0, 0) i32 @range_attribute_same_range_other_bitwidth(i8 range(i8 0, 42) %a) {
ret i32 0
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,14 @@ define i8 @caller15_okay_intersect_ranges() {
call void @use.val(i8 %r)
ret i8 %r
}

define i8 @caller16_not_intersecting_ranges() {
; CHECK-LABEL: define i8 @caller16_not_intersecting_ranges() {
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 0, 0) i8 @val8()
; CHECK-NEXT: call void @use.val(i8 [[R_I]])
; CHECK-NEXT: ret i8 [[R_I]]
;
%r = call range(i8 0, 5) i8 @callee15()
call void @use.val(i8 %r)
ret i8 %r
}
Loading