{"action":"closed","issue":{"url":"https://api.github.com/repos/mmastrac/rust-ctor/issues/16","repository_url":"https://api.github.com/repos/mmastrac/rust-ctor","labels_url":"https://api.github.com/repos/mmastrac/rust-ctor/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/mmastrac/rust-ctor/issues/16/comments","events_url":"https://api.github.com/repos/mmastrac/rust-ctor/issues/16/events","html_url":"https://github.com/mmastrac/rust-ctor/issues/16","id":434147033,"node_id":"MDU6SXNzdWU0MzQxNDcwMzM=","number":16,"title":"Cross language plugin LTO panics","user":{"login":"pquux","id":49182726,"node_id":"MDQ6VXNlcjQ5MTgyNzI2","avatar_url":"https://avatars0.githubusercontent.com/u/49182726?v=4","gravatar_id":"","url":"https://api.github.com/users/pquux","html_url":"https://github.com/pquux","followers_url":"https://api.github.com/users/pquux/followers","following_url":"https://api.github.com/users/pquux/following{/other_user}","gists_url":"https://api.github.com/users/pquux/gists{/gist_id}","starred_url":"https://api.github.com/users/pquux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pquux/subscriptions","organizations_url":"https://api.github.com/users/pquux/orgs","repos_url":"https://api.github.com/users/pquux/repos","events_url":"https://api.github.com/users/pquux/events{/privacy}","received_events_url":"https://api.github.com/users/pquux/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2019-04-17T08:06:40Z","updated_at":"2019-10-27T00:23:20Z","closed_at":"2019-10-27T00:23:20Z","author_association":"NONE","body":"Cross language Link Time Optimization (LTO) is a nice feature where instead of compiling binary object files, we have clang create llvm bitcode which we link together and optimize when we generate an output binary. This is particularly useful for rust FFI cases, where we can compile a legacy C application and perform LTO when linking it with rust, thus hopefully removing some of the FFI overhead on hot codepaths.\r\n\r\nMore info can be found [here](https://doc.rust-lang.org/rustc/linker-plugin-lto.html) and [here](https://github.com/rust-lang/rust/pull/58057)\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate ctor;\r\n\r\nuse std::collections::HashMap;\r\n\r\n#[ctor]\r\n/// This is an immutable static, evaluated at init time\r\nstatic STATIC_CTOR: HashMap<u32, &'static str> = {\r\n    let mut m = HashMap::new();\r\n    m.insert(0, \"foo\");\r\n    m.insert(1, \"bar\");\r\n    m.insert(2, \"baz\");\r\n    m\r\n};\r\n\r\nfn main() {\r\n    println!(\"{:x} -> {}\", 1, STATIC_CTOR.get(&1).unwrap());\r\n}\r\n```\r\n\r\nYou can compile with:\r\n```\r\n$ RUSTFLAGS=\"-Clinker-plugin-lto -Clinker=clang-8 -Clink-arg=-fuse-ld=lld-8\" cargo run --release\r\n   Compiling proc-macro2 v0.4.27\r\n   Compiling unicode-xid v0.1.0\r\n   Compiling syn v0.15.31\r\n   Compiling quote v0.6.12\r\n   Compiling ctor v0.1.9\r\n   Compiling ctor-opt v0.1.0 (/home/x/ctor-opt)\r\n    Finished release [optimized] target(s) in 51.82s\r\n     Running `target/release/ctor-opt`\r\nthread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21\r\nstack backtrace:\r\n   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace\r\n             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39\r\n   1: std::sys_common::backtrace::_print\r\n             at src/libstd/sys_common/backtrace.rs:71\r\n   2: std::panicking::default_hook::{{closure}}\r\n             at src/libstd/sys_common/backtrace.rs:59\r\n             at src/libstd/panicking.rs:197\r\n   3: std::panicking::default_hook\r\n             at src/libstd/panicking.rs:211\r\n   4: std::panicking::rust_panic_with_hook\r\n             at src/libstd/panicking.rs:474\r\n   5: std::panicking::continue_panic_fmt\r\n             at src/libstd/panicking.rs:381\r\n   6: rust_begin_unwind\r\n             at src/libstd/panicking.rs:308\r\n   7: core::panicking::panic_fmt\r\n             at src/libcore/panicking.rs:85\r\n   8: core::panicking::panic\r\n             at src/libcore/panicking.rs:49\r\n   9: ctor_opt::main\r\n  10: std::rt::lang_start::{{closure}}\r\n  11: std::panicking::try::do_call\r\n             at src/libstd/rt.rs:49\r\n             at src/libstd/panicking.rs:293\r\n  12: __rust_maybe_catch_panic\r\n             at src/libpanic_unwind/lib.rs:85\r\n  13: std::rt::lang_start_internal\r\n             at src/libstd/panicking.rs:272\r\n             at src/libstd/panic.rs:388\r\n             at src/libstd/rt.rs:48\r\n  14: main\r\n  15: __libc_start_main\r\n  16: _start\r\n```\r\n\r\nThis was tested on debian sid with rust nightly, clang-8 and lld-8 packages installed. Note that the clang and lld version unfortunately do need to match the rust llvm version."}}
{"actor":{"display_login":"pquux"}}