tvm_tirx/ir/location.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// The generated `new` of a field-less base needs no `Default`, and `UnknownLoc::new` fetches a
20// shared FFI singleton.
21#![allow(clippy::new_without_default)]
22
23//! Source locations. Every IR node carries a [`Location`] for diagnostics: an [`UnknownLoc`], a
24//! [`SourceLoc`] span in a source, or a [`CallSiteLoc`] that pairs a location with its caller's.
25
26// tvm-ffi-stubgen(file): mod(missing_docs = allow)
27// C++ has one `UnknownLoc`, which `UnknownLoc::new` returns: `opaque` generates no allocator for
28// another one. It has no fields, so it loses no field access.
29// tvm-ffi-stubgen(object): ir.UnknownLoc(opaque)
30// `no_new` generates no allocator for the abstract base: a value is one of its subtypes.
31// tvm-ffi-stubgen(object): ir.Location(no_new)
32// `SourceName::get` interns names; `no_new` generates no allocator for a second object of a name.
33// tvm-ffi-stubgen(object): ir.SourceName(no_new)
34// tvm-ffi-stubgen(field): ir.SourceLoc.start_line(type = i32)
35// tvm-ffi-stubgen(field): ir.SourceLoc.start_column(type = i32)
36// tvm-ffi-stubgen(field): ir.SourceLoc.end_line(type = i32)
37// tvm-ffi-stubgen(field): ir.SourceLoc.end_column(type = i32)
38// Names the hand-written wrappers use: the import section keeps them even when no generated block
39// does.
40// tvm-ffi-stubgen(import): crate::ir::support
41// tvm-ffi-stubgen(import): tvm_ffi::IntoArg
42
43// tvm-ffi-stubgen(begin): import-section
44use crate::ir::support;
45use std::ops::Deref;
46use tvm_ffi::IntoArg;
47use tvm_ffi::Object;
48use tvm_ffi::ObjectArc;
49use tvm_ffi::String;
50// tvm-ffi-stubgen(end)
51
52/// Where an IR node comes from, for diagnostics: an [`UnknownLoc`], a [`SourceLoc`] or a
53/// [`CallSiteLoc`].
54///
55/// A constructor takes its `loc` as any location, such as a `SourceLoc` or [`UNKNOWN_LOC`].
56///
57/// # Examples
58///
59/// ```
60/// use tvm_tirx::ir::{PrimType, PrimVar, SourceLoc, SourceName, Var};
61/// use tvm_tirx::tvm_ffi::ObjectRefCore;
62///
63/// let span = SourceLoc::new(SourceName::get("kernel.py"), 3, 1, 3, 10);
64///
65/// let x = PrimVar::new("x", PrimType::int(32), &span);
66/// assert!(x.loc.same_as(&span));
67/// # Ok::<(), tvm_tirx::tvm_ffi::Error>(())
68/// ```
69// tvm-ffi-stubgen(begin): object/ir.Location
70#[allow(missing_docs)]
71#[repr(C)]
72#[derive(tvm_ffi::derive::ObjectRef, Clone)]
73pub struct Location {
74 data_: ObjectArc<LocationObj>,
75}
76
77/// The object behind [`Location`].
78// Complete: reflected fields fill [24, 24) exactly.
79#[allow(missing_docs)]
80#[repr(C)]
81#[derive(tvm_ffi::derive::Object)]
82#[object(type_key = "ir.Location", immutable, rs_parent)]
83pub struct LocationObj {
84 base_: Object,
85}
86
87const _: () = {
88 assert!(::core::mem::size_of::<LocationObj>() == 24);
89 assert!(::core::mem::align_of::<LocationObj>() == 8);
90};
91
92#[allow(missing_docs)]
93impl LocationObj {
94 pub fn new() -> Self {
95 let base_ = Object::new();
96 Self { base_ }
97 }
98}
99// tvm-ffi-stubgen(end)
100
101/// The location of a node with no source information, a single shared object.
102// tvm-ffi-stubgen(begin): object/ir.UnknownLoc
103#[allow(missing_docs)]
104#[repr(C)]
105#[derive(tvm_ffi::derive::ObjectRef, Clone)]
106pub struct UnknownLoc {
107 data_: ObjectArc<UnknownLocObj>,
108}
109
110/// The object behind [`UnknownLoc`].
111// Opaque: vetoed by directive although the layout is reproducible. Fields are read through the C ABI getters.
112#[allow(missing_docs)]
113#[repr(C)]
114#[derive(tvm_ffi::derive::Object)]
115#[object(type_key = "ir.UnknownLoc", final, immutable)]
116pub struct UnknownLocObj {
117 base_: LocationObj,
118}
119
120impl Deref for UnknownLocObj {
121 type Target = LocationObj;
122 fn deref(&self) -> &LocationObj {
123 &self.base_
124 }
125}
126
127tvm_ffi::impl_object_ref_upcast!(UnknownLoc => Location);
128// tvm-ffi-stubgen(end)
129
130impl UnknownLoc {
131 /// Return the shared unknown location, which IR constructors default to, fetched once per
132 /// thread (`ir.UnknownLoc`).
133 pub fn new() -> Self {
134 thread_local! {
135 static UNKNOWN_LOC: std::cell::OnceCell<UnknownLoc> = const { std::cell::OnceCell::new() };
136 }
137 support::shared(&UNKNOWN_LOC, || {
138 tvm_ffi::cached_global_func!("ir.UnknownLoc").call_tuple(())?.try_into()
139 })
140 }
141}
142
143/// A marker for the unknown location as the `loc` argument of a constructor:
144/// `Var::new("x", ty, UNKNOWN_LOC)`.
145///
146/// [`UnknownLoc::new`] is the same location as a value.
147#[derive(Clone, Copy, Debug)]
148pub struct Unknown;
149
150/// The unknown location, as the `loc` argument of a constructor.
151pub const UNKNOWN_LOC: Unknown = Unknown;
152
153impl From<Unknown> for Location {
154 fn from(_: Unknown) -> Self {
155 UnknownLoc::new().into()
156 }
157}
158
159impl IntoArg<Location> for Unknown {
160 type Output = Location;
161 #[inline]
162 fn into_arg(self) -> Location {
163 self.into()
164 }
165 #[inline]
166 fn into_owned(self) -> Location {
167 self.into()
168 }
169}
170
171/// The span from `start_line`, `start_column` to `end_line`, `end_column` in the source
172/// `source_name`, in the line and column numbering of the frontend that produced it.
173// tvm-ffi-stubgen(begin): object/ir.SourceLoc
174#[allow(missing_docs)]
175#[repr(C)]
176#[derive(tvm_ffi::derive::ObjectRef, Clone)]
177pub struct SourceLoc {
178 data_: ObjectArc<SourceLocObj>,
179}
180
181/// The object behind [`SourceLoc`].
182// Complete: reflected fields fill [24, 48) exactly.
183#[allow(missing_docs)]
184#[repr(C)]
185#[derive(tvm_ffi::derive::Object)]
186#[object(type_key = "ir.SourceLoc", final, immutable)]
187pub struct SourceLocObj {
188 base_: LocationObj,
189 pub source_name: SourceName,
190 pub start_line: i32,
191 pub start_column: i32,
192 pub end_line: i32,
193 pub end_column: i32,
194}
195
196const _: () = {
197 assert!(::core::mem::size_of::<SourceLocObj>() == 48);
198 assert!(::core::mem::align_of::<SourceLocObj>() == 8);
199};
200
201impl Deref for SourceLocObj {
202 type Target = LocationObj;
203 fn deref(&self) -> &LocationObj {
204 &self.base_
205 }
206}
207
208#[allow(missing_docs)]
209impl SourceLocObj {
210 pub(crate) fn new(
211 source_name: SourceName,
212 start_line: i32,
213 start_column: i32,
214 end_line: i32,
215 end_column: i32,
216 ) -> Self {
217 let base_ = LocationObj::new();
218 Self { base_, source_name, start_line, start_column, end_line, end_column }
219 }
220}
221
222#[allow(missing_docs)]
223impl SourceLoc {
224 /// Create a `SourceLoc` from its field values.
225 pub fn new(
226 source_name: impl IntoArg<SourceName>,
227 start_line: i32,
228 start_column: i32,
229 end_line: i32,
230 end_column: i32,
231 ) -> Self {
232 let obj = SourceLocObj::new(
233 source_name.into_owned(),
234 start_line,
235 start_column,
236 end_line,
237 end_column,
238 );
239 Self { data_: ObjectArc::new(obj) }
240 }
241}
242
243tvm_ffi::impl_object_ref_upcast!(SourceLoc => Location);
244// tvm-ffi-stubgen(end)
245
246// `ir.SourceLoc` stores its arguments as given: the generated `SourceLoc::new` is the same.
247
248/// The location `callee` of a node, together with the location `caller` of the call site it
249/// comes from.
250// tvm-ffi-stubgen(begin): object/ir.CallSiteLoc
251#[allow(missing_docs)]
252#[repr(C)]
253#[derive(tvm_ffi::derive::ObjectRef, Clone)]
254pub struct CallSiteLoc {
255 data_: ObjectArc<CallSiteLocObj>,
256}
257
258/// The object behind [`CallSiteLoc`].
259// Complete: reflected fields fill [24, 40) exactly.
260#[allow(missing_docs)]
261#[repr(C)]
262#[derive(tvm_ffi::derive::Object)]
263#[object(type_key = "ir.CallSiteLoc", final, immutable)]
264pub struct CallSiteLocObj {
265 base_: LocationObj,
266 pub callee: Location,
267 pub caller: Location,
268}
269
270const _: () = {
271 assert!(::core::mem::size_of::<CallSiteLocObj>() == 40);
272 assert!(::core::mem::align_of::<CallSiteLocObj>() == 8);
273};
274
275impl Deref for CallSiteLocObj {
276 type Target = LocationObj;
277 fn deref(&self) -> &LocationObj {
278 &self.base_
279 }
280}
281
282#[allow(missing_docs)]
283impl CallSiteLocObj {
284 pub(crate) fn new(callee: Location, caller: Location) -> Self {
285 let base_ = LocationObj::new();
286 Self { base_, callee, caller }
287 }
288}
289
290#[allow(missing_docs)]
291impl CallSiteLoc {
292 /// Create a `CallSiteLoc` from its field values.
293 pub fn new(callee: impl IntoArg<Location>, caller: impl IntoArg<Location>) -> Self {
294 let obj = CallSiteLocObj::new(callee.into_owned(), caller.into_owned());
295 Self { data_: ObjectArc::new(obj) }
296 }
297}
298
299tvm_ffi::impl_object_ref_upcast!(CallSiteLoc => Location);
300// tvm-ffi-stubgen(end)
301
302// `ir.CallSiteLoc` stores its arguments as given: the generated `CallSiteLoc::new` is the same.
303
304/// The name of a source, such as a file name; [`SourceName::get`] returns the shared object for a
305/// name.
306// tvm-ffi-stubgen(begin): object/ir.SourceName
307#[allow(missing_docs)]
308#[repr(C)]
309#[derive(tvm_ffi::derive::ObjectRef, Clone)]
310pub struct SourceName {
311 data_: ObjectArc<SourceNameObj>,
312}
313
314/// The object behind [`SourceName`].
315// Complete: reflected fields fill [24, 40) exactly.
316#[allow(missing_docs)]
317#[repr(C)]
318#[derive(tvm_ffi::derive::Object)]
319#[object(type_key = "ir.SourceName", final, immutable)]
320pub struct SourceNameObj {
321 base_: Object,
322 pub name: String,
323}
324
325const _: () = {
326 assert!(::core::mem::size_of::<SourceNameObj>() == 40);
327 assert!(::core::mem::align_of::<SourceNameObj>() == 8);
328};
329// tvm-ffi-stubgen(end)
330
331impl SourceName {
332 /// Return the source name `name`, interned so that equal names share one object
333 /// (`ir.SourceName`).
334 pub fn get(name: &str) -> Self {
335 support::infallible(
336 tvm_ffi::cached_global_func!("ir.SourceName")
337 .call_tuple((String::from(name),))
338 .and_then(|any| any.try_into()),
339 )
340 }
341}