Skip to content

Commit

Permalink
Allow ftz and saturated conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
vosen committed Sep 3, 2024
1 parent 6a7c871 commit 3f31069
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
12 changes: 0 additions & 12 deletions ptx/src/pass/emit_spirv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2163,19 +2163,13 @@ fn emit_cvt(
builder.sat_convert_s_to_u(result_type.0, Some(arg.dst.0), arg.src.0)?;
}
ptx_parser::CvtMode::FPExtend { flush_to_zero } => {
if flush_to_zero == Some(true) {
todo!()
}
let result_type = map.get_or_add(builder, SpirvType::from(dets.to));
builder.f_convert(result_type.0, Some(arg.dst.0), arg.src.0)?;
}
ptx_parser::CvtMode::FPTruncate {
rounding,
flush_to_zero,
} => {
if flush_to_zero == Some(true) {
todo!()
}
let result_type = map.get_or_add(builder, SpirvType::from(dets.to));
builder.f_convert(result_type.0, Some(arg.dst.0), arg.src.0)?;
emit_rounding_decoration(builder, arg.dst, Some(rounding));
Expand Down Expand Up @@ -2234,9 +2228,6 @@ fn emit_cvt(
rounding,
flush_to_zero,
} => {
if flush_to_zero == Some(true) {
todo!()
}
let dest_t: ast::ScalarType = dets.to.into();
let result_type = map.get_or_add(builder, SpirvType::from(dest_t));
builder.convert_f_to_s(result_type.0, Some(arg.dst.0), arg.src.0)?;
Expand All @@ -2246,9 +2237,6 @@ fn emit_cvt(
rounding,
flush_to_zero,
} => {
if flush_to_zero == Some(true) {
todo!()
}
let dest_t: ast::ScalarType = dets.to.into();
let result_type = map.get_or_add(builder, SpirvType::from(dest_t));
builder.convert_f_to_u(result_type.0, Some(arg.dst.0), arg.src.0)?;
Expand Down
17 changes: 15 additions & 2 deletions ptx_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,8 @@ impl CvtDetails {
dst: ScalarType,
src: ScalarType,
) -> Self {
if saturate {
errors.push(PtxError::Todo);
if saturate && dst.kind() == ScalarKind::Float {
errors.push(PtxError::SyntaxError);
}
// Modifier .ftz can only be specified when either .dtype or .atype is .f32 and applies only to single precision (.f32) inputs and results.
let flush_to_zero = match (dst, src) {
Expand Down Expand Up @@ -1432,6 +1432,18 @@ impl CvtDetails {
},
(ScalarKind::Float, ScalarKind::Unsigned) => CvtMode::FPFromUnsigned(unwrap_rounding()),
(ScalarKind::Float, ScalarKind::Signed) => CvtMode::FPFromSigned(unwrap_rounding()),
(ScalarKind::Signed, ScalarKind::Unsigned) if saturate => {
CvtMode::SaturateUnsignedToSigned
}
(ScalarKind::Unsigned, ScalarKind::Signed) if saturate => {
CvtMode::SaturateSignedToUnsigned
}
(ScalarKind::Unsigned, ScalarKind::Signed)
| (ScalarKind::Signed, ScalarKind::Unsigned)
if dst.size_of() == src.size_of() =>
{
CvtMode::Bitcast
}
(ScalarKind::Unsigned, ScalarKind::Unsigned)
| (ScalarKind::Signed, ScalarKind::Signed) => match dst.size_of().cmp(&src.size_of()) {
Ordering::Less => CvtMode::Truncate,
Expand All @@ -1444,6 +1456,7 @@ impl CvtDetails {
}
}
},
(ScalarKind::Unsigned, ScalarKind::Signed) => CvtMode::SaturateSignedToUnsigned,
(_, _) => {
errors.push(PtxError::SyntaxError);
CvtMode::Bitcast
Expand Down
26 changes: 18 additions & 8 deletions ptx_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ pub fn parse_module_unchecked<'input>(text: &'input str) -> Option<ast::Module<'
state,
input: &input[..],
};
module.parse(parser).ok()
let parsing_result = module.parse(parser).ok();
if !errors.is_empty() {
None
} else {
parsing_result
}
}

pub fn parse_module_checked<'input>(
Expand All @@ -314,19 +319,24 @@ pub fn parse_module_checked<'input>(
if !errors.is_empty() {
return Err(errors);
}
let parse_error = {
let parse_result = {
let state = PtxParserState::new(&mut errors);
let parser = PtxParser {
state,
input: &tokens[..],
};
match module.parse(parser) {
Ok(ast) => return Ok(ast),
Err(err) => PtxError::Parser(err.into_inner()),
}
module
.parse(parser)
.map_err(|err| PtxError::Parser(err.into_inner()))
};
errors.push(parse_error);
Err(errors)
match parse_result {
Ok(result) if errors.is_empty() => Ok(result),
Ok(_) => Err(errors),
Err(err) => {
errors.push(err);
Err(errors)
}
}
}

fn module<'a, 'input>(stream: &mut PtxParser<'a, 'input>) -> PResult<ast::Module<'input>> {
Expand Down

0 comments on commit 3f31069

Please sign in to comment.