Skip to content

Commit 2a1786f

Browse files
committed
rustllvm: Add LLVMRustArrayType
LLVM internally uses `uint64_t` for array size, but the corresponding C API (`LLVMArrayType`) uses `unsigned int` so ths value is truncated. Therefore rustc generates wrong type for fixed-sized large vector e.g. `[0 x i8]` for `[0u8, ..(1 << 32)]`. This patch adds `LLVMRustArrayType` function for `uint64_t` support.
1 parent e8053b9 commit 2a1786f

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/librustc/lib/llvm.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ pub mod llvm {
398398
pub fn LLVMIsPackedStruct(StructTy: TypeRef) -> Bool;
399399

400400
/* Operations on array, pointer, and vector types (sequence types) */
401-
pub fn LLVMArrayType(ElementType: TypeRef, ElementCount: c_uint)
402-
-> TypeRef;
401+
pub fn LLVMRustArrayType(ElementType: TypeRef, ElementCount: u64) -> TypeRef;
403402
pub fn LLVMPointerType(ElementType: TypeRef, AddressSpace: c_uint)
404403
-> TypeRef;
405404
pub fn LLVMVectorType(ElementType: TypeRef, ElementCount: c_uint)

src/librustc/middle/trans/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl Type {
207207
}
208208

209209
pub fn array(ty: &Type, len: u64) -> Type {
210-
ty!(llvm::LLVMArrayType(ty.to_ref(), len as c_uint))
210+
ty!(llvm::LLVMRustArrayType(ty.to_ref(), len))
211211
}
212212

213213
pub fn vector(ty: &Type, len: u64) -> Type {

src/rustllvm/RustWrapper.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,9 @@ LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) {
754754
*ptr = ret.data();
755755
return ret.size();
756756
}
757+
758+
// LLVMArrayType function does not support 64-bit ElementCount
759+
extern "C" LLVMTypeRef
760+
LLVMRustArrayType(LLVMTypeRef ElementType, uint64_t ElementCount) {
761+
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
762+
}

src/test/run-pass/vec-fixed-length.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::mem::size_of;
12+
1113
pub fn main() {
1214
let x: [int, ..4] = [1, 2, 3, 4];
13-
println!("{}", x[0]);
15+
assert_eq!(x[0], 1);
16+
assert_eq!(x[1], 2);
17+
assert_eq!(x[2], 3);
18+
assert_eq!(x[3], 4);
19+
20+
assert_eq!(size_of::<[u8, ..4]>(), 4u);
21+
22+
// FIXME #10183
23+
if cfg!(target_word_size = "64") {
24+
assert_eq!(size_of::<[u8, ..(1 << 32)]>(), (1u << 32));
25+
}
1426
}

0 commit comments

Comments
 (0)