Skip to main content

teeny_compiler/compiler/target/
cuda.rs

1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::collections::HashMap;
18
19use crate::errors::{Error, Result};
20use teeny_core::compiler::Capability;
21
22/// A CUDA compilation target: a single GPU compute capability.
23pub struct Target {
24    /// The target GPU's compute capability.
25    pub capability: Capability,
26}
27
28impl Target {
29    /// Creates a target for the given compute `capability`.
30    pub fn new(capability: Capability) -> Self {
31        Self { capability }
32    }
33}
34
35impl teeny_core::compiler::Target for Target {
36    fn target_cpu(&self) -> Option<String> {
37        Some(self.capability.to_string())
38    }
39}
40
41impl TryFrom<(i32, i32)> for Target {
42    type Error = anyhow::Error;
43
44    fn try_from((major, minor): (i32, i32)) -> Result<Self> {
45        let capabilities: HashMap<i32, Capability> = vec![
46            (75, Capability::Sm75),
47            (80, Capability::Sm80),
48            (86, Capability::Sm86),
49            (87, Capability::Sm87),
50            (89, Capability::Sm89),
51            (90, Capability::Sm90),
52            (100, Capability::Sm100),
53            (120, Capability::Sm120),
54        ]
55        .into_iter()
56        .collect();
57        let capability = capabilities
58            .get(&(major * 10 + minor))
59            .cloned()
60            .ok_or_else(|| {
61                Error::UnknownCapability(format!("Capability not found: {major}.{minor}"))
62            })?;
63
64        Ok(Self { capability })
65    }
66}