Skip to main content

code_transpiler/
capability_matrix.rs

1use crate::call_resolution::EXACT_CALL_RESOLUTION_CAPABILITY;
2use crate::matrix::SparseMatrix;
3use crate::registry::{backend_capability, backends, CapabilityStatus};
4use crate::signature::EXACT_SIGNATURE_CAPABILITY;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct CapabilityMatrix {
8    pub features: Vec<String>,
9    pub targets: Vec<String>,
10    pub native: SparseMatrix,
11    pub lowering: SparseMatrix,
12    pub emulated: SparseMatrix,
13    pub unsupported: SparseMatrix,
14}
15
16pub fn semantic_capability_matrix(extra: &[String]) -> CapabilityMatrix {
17    let mut features = vec![
18        "core",
19        "integer.int32.exact",
20        "integer.uint32.exact",
21        "integer.int64.exact",
22        "integer.uint64.exact",
23        "integer.arbitrary",
24        "pointer",
25        "ownership.borrow",
26        "exceptions",
27        "classes",
28        "generics",
29        "concurrency",
30        "ffi",
31        "reflection",
32        "gpu.compute",
33        "index.zero_based",
34        "index.negative",
35        "native.go.lowering",
36        "native.go.scalar",
37        "native.go.functions",
38        "native.c.lowering",
39        "native.rust.lowering",
40        "integer.int8.exact",
41        "integer.uint8.exact",
42        "integer.int16.exact",
43        "integer.uint16.exact",
44        "integer.operations.v1",
45        EXACT_SIGNATURE_CAPABILITY,
46        EXACT_CALL_RESOLUTION_CAPABILITY,
47    ]
48    .into_iter()
49    .map(String::from)
50    .collect::<Vec<_>>();
51    for f in extra {
52        if !features.contains(f) {
53            features.push(f.clone());
54        }
55    }
56    let targets = backends()
57        .iter()
58        .map(|b| b.id.to_string())
59        .collect::<Vec<_>>();
60    let n = features.len();
61    let k = targets.len();
62    let mut m = CapabilityMatrix {
63        features,
64        targets,
65        native: SparseMatrix::new(n, k),
66        lowering: SparseMatrix::new(n, k),
67        emulated: SparseMatrix::new(n, k),
68        unsupported: SparseMatrix::new(n, k),
69    };
70    for i in 0..n {
71        for j in 0..k {
72            match backend_capability(&m.features[i], &m.targets[j]).status {
73                CapabilityStatus::Native => m.native.set(i, j, 1.0),
74                CapabilityStatus::Lowering => m.lowering.set(i, j, 1.0),
75                CapabilityStatus::Emulated => m.emulated.set(i, j, 1.0),
76                CapabilityStatus::Unsupported => m.unsupported.set(i, j, 1.0),
77            }
78        }
79    }
80    m
81}
82impl CapabilityMatrix {
83    pub fn rejected_targets(&self, requirements: &[String]) -> Result<SparseMatrix, String> {
84        let mut vector = SparseMatrix::new(1, self.features.len());
85        for requirement in requirements {
86            let i = self
87                .features
88                .iter()
89                .position(|f| f == requirement)
90                .ok_or_else(|| {
91                    format!(
92                        "requirement {:?} missing from capability matrix",
93                        requirement
94                    )
95                })?;
96            vector.set(0, i, 1.0);
97        }
98        vector.multiply(&self.unsupported)
99    }
100}