-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-llvm-ir Author: Andreas Jonson (andjo403) Changes@nikic was it something like this that you was thinking of? Full diff: https://github.com/llvm/llvm-project/pull/90505.diff 3 Files Affected:
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) {
|
In that case you'd usually add a unit test instead, e.g. in llvm/unittests/IR/AttributesTest.cpp. |
Added test and fixed the comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
llvm/unittests/IR/AttributesTest.cpp
Outdated
auto Range = | ||
ConstantRange(APInt(NumBits, ArrayRef<uint64_t>(LowerWords, 1)), | ||
APInt(NumBits, ArrayRef<uint64_t>(UpperWords, 1))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice much cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Need help merging |
@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