Skip to content

Commit

Permalink
Fix the representation of C void pointers in LLVM IR
Browse files Browse the repository at this point in the history
Currently, we have c_void defined to be represented as an empty struct,
but LLVM expects C's void* to be represented as i8*. That means we
currently generate code in which LLVM doesn't recognize malloc() and
free() and can't apply certain optimization that would remove calls to
those functions.
  • Loading branch information
dotdash committed Jan 14, 2014
1 parent 77eedda commit 5902263
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/libstd/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,19 @@ pub mod types {
This type is only useful as a pointer target. Do not use it as a
return type for FFI functions which have the `void` return type in
C. Use the unit type `()` or omit the return type instead.
For LLVM to recognize the void pointer type and by extension
functions like malloc(), we need to have it represented as i8* in
LLVM bitcode. The enum used here ensures this and prevents misuse
of the "raw" type by only having private variants.. We need two
variants, because the compiler complains about the repr attribute
otherwise.
*/
pub enum c_void {}
#[repr(u8)]
pub enum c_void {
priv variant1,
priv variant2
}
pub enum FILE {}
pub enum fpos_t {}
}
Expand Down

5 comments on commit 5902263

@bors
Copy link
Contributor

@bors bors commented on 5902263 Jan 14, 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 dotdash@5902263

@bors
Copy link
Contributor

@bors bors commented on 5902263 Jan 14, 2014

Choose a reason for hiding this comment

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

merging dotdash/rust/void_type_fixup = 5902263 into auto

@bors
Copy link
Contributor

@bors bors commented on 5902263 Jan 14, 2014

Choose a reason for hiding this comment

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

dotdash/rust/void_type_fixup = 5902263 merged ok, testing candidate = b77a7e7

@bors
Copy link
Contributor

@bors bors commented on 5902263 Jan 14, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 5902263 Jan 14, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = b77a7e7

Please sign in to comment.