[llvm] [C API] Add function to create constantRange attributes to C API (PR #90505)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 21:08:40 PDT 2024


https://github.com/andjo403 updated https://github.com/llvm/llvm-project/pull/90505

>From 6916226116d365b3ad92fd5f5567ebced6e05e84 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Mon, 29 Apr 2024 20:15:53 +0200
Subject: [PATCH 1/3] [C API] Add function to create constantRange attributes
 to C API

---
 llvm/docs/ReleaseNotes.rst |  2 ++
 llvm/include/llvm-c/Core.h |  8 ++++++++
 llvm/lib/IR/Core.cpp       | 14 ++++++++++++++
 3 files changed, 24 insertions(+)

diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 46d79d6c5822b1..0c5697e6ec949d 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -161,6 +161,8 @@ Changes to the C API
 * Added ``LLVMAtomicRMWBinOpUIncWrap`` and ``LLVMAtomicRMWBinOpUDecWrap`` to
   ``LLVMAtomicRMWBinOp`` enum for AtomicRMW instructions.
 
+* Added ``LLVMCreateConstantRangeAttribute`` function for creating ConstantRange Attributes.
+
 Changes to the CodeGen infrastructure
 -------------------------------------
 
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 0b03f3b36fcdd3..3670fa88a734fc 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -673,6 +673,14 @@ LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
  */
 LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);
 
+/**
+ * Create a constantRange attribute
+ */
+LLVMAttributeRef LLVMCreateConstantRangeAttribute(
+    LLVMContextRef C, unsigned KindID, LLVMTypeRef IntTy,
+    unsigned LowerNumWords, const uint64_t LowerWords[], unsigned UpperNumWords,
+    const uint64_t UpperWords[]);
+
 /**
  * Create a string attribute.
  */
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 6aff94f39d9c0c..b68b304fffb8d4 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -14,6 +14,7 @@
 #include "llvm-c/Core.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/ConstantRange.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DerivedTypes.h"
@@ -178,6 +179,19 @@ LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
   return wrap(Attr.getValueAsType());
 }
 
+LLVMAttributeRef LLVMCreateConstantRangeAttribute(
+    LLVMContextRef C, unsigned KindID, LLVMTypeRef IntTy,
+    unsigned LowerNumWords, const uint64_t LowerWords[], unsigned UpperNumWords,
+    const uint64_t UpperWords[]) {
+  unsigned BitWidth = unwrap<IntegerType>(IntTy)->getBitWidth();
+  auto &Ctx = *unwrap(C);
+  auto AttrKind = (Attribute::AttrKind)KindID;
+  return wrap(Attribute::get(
+      Ctx, AttrKind,
+      ConstantRange(APInt(BitWidth, ArrayRef(LowerWords, LowerNumWords)),
+                    APInt(BitWidth, ArrayRef(UpperWords, UpperNumWords)))));
+}
+
 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
                                            const char *K, unsigned KLength,
                                            const char *V, unsigned VLength) {

>From 8b1d790020284f317c0057ca4c14c3d19650e494 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Tue, 30 Apr 2024 09:02:47 +0200
Subject: [PATCH 2/3] fixup! [C API] Add function to create constantRange
 attributes to C API

---
 llvm/include/llvm-c/Core.h           |  2 +-
 llvm/unittests/IR/AttributesTest.cpp | 34 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 3670fa88a734fc..b9e903992560ef 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -674,7 +674,7 @@ LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
 LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);
 
 /**
- * Create a constantRange attribute
+ * Create a ConstantRange attribute.
  */
 LLVMAttributeRef LLVMCreateConstantRangeAttribute(
     LLVMContextRef C, unsigned KindID, LLVMTypeRef IntTy,
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index 1accdfd3f6a423..1db6b5c9adc8d0 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -7,8 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/IR/Attributes.h"
+#include "llvm-c/Core.h"
 #include "llvm/AsmParser/Parser.h"
 #include "llvm/IR/AttributeMask.h"
+#include "llvm/IR/ConstantRange.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/LLVMContext.h"
@@ -308,4 +310,36 @@ TEST(Attributes, RemoveParamAttributes) {
   EXPECT_EQ(AL.getNumAttrSets(), 0U);
 }
 
+TEST(Attributes, ConstantRangeAttributeCAPI) {
+  LLVMContext C;
+  {
+    Type *IntTy = Type::getInt8Ty(C);
+
+    const uint64_t LowerWords[] = {0};
+    const uint64_t UpperWords[] = {42};
+
+    auto Range = ConstantRange(APInt(8, ArrayRef<uint64_t>(LowerWords, 1)),
+                               APInt(8, ArrayRef<uint64_t>(UpperWords, 1)));
+
+    Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
+    auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
+        wrap(&C), Attribute::Range, wrap(IntTy), 1, LowerWords, 1, UpperWords));
+    EXPECT_EQ(OutAttr, RangeAttr);
+  }
+  {
+    Type *IntTy = Type::getInt128Ty(C);
+
+    const uint64_t LowerWords[] = {1, 1};
+    const uint64_t UpperWords[] = {42, 42};
+
+    auto Range = ConstantRange(APInt(128, ArrayRef<uint64_t>(LowerWords, 2)),
+                               APInt(128, ArrayRef<uint64_t>(UpperWords, 2)));
+
+    Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
+    auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
+        wrap(&C), Attribute::Range, wrap(IntTy), 2, LowerWords, 2, UpperWords));
+    EXPECT_EQ(OutAttr, RangeAttr);
+  }
+}
+
 } // end anonymous namespace

>From 665f76ec231a8142ca8cc03a4f05e85efab51e81 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Wed, 1 May 2024 06:07:53 +0200
Subject: [PATCH 3/3] fixup! [C API] Add function to create constantRange
 attributes to C API

---
 llvm/include/llvm-c/Core.h           |  4 ++--
 llvm/lib/IR/Core.cpp                 |  9 ++++-----
 llvm/unittests/IR/AttributesTest.cpp | 20 ++++++++++----------
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index b9e903992560ef..caa83bd5c0b57f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -677,8 +677,8 @@ LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);
  * Create a ConstantRange attribute.
  */
 LLVMAttributeRef LLVMCreateConstantRangeAttribute(
-    LLVMContextRef C, unsigned KindID, LLVMTypeRef IntTy,
-    unsigned LowerNumWords, const uint64_t LowerWords[], unsigned UpperNumWords,
+    LLVMContextRef C, unsigned KindID, unsigned NumBits, unsigned LowerNumWords,
+    const uint64_t LowerWords[], unsigned UpperNumWords,
     const uint64_t UpperWords[]);
 
 /**
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index b68b304fffb8d4..af6b272c4f6186 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -180,16 +180,15 @@ LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
 }
 
 LLVMAttributeRef LLVMCreateConstantRangeAttribute(
-    LLVMContextRef C, unsigned KindID, LLVMTypeRef IntTy,
-    unsigned LowerNumWords, const uint64_t LowerWords[], unsigned UpperNumWords,
+    LLVMContextRef C, unsigned KindID, unsigned NumBits, unsigned LowerNumWords,
+    const uint64_t LowerWords[], unsigned UpperNumWords,
     const uint64_t UpperWords[]) {
-  unsigned BitWidth = unwrap<IntegerType>(IntTy)->getBitWidth();
   auto &Ctx = *unwrap(C);
   auto AttrKind = (Attribute::AttrKind)KindID;
   return wrap(Attribute::get(
       Ctx, AttrKind,
-      ConstantRange(APInt(BitWidth, ArrayRef(LowerWords, LowerNumWords)),
-                    APInt(BitWidth, ArrayRef(UpperWords, UpperNumWords)))));
+      ConstantRange(APInt(NumBits, ArrayRef(LowerWords, LowerNumWords)),
+                    APInt(NumBits, ArrayRef(UpperWords, UpperNumWords)))));
 }
 
 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
diff --git a/llvm/unittests/IR/AttributesTest.cpp b/llvm/unittests/IR/AttributesTest.cpp
index 1db6b5c9adc8d0..b4fb976ad2d736 100644
--- a/llvm/unittests/IR/AttributesTest.cpp
+++ b/llvm/unittests/IR/AttributesTest.cpp
@@ -313,31 +313,31 @@ TEST(Attributes, RemoveParamAttributes) {
 TEST(Attributes, ConstantRangeAttributeCAPI) {
   LLVMContext C;
   {
-    Type *IntTy = Type::getInt8Ty(C);
-
+    const unsigned numBits = 8;
     const uint64_t LowerWords[] = {0};
     const uint64_t UpperWords[] = {42};
 
-    auto Range = ConstantRange(APInt(8, ArrayRef<uint64_t>(LowerWords, 1)),
-                               APInt(8, ArrayRef<uint64_t>(UpperWords, 1)));
+    auto Range =
+        ConstantRange(APInt(numBits, ArrayRef<uint64_t>(LowerWords, 1)),
+                      APInt(numBits, ArrayRef<uint64_t>(UpperWords, 1)));
 
     Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
     auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
-        wrap(&C), Attribute::Range, wrap(IntTy), 1, LowerWords, 1, UpperWords));
+        wrap(&C), Attribute::Range, numBits, 1, LowerWords, 1, UpperWords));
     EXPECT_EQ(OutAttr, RangeAttr);
   }
   {
-    Type *IntTy = Type::getInt128Ty(C);
-
+    const unsigned numBits = 128;
     const uint64_t LowerWords[] = {1, 1};
     const uint64_t UpperWords[] = {42, 42};
 
-    auto Range = ConstantRange(APInt(128, ArrayRef<uint64_t>(LowerWords, 2)),
-                               APInt(128, ArrayRef<uint64_t>(UpperWords, 2)));
+    auto Range =
+        ConstantRange(APInt(numBits, ArrayRef<uint64_t>(LowerWords, 2)),
+                      APInt(numBits, ArrayRef<uint64_t>(UpperWords, 2)));
 
     Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
     auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
-        wrap(&C), Attribute::Range, wrap(IntTy), 2, LowerWords, 2, UpperWords));
+        wrap(&C), Attribute::Range, numBits, 2, LowerWords, 2, UpperWords));
     EXPECT_EQ(OutAttr, RangeAttr);
   }
 }



More information about the llvm-commits mailing list