Skip to content

Commit 5755362

Browse files
committed
Assert range attribute is not empty nor full
1 parent 6aa7270 commit 5755362

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

llvm/include/llvm/IR/Attributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class Attribute {
160160
static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind);
161161
static Attribute getWithMemoryEffects(LLVMContext &Context, MemoryEffects ME);
162162
static Attribute getWithNoFPClass(LLVMContext &Context, FPClassTest Mask);
163+
static Attribute getWithRange(LLVMContext &Context, const ConstantRange &CR);
163164

164165
/// For a typed attribute, return the equivalent attribute with the type
165166
/// changed to \p ReplacementTy.

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,9 @@ Error BitcodeReader::parseAttributeGroupBlock() {
23692369
return MaybeCR.takeError();
23702370
i--;
23712371

2372-
B.addConstantRangeAttr(Kind, MaybeCR.get());
2372+
assert(Kind == Attribute::Range &&
2373+
"Unhandled ConstantRangeAttribute");
2374+
B.addRangeAttr(MaybeCR.get());
23732375
} else if (Record[i] == 8) {
23742376
Attribute::AttrKind Kind;
23752377

llvm/lib/IR/Attributes.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ Attribute Attribute::getWithNoFPClass(LLVMContext &Context,
286286
return get(Context, NoFPClass, ClassMask);
287287
}
288288

289+
Attribute Attribute::getWithRange(LLVMContext &Context,
290+
const ConstantRange &CR) {
291+
assert(!CR.isEmptySet() && "Range must not be empty!");
292+
assert(!CR.isFullSet() && "Range must not be full!");
293+
return get(Context, Range, CR);
294+
}
295+
289296
Attribute
290297
Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
291298
const std::optional<unsigned> &NumElemsArg) {
@@ -2024,7 +2031,10 @@ AttrBuilder &AttrBuilder::addConstantRangeAttr(Attribute::AttrKind Kind,
20242031
}
20252032

20262033
AttrBuilder &AttrBuilder::addRangeAttr(const ConstantRange &CR) {
2027-
return addConstantRangeAttr(Attribute::Range, CR);
2034+
if (CR.isEmptySet() || CR.isFullSet())
2035+
return *this;
2036+
2037+
return addAttribute(Attribute::getWithRange(Ctx, CR));
20282038
}
20292039

20302040
AttrBuilder &

llvm/lib/IR/Core.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
191191
const uint64_t UpperWords[]) {
192192
auto &Ctx = *unwrap(C);
193193
auto AttrKind = (Attribute::AttrKind)KindID;
194+
assert(AttrKind == Attribute::Range && "Unhandled ConstantRangeAttribute");
194195
unsigned NumWords = divideCeil(NumBits, 64);
195-
return wrap(Attribute::get(
196-
Ctx, AttrKind,
197-
ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
198-
APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
196+
return wrap(Attribute::getWithRange(
197+
Ctx, ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
198+
APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
199199
}
200200

201201
LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,

llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ define i8 @caller15_okay_intersect_ranges() {
413413

414414
define i8 @caller16_not_intersecting_ranges() {
415415
; CHECK-LABEL: define i8 @caller16_not_intersecting_ranges() {
416-
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 0, 0) i8 @val8()
416+
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 0, 5) i8 @val8()
417417
; CHECK-NEXT: call void @use.val(i8 [[R_I]])
418418
; CHECK-NEXT: ret i8 [[R_I]]
419419
;

0 commit comments

Comments
 (0)