Skip to content

Commit

Permalink
Add parentheses to make precedence explicit (#6030)
Browse files Browse the repository at this point in the history
* Add parentheses to make precedence explicit

Add parentheses for a few cases that Dawn/Tint (WGSL compiler) complains about.
Closes #6005.

* format code

---------

Co-authored-by: slangbot <[email protected]>
Co-authored-by: Yong He <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent 2249d6f commit dab6cec
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/slang/slang-emit-c-like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,34 @@ bool CLikeSourceEmitter::maybeEmitParens(EmitOpInfo& outerPrec, const EmitOpInfo
{
needParens = true;
}
// a ^ b + c => a ^ (b + c)
else if (
prec.leftPrecedence == EPrecedence::kEPrecedence_Additive_Left &&
outerPrec.leftPrecedence == EPrecedence::kEPrecedence_BitXor_Right)
{
needParens = true;
}
// a + b ^ c => (a + b) ^ c
else if (
prec.rightPrecedence == EPrecedence::kEPrecedence_Additive_Right &&
outerPrec.rightPrecedence == EPrecedence::kEPrecedence_BitXor_Left)
{
needParens = true;
}
// a | b + c => a | (b + c)
else if (
prec.leftPrecedence == EPrecedence::kEPrecedence_Additive_Left &&
outerPrec.leftPrecedence == EPrecedence::kEPrecedence_BitOr_Right)
{
needParens = true;
}
// a + b | c => (a + b) | c
else if (
prec.rightPrecedence == EPrecedence::kEPrecedence_Additive_Right &&
outerPrec.rightPrecedence == EPrecedence::kEPrecedence_BitOr_Left)
{
needParens = true;
}

if (needParens)
{
Expand Down
45 changes: 45 additions & 0 deletions tests/wgsl/operator-precedence.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//TEST(compute):COMPARE_COMPUTE:-shaderobj

//TEST_INPUT:ubuffer(data=[3 7 8], stride=4):name=inputBuffer
RWStructuredBuffer<uint> inputBuffer;

//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<uint> outputBuffer;

[numthreads(1,1,1)]
void computeMain()
{
uint a = inputBuffer[0];
uint b = inputBuffer[1];
uint c = inputBuffer[2];

outputBuffer[0] = a+b ^ c;
outputBuffer[1] = a ^ b+c;

outputBuffer[2] = a-b | c;
outputBuffer[3] = a | b-c;

outputBuffer[4] = a+b & c;
outputBuffer[5] = a & b+c;

outputBuffer[6] = a<<b ^ c;
outputBuffer[7] = a ^ b<<c;

outputBuffer[8] = a>>b | c;
outputBuffer[9] = a | b>>c;

outputBuffer[10] = a<<b & c;
outputBuffer[11] = a & b<<c;

outputBuffer[12] = a<b && b!=c;
outputBuffer[13] = a==b || b>=c;

outputBuffer[14] = a*b ^ c;
outputBuffer[15] = a ^ b*c;

outputBuffer[16] = a/b | c;
outputBuffer[17] = a | b/c;

outputBuffer[18] = a*b & c;
outputBuffer[19] = a & b*c;
}
20 changes: 20 additions & 0 deletions tests/wgsl/operator-precedence.slang.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
2
C
FFFFFFFC
FFFFFFFF
8
3
188
703
8
3
0
0
1
0
1D
3B
8
3
0
0

0 comments on commit dab6cec

Please sign in to comment.