-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
[llvm-c] Add C API methods to match size_t ConstantDataArray C++ API signatures #84433
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-ir Author: None (erer1243) ChangesAdds As an aside, the issue of 32 bit overflow on constants is present in the C++ APIs as well. A few classes, e.g. Full diff: https://github.com/llvm/llvm-project/pull/84433.diff 3 Files Affected:
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index d74d8030cea0de..55679f218b307e 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1043,14 +1043,14 @@ value llvm_const_float_of_string(value RealTy, value S) {
/* llcontext -> string -> llvalue */
value llvm_const_string(value Context, value Str) {
- return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
- caml_string_length(Str), 1));
+ return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
+ caml_string_length(Str), 1));
}
/* llcontext -> string -> llvalue */
value llvm_const_stringz(value Context, value Str) {
- return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
- caml_string_length(Str), 0));
+ return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
+ caml_string_length(Str), 0));
}
/* lltype -> llvalue array -> llvalue */
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 09746bdaf0c94e..6ac0b77b4f2ce8 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2165,23 +2165,48 @@ double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
/**
* Create a ConstantDataSequential and initialize it with a string.
*
+ * @deprecated LLVMConstStringInContext is deprecated in favor of the API accurate
+ * LLVMConstStringInContext2
* @see llvm::ConstantDataArray::getString()
*/
LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
unsigned Length, LLVMBool DontNullTerminate);
+/**
+ * Create a ConstantDataSequential and initialize it with a string.
+ *
+ * @see llvm::ConstantDataArray::getString()
+ */
+LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
+ size_t Length, LLVMBool DontNullTerminate);
+
/**
* Create a ConstantDataSequential with string content in the global context.
*
* This is the same as LLVMConstStringInContext except it operates on the
* global context.
*
+ * @deprecated LLVMConstString is deprecated in favor of the API accurate
+ * LLVMConstString2
* @see LLVMConstStringInContext()
* @see llvm::ConstantDataArray::getString()
*/
LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
LLVMBool DontNullTerminate);
+/**
+ * Create a ConstantDataSequential with string content in the global context.
+ *
+ * This is the same as LLVMConstStringInContext2 except it operates on the
+ * global context.
+ *
+ * @see LLVMConstStringInContext2()
+ * @see llvm::ConstantDataArray::getString()
+ */
+LLVMValueRef LLVMConstString2(const char *Str, unsigned Length,
+ LLVMBool DontNullTerminate);
+
+
/**
* Returns true if the specified constant is an array of i8.
*
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index d6d159ab8b9e83..bb9d404790e77e 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1522,12 +1522,28 @@ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
DontNullTerminate == 0));
}
+LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
+ size_t Length,
+ LLVMBool DontNullTerminate) {
+ /* Inverted the sense of AddNull because ', 0)' is a
+ better mnemonic for null termination than ', 1)'. */
+ return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
+ DontNullTerminate == 0));
+}
+
+
LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
LLVMBool DontNullTerminate) {
return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
DontNullTerminate);
}
+LLVMValueRef LLVMConstString2(const char *Str, size_t Length,
+ LLVMBool DontNullTerminate) {
+ return LLVMConstStringInContext2(LLVMGetGlobalContext(), Str, Length,
+ DontNullTerminate);
+}
+
LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
I think we could replace the older function directly. Please run |
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.
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
Can't say this is in general, but at least updating ConstantDataSequential::getNumElements() to use uint64_t sounds like a good idea. |
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.
⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo. Please turn off Keep my email addresses private setting in your account. See LLVM Discourse for more information.
I can merge this PR later, but would you mind making the email publicly available? (This is not an enforcement rule.)
Can you please also add a note in https://github.com/llvm/llvm-project/blob/main/llvm/docs/ReleaseNotes.rst#changes-to-the-c-api? |
6f4c2e0
to
7568060
Compare
@DianQK I amended the commit to use a regular name & email - is that ok? Also removed LLVMConstString2 and added a release note. |
"Squash and merge" is saying "This commit will be authored by 1377477+erer1243@users.noreply.github.com". Uncheck "Keep my email addresses private" on https://github.com/settings/emails? |
Ok I unchecked that |
@erer1243 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Adds
LLVMConstStringInContext2
andLLVMConstString2
, which are identical to originals except that they usesize_t
for length. This is a clone of 35276f1 and is needed for rust-lang/rust#122000.As an aside, the issue of 32 bit overflow on constants is present in the C++ APIs as well. A few classes, e.g.
ConstantDataArray
andConstantAggregateZero
, can hold 64-bit ArrayTypes but their length accessors return 32-bit values. This means the same issue from the original Rust report is also present in LLVM itself. Would it be a reasonable goal to update all of these length methods & types to be uint64_t, or would that be too breaking? Alternatively, we could use safe fallible casts instead of implicit ones inside the accessors (if an overflow does happen, the solution would be to useMyValue->getType()->getArrayNumElements()
instead).