From 6af3381f47e3c22e1657c0e0064fa466e8bde0f6 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 6 Oct 2025 17:21:37 -0700 Subject: Use symbol alias instead of wrapper synthesis to implement link-time types. (#8603) This change achieves link-time type resolution with a different mechanism. For `extern struct Foo : IFoo = FooImpl;`, instead of synthesizing a wrapper type `Foo` that has a `FooImpl inner` field and dispatches all interface method calls to `inner.method()`, this PR completely removes this synthesis step, and instead just lower such `extern`/`export` types as `IRSymbolAlias` instructions that is just a reference to the type being wrapped. Then we extend the linker logic to clone the referenced symbol instead of the SymbolAlias insts itself during linking. By doing so, we greatly simply the logic need to support link-time types, and achieves higher robustness by not having to deal with many AST synthesis scenarios. Closes #8554. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- .../unit-test-link-time-type-reflection.cpp | 90 +++++++++++++++++++++- 1 file changed, 87 insertions(+), 3 deletions(-) (limited to 'tools/slang-unit-test/unit-test-link-time-type-reflection.cpp') diff --git a/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp b/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp index c42fd2f16..0bd580c84 100644 --- a/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp +++ b/tools/slang-unit-test/unit-test-link-time-type-reflection.cpp @@ -27,16 +27,21 @@ SLANG_UNIT_TEST(linkTimeTypeReflection) interface IMaterial { float4 load(); } extern struct Material : IMaterial; ConstantBuffer gMaterial; - + + interface IFoo { float getVal(); } + struct DefaultFoo : IFoo { float getVal() { return 0.0f; } } + extern struct Foo : IFoo = DefaultFoo; + RWTexture2D tex; extern static const int count; uniform uint4 buffers[count]; + uniform Foo gFoo; [numthreads(1,1,1)] [shader("compute")] void computeMain() { - tex[uint2(0, 0)] = gMaterial.load(); + tex[uint2(0, 0)] = gMaterial.load() + gFoo.getVal(); } )"; @@ -65,7 +70,8 @@ SLANG_UNIT_TEST(linkTimeTypeReflection) String configModuleSource = "import " + moduleName + ";\n" + R"( export struct Material : IMaterial = MyMaterial; export static const int count = 11; - + struct FooImpl : IFoo { T vals[x]; float getVal() { return x; } } + export struct Foo : IFoo = FooImpl; struct MyMaterial : IMaterial { int data; Texture2D diffuse; @@ -110,6 +116,9 @@ SLANG_UNIT_TEST(linkTimeTypeReflection) auto var2 = programLayout->getParameterByIndex(2); SLANG_CHECK(var2->getTypeLayout()->getSize() == 11 * 16); + auto var3 = programLayout->getParameterByIndex(3); + SLANG_CHECK(var3->getTypeLayout()->getSize() == 32); + ComPtr codeBlob; linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef()); @@ -226,3 +235,78 @@ SLANG_UNIT_TEST(linkTimeConditionalReflection) SLANG_CHECK(spirvStr.indexOf(toSlice("Location 1")) != -1); SLANG_CHECK(spirvStr.indexOf(toSlice("Location 2")) == -1); } + +// Test that loading a module that defines an `export` type, but not linking with the module should +// not affect the type layout. + +SLANG_UNIT_TEST(linkTimeTypeReflectionWithLoadedButNotLinkedModule) +{ + // Source for a module that contains can be specialized with a link-time type. + const char* userSourceBody = R"( + interface IFoo { float getVal(); } + struct DefaultFoo : IFoo { float getVal() { return 0.0f; } } + extern struct Foo : IFoo = DefaultFoo; + + uniform Foo gFoo; + RWTexture2D tex; + + [numthreads(1,1,1)] + [shader("compute")] + void computeMain() { + tex[uint2(0, 0)] = gFoo.getVal(); + } + )"; + + String moduleName = "linkTimeTypeReflection_Compute"; + + ComPtr globalSession; + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_SPIRV_ASM; + targetDesc.profile = globalSession->findProfile("spirv_1_5"); + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + sessionDesc.targets = &targetDesc; + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr diagnosticBlob; + auto module = session->loadModuleFromSourceString( + moduleName.getBuffer(), + (moduleName + ".slang").getBuffer(), + userSourceBody, + diagnosticBlob.writeRef()); + SLANG_CHECK_ABORT(module != nullptr); + + // Source for a module that defines the link-time type, but we won't link with it. + String configModuleSource = "import " + moduleName + ";\n" + R"( + struct FooImpl : IFoo { T vals[x]; float getVal() { return x; } } + export struct Foo : IFoo = FooImpl; + )"; + auto configModule = session->loadModuleFromSourceString( + "config", + "config.slang", + configModuleSource.getBuffer(), + diagnosticBlob.writeRef()); + SLANG_CHECK_ABORT(configModule != nullptr); + + ComPtr linkedProgram; + module->link(linkedProgram.writeRef(), diagnosticBlob.writeRef()); + SLANG_CHECK_ABORT(linkedProgram != nullptr); + + auto programLayout = linkedProgram->getLayout(); + auto var0 = programLayout->getParameterByIndex(0); + + // Size of `gFoo` is 0, because the module that defines `Foo = FooImpl` is not linked. + // Therefore `gFoo`'s type is defaulted to `DefaultFoo`, which has no fields. + SLANG_CHECK(var0->getTypeLayout()->getSize() == 0); + + ComPtr codeBlob; + linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef()); + + SLANG_CHECK_ABORT(codeBlob.get()); + + auto spirvStr = UnownedStringSlice((const char*)codeBlob->getBufferPointer()); + + SLANG_CHECK(spirvStr.indexOf(toSlice("OpDecorate %tex Binding 0")) != -1); +} -- cgit v1.2.3