Skip to content

Commit e1405e4

Browse files
authoredMar 9, 2024··
[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 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).
1 parent 2709baf commit e1405e4

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed
 

‎llvm/bindings/ocaml/llvm/llvm_ocaml.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1043,14 +1043,14 @@ value llvm_const_float_of_string(value RealTy, value S) {
10431043

10441044
/* llcontext -> string -> llvalue */
10451045
value llvm_const_string(value Context, value Str) {
1046-
return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
1047-
caml_string_length(Str), 1));
1046+
return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
1047+
caml_string_length(Str), 1));
10481048
}
10491049

10501050
/* llcontext -> string -> llvalue */
10511051
value llvm_const_stringz(value Context, value Str) {
1052-
return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
1053-
caml_string_length(Str), 0));
1052+
return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
1053+
caml_string_length(Str), 0));
10541054
}
10551055

10561056
/* lltype -> llvalue array -> llvalue */

‎llvm/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Changes to the C API
128128
* Added ``LLVMGetBlockAddressFunction`` and ``LLVMGetBlockAddressBasicBlock``
129129
functions for accessing the values in a blockaddress constant.
130130

131+
* Added ``LLVMConstStringInContext2`` function, which better matches the C++
132+
API by using ``size_t`` for string length. Deprecated ``LLVMConstStringInContext``.
133+
131134
Changes to the CodeGen infrastructure
132135
-------------------------------------
133136

‎llvm/include/llvm-c/Core.h

+11
Original file line numberDiff line numberDiff line change
@@ -2165,11 +2165,22 @@ double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
21652165
/**
21662166
* Create a ConstantDataSequential and initialize it with a string.
21672167
*
2168+
* @deprecated LLVMConstStringInContext is deprecated in favor of the API
2169+
* accurate LLVMConstStringInContext2
21682170
* @see llvm::ConstantDataArray::getString()
21692171
*/
21702172
LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
21712173
unsigned Length, LLVMBool DontNullTerminate);
21722174

2175+
/**
2176+
* Create a ConstantDataSequential and initialize it with a string.
2177+
*
2178+
* @see llvm::ConstantDataArray::getString()
2179+
*/
2180+
LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
2181+
size_t Length,
2182+
LLVMBool DontNullTerminate);
2183+
21732184
/**
21742185
* Create a ConstantDataSequential with string content in the global context.
21752186
*

‎llvm/lib/IR/Core.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,15 @@ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
15221522
DontNullTerminate == 0));
15231523
}
15241524

1525+
LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
1526+
size_t Length,
1527+
LLVMBool DontNullTerminate) {
1528+
/* Inverted the sense of AddNull because ', 0)' is a
1529+
better mnemonic for null termination than ', 1)'. */
1530+
return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1531+
DontNullTerminate == 0));
1532+
}
1533+
15251534
LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
15261535
LLVMBool DontNullTerminate) {
15271536
return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,

0 commit comments

Comments
 (0)
Please sign in to comment.