From 3f0465be59f35d99e71e18f2fe6ccad48ad8826a Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Mon, 20 Nov 2023 16:55:22 -0800 Subject: [PATCH] [naga wgsl-in] Preserve type names in `alias` declarations. Given a WGSL `alias` declaration, create a Naga `Type` with the alias's name, rather than dropping the type name on the floor. --- CHANGELOG.md | 2 ++ naga/src/front/wgsl/lower/mod.rs | 40 ++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0993e6f814..41d5ea403f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,8 @@ Passing an owned value `window` to `Surface` will return a `Surface<'static>`. S - Preserve the source spans for constants and expressions correctly across module compaction. By @jimblandy in [#4696](https://github.com/gfx-rs/wgpu/pull/4696). +- Record the names of WGSL `alias` declarations in Naga IR `Type`s. By @jimblandy in [#4733](https://github.com/gfx-rs/wgpu/pull/4733). + ### Examples - remove winit dependency from hello-compute example by @psvri in [#4699](https://github.com/gfx-rs/wgpu/pull/4699) diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 34940e4515..02e1407adf 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -98,10 +98,14 @@ impl<'source> GlobalContext<'source, '_, '_> { } } - fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle { + fn ensure_type_exists( + &mut self, + name: Option, + inner: crate::TypeInner, + ) -> Handle { self.module .types - .insert(crate::Type { inner, name: None }, Span::UNDEFINED) + .insert(crate::Type { inner, name }, Span::UNDEFINED) } } @@ -635,7 +639,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { } fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle { - self.as_global().ensure_type_exists(inner) + self.as_global().ensure_type_exists(None, inner) } } @@ -936,7 +940,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { .insert(s.name.name, LoweredGlobalDecl::Type(handle)); } ast::GlobalDeclKind::Type(ref alias) => { - let ty = self.resolve_ast_type(alias.ty, &mut ctx)?; + let ty = self.resolve_named_ast_type( + alias.ty, + Some(alias.name.name.to_string()), + &mut ctx, + )?; ctx.globals .insert(alias.name.name, LoweredGlobalDecl::Type(ty)); } @@ -2513,10 +2521,19 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { }) } - /// Return a Naga `Handle` representing the front-end type `handle`. - fn resolve_ast_type( + /// Build the Naga equivalent of a named AST type. + /// + /// Return a Naga `Handle` representing the front-end type + /// `handle`, which should be named `name`, if given. + /// + /// If `handle` refers to a type cached in [`SpecialTypes`], + /// `name` may be ignored. + /// + /// [`SpecialTypes`]: crate::SpecialTypes + fn resolve_named_ast_type( &mut self, handle: Handle>, + name: Option, ctx: &mut GlobalContext<'source, '_, '_>, ) -> Result, Error<'source>> { let inner = match ctx.types[handle] { @@ -2577,7 +2594,16 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { } }; - Ok(ctx.ensure_type_exists(inner)) + Ok(ctx.ensure_type_exists(name, inner)) + } + + /// Return a Naga `Handle` representing the front-end type `handle`. + fn resolve_ast_type( + &mut self, + handle: Handle>, + ctx: &mut GlobalContext<'source, '_, '_>, + ) -> Result, Error<'source>> { + self.resolve_named_ast_type(handle, None, ctx) } fn binding(