summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/emit.cpp14
-rw-r--r--tests/bugs/paren-insertion-bug.slang34
-rw-r--r--tests/bugs/paren-insertion-bug.slang.expected.txt4
3 files changed, 49 insertions, 3 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp
index f8d90bc2e..bb9a6ad8b 100644
--- a/source/slang/emit.cpp
+++ b/source/slang/emit.cpp
@@ -3596,17 +3596,25 @@ struct EmitVisitor
// Simple constructor call
if( getTarget(ctx) == CodeGenTarget::HLSL )
{
+ auto prec = kEOp_Prefix;
+ needClose = maybeEmitParens(outerPrec, prec);
+
emit("(");
emitIRType(ctx, inst->getDataType());
emit(")");
+
+ emitIROperand(ctx, inst->getOperand(0), mode, rightSide(outerPrec,prec));
}
else
{
+ auto prec = kEOp_Postfix;
+ needClose = maybeEmitParens(outerPrec, prec);
+
emitIRType(ctx, inst->getDataType());
+ emit("(");
+ emitIROperand(ctx, inst->getOperand(0), mode, kEOp_General);
+ emit(")");
}
- emit("(");
- emitIROperand(ctx, inst->getOperand(0), mode, kEOp_General);
- emit(")");
break;
case kIROp_FieldExtract:
diff --git a/tests/bugs/paren-insertion-bug.slang b/tests/bugs/paren-insertion-bug.slang
new file mode 100644
index 000000000..608f8a9dd
--- /dev/null
+++ b/tests/bugs/paren-insertion-bug.slang
@@ -0,0 +1,34 @@
+// paren-insertion-bug.slang
+
+// Confirm that precedence is correctly handled
+// for cast from scalar to vector.
+
+//TEST(compute):COMPARE_COMPUTE:
+
+int test(float a)
+{
+ // This line performs a cast from a scalar result to a vector
+ float3 b = pow(a, 2.0);
+
+ // If the computation of `b` above gets folded into this
+ // line of code (and we expect it to) we need to correctly
+ // parenthesize the generated cast so that the `.xyz` swizzle
+ // applies to the result of the cast, rather than the input.
+ //
+ return int(float4(b.xyz * 2.0, 1.0).x);
+}
+
+
+//TEST_INPUT: ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out
+RWStructuredBuffer<uint> outputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ uint tid = dispatchThreadID.x;
+
+ uint inVal = tid;
+ uint outVal = test(inVal);
+
+ outputBuffer[tid] = outVal;
+}
diff --git a/tests/bugs/paren-insertion-bug.slang.expected.txt b/tests/bugs/paren-insertion-bug.slang.expected.txt
new file mode 100644
index 000000000..51033ef27
--- /dev/null
+++ b/tests/bugs/paren-insertion-bug.slang.expected.txt
@@ -0,0 +1,4 @@
+0
+2
+8
+12