Skip to content
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

Merged
merged 1 commit into from
Mar 9, 2024

Conversation

erer1243
Copy link
Contributor

@erer1243 erer1243 commented Mar 8, 2024

Adds LLVMConstStringInContext2 and LLVMConstString2, which are identical to originals except that they use size_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 and ConstantAggregateZero, 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 use MyValue->getType()->getArrayNumElements() instead).

Copy link

github-actions bot commented Mar 8, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added the llvm:ir label Mar 8, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 8, 2024

@llvm/pr-subscribers-llvm-ir

Author: None (erer1243)

Changes

Adds LLVMConstStringInContext2 and LLVMConstString2, which are identical to originals except that they use size_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 and ConstantAggregateZero, 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 use MyValue->getType()->getArrayNumElements() instead).


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

3 Files Affected:

  • (modified) llvm/bindings/ocaml/llvm/llvm_ocaml.c (+4-4)
  • (modified) llvm/include/llvm-c/Core.h (+25)
  • (modified) llvm/lib/IR/Core.cpp (+16)
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));
 }

Copy link

github-actions bot commented Mar 8, 2024

⚠️ 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.

Copy link

github-actions bot commented Mar 8, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@DianQK
Copy link
Member

DianQK commented Mar 8, 2024

I think we could replace the older function directly.

Please run ./clang/tools/clang-format/git-clang-format HEAD~1or git clang-format HEAD~1 locally.

@DianQK DianQK requested review from nikic, fhahn and dtcxzyw March 8, 2024 06:01
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

LGTM.

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
Copy link
Contributor

nikic commented Mar 8, 2024

Would it be a reasonable goal to update all of these length methods & types to be uint64_t, or would that be too breaking?

Can't say this is in general, but at least updating ConstantDataSequential::getNumElements() to use uint64_t sounds like a good idea.

Copy link
Member

@DianQK DianQK left a 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.)

llvm/include/llvm-c/Core.h Outdated Show resolved Hide resolved
@nikic
Copy link
Contributor

nikic commented Mar 8, 2024

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?

@erer1243
Copy link
Contributor Author

erer1243 commented Mar 8, 2024

@DianQK I amended the commit to use a regular name & email - is that ok?

Also removed LLVMConstString2 and added a release note.

@DianQK
Copy link
Member

DianQK commented Mar 9, 2024

"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?

@erer1243
Copy link
Contributor Author

erer1243 commented Mar 9, 2024

Ok I unchecked that

@DianQK DianQK merged commit e1405e4 into llvm:main Mar 9, 2024
5 checks passed
Copy link

github-actions bot commented Mar 9, 2024

@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
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

None yet

6 participants