Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

spv-in: Implement more sign agnostic operations #1651

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/front/spv/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(super) fn map_binary_operator(word: spirv::Op) -> Result<crate::BinaryOperat
Op::IMul | Op::FMul => Ok(BinaryOperator::Multiply),
Op::UDiv | Op::SDiv | Op::FDiv => Ok(BinaryOperator::Divide),
Op::UMod | Op::SMod | Op::FMod => Ok(BinaryOperator::Modulo),
Op::SRem => Ok(BinaryOperator::Modulo),
// Relational and Logical Instructions
Op::IEqual | Op::FOrdEqual | Op::FUnordEqual | Op::LogicalEqual => {
Ok(BinaryOperator::Equal)
Expand All @@ -34,6 +35,9 @@ pub(super) fn map_binary_operator(word: spirv::Op) -> Result<crate::BinaryOperat
| Op::SGreaterThanEqual
| Op::FOrdGreaterThanEqual
| Op::FUnordGreaterThanEqual => Ok(BinaryOperator::GreaterEqual),
Op::BitwiseOr => Ok(BinaryOperator::InclusiveOr),
Op::BitwiseXor => Ok(BinaryOperator::ExclusiveOr),
Op::BitwiseAnd => Ok(BinaryOperator::And),
_ => Err(Error::UnknownBinaryOperator(word)),
}
}
Expand Down
97 changes: 80 additions & 17 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,59 @@ impl<I: Iterator<Item = u32>> Parser<I> {
Ok(())
}

/// A more complicated version of the unary op,
/// where we force the operand to have the same type as the result.
fn parse_expr_unary_op_sign_adjusted(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
op: crate::UnaryOperator,
) -> Result<(), Error> {
let start = self.data_offset;
let result_type_id = self.next()?;
let result_id = self.next()?;
let p1_id = self.next()?;
let span = self.span_from_with_op(start);

let p1_lexp = self.lookup_expression.lookup(p1_id)?;
let left = self.get_expr_handle(p1_id, p1_lexp, ctx, emitter, block, body_idx);

let result_lookup_ty = self.lookup_type.lookup(result_type_id)?;
let kind = ctx.type_arena[result_lookup_ty.handle]
.inner
.scalar_kind()
.unwrap();

let expr = crate::Expression::Unary {
op,
expr: if p1_lexp.type_id == result_type_id {
left
} else {
ctx.expressions.append(
crate::Expression::As {
expr: left,
kind,
convert: None,
},
span,
)
},
};

self.lookup_expression.insert(
result_id,
LookupExpression {
handle: ctx.expressions.append(expr, span),
type_id: result_type_id,
block_id,
},
);
Ok(())
}

/// A more complicated version of the binary op,
/// where we force the operand to have the same type as the result.
/// This is mostly needed for "i++" and "i--" coming from GLSL.
Expand Down Expand Up @@ -1872,9 +1925,24 @@ impl<I: Iterator<Item = u32>> Parser<I> {
// Arithmetic Instructions +, -, *, /, %
Op::SNegate | Op::FNegate => {
inst.expect(4)?;
parse_expr_op!(crate::UnaryOperator::Negate, UNARY)?;
self.parse_expr_unary_op_sign_adjusted(
ctx,
&mut emitter,
&mut block,
block_id,
body_idx,
crate::UnaryOperator::Negate,
)?;
}
Op::IAdd | Op::ISub | Op::IMul => {
Op::IAdd
| Op::ISub
| Op::IMul
| Op::BitwiseOr
| Op::BitwiseXor
| Op::BitwiseAnd
| Op::SDiv
| Op::SMod
| Op::SRem => {
inst.expect(5)?;
let operator = map_binary_operator(inst.op)?;
self.parse_expr_binary_op_sign_adjusted(
Expand Down Expand Up @@ -1912,11 +1980,11 @@ impl<I: Iterator<Item = u32>> Parser<I> {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::Multiply, BINARY)?;
}
Op::SDiv | Op::UDiv | Op::FDiv => {
Op::UDiv | Op::FDiv => {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::Divide, BINARY)?;
}
Op::SMod | Op::UMod | Op::FMod | Op::SRem | Op::FRem => {
Op::UMod | Op::FMod | Op::FRem => {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::Modulo, BINARY)?;
}
Expand Down Expand Up @@ -2151,19 +2219,14 @@ impl<I: Iterator<Item = u32>> Parser<I> {
// Bitwise instructions
Op::Not => {
inst.expect(4)?;
parse_expr_op!(crate::UnaryOperator::Not, UNARY)?;
}
Op::BitwiseOr => {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::InclusiveOr, BINARY)?;
}
Op::BitwiseXor => {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::ExclusiveOr, BINARY)?;
}
Op::BitwiseAnd => {
inst.expect(5)?;
parse_expr_op!(crate::BinaryOperator::And, BINARY)?;
self.parse_expr_unary_op_sign_adjusted(
ctx,
&mut emitter,
&mut block,
block_id,
body_idx,
crate::UnaryOperator::Not,
)?;
}
Op::ShiftRightLogical => {
inst.expect(5)?;
Expand Down