From 05de8d86370d7a329bdf4cb4b9e5b270f6696076 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Sat, 8 Oct 2022 16:46:18 -0400 Subject: Fix for issue with user attribute lookup (#2437) * #include an absolute path didn't work - because paths were taken to always be relative. * Add handling for root paths. * Fixes around absolute paths. * Add SimplifyStyle * Remove unrequire include. * Fix some details around RelativeFileSystem canonical paths. * For MemoryFileSystem make sure "/a" and "a" maps to same canonical path. * Add test for canonicalPath. * Improve comment. * More testing around canonical paths. * Fix for user attribute lookup issue. * Add a test. * Small improvements in test. * Improve the comments around lookup workaround. --- source/slang/slang-check-impl.h | 5 ++++ source/slang/slang-check-modifier.cpp | 13 +++++++++ source/slang/slang-check.cpp | 20 ++++++++++++++ tests/bugs/user-attribute-lookup.slang | 32 ++++++++++++++++++++++ .../bugs/user-attribute-lookup.slang.expected.txt | 4 +++ 5 files changed, 74 insertions(+) create mode 100644 tests/bugs/user-attribute-lookup.slang create mode 100644 tests/bugs/user-attribute-lookup.slang.expected.txt diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h index 433c6ca70..c15428877 100644 --- a/source/slang/slang-check-impl.h +++ b/source/slang/slang-check-impl.h @@ -237,6 +237,11 @@ namespace Slang Dictionary lookupCache; }; + + /// Give a cache and a name, will remove all entries associated with a name + /// Might be useful/necessary if a new name is introduced + void removeLookupForName(TypeCheckingCache* cache, Name* name); + /// Shared state for a semantics-checking session. struct SharedSemanticsContext { diff --git a/source/slang/slang-check-modifier.cpp b/source/slang/slang-check-modifier.cpp index a7fb641a2..a2b411c22 100644 --- a/source/slang/slang-check-modifier.cpp +++ b/source/slang/slang-check-modifier.cpp @@ -226,6 +226,19 @@ namespace Slang attrDecl->parentDecl = parentDecl; parentDecl->members.add(attrDecl); + SLANG_ASSERT(!parentDecl->isMemberDictionaryValid()); + + // TODO(JS): A bit of a work around(!) + // + // To get to this point we must have already have performed a lookup for attributeName, + // and it failed. That lookup used the TypeCheckingCache, and + // so we know there is a cache entry that will be *wrong*, now we have created and + // added the AttributeDecl with the attributeName. + // + // To work around, we remove all cached lookups around the name, such that when a subsequent + // lookup is made, the cache will not return the old (wrong) result. + removeLookupForName(getLinkage()->getTypeCheckingCache(), attributeName); + // Finally, we perform any required semantic checks on // the newly constructed attribute decl. // diff --git a/source/slang/slang-check.cpp b/source/slang/slang-check.cpp index bcc74a6d0..8c6cddbfe 100644 --- a/source/slang/slang-check.cpp +++ b/source/slang/slang-check.cpp @@ -210,4 +210,24 @@ namespace Slang throw; } } + + void removeLookupForName(TypeCheckingCache* cache, Name* name) + { + auto& lookupCache = cache->lookupCache; + + List keys; + + for (const auto& pairs : lookupCache) + { + const auto& key = pairs.Key; + if (key.name == name) + { + keys.add(key); + } + } + for (auto& key : keys) + { + lookupCache.Remove(key); + } + } } diff --git a/tests/bugs/user-attribute-lookup.slang b/tests/bugs/user-attribute-lookup.slang new file mode 100644 index 000000000..96cd3ad19 --- /dev/null +++ b/tests/bugs/user-attribute-lookup.slang @@ -0,0 +1,32 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + +// To test for a bug where a user attribute, gets incorrectly looked up +// as a cached lookup made before creation will make it multiply defined. + +// Define an attribute that will appear in reflection +[__AttributeUsage(_AttributeTargets.Var)] +struct StaticSamplerAttribute {}; + +struct Thing +{ + [StaticSampler] SamplerState samplerState1; + [StaticSampler] SamplerState samplerState2; +} + +[StaticSampler] SamplerState samplerState; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + + float inVal = float(tid); + + outputBuffer[tid] = int(inVal); +} + + diff --git a/tests/bugs/user-attribute-lookup.slang.expected.txt b/tests/bugs/user-attribute-lookup.slang.expected.txt new file mode 100644 index 000000000..bc856dafa --- /dev/null +++ b/tests/bugs/user-attribute-lookup.slang.expected.txt @@ -0,0 +1,4 @@ +0 +1 +2 +3 -- cgit v1.2.3