summaryrefslogtreecommitdiffstats
path: root/source/slang
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2024-11-08 13:03:42 -0500
committerGitHub <noreply@github.com>2024-11-08 13:03:42 -0500
commitf66b046e2767cd7a38acec8c0f988e5937f062e7 (patch)
tree178b745cea2479675a368cb67526833aad554630 /source/slang
parent0f46ce82998b2b1cb68f04bef3a097ea850ad453 (diff)
[WGSL] [WASM] Add reflection endpoints + Fix bit manipulation operations (#5499)
* Add key reflection endpoints for WASM * Fix WGSL output around bit-manipulation operators * format code * Fix pointer ownership * fix formatting --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/slang-emit-c-like.cpp7
-rw-r--r--source/slang/slang-emit-wgsl.cpp66
2 files changed, 58 insertions, 15 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 020c31fdc..790162c5b 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -693,6 +693,13 @@ bool CLikeSourceEmitter::maybeEmitParens(EmitOpInfo& outerPrec, const EmitOpInfo
{
needParens = true;
}
+ // a ^ b * c => (a ^ b) * c
+ else if (
+ prec.rightPrecedence == EPrecedence::kEPrecedence_BitXor_Right &&
+ outerPrec.rightPrecedence == EPrecedence::kEPrecedence_Multiplicative_Left)
+ {
+ needParens = true;
+ }
if (needParens)
{
diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp
index ca51f7e15..5df94f561 100644
--- a/source/slang/slang-emit-wgsl.cpp
+++ b/source/slang/slang-emit-wgsl.cpp
@@ -1304,28 +1304,64 @@ bool WGSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
// https://www.w3.org/TR/WGSL/#bit-expr
IRInst* const shiftAmount = inst->getOperand(1);
IRType* const shiftAmountType = shiftAmount->getDataType();
- if (shiftAmountType->getOp() == kIROp_IntType)
- {
- // Dawn complains about "mixing '<<' and '|' requires parenthesis", so let's
- // add parenthesis.
- m_writer->emit("(");
- const auto emitOp = getEmitOpForOp(inst->getOp());
- const auto info = getInfo(emitOp);
+ // Dawn complains about mixing '<<' and '|', '^' and a bunch of other bit operators
+ // without a paranthesis, so we'll always emit paranthesis around the shift amount.
+ //
- const bool needClose = maybeEmitParens(outerPrec, info);
- emitOperand(inst->getOperand(0), leftSide(outerPrec, info));
- m_writer->emit(" ");
- m_writer->emit(info.op);
- m_writer->emit(" ");
+ m_writer->emit("(");
+
+ const auto emitOp = getEmitOpForOp(inst->getOp());
+ const auto info = getInfo(emitOp);
+
+ const bool needClose = maybeEmitParens(outerPrec, info);
+ emitOperand(inst->getOperand(0), leftSide(outerPrec, info));
+ m_writer->emit(" ");
+ m_writer->emit(info.op);
+ m_writer->emit(" ");
+
+ if (shiftAmountType->getOp() == kIROp_IntType)
+ {
m_writer->emit("bitcast<u32>(");
emitOperand(inst->getOperand(1), rightSide(outerPrec, info));
m_writer->emit(")");
- maybeCloseParens(needClose);
-
+ }
+ else
+ {
+ m_writer->emit("(");
+ emitOperand(inst->getOperand(1), rightSide(outerPrec, info));
m_writer->emit(")");
- return true;
}
+
+ maybeCloseParens(needClose);
+
+ m_writer->emit(")");
+
+ return true;
+ }
+ case kIROp_BitXor:
+ case kIROp_BitOr:
+ {
+ // Emit bitwise operators with paranthesis to avoid precedence issues
+ const auto emitOp = getEmitOpForOp(inst->getOp());
+ const auto info = getInfo(emitOp);
+
+ m_writer->emit("(");
+
+ const bool needClose = maybeEmitParens(outerPrec, info);
+ emitOperand(inst->getOperand(0), leftSide(outerPrec, info));
+ m_writer->emit(" ");
+
+ m_writer->emit(info.op);
+
+ m_writer->emit(" (");
+ emitOperand(inst->getOperand(1), rightSide(outerPrec, info));
+ m_writer->emit(")");
+
+ maybeCloseParens(needClose);
+
+ m_writer->emit(")");
+ return true;
}
break;