Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gfx-unit-test/link-time-constant.slang fails with precompiled SPIR-V modules #6524

Open
cheneym2 opened this issue Mar 4, 2025 · 4 comments
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:bug something doesn't work like it should

Comments

@cheneym2
Copy link
Collaborator

cheneym2 commented Mar 4, 2025

Originally seen in gfx-unit-test-tool/linkTimeConstantD3D12.internal with precompiled target.

Repro:
slang-test gfx-unit-test-tool/linkTimeConstantD3D12

Module 1:

extern static const bool turnOnFeature;
extern static const float constValue;
extern static const uint numthread = 0;
extern static const int arraySize = -1;

// Main entry-point. Write some value into buffer depending on link
// time constant.
[shader("compute")]
[numthreads(numthread, 1, 1)]
void computeMain(
    uint3 sv_dispatchThreadID: SV_DispatchThreadID,
    uniform RWStructuredBuffer<float> buffer)
{
    int array[arraySize];

    array[sv_dispatchThreadID.x] = sv_dispatchThreadID.x;
    if (turnOnFeature)
    {
        buffer[array[0]] = constValue;
    }
    else
    {
        buffer[0] = -1.0;
    }
}

Module 2:

export static const bool turnOnFeature = true;
export static const float constValue = 2.0;
export static const uint numthread = 2;
export static const int arraySize = 4;
@cheneym2 cheneym2 added the kind:bug something doesn't work like it should label Mar 4, 2025
@cheneym2 cheneym2 added this to the Q1 2025 (Winter) milestone Mar 4, 2025
@cheneym2 cheneym2 self-assigned this Mar 4, 2025
@cheneym2
Copy link
Collaborator Author

cheneym2 commented Mar 5, 2025

Looks like the link time constants like

extern static const bool turnOnFeature;
extern static const float constValue;

Are causing compilation failures because they're unresolved by the module that's being compiled.

>	slang.dll!Slang::diagnoseUnresolvedSymbols(Slang::TargetRequest * req, Slang::DiagnosticSink * sink, Slang::IRModule * module) Line 1730	C++
 	slang.dll!Slang::linkIR(Slang::CodeGenContext * codeGenContext) Line 2262	C++
 	slang.dll!Slang::linkAndOptimizeIR(Slang::CodeGenContext * codeGenContext, const Slang::LinkingAndOptimizationOptions & options, Slang::LinkedIR & outLinkedIR) Line 644	C++
 	slang.dll!Slang::emitSPIRVForEntryPointsDirectly(Slang::CodeGenContext * codeGenContext, Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 2080	C++
 	slang.dll!Slang::CodeGenContext::_emitEntryPoints(Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 1816	C++
 	slang.dll!Slang::CodeGenContext::emitPrecompiledDownstreamIR(Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 800	C++
 	slang.dll!Slang::Module::precompileForTarget(SlangCompileTarget target, ISlangBlob * * outDiagnostics) Line 170	C++

static void diagnoseUnresolvedSymbols(TargetRequest* req, DiagnosticSink* sink, IRModule* module)
{
    for (auto globalSym : module->getGlobalInsts())
    {
        if (globalSym->findDecoration<IRImportDecoration>())
        {
            for (;;)
            {
                if (auto constant = as<IRGlobalConstant>(globalSym))
                {
                    if (constant->getOperandCount() == 0)
-->                        sink->diagnose(
                            globalSym->sourceLoc,
                            Diagnostics::unresolvedSymbol,
                            globalSym);
                }

@cheneym2
Copy link
Collaborator Author

cheneym2 commented Mar 6, 2025

Here's a directed test to show the issue:

Test name: tests/library/precompiled-spirv-global.slang

//TEST:COMPILE: tests/library/precompiled-spirv-global.slang -o tests/library/precompiled-spirv-global.slang-module -target spirv -embed-downstream-ir

//import "export-library-global.slang";  //This would make the test pass (it defined myGLobalVar = 42)

extern int myGlobalVar; //This makes the test fail

layout(location = 0) out float4 fragColor;

[shader("fragment")]
void main() {
    fragColor = float4(myGlobalVar);
}

@cheneym2
Copy link
Collaborator Author

cheneym2 commented Mar 6, 2025

Precompiling the module to SPIRV fails when the globals referenced are included only via "extern" declarations and the definitions are not included via directly importing the definition. If "myGlobalVar" is actually defined by importing a module that defines it, there's no problem, as the definition is included.

I think we can support this usecase though by leaving the mGlobalVar declaration in the precompiled SPIRV with import Linkage. But it will only work if the module actually defining myGlobalVar also receives complementary export Linkage.

cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 6, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 6, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 6, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 6, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
@cheneym2
Copy link
Collaborator Author

cheneym2 commented Mar 6, 2025

I've posted a directed test for this here: #6532

To run:

build/Release/bin/slang-test.exe tests/library/precompiled-spirv-global

The test crashes as-is, and so it's on the "expected failure list". Any fix for this bug should remove "tests/library/precompiled-spirv-global" from tests/expected-failure-github.txt

cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 6, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 12, 2025
This test exercises link time constants used by modules that are precompiled to SPIR-V.

Work on shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 12, 2025
When modules contain extern references to globals they don't define, or when the contain exports of globals they may or may not use, utilize SPIR-V linkage attributes to define and use the variables in embedded SPIR-V IR.

Fixes shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 12, 2025
When modules contain extern references to globals they don't define, or when the contain exports of globals they may or may not use, utilize SPIR-V linkage attributes to define and use the variables in embedded SPIR-V IR.

Fixes shader-slang#6524
@bmillsNV bmillsNV added the goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang label Mar 13, 2025
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 14, 2025
When modules contain extern references to globals they don't define, or when the contain exports of globals they may or may not use, utilize SPIR-V linkage attributes to define and use the variables in embedded SPIR-V IR.

Fixes shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 14, 2025
When modules contain extern references to globals they don't define, or when the contain exports of globals they may or may not use, utilize SPIR-V linkage attributes to define and use the variables in embedded SPIR-V IR.

Fixes shader-slang#6524
cheneym2 added a commit to cheneym2/slang that referenced this issue Mar 14, 2025
When modules contain extern references to globals they don't define, or when the contain exports of globals they may or may not use, utilize SPIR-V linkage attributes to define and use the variables in embedded SPIR-V IR.

Fixes shader-slang#6524
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:bug something doesn't work like it should
Projects
None yet
Development

No branches or pull requests

2 participants