tvm_tirx/codegen.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20//! Code generation. [`build_module`] compiles an [`IRModule`] into a runtime [`Module`] with the
21//! code generator of the target's kind, and [`llvm`] queries the LLVM backend.
22
23// tvm-ffi-stubgen(file): mod(missing_docs = allow)
24// Names the hand-written wrappers use: the import section keeps them even when no generated block
25// does.
26// tvm-ffi-stubgen(import): tvm_ffi::Module
27// tvm-ffi-stubgen(import): tvm_ffi::Result
28// tvm-ffi-stubgen(import): tvm_ffi::String
29// tvm-ffi-stubgen(import): crate::ir::module::IRModule
30// tvm-ffi-stubgen(import): crate::target::Target
31// tvm-ffi-stubgen(import): tvm_ffi::IntoArg
32// tvm-ffi-stubgen(import): std::borrow::Borrow
33
34pub mod llvm;
35
36// tvm-ffi-stubgen(begin): import-section
37use crate::ir::module::IRModule;
38use crate::target::Target;
39use std::borrow::Borrow;
40use tvm_ffi::IntoArg;
41use tvm_ffi::Module;
42use tvm_ffi::Result;
43use tvm_ffi::String;
44// tvm-ffi-stubgen(end)
45
46/// Compile `module` into a runtime module with the code generator of the kind of `target`
47/// (`codegen.Build`).
48///
49/// # Errors
50///
51/// Returns an error if the code generator of the kind is not built in or loaded, or the error it
52/// raises.
53///
54/// # Examples
55///
56/// ```
57/// use tvm_tirx::codegen;
58/// use tvm_tirx::ir::{DictAttrs, GlobalVar, IRModule, PrimVar, UNKNOWN_LOC};
59/// use tvm_tirx::target::Target;
60/// use tvm_tirx::tvm_ffi::Map;
61/// # use tvm_tirx::ir::{Expr, PrimType, Return, SeqStmt, Var};
62/// # use tvm_tirx::tirx::Function;
63/// # use tvm_tirx::tvm_ffi::{Any, String};
64/// # let x = PrimVar::new("x", PrimType::int(32), UNKNOWN_LOC);
65/// # let body = SeqStmt::new([Return::new(Expr::try_from(&x + 1)?, UNKNOWN_LOC)], UNKNOWN_LOC);
66/// # let name = Any::from(String::from("add_one"));
67/// # let attrs = DictAttrs::new(Map::from_iter([(String::from("global_symbol"), name)]));
68/// # let add_one = Function::new([&x], &body, PrimType::int(32), attrs, UNKNOWN_LOC);
69///
70/// let functions = Map::from_iter([(GlobalVar::new("add_one", UNKNOWN_LOC), add_one.into())]);
71/// let module = IRModule::new(&functions, DictAttrs::new(Map::new()), Map::new())?;
72///
73/// let compiled = codegen::build_module(&module, Target::new("c")?)?;
74/// assert!(compiled.implements_function_with_imports("add_one")?);
75/// # Ok::<(), tvm_tirx::tvm_ffi::Error>(())
76/// ```
77pub fn build_module(
78 module: impl IntoArg<IRModule>,
79 target: impl IntoArg<Target>,
80) -> Result<Module> {
81 tvm_ffi::cached_global_func!("codegen.Build")
82 .call_tuple((module.into_arg().borrow(), target.into_arg().borrow()))?
83 .try_into()
84}
85
86/// Return whether the explicit LLVM target `target` has the CPU feature `feature`,
87/// or `false` for a target of another kind (`codegen.target_has_feature`).
88///
89/// # Errors
90///
91/// Returns an error if the LLVM backend, which registers the query, is not loaded, or if `target`
92/// cannot be queried by the LLVM backend.
93pub fn target_has_feature(feature: &str, target: &Target) -> Result<bool> {
94 tvm_ffi::Function::get_global("codegen.target_has_feature")?
95 .call_tuple((String::from(feature), target))?
96 .try_into()
97}