Skip to content

[C API] Add function to create ConstantRange attributes to C API #90505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 4, 2024

Conversation

andjo403
Copy link
Contributor

@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

@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2024

@llvm/pr-subscribers-llvm-ir

Author: Andreas Jonson (andjo403)

Changes

@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 --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) {

@nikic
Copy link
Contributor

nikic commented Apr 30, 2024

@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

In that case you'd usually add a unit test instead, e.g. in llvm/unittests/IR/AttributesTest.cpp.

@andjo403
Copy link
Contributor Author

Added test and fixed the comment

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 320 to 322
auto Range =
ConstantRange(APInt(NumBits, ArrayRef<uint64_t>(LowerWords, 1)),
APInt(NumBits, ArrayRef<uint64_t>(UpperWords, 1)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto Range =
ConstantRange(APInt(NumBits, ArrayRef<uint64_t>(LowerWords, 1)),
APInt(NumBits, ArrayRef<uint64_t>(UpperWords, 1)));
ConstantRange Range(APInt(NumBits, ArrayRef<uint64_t>(LowerWords, 1)),
APInt(NumBits, ArrayRef<uint64_t>(UpperWords, 1)));

is how you'd usually write this in C++...

I think you can probably also use ArrayRef(LowerWords) here, there should be a ctor for plain arrays and a template deduction guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice much cleaner

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic changed the title [C API] Add function to create constantRange attributes to C API [C API] Add function to create ConstantRange attributes to C API May 3, 2024
@andjo403
Copy link
Contributor Author

andjo403 commented May 4, 2024

Need help merging

@nikic nikic merged commit 1343e68 into llvm:main May 4, 2024
@andjo403 andjo403 deleted the cApiForRangeAttribute branch May 4, 2024 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants