From 57b09a8986668626c37055e431fa0ac6449d7214 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:27:23 -0800 Subject: Use and() and or() functions for logical-AND and OR (#6310) * Use and() and or() functions for logical-AND and OR With this commit, Slang will emit function calls to `and()` and `or()` for the logical-AND and logical-OR when the operands are non-scalar and the target profile is SM6.0 and above. This is required change from SM6.0. For WGSL, there is no operator overloadings of `&&` and `||` when the operands are non-scalar. Unlike HLSL, WGSL also don't have `and()` nor `or()`. Alternatively, we can use `select()`. --- .../logic-no-short-circuit-evaluation.slang | 66 ++++++++++++++++++++++ tests/compute/logic-short-circuit-evaluation.slang | 15 +++-- ...gic-short-circuit-evaluation.slang.expected.txt | 16 ------ 3 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 tests/compute/logic-no-short-circuit-evaluation.slang delete mode 100644 tests/compute/logic-short-circuit-evaluation.slang.expected.txt (limited to 'tests/compute') diff --git a/tests/compute/logic-no-short-circuit-evaluation.slang b/tests/compute/logic-no-short-circuit-evaluation.slang new file mode 100644 index 000000000..74351a505 --- /dev/null +++ b/tests/compute/logic-no-short-circuit-evaluation.slang @@ -0,0 +1,66 @@ +//TEST(compute):SIMPLE(filecheck=SM5):-target hlsl -profile cs_5_1 -entry computeMain +//TEST(compute):SIMPLE(filecheck=SM6):-target hlsl -profile cs_6_0 -entry computeMain +//TEST(compute):SIMPLE(filecheck=WGS):-target wgsl -stage compute -entry computeMain +//TEST(compute):SIMPLE(filecheck=MTL):-target metal -stage compute -entry computeMain +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-slang -compute -shaderobj -output-using-type -xslang -Wno-30056 +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-vk -compute -shaderobj -output-using-type -xslang -Wno-30056 +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-mtl -compute -shaderobj -output-using-type -xslang -Wno-30056 +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHK):-cuda -compute -shaderobj -output-using-type -xslang -Wno-30056 +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHK):-cpu -compute -shaderobj -output-using-type -xslang -Wno-30056 + +// Testnig logical-AND, logical-OR and ternary operator with non-scalar operands + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +static int result = 0; + +bool2 assignFunc(int index) +{ + result += 10; + return bool2(true); +} + +[numthreads(4, 1, 1)] +void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) +{ + int index = dispatchThreadID.x; + + // No short-circuiting for vector types + + //SM5:(all({{.*}}&& + //SM6:(all(and( + //WGS:(all(select(vec2(false), + //MTL:(all({{.*}}&& + if (all(bool2(index >= 1) && assignFunc(index))) + { + result++; + } + + // Intentionally using non-boolean type for testing. + + //SM5:(all({{.*}}|| + //SM6:(or(vector( + //WGS:(select({{.*}}, vec2(true), vec2( + //MTL:(all(bool2({{.*}}|| + if (all(int2(index >= 2) || !assignFunc(index))) + { + result++; + } + + //SM5:(all({{.*}}?{{.*}}: + //SM6:(all(select( + //WGS:(all(select(vec2(false), + //MTL:(all(select(bool2(false) + if (all(bool2(index >= 3) ? assignFunc(index) : bool2(false))) + { + result++; + } + + outputBuffer[index] = result; + + //CHK:30 + //CHK-NEXT:31 + //CHK-NEXT:32 + //CHK-NEXT:33 +} diff --git a/tests/compute/logic-short-circuit-evaluation.slang b/tests/compute/logic-short-circuit-evaluation.slang index 585a04770..eed30898f 100644 --- a/tests/compute/logic-short-circuit-evaluation.slang +++ b/tests/compute/logic-short-circuit-evaluation.slang @@ -1,8 +1,9 @@ -//TEST(compute):COMPARE_COMPUTE:-dx12 -compute -shaderobj -//TEST(compute):COMPARE_COMPUTE:-vk -compute -shaderobj -//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -compile-arg -O3 -shaderobj -//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-dx12 -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-vk -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHK):-mtl -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHK):-cuda -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHK):-cpu -compute -compile-arg -O3 -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHK):-slang -compute -shaderobj // Test doing vector comparisons @@ -25,4 +26,8 @@ void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) // Only the last 4 elements will be 1. (index < 12) || assignFunc(index); + + //CHK-COUNT-4: 1 + //CHK-COUNT-8: 0 + //CHK-COUNT-4: 1 } diff --git a/tests/compute/logic-short-circuit-evaluation.slang.expected.txt b/tests/compute/logic-short-circuit-evaluation.slang.expected.txt deleted file mode 100644 index 945f08f2c..000000000 --- a/tests/compute/logic-short-circuit-evaluation.slang.expected.txt +++ /dev/null @@ -1,16 +0,0 @@ -1 -1 -1 -1 -0 -0 -0 -0 -0 -0 -0 -0 -1 -1 -1 -1 -- cgit v1.2.3