[llvm] e1405e4 - [llvm-c] Add C API methods to match size_t ConstantDataArray C++ API signatures (#84433)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 8 18:29:37 PST 2024
Author: erer1243
Date: 2024-03-09T10:29:33+08:00
New Revision: e1405e4f71c899420ebf8262d5e9745598419df8
URL: https://github.com/llvm/llvm-project/commit/e1405e4f71c899420ebf8262d5e9745598419df8
DIFF: https://github.com/llvm/llvm-project/commit/e1405e4f71c899420ebf8262d5e9745598419df8.diff
LOG: [llvm-c] Add C API methods to match size_t ConstantDataArray C++ API signatures (#84433)
Adds `LLVMConstStringInContext2` and `LLVMConstString2`, which are
identical to originals except that they use `size_t` for length. This is
a clone of
https://github.com/llvm/llvm-project/commit/35276f16e5a2cae0dfb49c0fbf874d4d2f177acc
and is needed for https://github.com/rust-lang/rust/pull/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).
Added:
Modified:
llvm/bindings/ocaml/llvm/llvm_ocaml.c
llvm/docs/ReleaseNotes.rst
llvm/include/llvm-c/Core.h
llvm/lib/IR/Core.cpp
Removed:
################################################################################
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/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 56abe7fe40270f..b34a5f31c5eb0a 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -128,6 +128,9 @@ Changes to the C API
* Added ``LLVMGetBlockAddressFunction`` and ``LLVMGetBlockAddressBasicBlock``
functions for accessing the values in a blockaddress constant.
+* Added ``LLVMConstStringInContext2`` function, which better matches the C++
+ API by using ``size_t`` for string length. Deprecated ``LLVMConstStringInContext``.
+
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 09746bdaf0c94e..7cfe4dc4f775fd 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2165,11 +2165,22 @@ 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.
*
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index d6d159ab8b9e83..4b804a41a1676f 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1522,6 +1522,15 @@ 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,
More information about the llvm-commits
mailing list