From 2812ed4aef57d65ca80ce48e332858b926bf16a2 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 6 Oct 2013 07:21:07 -0700 Subject: [PATCH] Created Mixed language link time optimization (markdown) --- Mixed-language-link-time-optimization.md | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Mixed-language-link-time-optimization.md diff --git a/Mixed-language-link-time-optimization.md b/Mixed-language-link-time-optimization.md new file mode 100644 index 0000000..72b7716 --- /dev/null +++ b/Mixed-language-link-time-optimization.md @@ -0,0 +1,67 @@ +main.c + +~~~ +#include + +uint32_t *foo(); + +int main() { + free(foo()); +} +~~~ + +foo.rs + +~~~ +#[no_std]; +#[allow(ctypes, cstack)]; + +extern { + fn malloc(size: uint) -> *mut u8; + fn free(ptr: *mut u8); + fn abort() -> !; +} + +#[lang = "exchange_malloc"] +#[inline(always)] +unsafe fn exchange_malloc(size: uint) -> *mut u8 { + let ptr = malloc(size); + if ptr == 0 as *mut u8 { + abort() + } + + ptr +} + +#[lang = "exchange_free"] +#[inline(always)] +unsafe fn exchange_free(ptr: *mut u8) { + free(ptr) +} + +#[no_mangle] +extern "C" fn foo() -> ~u32 { + ~5 +} +~~~ + +Build LLVM bytecode from the Rust code: + + $ rustc foo.rs --emit-llvm --lib -O + +The `--lib` flag is necessary even if Rust defines `main`, to avoid treating +the Rust part as the entire application. + +Compile together the C and Rust code with `clang`: + + $ clang foo.bc main.c -flto -O3 -o main + +Verify that LLVM eliminated the call to `foo` via inlining and removed the +allocation as a dead store: + + $ gdb main + (gdb) disassemble main + Dump of assembler code for function main: + 0x00000000004005b0 <+0>: xor %eax,%eax + 0x00000000004005b2 <+2>: retq + End of assembler dump. \ No newline at end of file