-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Builtin function database, for automatic conversions #6833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d94cbfe
[naga] Move `TypeInner::automatically_converts_to` into `proc`.
jimblandy 8c2dfea
[naga] Move predeclared type name generation to common code.
jimblandy 19026bb
[naga] Reorder the WGSL forms of `Scalar` types.
jimblandy 8941c73
[naga] Put `Scalar::F16` constant in the right place.
jimblandy eb32eb9
[naga] Add `DiagnosticDisplay` and `DiagnosticDebug` wrappers.
jimblandy 061a8bf
[naga wgsl-in] Introduce `ExpressionContext::is_const` helper.
jimblandy 50c1438
[naga] Add `TypeInner::scalar_for_conversions` helper function.
jimblandy 6bd163e
[naga wgsl-in] Minor doc fix.
jimblandy b1f7670
[naga] Introduce `ForDebug` and `ForDebugWithTypes` traits
jimblandy 93aa210
[naga] Support `MathFunction` overloads correctly.
jimblandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//! Displaying Naga IR terms in debugging output. | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
use crate::common::wgsl::TypeContext; | ||
|
||
use crate::proc::TypeResolution; | ||
use crate::{Handle, Scalar, Type, TypeInner, UniqueArena}; | ||
|
||
use core::fmt; | ||
|
||
/// A wrapper for displaying Naga IR terms in debugging output. | ||
/// | ||
/// This is like [`DiagnosticDisplay`], but requires weaker context | ||
/// and produces correspondingly lower-fidelity output. For example, | ||
/// this cannot show the override names for override-sized array | ||
/// lengths. | ||
/// | ||
/// [`DiagnosticDisplay`]: super::DiagnosticDisplay | ||
pub struct DiagnosticDebug<T>(pub T); | ||
jimblandy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl fmt::Debug for DiagnosticDebug<(Handle<Type>, &UniqueArena<Type>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (handle, ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type(handle, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{handle:?}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Debug for DiagnosticDebug<(&TypeInner, &UniqueArena<Type>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (inner, ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type_inner(inner, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{inner:?}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Debug for DiagnosticDebug<(&TypeResolution, &UniqueArena<Type>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (resolution, ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type_resolution(resolution, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{resolution:?}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Debug for DiagnosticDebug<Scalar> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let scalar = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics( | ||
scalar, | ||
))?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
write!(f, "{scalar:?}")?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub trait ForDebug: Sized { | ||
/// Format this type using [`core::fmt::Debug`]. | ||
/// | ||
/// Return a value that implements the [`core::fmt::Debug`] trait | ||
/// by displaying `self` in a language-appropriate way. For | ||
/// example: | ||
/// | ||
/// # use naga::common::ForDebug; | ||
/// # let scalar: naga::Scalar = naga::Scalar::F32; | ||
/// log::debug!("My scalar: {:?}", scalar.for_debug()); | ||
fn for_debug(self) -> DiagnosticDebug<Self> { | ||
DiagnosticDebug(self) | ||
} | ||
} | ||
|
||
impl ForDebug for Scalar {} | ||
|
||
pub trait ForDebugWithTypes: Sized { | ||
/// Format this type using [`core::fmt::Debug`]. | ||
/// | ||
/// Given an arena to look up type handles in, return a value that | ||
/// implements the [`core::fmt::Debug`] trait by displaying `self` | ||
/// in a language-appropriate way. For example: | ||
/// | ||
/// # use naga::{Span, Type, TypeInner, Scalar, UniqueArena}; | ||
/// # use naga::common::ForDebugWithTypes; | ||
/// # let mut types = UniqueArena::<Type>::default(); | ||
/// # let inner = TypeInner::Scalar(Scalar::F32); | ||
/// # let span = Span::UNDEFINED; | ||
/// # let handle = types.insert(Type { name: None, inner }, span); | ||
/// log::debug!("My type: {:?}", handle.for_debug(&types)); | ||
fn for_debug(self, types: &UniqueArena<Type>) -> DiagnosticDebug<(Self, &UniqueArena<Type>)> { | ||
DiagnosticDebug((self, types)) | ||
} | ||
} | ||
|
||
impl ForDebugWithTypes for Handle<Type> {} | ||
impl ForDebugWithTypes for &TypeInner {} | ||
impl ForDebugWithTypes for &TypeResolution {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//! Displaying Naga IR terms in diagnostic output. | ||
|
||
use crate::proc::{GlobalCtx, Rule}; | ||
use crate::{Handle, Scalar, Type, TypeInner}; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
use crate::common::wgsl::TypeContext; | ||
|
||
use core::fmt; | ||
|
||
/// A wrapper for displaying Naga IR terms in diagnostic output. | ||
/// | ||
/// For some Naga IR type `T`, `DiagnosticDisplay<T>` implements | ||
/// [`core::fmt::Display`] in a way that displays values of type `T` | ||
/// appropriately for diagnostic messages presented to human readers. | ||
/// | ||
/// For example, the implementation of [`Display`] for | ||
/// `DiagnosticDisplay<Scalar>` formats the type represented by the | ||
/// given [`Scalar`] appropriately for users. | ||
/// | ||
/// Some types like `Handle<Type>` require contextual information like | ||
/// a type arena to be displayed. In such cases, we implement [`Display`] | ||
/// for a type like `DiagnosticDisplay<(Handle<Type>, GlobalCtx)>`, where | ||
/// the [`GlobalCtx`] type provides the necessary context. | ||
/// | ||
/// If you only need debugging output, [`DiagnosticDebug`] uses | ||
/// easier-to-obtain context types but still does a good enough job | ||
/// for logging or debugging. | ||
/// | ||
/// [`Display`]: core::fmt::Display | ||
/// [`Scalar`]: crate::Scalar | ||
/// [`GlobalCtx`]: crate::proc::GlobalCtx | ||
/// [`DiagnosticDebug`]: super::DiagnosticDebug | ||
/// | ||
/// ## Language-sensitive diagnostics | ||
/// | ||
/// Diagnostic output ought to depend on the source language from | ||
/// which the IR was produced: diagnostics resulting from processing | ||
/// GLSL code should use GLSL type syntax, for example. That means | ||
/// that `DiagnosticDisplay` ought to include some indication of which | ||
/// notation to use. | ||
/// | ||
/// For the moment, only WGSL output is implemented, so | ||
/// `DiagnosticDisplay` lacks any support for this (#7268). However, | ||
/// the plan is that all language-independent code in Naga should use | ||
/// `DiagnosticDisplay` wherever appropriate, such that when its | ||
/// definition is expanded to include some indication of the right | ||
/// source language to use, any use site that does not supply this | ||
/// indication will provoke a compile-time error. | ||
pub struct DiagnosticDisplay<T>(pub T); | ||
ErichDonGubler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl fmt::Display for DiagnosticDisplay<(Handle<Type>, GlobalCtx<'_>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (handle, ref ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type(handle, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{handle:?}")?; | ||
} | ||
ErichDonGubler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for DiagnosticDisplay<(&TypeInner, GlobalCtx<'_>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (inner, ref ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type_inner(inner, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{inner:?}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for DiagnosticDisplay<(&str, &Rule, GlobalCtx<'_>)> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (name, rule, ref ctx) = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
ctx.write_type_rule(name, rule, f)?; | ||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
{ | ||
let _ = ctx; | ||
write!(f, "{name}({:?}) -> {:?}", rule.arguments, rule.conclusion)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for DiagnosticDisplay<Scalar> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let scalar = self.0; | ||
|
||
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] | ||
f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics( | ||
scalar, | ||
))?; | ||
ErichDonGubler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))] | ||
write!(f, "{scalar:?}")?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//! Generating names for predeclared types. | ||
|
||
use crate::ir; | ||
|
||
use alloc::format; | ||
use alloc::string::String; | ||
|
||
impl ir::PredeclaredType { | ||
pub fn struct_name(&self) -> String { | ||
use crate::PredeclaredType as Pt; | ||
match *self { | ||
Pt::AtomicCompareExchangeWeakResult(scalar) => { | ||
format!( | ||
"__atomic_compare_exchange_result<{:?},{}>", | ||
scalar.kind, scalar.width, | ||
) | ||
} | ||
Pt::ModfResult { size, scalar } => frexp_mod_name("modf", size, scalar), | ||
Pt::FrexpResult { size, scalar } => frexp_mod_name("frexp", size, scalar), | ||
} | ||
} | ||
} | ||
|
||
fn frexp_mod_name(function: &str, size: Option<ir::VectorSize>, scalar: ir::Scalar) -> String { | ||
let bits = 8 * scalar.width; | ||
match size { | ||
Some(size) => { | ||
let size = size as u8; | ||
format!("__{function}_result_vec{size}_f{bits}") | ||
} | ||
None => format!("__{function}_result_f{bits}"), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.