[llvm] [llvm-c] Add C API methods to match size_t ConstantDataArray C++ API signatures (PR #84433)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 7 21:39:29 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: None (erer1243)
<details>
<summary>Changes</summary>
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).
---
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
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));
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/84433
More information about the llvm-commits
mailing list