Skip to content

Commit 29f57db

Browse files
committed
use upstream 64-bit functions, add overflow assertions where none exist
1 parent 2bbffa9 commit 29f57db

File tree

4 files changed

+48
-62
lines changed

4 files changed

+48
-62
lines changed

Diff for: compiler/rustc_codegen_llvm/src/common.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ impl<'ll> BackendTypes for CodegenCx<'ll, '_> {
9595

9696
impl<'ll> CodegenCx<'ll, '_> {
9797
pub fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
98-
unsafe { llvm::LLVMRustConstArray(ty, elts.as_ptr(), elts.len()) }
98+
unsafe { llvm::LLVMConstArray2(ty, elts.as_ptr(), elts.len()) }
9999
}
100100

101101
pub fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
102-
unsafe { llvm::LLVMRustConstVector(elts.as_ptr(), elts.len()) }
102+
let len = c_uint::try_from(elts.len()).expect("LLVMConstVector elements len overflow");
103+
unsafe { llvm::LLVMConstVector(elts.as_ptr(), len) }
103104
}
104105

105106
pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
@@ -329,7 +330,7 @@ pub fn val_ty(v: &Value) -> &Type {
329330
pub fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
330331
unsafe {
331332
let ptr = bytes.as_ptr() as *const c_char;
332-
llvm::LLVMRustConstStringInContext(llcx, ptr, bytes.len(), True)
333+
llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), True)
333334
}
334335
}
335336

@@ -338,7 +339,8 @@ pub fn struct_in_context<'ll>(
338339
elts: &[&'ll Value],
339340
packed: bool,
340341
) -> &'ll Value {
341-
unsafe { llvm::LLVMRustConstStructInContext(llcx, elts.as_ptr(), elts.len(), packed as Bool) }
342+
let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
343+
unsafe { llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), len, packed as Bool) }
342344
}
343345

344346
#[inline]

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,27 @@ extern "C" {
931931
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
932932
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
933933

934+
// Operations on composite constants
935+
pub fn LLVMConstArray2<'a>(
936+
ElementTy: &'a Type,
937+
ConstantVals: *const &'a Value,
938+
Length: size_t,
939+
) -> &'a Value;
940+
pub fn LLVMArrayType2(ElementType: &Type, ElementCount: u64) -> &Type;
941+
pub fn LLVMConstStringInContext2(
942+
C: &Context,
943+
Str: *const c_char,
944+
Length: size_t,
945+
DontNullTerminate: Bool,
946+
) -> &Value;
947+
pub fn LLVMConstStructInContext<'a>(
948+
C: &'a Context,
949+
ConstantVals: *const &'a Value,
950+
Count: c_uint,
951+
Packed: Bool,
952+
) -> &'a Value;
953+
pub fn LLVMConstVector(ScalarConstantVals: *const &Value, Size: c_uint) -> &Value;
954+
934955
// Constant expressions
935956
pub fn LLVMConstInBoundsGEP2<'a>(
936957
ty: &'a Type,
@@ -1510,9 +1531,6 @@ extern "C" {
15101531
/// See llvm::LLVMTypeKind::getTypeID.
15111532
pub fn LLVMRustGetTypeKind(Ty: &Type) -> TypeKind;
15121533

1513-
// Operations on array, pointer, and vector types (sequence types)
1514-
pub fn LLVMRustArrayType(ElementType: &Type, ElementCount: u64) -> &Type;
1515-
15161534
// Operations on all values
15171535
pub fn LLVMRustGlobalAddMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
15181536
pub fn LLVMRustIsNonGVFunctionPointerTy(Val: &Value) -> bool;
@@ -1526,31 +1544,6 @@ extern "C" {
15261544
low: &mut u64,
15271545
) -> bool;
15281546

1529-
// Operations on composite constants.
1530-
// They match LLVMConst* but use size_t lengths instead of unsigned int.
1531-
// See https://github.com/rust-lang/rust/issues/121868
1532-
// See https://llvm.org/doxygen/group__LLVMCCoreValueConstantComposite.html
1533-
pub fn LLVMRustConstStringInContext(
1534-
C: &Context,
1535-
Str: *const c_char,
1536-
Length: size_t,
1537-
DontNullTerminate: Bool,
1538-
) -> &Value;
1539-
pub fn LLVMRustConstStructInContext<'a>(
1540-
C: &'a Context,
1541-
ConstantVals: *const &'a Value,
1542-
Count: size_t,
1543-
Packed: Bool,
1544-
) -> &'a Value;
1545-
// FIXME: replace with LLVMConstArray2 when bumped minimal version to llvm-17
1546-
// https://github.com/llvm/llvm-project/commit/35276f16e5a2cae0dfb49c0fbf874d4d2f177acc
1547-
pub fn LLVMRustConstArray<'a>(
1548-
ElementTy: &'a Type,
1549-
ConstantVals: *const &'a Value,
1550-
Length: size_t,
1551-
) -> &'a Value;
1552-
pub fn LLVMRustConstVector(ScalarConstantVals: *const &Value, Size: size_t) -> &Value;
1553-
15541547
// Operations on global variables, functions, and aliases (globals)
15551548
pub fn LLVMRustGetLinkage(Global: &Value) -> Linkage;
15561549
pub fn LLVMRustSetLinkage(Global: &Value, RustLinkage: Linkage);

Diff for: compiler/rustc_codegen_llvm/src/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
233233
}
234234

235235
fn type_array(&self, ty: &'ll Type, len: u64) -> &'ll Type {
236-
unsafe { llvm::LLVMRustArrayType(ty, len) }
236+
unsafe { llvm::LLVMArrayType2(ty, len) }
237237
}
238238
}
239239

Diff for: compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+20-29
Original file line numberDiff line numberDiff line change
@@ -1214,14 +1214,6 @@ extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
12141214
}
12151215
}
12161216

1217-
// LLVMArrayType function does not support 64-bit ElementCount
1218-
// FIXME: replace with LLVMArrayType2 when bumped minimal version to llvm-17
1219-
// https://github.com/llvm/llvm-project/commit/35276f16e5a2cae0dfb49c0fbf874d4d2f177acc
1220-
extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
1221-
uint64_t ElementCount) {
1222-
return wrap(ArrayType::get(unwrap(ElementTy), ElementCount));
1223-
}
1224-
12251217
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
12261218

12271219
extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
@@ -2082,34 +2074,33 @@ extern "C" bool LLVMRustLLVMHasZstdCompressionForDebugSymbols() {
20822074
}
20832075

20842076
// Operations on composite constants.
2085-
// They match LLVMConst* but use size_t lengths instead of unsigned int.
2077+
// These are clones of LLVM api functions that will become available in future releases.
2078+
// They can be removed once Rust's minimum supported LLVM version supports them.
20862079
// See https://github.com/rust-lang/rust/issues/121868
20872080
// See https://llvm.org/doxygen/group__LLVMCCoreValueConstantComposite.html
2088-
extern "C" LLVMValueRef LLVMRustConstStringInContext(LLVMContextRef C,
2089-
const char *Str,
2090-
size_t Length,
2091-
bool DontNullTerminate) {
2092-
return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), !DontNullTerminate));
2093-
}
20942081

2095-
// See LLVMRustConstStringInContext
2096-
extern "C" LLVMValueRef LLVMRustConstStructInContext(LLVMContextRef C,
2097-
LLVMValueRef *ConstantVals,
2098-
size_t Count,
2099-
bool Packed) {
2100-
Constant **Elements = unwrap<Constant>(ConstantVals, Count);
2101-
return wrap(ConstantStruct::getAnon(*unwrap(C), ArrayRef(Elements, Count), Packed));
2082+
// FIXME Change version check once patch is accepted into LLVM
2083+
#if LLVM_VERSION_LT(999, 0)
2084+
extern "C" LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C,
2085+
const char *Str,
2086+
size_t Length,
2087+
bool DontNullTerminate) {
2088+
return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), !DontNullTerminate));
21022089
}
2090+
#endif
21032091

2104-
// See LLVMRustConstStringInContext
2105-
extern "C" LLVMValueRef LLVMRustConstArray(LLVMTypeRef ElementTy,
2106-
LLVMValueRef *ConstantVals,
2107-
size_t Length) {
2092+
// FIXME: Remove when Rust's minimum supported LLVM version reaches 17.
2093+
// https://github.com/llvm/llvm-project/commit/35276f16e5a2cae0dfb49c0fbf874d4d2f177acc
2094+
#if LLVM_VERSION_LT(17, 0)
2095+
extern "C" LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy,
2096+
LLVMValueRef *ConstantVals,
2097+
size_t Length) {
21082098
ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
21092099
return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
21102100
}
21112101

2112-
// See LLVMRustConstStringInContext
2113-
extern "C" LLVMValueRef LLVMRustConstVector(LLVMValueRef *ScalarConstantVals, size_t Size) {
2114-
return wrap(ConstantVector::get(ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
2102+
extern "C" LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementTy,
2103+
uint64_t ElementCount) {
2104+
return wrap(ArrayType::get(unwrap(ElementTy), ElementCount));
21152105
}
2106+
#endif

0 commit comments

Comments
 (0)