Read the /proc/cpuinfo file and detect the extensions. We are looking for the isa line string, which contains the extensions. The format for this string is specified in the linux user space ABI for RISC-V: https://github.com/torvalds/linux/blob/09a9639e56c01c7a00d6c0ca63f4c7c41abe075d/Documentation/riscv/uabi.rst The format is fairly similar to the one specified in the RISC-V ISA manual, but all
(isa_builder: &mut dyn Configurable)
| 58 | /// |
| 59 | /// An example ISA string is: rv64imafdcvh_zawrs_zba_zbb_zicbom_zicboz_zicsr_zifencei_zihintpause |
| 60 | pub fn cpuinfo_detect(isa_builder: &mut dyn Configurable) -> Result<(), &'static str> { |
| 61 | let file = File::open("/proc/cpuinfo").map_err(|_| "failed to open /proc/cpuinfo")?; |
| 62 | |
| 63 | let isa_string = BufReader::new(file) |
| 64 | .lines() |
| 65 | .filter_map(Result::ok) |
| 66 | .find_map(|line| { |
| 67 | if let Some((k, v)) = line.split_once(':') { |
| 68 | if k.trim_end() == "isa" { |
| 69 | return Some(v.trim().to_string()); |
| 70 | } |
| 71 | } |
| 72 | None |
| 73 | }) |
| 74 | .ok_or("failed to find isa line in /proc/cpuinfo")?; |
| 75 | |
| 76 | for ext in isa_string_extensions(&isa_string) { |
| 77 | // Try enabling all the extensions that are parsed. |
| 78 | // Cranelift won't recognize all of them, but that's okay we just ignore them. |
| 79 | // Extensions flags in the RISC-V backend have the format of `has_x` for the `x` extension. |
| 80 | let _ = isa_builder.enable(&format!("has_{ext}")); |
| 81 | } |
| 82 | |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | /// Parses an ISA string and returns an iterator over the extensions. |
| 87 | fn isa_string_extensions(isa: &str) -> Vec<&str> { |
no test coverage detected