|
| 1 | +//! Run-time feature detection utility for RISC-V. |
| 2 | +//! |
| 3 | +//! On RISC-V, full feature detection needs a help of one or more |
| 4 | +//! feature detection mechanisms (usually provided by the operating system). |
| 5 | +//! |
| 6 | +//! RISC-V architecture defines many extensions and some have dependency to others. |
| 7 | +//! More importantly, some of them cannot be enabled without resolving such |
| 8 | +//! dependencies due to limited set of features that such mechanisms provide. |
| 9 | +//! |
| 10 | +//! This module provides an OS-independent utility to process such relations |
| 11 | +//! between RISC-V extensions. |
| 12 | +
|
| 13 | +use crate::detect::{Feature, cache}; |
| 14 | + |
| 15 | +/// Imply features by the given set of enabled features. |
| 16 | +/// |
| 17 | +/// Note that it does not perform any consistency checks including existence of |
| 18 | +/// conflicting extensions and/or complicated requirements. Eliminating such |
| 19 | +/// inconsistencies is the responsibility of the feature detection logic and |
| 20 | +/// its provider(s). |
| 21 | +pub(crate) fn imply_features(mut value: cache::Initializer) -> cache::Initializer { |
| 22 | + loop { |
| 23 | + // Check convergence of the feature flags later. |
| 24 | + let prev = value; |
| 25 | + |
| 26 | + // Expect that the optimizer turns repeated operations into |
| 27 | + // a fewer number of bit-manipulation operations. |
| 28 | + macro_rules! imply { |
| 29 | + // Regular implication: |
| 30 | + // A1 => (B1[, B2...]), A2 => (B1[, B2...]) and so on. |
| 31 | + ($($from: ident)|+ => $($to: ident)&+) => { |
| 32 | + if [$(Feature::$from as u32),+].iter().any(|&x| value.test(x)) { |
| 33 | + $( |
| 34 | + value.set(Feature::$to as u32); |
| 35 | + )+ |
| 36 | + } |
| 37 | + }; |
| 38 | + // Implication with multiple requirements: |
| 39 | + // A1 && A2 ... => (B1[, B2...]). |
| 40 | + ($($from: ident)&+ => $($to: ident)&+) => { |
| 41 | + if [$(Feature::$from as u32),+].iter().all(|&x| value.test(x)) { |
| 42 | + $( |
| 43 | + value.set(Feature::$to as u32); |
| 44 | + )+ |
| 45 | + } |
| 46 | + }; |
| 47 | + } |
| 48 | + macro_rules! group { |
| 49 | + ($group: ident == $($member: ident)&+) => { |
| 50 | + // Forward implication as defined in the specifications. |
| 51 | + imply!($group => $($member)&+); |
| 52 | + // Reverse implication to "group extension" from its members. |
| 53 | + // This is not a part of specifications but convenient for |
| 54 | + // feature detection and implemented in e.g. LLVM. |
| 55 | + imply!($($member)&+ => $group); |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + If a dependency/implication is not explicitly stated in the |
| 61 | + specification, it is denoted as a comment as follows: |
| 62 | + "defined as subset": |
| 63 | + The latter extension is described as a subset of the former |
| 64 | + (but the evidence is weak). |
| 65 | + */ |
| 66 | + |
| 67 | + imply!(zbc => zbkc); // defined as subset |
| 68 | + group!(zkn == zbkb & zbkc & zbkx & zkne & zknd & zknh); |
| 69 | + group!(zks == zbkb & zbkc & zbkx & zksed & zksh); |
| 70 | + group!(zk == zkn & zkr & zkt); |
| 71 | + |
| 72 | + group!(a == zalrsc & zaamo); |
| 73 | + |
| 74 | + group!(b == zba & zbb & zbs); |
| 75 | + |
| 76 | + imply!(zhinx => zhinxmin); |
| 77 | + imply!(zdinx | zhinxmin => zfinx); |
| 78 | + |
| 79 | + imply!(zfh => zfhmin); |
| 80 | + imply!(q => d); |
| 81 | + imply!(d | zfhmin => f); |
| 82 | + |
| 83 | + imply!(zicntr | zihpm | f | zfinx => zicsr); |
| 84 | + imply!(s | h => zicsr); |
| 85 | + |
| 86 | + // Loop until the feature flags converge. |
| 87 | + if prev == value { |
| 88 | + return value; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn simple_direct() { |
| 99 | + let mut value = cache::Initializer::default(); |
| 100 | + value.set(Feature::f as u32); |
| 101 | + // F (and other extensions with CSRs) -> Zicsr |
| 102 | + assert!(imply_features(value).test(Feature::zicsr as u32)); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn simple_indirect() { |
| 107 | + let mut value = cache::Initializer::default(); |
| 108 | + value.set(Feature::q as u32); |
| 109 | + // Q -> D, D -> F, F -> Zicsr |
| 110 | + assert!(imply_features(value).test(Feature::zicsr as u32)); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn group_simple_forward() { |
| 115 | + let mut value = cache::Initializer::default(); |
| 116 | + // A -> Zalrsc & Zaamo (forward implication) |
| 117 | + value.set(Feature::a as u32); |
| 118 | + let value = imply_features(value); |
| 119 | + assert!(value.test(Feature::zalrsc as u32)); |
| 120 | + assert!(value.test(Feature::zaamo as u32)); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn group_simple_backward() { |
| 125 | + let mut value = cache::Initializer::default(); |
| 126 | + // Zalrsc & Zaamo -> A (reverse implication) |
| 127 | + value.set(Feature::zalrsc as u32); |
| 128 | + value.set(Feature::zaamo as u32); |
| 129 | + assert!(imply_features(value).test(Feature::a as u32)); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn group_complex_convergence() { |
| 134 | + let mut value = cache::Initializer::default(); |
| 135 | + // Needs 2 iterations to converge |
| 136 | + // (and 3rd iteration for convergence checking): |
| 137 | + // 1. [Zk] -> Zkn & Zkr & Zkt |
| 138 | + // 2. Zkn -> {Zbkb} & {Zbkc} & {Zbkx} & {Zkne} & {Zknd} & {Zknh} |
| 139 | + value.set(Feature::zk as u32); |
| 140 | + let value = imply_features(value); |
| 141 | + assert!(value.test(Feature::zbkb as u32)); |
| 142 | + assert!(value.test(Feature::zbkc as u32)); |
| 143 | + assert!(value.test(Feature::zbkx as u32)); |
| 144 | + assert!(value.test(Feature::zkne as u32)); |
| 145 | + assert!(value.test(Feature::zknd as u32)); |
| 146 | + assert!(value.test(Feature::zknh as u32)); |
| 147 | + } |
| 148 | +} |
0 commit comments