Skip to content

Commit

Permalink
[llvm-c] Add C API methods to match size_t ConstantDataArray C++ API …
Browse files Browse the repository at this point in the history
…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).
  • Loading branch information
erer1243 authored Mar 9, 2024
1 parent 2709baf commit e1405e4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions llvm/bindings/ocaml/llvm/llvm_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e1405e4

Please sign in to comment.