|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use anyhow::Context; |
| 4 | + |
| 5 | +use crate::Optimization; |
| 6 | +use crate::Target; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct Session { |
| 10 | + target: Target, |
| 11 | + cpu: Option<String>, |
| 12 | + symbols: Vec<String>, |
| 13 | + |
| 14 | + /// A file that `llvm-link` supports, like a bitcode file or an archive. |
| 15 | + files: Vec<PathBuf>, |
| 16 | + |
| 17 | + // Output files |
| 18 | + link_path: PathBuf, |
| 19 | + opt_path: PathBuf, |
| 20 | + sym_path: PathBuf, |
| 21 | + out_path: PathBuf, |
| 22 | +} |
| 23 | + |
| 24 | +impl Session { |
| 25 | + pub fn new(target: crate::Target, cpu: Option<String>, out_path: PathBuf) -> Self { |
| 26 | + let link_path = out_path.with_extension("o"); |
| 27 | + let opt_path = out_path.with_extension("optimized.o"); |
| 28 | + let sym_path = out_path.with_extension("symbols.txt"); |
| 29 | + |
| 30 | + Session { |
| 31 | + target, |
| 32 | + cpu, |
| 33 | + symbols: Vec::new(), |
| 34 | + files: Vec::new(), |
| 35 | + link_path, |
| 36 | + opt_path, |
| 37 | + sym_path, |
| 38 | + out_path, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Add a file, like an rlib or bitcode file that should be linked |
| 43 | + pub fn add_file(&mut self, path: PathBuf) { |
| 44 | + self.files.push(path); |
| 45 | + } |
| 46 | + |
| 47 | + /// Add a Vec of symbols to the list of exported symbols |
| 48 | + pub fn add_exported_symbols(&mut self, symbols: Vec<String>) { |
| 49 | + self.symbols.extend(symbols); |
| 50 | + } |
| 51 | + |
| 52 | + /// Reads every file that was added to the session and link them without optimization. |
| 53 | + /// |
| 54 | + /// The resulting artifact will be written to a file that can later be read to perform |
| 55 | + /// optimizations and/or compilation from bitcode to the final artifact. |
| 56 | + fn link(&mut self) -> anyhow::Result<()> { |
| 57 | + tracing::info!("Linking {} files using llvm-link", self.files.len()); |
| 58 | + |
| 59 | + let llvm_link_output = std::process::Command::new("llvm-link") |
| 60 | + .arg("--ignore-non-bitcode") |
| 61 | + .args(&self.files) |
| 62 | + .arg("-o") |
| 63 | + .arg(&self.link_path) |
| 64 | + .output() |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + if !llvm_link_output.status.success() { |
| 68 | + tracing::error!( |
| 69 | + "llvm-link returned with Exit status: {}\n stdout: {}\n stderr: {}", |
| 70 | + llvm_link_output.status, |
| 71 | + String::from_utf8(llvm_link_output.stdout).unwrap(), |
| 72 | + String::from_utf8(llvm_link_output.stderr).unwrap(), |
| 73 | + ); |
| 74 | + anyhow::bail!("llvm-link failed to link files {:?}", self.files); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | + |
| 80 | + /// Optimize and compile to native format using `opt` and `llc` |
| 81 | + /// |
| 82 | + /// Before this can be called `link` needs to be called |
| 83 | + fn optimize(&mut self, optimization: Optimization, mut debug: bool) -> anyhow::Result<()> { |
| 84 | + let mut passes = format!("default<{}>", optimization); |
| 85 | + |
| 86 | + // FIXME(@kjetilkjeka) Debug symbol generation is broken for nvptx64 so we must remove them even in debug mode |
| 87 | + if debug && self.target == crate::Target::Nvptx64NvidiaCuda { |
| 88 | + tracing::warn!("nvptx64 target detected - stripping debug symbols"); |
| 89 | + debug = false; |
| 90 | + } |
| 91 | + |
| 92 | + // We add an internalize pass as the rust compiler as we require exported symbols to be explicitly marked |
| 93 | + passes.push_str(",internalize,globaldce"); |
| 94 | + let symbol_file_content = self.symbols.iter().fold(String::new(), |s, x| s + &x + "\n"); |
| 95 | + std::fs::write(&self.sym_path, symbol_file_content) |
| 96 | + .context(format!("Failed to write symbol file: {}", self.sym_path.display()))?; |
| 97 | + |
| 98 | + tracing::info!("optimizing bitcode with passes: {}", passes); |
| 99 | + let mut opt_cmd = std::process::Command::new("opt"); |
| 100 | + opt_cmd |
| 101 | + .arg(&self.link_path) |
| 102 | + .arg("-o") |
| 103 | + .arg(&self.opt_path) |
| 104 | + .arg(format!("--internalize-public-api-file={}", self.sym_path.display())) |
| 105 | + .arg(format!("--passes={}", passes)); |
| 106 | + |
| 107 | + if !debug { |
| 108 | + opt_cmd.arg("--strip-debug"); |
| 109 | + } |
| 110 | + |
| 111 | + let opt_output = opt_cmd.output().unwrap(); |
| 112 | + |
| 113 | + if !opt_output.status.success() { |
| 114 | + tracing::error!( |
| 115 | + "opt returned with Exit status: {}\n stdout: {}\n stderr: {}", |
| 116 | + opt_output.status, |
| 117 | + String::from_utf8(opt_output.stdout).unwrap(), |
| 118 | + String::from_utf8(opt_output.stderr).unwrap(), |
| 119 | + ); |
| 120 | + anyhow::bail!("opt failed optimize bitcode: {}", self.link_path.display()); |
| 121 | + }; |
| 122 | + |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | + |
| 126 | + /// Compile the optimized bitcode file to native format using `llc` |
| 127 | + /// |
| 128 | + /// Before this can be called `optimize` needs to be called |
| 129 | + fn compile(&mut self) -> anyhow::Result<()> { |
| 130 | + let mut lcc_command = std::process::Command::new("llc"); |
| 131 | + |
| 132 | + if let Some(mcpu) = &self.cpu { |
| 133 | + lcc_command.arg("--mcpu").arg(mcpu); |
| 134 | + } |
| 135 | + |
| 136 | + let lcc_output = |
| 137 | + lcc_command.arg(&self.opt_path).arg("-o").arg(&self.out_path).output().unwrap(); |
| 138 | + |
| 139 | + if !lcc_output.status.success() { |
| 140 | + tracing::error!( |
| 141 | + "llc returned with Exit status: {}\n stdout: {}\n stderr: {}", |
| 142 | + lcc_output.status, |
| 143 | + String::from_utf8(lcc_output.stdout).unwrap(), |
| 144 | + String::from_utf8(lcc_output.stderr).unwrap(), |
| 145 | + ); |
| 146 | + |
| 147 | + anyhow::bail!( |
| 148 | + "llc failed to compile {} into {}", |
| 149 | + self.opt_path.display(), |
| 150 | + self.out_path.display() |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + Ok(()) |
| 155 | + } |
| 156 | + |
| 157 | + /// Links, optimizes and compiles to the native format |
| 158 | + pub fn lto(&mut self, optimization: crate::Optimization, debug: bool) -> anyhow::Result<()> { |
| 159 | + self.link()?; |
| 160 | + self.optimize(optimization, debug)?; |
| 161 | + self.compile() |
| 162 | + } |
| 163 | +} |
0 commit comments