{"record":{"id":"435e6c7bcab30a37","repo":"rust-lang/rust","slug":"opt-failed-optimize-bitcode","errorCode":null,"errorMessage":"opt failed optimize bitcode: {}","messagePattern":"opt failed optimize bitcode: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/tools/llvm-bitcode-linker/src/linker.rs","lineNumber":122,"sourceCode":"            .arg(format!(\"--internalize-public-api-file={}\", self.sym_path.display()))\n            .arg(format!(\"--passes={}\", passes));\n\n        if !debug {\n            opt_cmd.arg(\"--strip-debug\");\n        }\n\n        let opt_output = opt_cmd.output().context(\n            \"An error occurred when calling opt. Make sure the llvm-tools component is installed.\",\n        )?;\n\n        if !opt_output.status.success() {\n            tracing::error!(\n                \"opt returned with Exit status: {}\\n stdout: {}\\n stderr: {}\",\n                opt_output.status,\n                String::from_utf8(opt_output.stdout).unwrap(),\n                String::from_utf8(opt_output.stderr).unwrap(),\n            );\n            anyhow::bail!(\"opt failed optimize bitcode: {}\", self.link_path.display());\n        };\n\n        Ok(())\n    }\n\n    /// Compile the optimized bitcode file to native format using `llc`\n    ///\n    /// Before this can be called `optimize` needs to be called\n    fn compile(&mut self) -> anyhow::Result<()> {\n        let mut lcc_command = std::process::Command::new(\"llc\");\n\n        if let Some(mcpu) = &self.cpu {\n            lcc_command.arg(\"--mcpu\").arg(mcpu);\n        }\n\n        if let Some(mattr) = &self.feature {\n            lcc_command.arg(&format!(\"--mattr={}\", mattr));\n        }","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/llvm-bitcode-linker/src/linker.rs#L104-L140","documentation":"Bailed by Session::optimize when the opt subprocess exits non-zero. opt runs the passes default<O>,internalize,globaldCE over the linked bitcode; failure indicates an invalid pass pipeline, an opt version that doesn't support the requested passes, or malformed input bitcode.","triggerScenarios":"Reached after a successful link(), when opt fails. Common when the opt binary is from a different LLVM than the one that produced the bitcode, or when the Optimization level is parsed into a value opt's default<> pipeline doesn't accept.","commonSituations":"PATH points at a system LLVM while rustc uses its bundled LLVM; an LLVM major-version bump renamed or removed a pass; the symbol file passed via --internalize-public-api-file is empty/malformed.","solutions":["Inspect the logged opt stderr for the exact pass/error.","Ensure the opt on PATH is the same LLVM version as rustc's bundled LLVM (rustup component llvm-tools).","Check that sym_path was written and contains one symbol per line.","Re-run with a lower Optimization level to isolate pass-specific failures."],"exampleFix":"// before\nanyhow::bail!(\"opt failed optimize bitcode: {}\", self.link_path.display());\n\n// after: include opt's stderr so callers can act without reading logs\nlet stderr = String::from_utf8_lossy(&opt_output.stderr);\nanyhow::bail!(\n    \"opt failed to optimize bitcode {} (passes={passes}): {}\",\n    self.link_path.display(),\n    stderr.trim()\n);","handlingStrategy":"validation","validationCode":"// Ensure the opt binary matches rustc's LLVM and supports the passes:\nfn opt_supports_passes(opt: &str, passes: &str) -> bool {\n    let out = std::process::Command::new(opt).arg(\"--help-passes\").output();\n    matches!(out, Ok(o) if String::from_utf8_lossy(&o.stdout).contains(passes)))\n}","typeGuard":"null","tryCatchPattern":"match session.lto(opt, debug) {\n    Ok(()) => Ok(()),\n    Err(e) if e.to_string().contains(\"opt failed\") => {\n        eprintln!(\"opt rejected the pass pipeline; check LLVM version alignment\");\n        Err(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Use the opt from rustup's llvm-tools, not a system LLVM.","Pin the LLVM major version across the toolchain.","Write the symbol file before invoking opt and assert it is non-empty when symbols are expected."],"tags":["llvm","optimizer","bitcode","subprocess","llvm-bitcode-linker"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}