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

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 29 11:20:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Andreas Jonson (andjo403)

<details>
<summary>Changes</summary>

@<!-- -->nikic was it something like this that you was thinking of? 
Do not know if it makes sense to have a test as there is no way to get the range again so not possible to test it in eg. echo.ll

---
Full diff: https://github.com/llvm/llvm-project/pull/90505.diff


3 Files Affected:

- (modified) llvm/docs/ReleaseNotes.rst (+2) 
- (modified) llvm/include/llvm-c/Core.h (+8) 
- (modified) llvm/lib/IR/Core.cpp (+14) 


``````````diff
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) {

``````````

</details>


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


More information about the llvm-commits mailing list