From c5b0708ead5de2d90ef14f20b5b8e3ed4f576373 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 30 Jun 2023 15:25:59 -0400 Subject: Fix for operator assignment issue (#2951) * WIP handling LValue coercion via LValueImplicitCast * Need to have the ptr type for the cast. * Casting conversion working on C++. * Make the LValue casts record if in or in/out as we can produce better code if we know the difference. * WIP LValueCast pass * Fix tests so we don't fail because downstream compilers detect use of uninitialized variable. * Do conversions through through tmp for l-value scenarios that can't work other ways. * Fix a typo. * Change diagnostic implicit-cast-lvalue for a type that still exhibits the issue. * Add matrix test. * Added a bit more clarity around LValue casting choices. * Small comment improvements. Improvements based on comments on PR. * Use findOuterGeneric. --- tests/bugs/op-assignment-unify-mat.slang | 47 ++++++++++++++++++++++ .../op-assignment-unify-mat.slang.expected.txt | 4 ++ tests/bugs/op-assignment-unify-vec.slang | 47 ++++++++++++++++++++++ .../op-assignment-unify-vec.slang.expected.txt | 6 +++ tests/bugs/op-assignment-unify.slang | 44 ++++++++++++++++++++ tests/bugs/op-assignment-unify.slang.expected.txt | 6 +++ 6 files changed, 154 insertions(+) create mode 100644 tests/bugs/op-assignment-unify-mat.slang create mode 100644 tests/bugs/op-assignment-unify-mat.slang.expected.txt create mode 100644 tests/bugs/op-assignment-unify-vec.slang create mode 100644 tests/bugs/op-assignment-unify-vec.slang.expected.txt create mode 100644 tests/bugs/op-assignment-unify.slang create mode 100644 tests/bugs/op-assignment-unify.slang.expected.txt (limited to 'tests/bugs') diff --git a/tests/bugs/op-assignment-unify-mat.slang b/tests/bugs/op-assignment-unify-mat.slang new file mode 100644 index 000000000..13b6ecf48 --- /dev/null +++ b/tests/bugs/op-assignment-unify-mat.slang @@ -0,0 +1,47 @@ +// We can't test on VK, as currently we don't support integer matrix types +//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -vk +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -cpu + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + +void silly(matrix a, inout matrix b) +{ + b *= a; +} + +void silly2(matrix a, out matrix b) +{ + b = a; +} + + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + matrix v = { 1, 2, 3, 4}; + matrix u = { 5, 6, 7, 8}; + + int idx = int(dispatchThreadID.x); + + matrix uidx = { idx, idx * idx, idx + idx, idx + 2 }; + + v += uidx; + u += idx; + + silly(u, v); + silly(v, u); + + // Check that detects issues with undefined variables. + + matrix undef1, undef2; // undefined + // Downstream compilers detect this + //silly(u, undef1); + silly2(u, undef2); + + matrix added = (u + v + undef2); + uint2 added2 = added[0] + added[1]; + + outputBuffer[idx] = added2.x + added2.y; +} diff --git a/tests/bugs/op-assignment-unify-mat.slang.expected.txt b/tests/bugs/op-assignment-unify-mat.slang.expected.txt new file mode 100644 index 000000000..f5973ac46 --- /dev/null +++ b/tests/bugs/op-assignment-unify-mat.slang.expected.txt @@ -0,0 +1,4 @@ +53E +92C +FA8 +19C0 diff --git a/tests/bugs/op-assignment-unify-vec.slang b/tests/bugs/op-assignment-unify-vec.slang new file mode 100644 index 000000000..4cc3154a0 --- /dev/null +++ b/tests/bugs/op-assignment-unify-vec.slang @@ -0,0 +1,47 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -vk +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -cpu + + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + + +void silly(vector a, inout vector b) +{ + b *= a; +} + +void silly2(vector a, out vector b) +{ + b = a; +} + + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int2 v = int2(0, 0); + uint2 u = uint2(0, 0); + + int idx = int(dispatchThreadID.x); + + uint2 uidx = uint2(idx, idx * idx); + + v += uidx; + u += idx; + + silly(u, v); + silly(v, u); + + // Check that detects issues with undefined variables. + + int2 undef1, undef2; // undefined + // Downstream compilers detect this + //silly(u, undef1); + silly2(u, undef2); + + uint2 added = (u + v + undef2); + + outputBuffer[idx] = added.x + added.y; +} diff --git a/tests/bugs/op-assignment-unify-vec.slang.expected.txt b/tests/bugs/op-assignment-unify-vec.slang.expected.txt new file mode 100644 index 000000000..d92319a24 --- /dev/null +++ b/tests/bugs/op-assignment-unify-vec.slang.expected.txt @@ -0,0 +1,6 @@ +0 +6 +3C +FC +0 +0 diff --git a/tests/bugs/op-assignment-unify.slang b/tests/bugs/op-assignment-unify.slang new file mode 100644 index 000000000..738b49677 --- /dev/null +++ b/tests/bugs/op-assignment-unify.slang @@ -0,0 +1,44 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -vk +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -cpu + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + + +void silly(T a, inout T b) +{ + b *= a; +} + +void silly2(T a, out T b) +{ + b = a; +} + + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int v = 0; + uint u = 0; + + int idx = int(dispatchThreadID.x); + uint uidx = uint(idx); + + v += uidx; + u += idx; + + silly(u, v); + silly(v, u); + + // Check that detects issues with undefined variables. + + int undef1, undef2; // undefined + + // Downstream compilers detect this... + //silly(u, undef1); + silly2(u, undef2); + + outputBuffer[idx] = u + v + undef2; +} diff --git a/tests/bugs/op-assignment-unify.slang.expected.txt b/tests/bugs/op-assignment-unify.slang.expected.txt new file mode 100644 index 000000000..539ab696c --- /dev/null +++ b/tests/bugs/op-assignment-unify.slang.expected.txt @@ -0,0 +1,6 @@ +0 +3 +14 +3F +0 +0 -- cgit v1.2.3