Skip to content

Commit

Permalink
rustllvm: Add LLVMRustArrayType
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
klutzy committed May 13, 2014
1 parent e8053b9 commit 2a1786f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ pub mod llvm {
pub fn LLVMIsPackedStruct(StructTy: TypeRef) -> Bool;

/* Operations on array, pointer, and vector types (sequence types) */
pub fn LLVMArrayType(ElementType: TypeRef, ElementCount: c_uint)
-> TypeRef;
pub fn LLVMRustArrayType(ElementType: TypeRef, ElementCount: u64) -> TypeRef;
pub fn LLVMPointerType(ElementType: TypeRef, AddressSpace: c_uint)
-> TypeRef;
pub fn LLVMVectorType(ElementType: TypeRef, ElementCount: c_uint)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Type {
}

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

pub fn vector(ty: &Type, len: u64) -> Type {
Expand Down
6 changes: 6 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,9 @@ LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) {
*ptr = ret.data();
return ret.size();
}

// LLVMArrayType function does not support 64-bit ElementCount
extern "C" LLVMTypeRef
LLVMRustArrayType(LLVMTypeRef ElementType, uint64_t ElementCount) {
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
}
14 changes: 13 additions & 1 deletion src/test/run-pass/vec-fixed-length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem::size_of;

pub fn main() {
let x: [int, ..4] = [1, 2, 3, 4];
println!("{}", x[0]);
assert_eq!(x[0], 1);
assert_eq!(x[1], 2);
assert_eq!(x[2], 3);
assert_eq!(x[3], 4);

assert_eq!(size_of::<[u8, ..4]>(), 4u);

// FIXME #10183
if cfg!(target_word_size = "64") {
assert_eq!(size_of::<[u8, ..(1 << 32)]>(), (1u << 32));
}
}

4 comments on commit 2a1786f

@bors
Copy link
Contributor

@bors bors commented on 2a1786f May 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at klutzy@2a1786f

@bors
Copy link
Contributor

@bors bors commented on 2a1786f May 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging klutzy/rust/llvmrust-arraytype = 2a1786f into auto

@bors
Copy link
Contributor

@bors bors commented on 2a1786f May 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

klutzy/rust/llvmrust-arraytype = 2a1786f merged ok, testing candidate = 049f7625

Please sign in to comment.