[llvm] Assert range attribute is not empty nor full (PR #100601)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 10:09:19 PDT 2024


https://github.com/andjo403 created https://github.com/llvm/llvm-project/pull/100601

alternative to #100465

fix #99619

>From 4c89f965b72abd481ff6b2bbb2033917074facbf Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Thu, 25 Jul 2024 19:06:47 +0200
Subject: [PATCH] Assert range attribute is not empty nor full

---
 llvm/include/llvm/IR/Attributes.h         | 1 +
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 4 +++-
 llvm/lib/IR/Attributes.cpp                | 9 ++++++++-
 llvm/lib/IR/Core.cpp                      | 8 ++++----
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index 5a80a072dbbd2..cefbe1d760588 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -160,6 +160,7 @@ class Attribute {
   static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind);
   static Attribute getWithMemoryEffects(LLVMContext &Context, MemoryEffects ME);
   static Attribute getWithNoFPClass(LLVMContext &Context, FPClassTest Mask);
+  static Attribute getWithRange(LLVMContext &Context, const ConstantRange &CR);
 
   /// For a typed attribute, return the equivalent attribute with the type
   /// changed to \p ReplacementTy.
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index fd4ae109b4bb8..091065cf6d55f 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -2369,7 +2369,9 @@ Error BitcodeReader::parseAttributeGroupBlock() {
             return MaybeCR.takeError();
           i--;
 
-          B.addConstantRangeAttr(Kind, MaybeCR.get());
+          assert(Kind == Attribute::Range &&
+                 "Unhandled ConstantRangeAttribute");
+          B.addRangeAttr(MaybeCR.get());
         } else if (Record[i] == 8) {
           Attribute::AttrKind Kind;
 
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index b2fdd218e5d4b..4558e8081a67b 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -286,6 +286,13 @@ Attribute Attribute::getWithNoFPClass(LLVMContext &Context,
   return get(Context, NoFPClass, ClassMask);
 }
 
+Attribute Attribute::getWithRange(LLVMContext &Context,
+                                  const ConstantRange &CR) {
+  assert(!CR.isEmptySet() && "Range must not be empty!");
+  assert(!CR.isFullSet() && "Range must not be full!");
+  return get(Context, Range, CR);
+}
+
 Attribute
 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
                                 const std::optional<unsigned> &NumElemsArg) {
@@ -2024,7 +2031,7 @@ AttrBuilder &AttrBuilder::addConstantRangeAttr(Attribute::AttrKind Kind,
 }
 
 AttrBuilder &AttrBuilder::addRangeAttr(const ConstantRange &CR) {
-  return addConstantRangeAttr(Attribute::Range, CR);
+  return addAttribute(Attribute::getWithRange(Ctx, CR));
 }
 
 AttrBuilder &
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 17c0bf72ef05d..f3e0ed385ae2b 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -191,11 +191,11 @@ LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
                                                   const uint64_t UpperWords[]) {
   auto &Ctx = *unwrap(C);
   auto AttrKind = (Attribute::AttrKind)KindID;
+  assert(AttrKind == Attribute::Range && "Unhandled ConstantRangeAttribute");
   unsigned NumWords = divideCeil(NumBits, 64);
-  return wrap(Attribute::get(
-      Ctx, AttrKind,
-      ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
-                    APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
+  return wrap(Attribute::getWithRange(
+      Ctx, ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
+                         APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
 }
 
 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,



More information about the llvm-commits mailing list