tvm_tirx/codegen/llvm.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//! The LLVM backend. Queries of LLVM intrinsics, the host CPU and the CPUs and features of LLVM
21//! targets, which `libtvm_tirx_llvm_backend` registers when it is loaded. Target-dependent
22//! queries require an explicit target.
23
24// tvm-ffi-stubgen(file): mod(missing_docs = allow)
25// Names the hand-written wrappers use: the import section keeps them even when no generated block
26// does.
27// tvm-ffi-stubgen(import): tvm_ffi::Array
28// tvm-ffi-stubgen(import): tvm_ffi::Map
29// tvm-ffi-stubgen(import): tvm_ffi::Result
30// tvm-ffi-stubgen(import): tvm_ffi::String
31// tvm-ffi-stubgen(import): crate::ir::expr::IntImm
32// tvm-ffi-stubgen(import): crate::target::Target
33
34// tvm-ffi-stubgen(begin): import-section
35use crate::ir::expr::IntImm;
36use crate::target::Target;
37use tvm_ffi::Array;
38use tvm_ffi::Map;
39use tvm_ffi::Result;
40use tvm_ffi::String;
41// tvm-ffi-stubgen(end)
42
43// The LLVM backend registers these functions only when TIRx is built with LLVM: they are looked up
44// when called, and their absence is an error.
45
46/// Return the id of the LLVM intrinsic `name`, or 0 when there is none
47/// (`codegen.llvm_lookup_intrinsic_id`).
48pub fn lookup_intrinsic_id(name: &str) -> Result<i64> {
49 tvm_ffi::Function::get_global("codegen.llvm_lookup_intrinsic_id")?
50 .call_tuple((String::from(name),))?
51 .try_into()
52}
53
54/// Return the name of the LLVM intrinsic `intrin_id` (`codegen.llvm_get_intrinsic_name`).
55///
56/// # Errors
57///
58/// Returns an error if `intrin_id` is not the id of an intrinsic.
59pub fn get_intrinsic_name(intrin_id: i64) -> Result<String> {
60 tvm_ffi::Function::get_global("codegen.llvm_get_intrinsic_name")?
61 .call_tuple((intrin_id,))?
62 .try_into()
63}
64
65/// Return the vendor of the host x86 CPU: `intel`, `amd`, `unknown`, or `unimplemented` on other
66/// CPUs (`codegen.llvm_get_system_x86_vendor`).
67pub fn get_system_x86_vendor() -> Result<String> {
68 tvm_ffi::Function::get_global("codegen.llvm_get_system_x86_vendor")?.call_tuple(())?.try_into()
69}
70
71/// Return the default LLVM target triple of the host (`codegen.llvm_get_system_triple`).
72pub fn get_system_triple() -> Result<String> {
73 tvm_ffi::Function::get_global("codegen.llvm_get_system_triple")?.call_tuple(())?.try_into()
74}
75
76/// Return the LLVM name of the host CPU (`codegen.llvm_get_system_cpu`).
77pub fn get_system_cpu() -> Result<String> {
78 tvm_ffi::Function::get_global("codegen.llvm_get_system_cpu")?.call_tuple(())?.try_into()
79}
80
81/// Return the targets LLVM was built with (`codegen.llvm_get_targets`).
82pub fn get_targets() -> Result<Array<String>> {
83 tvm_ffi::Function::get_global("codegen.llvm_get_targets")?.call_tuple(())?.try_into()
84}
85
86/// Return the CPUs of the `-mtriple` of `target` (`codegen.llvm_get_cpu_archlist`).
87pub fn get_cpu_archlist(target: &Target) -> Result<Array<String>> {
88 tvm_ffi::Function::get_global("codegen.llvm_get_cpu_archlist")?
89 .call_tuple((target,))?
90 .try_into()
91}
92
93/// Return the CPU features of the `-mtriple`, `-mcpu` and `-mattr` of `target`
94/// (`codegen.llvm_get_cpu_features`).
95pub fn get_cpu_features(target: &Target) -> Result<Map<String, String>> {
96 tvm_ffi::Function::get_global("codegen.llvm_get_cpu_features")?
97 .call_tuple((target,))?
98 .try_into()
99}
100
101/// Return whether the CPU of `target` has the feature `feature` (`codegen.llvm_cpu_has_feature`).
102pub fn cpu_has_feature(feature: &str, target: &Target) -> Result<bool> {
103 tvm_ffi::Function::get_global("codegen.llvm_cpu_has_feature")?
104 .call_tuple((String::from(feature), target))?
105 .try_into()
106}
107
108/// Return the vector width of the CPU of `target` in bits, or -1 for a target that is not `llvm`
109/// (`codegen.llvm_get_vector_width`).
110pub fn get_vector_width(target: &Target) -> Result<i32> {
111 tvm_ffi::Function::get_global("codegen.llvm_get_vector_width")?
112 .call_tuple((target,))?
113 .try_into()
114}
115
116/// Return whether LLVM knows the CPU `cpu` for the target triple `triple`
117/// (`codegen.llvm_is_valid_cpu`).
118pub fn is_valid_cpu(cpu: &str, triple: &str) -> Result<bool> {
119 tvm_ffi::Function::get_global("codegen.llvm_is_valid_cpu")?
120 .call_tuple((String::from(cpu), String::from(triple)))?
121 .try_into()
122}
123
124/// Return the major version of LLVM (`codegen.llvm_version_major`).
125pub fn version_major() -> Result<i32> {
126 tvm_ffi::Function::get_global("codegen.llvm_version_major")?.call_tuple(())?.try_into()
127}
128
129/// Return whether LLVM can create a target machine for the target string `target`, such as
130/// `"llvm -mtriple=aarch64-linux-gnu"` (`codegen.llvm_target_enabled`).
131pub fn target_enabled(target: &str) -> Result<bool> {
132 tvm_ffi::Function::get_global("codegen.llvm_target_enabled")?
133 .call_tuple((String::from(target),))?
134 .try_into()
135}
136
137/// Return the default LLVM target triple of the host (`tvm.codegen.llvm.GetDefaultTargetTriple`).
138pub fn get_default_target_triple() -> Result<String> {
139 tvm_ffi::Function::get_global("tvm.codegen.llvm.GetDefaultTargetTriple")?
140 .call_tuple(())?
141 .try_into()
142}
143
144/// Return the LLVM target triple of the running process (`tvm.codegen.llvm.GetProcessTriple`).
145pub fn get_process_triple() -> Result<String> {
146 tvm_ffi::Function::get_global("tvm.codegen.llvm.GetProcessTriple")?.call_tuple(())?.try_into()
147}
148
149/// Return the LLVM name of the host CPU (`tvm.codegen.llvm.GetHostCPUName`).
150pub fn get_host_cpu_name() -> Result<String> {
151 tvm_ffi::Function::get_global("tvm.codegen.llvm.GetHostCPUName")?.call_tuple(())?.try_into()
152}
153
154/// Return the features of the host CPU, each with a boolean saying whether it is enabled, or none
155/// when LLVM cannot tell (`tvm.codegen.llvm.GetHostCPUFeatures`).
156pub fn get_host_cpu_features() -> Result<Map<String, IntImm>> {
157 tvm_ffi::Function::get_global("tvm.codegen.llvm.GetHostCPUFeatures")?.call_tuple(())?.try_into()
158}