Skip to content

Commit 4c89f96

Browse files
committed
Assert range attribute is not empty nor full
1 parent a27f816 commit 4c89f96

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
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: 8 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,7 @@ AttrBuilder &AttrBuilder::addConstantRangeAttr(Attribute::AttrKind Kind,
20242031
}
20252032

20262033
AttrBuilder &AttrBuilder::addRangeAttr(const ConstantRange &CR) {
2027-
return addConstantRangeAttr(Attribute::Range, CR);
2034+
return addAttribute(Attribute::getWithRange(Ctx, CR));
20282035
}
20292036

20302037
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,

0 commit comments

Comments
 (0)