|
| 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 | + group!(zkn == zbkb & zbkc & zbkx & zkne & zknd & zknh); |
| 60 | + group!(zks == zbkb & zbkc & zbkx & zksed & zksh); |
| 61 | + group!(zk == zkn & zkr & zkt); |
| 62 | + |
| 63 | + group!(a == zalrsc & zaamo); |
| 64 | + |
| 65 | + group!(b == zba & zbb & zbs); |
| 66 | + |
| 67 | + imply!(zhinx => zhinxmin); |
| 68 | + imply!(zdinx | zhinxmin => zfinx); |
| 69 | + |
| 70 | + imply!(zfh => zfhmin); |
| 71 | + imply!(q => d); |
| 72 | + imply!(d | zfhmin => f); |
| 73 | + |
| 74 | + imply!(zicntr | zihpm | zkr | f | zfinx => zicsr); |
| 75 | + imply!(s | h => zicsr); |
| 76 | + |
| 77 | + // Loop until the feature flags converge. |
| 78 | + if prev == value { |
| 79 | + return value; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments