From 3d7552582bb4d4e330b1e90bcdced046cebd140c Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:24:18 -0400 Subject: Fix bug with overload resolution under nested generics (#3107) * Add test for generic param inference bug for nested generics * Change description & simplify test * Add expected file * Check parent decl before unifying type parameters --- source/slang/slang-check-constraint.cpp | 10 +++--- tests/compute/generics-overload-2.slang | 38 ++++++++++++++++++++++ .../compute/generics-overload-2.slang.expected.txt | 4 +++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/compute/generics-overload-2.slang create mode 100644 tests/compute/generics-overload-2.slang.expected.txt diff --git a/source/slang/slang-check-constraint.cpp b/source/slang/slang-check-constraint.cpp index d66bf35cc..e7eccfaec 100644 --- a/source/slang/slang-check-constraint.cpp +++ b/source/slang/slang-check-constraint.cpp @@ -705,14 +705,16 @@ namespace Slang auto fstDeclRef = fstDeclRefType->getDeclRef(); if (auto typeParamDecl = as(fstDeclRef.getDecl())) - return TryUnifyTypeParam(constraints, typeParamDecl, snd); + if (typeParamDecl->parentDecl == constraints.genericDecl) + return TryUnifyTypeParam(constraints, typeParamDecl, snd); if (auto sndDeclRefType = as(snd)) { auto sndDeclRef = sndDeclRefType->getDeclRef(); if (auto typeParamDecl = as(sndDeclRef.getDecl())) - return TryUnifyTypeParam(constraints, typeParamDecl, fst); + if (typeParamDecl->parentDecl == constraints.genericDecl) + return TryUnifyTypeParam(constraints, typeParamDecl, fst); // can't be unified if they refer to different declarations. if (fstDeclRef.getDecl() != sndDeclRef.getDecl()) return false; @@ -816,7 +818,7 @@ namespace Slang if (auto typeParamDecl = as(fstDeclRef.getDecl())) { - if(typeParamDecl->parentDecl == constraints.genericDecl ) + if(typeParamDecl->parentDecl == constraints.genericDecl) return TryUnifyTypeParam(constraints, typeParamDecl, snd); } } @@ -827,7 +829,7 @@ namespace Slang if (auto typeParamDecl = as(sndDeclRef.getDecl())) { - if(typeParamDecl->parentDecl == constraints.genericDecl ) + if(typeParamDecl->parentDecl == constraints.genericDecl) return TryUnifyTypeParam(constraints, typeParamDecl, fst); } } diff --git a/tests/compute/generics-overload-2.slang b/tests/compute/generics-overload-2.slang new file mode 100644 index 000000000..8ca55ce7d --- /dev/null +++ b/tests/compute/generics-overload-2.slang @@ -0,0 +1,38 @@ +//TEST(smoke,compute):COMPARE_COMPUTE: -shaderobj +//TEST(smoke,compute):COMPARE_COMPUTE:-cpu -shaderobj + +// Test overload resolution for nested generic definitions + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + + +__generic +struct Foo +{ + T test(uint index, T x) + { + return __realCast(1.f); + } + + __generic + T test(vector index, T x) + { + return __realCast(2.f); + } +}; + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x + 2; + + Foo obj; + + float outVal = obj.test(tid, 0.f); + outputBuffer[0] = outVal; // Expect: 1 + + float outVal2 = obj.test(uint2(tid, tid), 0.f); + outputBuffer[1] = outVal2; // Expect: 2 +} \ No newline at end of file diff --git a/tests/compute/generics-overload-2.slang.expected.txt b/tests/compute/generics-overload-2.slang.expected.txt new file mode 100644 index 000000000..b7ad1e4a8 --- /dev/null +++ b/tests/compute/generics-overload-2.slang.expected.txt @@ -0,0 +1,4 @@ +3F800000 +40000000 +0 +0 -- cgit v1.2.3