shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

NULL pointer access when retrieving field layout #4452

Open chaoticbob opened 1 week ago

chaoticbob commented 1 week ago

The shader below is causing a NULL pointer access when retrieving the field layout at slang-ir-insts.h::getFieldLayout(), the element at index is null. This may be related to https://github.com/shader-slang/slang/issues/4451.

CMD:

slangc -target spirv -lang slang -D__spirv__ -emit-spirv-directly -profile vs_6_0 -entry main shader.hlsl

Shader

struct S {
    float4 m : MMM;
};

struct T {
    float3 n : NNN;
};

struct Base {
    float4 a : AAA;
    float4 b : BBB;
    S      s;
    float4 p : SV_Position;
};

struct Derived : Base {
    T      t;
    float4 c : CCC;
    float4 d : DDD;
};

void main(in Derived input, out Derived output) {
    output = input;
}
csyonghe commented 1 week ago

Can you provide the host API calls so we can know which type and which field you are querying?

chaoticbob commented 1 week ago

Sorry, should've included this initially, here's the call stack:

slang.dll!Slang::IRInst::getOperandCount() Line 602
    at C:\code\source\slang\source\slang\slang-ir.h(602)
slang.dll!Slang::IRInst::getOperand(unsigned __int64 index) Line 756
    at C:\code\source\slang\source\slang\slang-ir.h(756)
slang.dll!Slang::IRStructFieldLayoutAttr::getLayout() Line 1931
    at C:\code\source\slang\source\slang\slang-ir-insts.h(1931)
slang.dll!Slang::IRStructTypeLayout::getFieldLayout(unsigned __int64 index) Line 1958
    at C:\code\source\slang\source\slang\slang-ir-insts.h(1958)
slang.dll!Slang::createGLSLGlobalVaryingsImpl(Slang::GLSLLegalizationContext * context, Slang::CodeGenContext * codeGenContext, Slang::IRBuilder * builder, Slang::IRType * type, Slang::IRVarLayout * varLayout, Slang::IRTypeLayout * typeLayout, slang::ParameterCategory kind, Slang::Stage stage, unsigned __int64 bindingIndex, unsigned __int64 bindingSpace, Slang::GlobalVaryingDeclarator * declarator, Slang::OuterParamInfoLink * outerParamInfo, Slang::IRInst * leafVar, Slang::StringBuilder & nameHintSB) Line 1717
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(1717)
slang.dll!Slang::createGLSLGlobalVaryingsImpl(Slang::GLSLLegalizationContext * context, Slang::CodeGenContext * codeGenContext, Slang::IRBuilder * builder, Slang::IRType * type, Slang::IRVarLayout * varLayout, Slang::IRTypeLayout * typeLayout, slang::ParameterCategory kind, Slang::Stage stage, unsigned __int64 bindingIndex, unsigned __int64 bindingSpace, Slang::GlobalVaryingDeclarator * declarator, Slang::OuterParamInfoLink * outerParamInfo, Slang::IRInst * leafVar, Slang::StringBuilder & nameHintSB) Line 1734
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(1734)
slang.dll!Slang::createGLSLGlobalVaryings(Slang::GLSLLegalizationContext * context, Slang::CodeGenContext * codeGenContext, Slang::IRBuilder * builder, Slang::IRType * type, Slang::IRVarLayout * layout, slang::ParameterCategory kind, Slang::Stage stage, Slang::IRInst * leafVar) Line 1812
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(1812)
slang.dll!Slang::legalizeEntryPointParameterForGLSL(Slang::GLSLLegalizationContext * context, Slang::CodeGenContext * codeGenContext, Slang::IRFunc * func, Slang::IRParam * pp, Slang::IRVarLayout * paramLayout) Line 3119
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(3119)
slang.dll!Slang::legalizeEntryPointForGLSL(Slang::Session * session, Slang::IRModule * module, Slang::IRFunc * func, Slang::CodeGenContext * codeGenContext, Slang::GLSLExtensionTracker * glslExtensionTracker) Line 3517
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(3517)
slang.dll!Slang::legalizeEntryPointsForGLSL(Slang::Session * session, Slang::IRModule * module, const Slang::List<Slang::IRFunc *,Slang::StandardAllocator> & funcs, Slang::CodeGenContext * context, Slang::GLSLExtensionTracker * glslExtensionTracker) Line 3624
    at C:\code\source\slang\source\slang\slang-ir-glsl-legalize.cpp(3624)
slang.dll!Slang::linkAndOptimizeIR(Slang::CodeGenContext * codeGenContext, const Slang::LinkingAndOptimizationOptions & options, Slang::LinkedIR & outLinkedIR) Line 1127
    at C:\code\source\slang\source\slang\slang-emit.cpp(1127)
slang.dll!Slang::emitSPIRVForEntryPointsDirectly(Slang::CodeGenContext * codeGenContext, Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 1648
    at C:\code\source\slang\source\slang\slang-emit.cpp(1648)
slang.dll!Slang::CodeGenContext::_emitEntryPoints(Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 1618
    at C:\code\source\slang\source\slang\slang-compiler.cpp(1618)
slang.dll!Slang::CodeGenContext::emitEntryPoints(Slang::ComPtr<Slang::IArtifact> & outArtifact) Line 1664
    at C:\code\source\slang\source\slang\slang-compiler.cpp(1664)
slang.dll!Slang::TargetProgram::_createWholeProgramResult(Slang::DiagnosticSink * sink, Slang::EndToEndCompileRequest * endToEndReq) Line 1817
    at C:\code\source\slang\source\slang\slang-compiler.cpp(1817)
slang.dll!Slang::EndToEndCompileRequest::generateOutput(Slang::TargetProgram * targetProgram) Line 1906
    at C:\code\source\slang\source\slang\slang-compiler.cpp(1906)
slang.dll!Slang::EndToEndCompileRequest::generateOutput(Slang::ComponentType * program) Line 2246
    at C:\code\source\slang\source\slang\slang-compiler.cpp(2246)
slang.dll!Slang::EndToEndCompileRequest::generateOutput() Line 2257
    at C:\code\source\slang\source\slang\slang-compiler.cpp(2257)
slang.dll!Slang::EndToEndCompileRequest::executeActionsInner() Line 3145
    at C:\code\source\slang\source\slang\slang.cpp(3145)
slang.dll!Slang::EndToEndCompileRequest::executeActions() Line 3154
    at C:\code\source\slang\source\slang\slang.cpp(3154)
slang.dll!Slang::EndToEndCompileRequest::compile() Line 6135
    at C:\code\source\slang\source\slang\slang.cpp(6135)
slang.dll!spCompile(slang::ICompileRequest * request) Line 636
    at C:\code\source\slang\source\slang\slang-api.cpp(636)
slangc.exe!_compile(slang::ICompileRequest * compileRequest, int argc, const char * const * argv) Line 53
    at C:\code\source\slang\source\slangc\main.cpp(53)
slangc.exe!innerMain(Slang::StdWriters * stdWriters, slang::IGlobalSession * sharedSession, int argc, const char * const * argv) Line 104
    at C:\code\source\slang\source\slangc\main.cpp(104)
slangc.exe!slangc_main(int argc, char * * argv) Line 114
    at C:\code\source\slang\source\slangc\main.cpp(114)
slangc.exe!wmain(int argc, wchar_t * * argv) Line 138
    at C:\code\source\slang\source\slangc\main.cpp(138)
csyonghe commented 1 week ago

Sorry I was asking for the host code in addition to the shader code so we can reproduce this.

chaoticbob commented 1 week ago

IIUC: this is happening just during the compilation from the command line. I'm currently not using any API calls.

csyonghe commented 1 week ago

I see, this is likely due to use of inheritance here. Our implementation of inheritance is closer to none than to complete so it shall be considered as not supported right now.

chaoticbob commented 1 week ago

Got it, thanks. I saw the mention in the docs of limited inheritance, I couldn't figure out if the docs were out of date or not. It's good to have this clarification.

jkwak-work commented 2 days ago

@swoods-nv I think we should close this for now and keep track it as a long term task, "Implement inheritance".