Skip to content

Commit cb0aa5e

Browse files
jimblandyandyleiserson
authored andcommitted
[naga wgsl-in] Consolidate bitcast and coopLoad handling.
In `naga::front::wgsl::lower::Lowerer::call_builtin`, consolidate the cases for `bitcast`, `coopLoad`, and `coopLoadT` with the other miscellaneous builtin functions.
1 parent 80fa7a5 commit cb0aa5e

1 file changed

Lines changed: 69 additions & 67 deletions

File tree

  • naga/src/front/wgsl/lower

naga/src/front/wgsl/lower/mod.rs

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,73 +2919,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
29192919
ctx: &mut ExpressionContext<'source, '_, '_>,
29202920
is_statement: bool,
29212921
) -> Result<'source, Option<Handle<ir::Expression>>> {
2922-
let expr = if function_name == "bitcast" {
2923-
let ty = template_params.ty(self, ctx)?;
2924-
2925-
let mut args = ctx.prepare_args(arguments, 1, function_span);
2926-
let expr = self.expression(args.next()?, ctx)?;
2927-
args.finish()?;
2928-
2929-
let element_scalar = match ctx.module.types[ty].inner {
2930-
ir::TypeInner::Scalar(scalar) => scalar,
2931-
ir::TypeInner::Vector { scalar, .. } => scalar,
2932-
_ => {
2933-
let ty_resolution = resolve!(ctx, expr);
2934-
return Err(Box::new(Error::BadTypeCast {
2935-
from_type: ctx.type_resolution_to_string(ty_resolution),
2936-
span: function_span,
2937-
to_type: ctx.type_to_string(ty),
2938-
}));
2939-
}
2940-
};
2941-
2942-
ir::Expression::As {
2943-
expr,
2944-
kind: element_scalar.kind,
2945-
convert: None,
2946-
}
2947-
} else if function_name == "coopLoad" || function_name == "coopLoadT" {
2948-
let row_major = function_name.ends_with("T");
2949-
let (matrix_ty, matrix_span) = template_params.ty_with_span(self, ctx)?;
2950-
2951-
let mut args = ctx.prepare_args(arguments, 1, call_span);
2952-
let pointer = self.expression(args.next()?, ctx)?;
2953-
let (columns, rows, role) = match ctx.module.types[matrix_ty].inner {
2954-
ir::TypeInner::CooperativeMatrix {
2955-
columns,
2956-
rows,
2957-
role,
2958-
..
2959-
} => (columns, rows, role),
2960-
_ => return Err(Box::new(Error::InvalidCooperativeLoadType(matrix_span))),
2961-
};
2962-
let stride = if args.total_args > 1 {
2963-
self.expression(args.next()?, ctx)?
2964-
} else {
2965-
// Infer the stride from the matrix type
2966-
let stride = if row_major {
2967-
columns as u32
2968-
} else {
2969-
rows as u32
2970-
};
2971-
ctx.append_expression(
2972-
ir::Expression::Literal(ir::Literal::U32(stride)),
2973-
Span::UNDEFINED,
2974-
)?
2975-
};
2976-
args.finish()?;
2977-
2978-
crate::Expression::CooperativeLoad {
2979-
columns,
2980-
rows,
2981-
role,
2982-
data: crate::CooperativeData {
2983-
pointer,
2984-
stride,
2985-
row_major,
2986-
},
2987-
}
2988-
} else if let Some(fun) = conv::map_relational_fun(function_name) {
2922+
let expr = if let Some(fun) = conv::map_relational_fun(function_name) {
29892923
let mut args = ctx.prepare_args(arguments, 1, function_span);
29902924
let argument = self.expression(args.next()?, ctx)?;
29912925
args.finish()?;
@@ -3038,6 +2972,74 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
30382972
return self.atomic_helper(function_span, fun, arguments, is_statement, ctx);
30392973
} else {
30402974
match function_name {
2975+
"bitcast" => {
2976+
let ty = template_params.ty(self, ctx)?;
2977+
2978+
let mut args = ctx.prepare_args(arguments, 1, function_span);
2979+
let expr = self.expression(args.next()?, ctx)?;
2980+
args.finish()?;
2981+
2982+
let element_scalar = match ctx.module.types[ty].inner {
2983+
ir::TypeInner::Scalar(scalar) => scalar,
2984+
ir::TypeInner::Vector { scalar, .. } => scalar,
2985+
_ => {
2986+
let ty_resolution = resolve!(ctx, expr);
2987+
return Err(Box::new(Error::BadTypeCast {
2988+
from_type: ctx.type_resolution_to_string(ty_resolution),
2989+
span: function_span,
2990+
to_type: ctx.type_to_string(ty),
2991+
}));
2992+
}
2993+
};
2994+
2995+
ir::Expression::As {
2996+
expr,
2997+
kind: element_scalar.kind,
2998+
convert: None,
2999+
}
3000+
}
3001+
"coopLoad" | "coopLoadT" => {
3002+
let row_major = function_name.ends_with("T");
3003+
let (matrix_ty, matrix_span) = template_params.ty_with_span(self, ctx)?;
3004+
3005+
let mut args = ctx.prepare_args(arguments, 1, call_span);
3006+
let pointer = self.expression(args.next()?, ctx)?;
3007+
let (columns, rows, role) = match ctx.module.types[matrix_ty].inner {
3008+
ir::TypeInner::CooperativeMatrix {
3009+
columns,
3010+
rows,
3011+
role,
3012+
..
3013+
} => (columns, rows, role),
3014+
_ => return Err(Box::new(Error::InvalidCooperativeLoadType(matrix_span))),
3015+
};
3016+
let stride = if args.total_args > 1 {
3017+
self.expression(args.next()?, ctx)?
3018+
} else {
3019+
// Infer the stride from the matrix type
3020+
let stride = if row_major {
3021+
columns as u32
3022+
} else {
3023+
rows as u32
3024+
};
3025+
ctx.append_expression(
3026+
ir::Expression::Literal(ir::Literal::U32(stride)),
3027+
Span::UNDEFINED,
3028+
)?
3029+
};
3030+
args.finish()?;
3031+
3032+
crate::Expression::CooperativeLoad {
3033+
columns,
3034+
rows,
3035+
role,
3036+
data: crate::CooperativeData {
3037+
pointer,
3038+
stride,
3039+
row_major,
3040+
},
3041+
}
3042+
}
30413043
"select" => {
30423044
let mut args = ctx.prepare_args(arguments, 3, function_span);
30433045

0 commit comments

Comments
 (0)