Skip to content

Commit

Permalink
Document #[repr(u8)] enum to enable LLVM optimizations
Browse files Browse the repository at this point in the history
See rust-lang/rust#27303 and rust-lang/libc#48 for discussion of the
optimization, or just try compiling `unsafe {free(malloc(16))}` with various
Rust definitions of `void *` in the FFI binding.
  • Loading branch information
geofft committed Apr 4, 2017
1 parent 616b984 commit 0a85061
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/other-reprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ same discriminant.
On non-C-like enums, this will inhibit certain optimizations like the null-
pointer optimization.

For C-like enums, these may also be useful to enable certain LLVM optimizations.
For instance, LLVM will recognize extern functions named `malloc` and `free` and
optimize away unnecessary calls, as long as the functions are of the right type:
`malloc` needs to return a C `void *`, which LLVM interprets as a pointer to
`u8`, and `free` needs to accept one. The libc crate uses `#[repr(u8)]` on its
`c_void` type to enable this optimization. However, note that this optimization
stops working if the enum is non-C-like, even if the only data is `()` or a
`PhantomData`!

These reprs have no effect on a struct.


Expand Down

0 comments on commit 0a85061

Please sign in to comment.