Skip to content

Out-of-line initialization of static const member arrays doesn't work, and wrongly requires static const at definition site #5823

Open

Description

Description
When compiling with -HV 202x -T ps_6_7 -E PSMain it works and produces DXIL, when adding -spirv it SIGSEGVs the compiler.

Steps to Reproduce
https://godbolt.org/z/WasWzsq4E

If you strip all my C++ compat code then you have
https://godbolt.org/z/dWMfjxza1

Actual Behavior
SIGSEGV of the compiler.

I think this is because static outside of the class makes no sense in C++ (and for LLVM), but I cannot use const on its own because I get a different problem.

Environment

  • DXC version Latest trunk according to Godbolt
  • Host Operating System Linux I guess

Activity

  1. added
    bug
    needs-triage
    spirv
    on Oct 3, 2023
  2. sudonatalie commented on Oct 17, 2023

    @sudonatalie
    Collaborator

    Thanks for reporting. Assigning to Cassandra Beckley (@cassiebeckley) to take a look.

  3. moved this from For Google to Done in HLSL Triageon Oct 17, 2023
  4. cassiebeckley commented on Oct 19, 2023

    @cassiebeckley
    Contributor

    I think this might actually be a parser or Sema bug. The AST I get for this code using -ast-dump is

    TranslationUnitDecl 0x561d24809980 <<invalid sloc>> <invalid sloc>
    |-ClassTemplateDecl 0x561d248380b0 <<source>:4:1, line:5:8> col:8 GaussLegendreValues
    | |-NonTypeTemplateParmDecl 0x561d24837ee0 <line:4:11, col:15> col:15 'int' Order
    | |-TemplateTypeParmDecl 0x561d24837f58 <col:22, col:39> col:31 typename float_t
    | | `-TemplateArgument type 'float'
    | `-CXXRecordDecl 0x561d24837ff8 <line:5:1, col:8> col:8 struct GaussLegendreValues
    |-ClassTemplatePartialSpecializationDecl 0x561d248414f0 <line:8:1, line:13:1> line:9:8 struct GaussLegendreValues definition
    | |-TemplateArgument integral 2
    | |-TemplateArgument type 'type-parameter-0-0'
    | |-TemplateTypeParmDecl 0x561d24838338 <line:8:11, col:20> col:20 referenced typename float_t
    | |-CXXRecordDecl 0x561d24841778 <line:9:1, col:8> col:8 implicit struct GaussLegendreValues
    | |-VarDecl 0x561d248418e8 <line:11:5, col:30> col:26 wi 'const float_t [2]' static
    | `-VarDecl 0x561d24841998 <line:12:5, col:30> col:26 xi 'const float_t [2]' static
    |-VarDecl 0x561d24841e38 parent 0x561d248414f0 prev 0x561d248418e8 <line:14:1, col:98> col:81 wi 'const float_t [2]' static cinit
    | `-InitListExpr 0x561d24841f28 <col:89, col:98> 'void'
    |   |-FloatingLiteral 0x561d24841ee8 <col:90> 'literal float' 1.000000e+00
    |   `-FloatingLiteral 0x561d24841f08 <col:95> 'literal float' 1.000000e+00
    |-CXXRecordDecl 0x561d24841f78 <line:18:1, line:22:1> line:18:8 referenced struct PSInput definition
    | |-CXXRecordDecl 0x561d248420b0 <col:1, col:8> col:8 implicit struct PSInput
    | |-FieldDecl 0x561d24842268 <line:20:5, col:12> col:12 position 'float4':'vector<float, 4>'
    | | `-SemanticDecl 0x561d248422c0 <col:23> "SV_Position"
    | `-FieldDecl 0x561d24842308 <line:21:5, col:12> col:12 referenced color 'float4':'vector<float, 4>'
    |   `-SemanticDecl 0x561d24842360 <col:23> "COLOR0"
    `-FunctionDecl 0x561d248476d8 <line:24:1, line:27:1> line:24:8 PSMain 'float4 (PSInput)'
      |-ParmVarDecl 0x561d24847588 <col:15, col:23> col:23 used input 'PSInput'
      |-CompoundStmt 0x561d248478b8 <line:25:1, line:27:1>
      | `-ReturnStmt 0x561d248478a0 <line:26:5, col:18>
      |   `-ImplicitCastExpr 0x561d24847888 <col:12, col:18> 'float4':'vector<float, 4>' <LValueToRValue>
      |     `-MemberExpr 0x561d24847838 <col:12, col:18> 'float4':'vector<float, 4>' lvalue .color 0x561d24842308
      |       `-DeclRefExpr 0x561d24847810 <col:12> 'PSInput' lvalue ParmVar 0x561d24847588 'input' 'PSInput'
      `-SemanticDecl 0x561d248477d8 <line:24:32> "SV_Target0"
    

    The relevant portion is

    InitListExpr 0x561d24841f28 <col:89, col:98> 'void'
    |-FloatingLiteral 0x561d24841ee8 <col:90> 'literal float' 1.000000e+00
    `-FloatingLiteral 0x561d24841f08 <col:95> 'literal float' 1.000000e+00
    

    where the InitListExpr is being marked as being of type 'void'. This tracks with the error message that I get from my local build:

    error: casting to type 'void' unimplemented
    

    Chris B (@llvm-beanz) is this AST correct for the given code? Do you know if it would be better to change the AST or to change the SPIR-V backend to use the type that the InitListExpr is being assigned to?

  5. llvm-beanz commented on Oct 19, 2023

    @llvm-beanz
    Collaborator

    I can't say that I understand it, but clang gives InitListExpr type void for C++ too:

    https://godbolt.org/z/oKrhbYGrn

    -VarDecl 0xd8337f8 parent 0xd832f98 prev 0xd833460 <line:12:1, col:122> col:74 xi 'const float_t[2]' cinit
      `-InitListExpr 0xd8518b8 <col:82, col:122> 'void'
        |-UnaryOperator 0xd851880 <col:83, col:84> 'double' prefix '-'
        | `-FloatingLiteral 0xd851860 <col:84> 'double' 5.773503e-01
        `-FloatingLiteral 0xd851898 <col:104> 'double' 5.773503e-01
    

    So I think that bit is right.

  6. cassiebeckley commented on Oct 19, 2023

    @cassiebeckley
    Contributor

    Huh, interesting. Thanks for pointing that out, I'll update the backend then.

  7. devshgraphicsprogramming commented on Jun 4, 2024

    @devshgraphicsprogramming
    Author

    Btw it works for non-array types
    https://godbolt.org/z/9G7vnhesr
    and on this simple repro
    https://godbolt.org/z/ze8jaoM3z

    But this is a full explicit specialization out of line initialization, not a partial.

  8. 5 remaining items

  9. devshgraphicsprogramming commented on Dec 10, 2025

    @devshgraphicsprogramming
    Author

    Now the original thing has a useful error

    error: casting to type 'void' unimplemented
    

    https://godbolt.org/z/14badfxqE

    Btw I don't get one thing, in c++20 for out of line consexpr declarations, I'd use const static at the member declaration.

    template <typename float_t>
    struct GaussLegendreValues<2,float_t>
    {
        const static float_t wi[2];
    };

    and then for the out-of line definitition constexpr or constexpr inline depending on the linkage I want

    template<typename float_t> constexpr inline float_t GaussLegendreValues<2,float_t>::wi[2] = {1.0, 1.0};

    https://godbolt.org/z/sr9vh3Gbb

    For obvious reasons in HLSL I need to use just const for the OOL definition.

    template<typename float_t> const float_t GaussLegendreValues<2,float_t>::wi[2] = {1.0, 1.0};

    With C++ it actually results in the same codegen if I don't do any exporting or linking
    https://godbolt.org/z/W3cnYjqb1

    Our repro was always wrong

    template<typename float_t> const static float_t GaussLegendreValues<2,float_t>::wi[2] = {1.0, 1.0};

    and compiler should emit a diagnostic like C++ https://godbolt.org/z/M9hajz5eE

    <source>:13:34: error: 'static' can only be specified inside the class definition
       13 | template<typename float_t> const static float_t GaussLegendreValues<2,float_t>::wi[2] = {1.0, 1.0};
          |                                  ^~~~~~
    1 error generated.
    Compiler returned: 1
    

    instead I get

    error: casting to type 'void' unimplemented
    

    But then if I try to use the "correct" thing, we have https://godbolt.org/z/fExjP8rEM

    <source>:14:28: error: 'const' is not a valid modifier for a field
    template<typename float_t> const float_t GaussLegendreValues<2,float_t>::wi[2] = {1.0, 1.0};
    

    So the error is that we don't get a diagnostic of trying to use static on a class/struct member outside the class definition, and that we get an error and a diagnostic when attempting to do the "right thing" (TM).

    Natalie Chouinard (@sudonatalie) I see that Cassandra Beckley (@cassiebeckley) has left Google, is there chance anyone else gets assigned to this?

  10. changed the title [-][SPIR-V] Out-of-line initialization of `static const` member arrays of templated types SIGSEGVs[/-] [+]Out-of-line initialization of `static const` member arrays doesn't work, and wrongly requires `static const` at definition site[/+] on Dec 10, 2025
  11. devshgraphicsprogramming commented on Dec 10, 2025

    @devshgraphicsprogramming
    Author

    Changed the title to reflect that it also affects DXIL
    https://godbolt.org/z/6fnGf8f6q

    It also doesn't fork on full specializations
    https://godbolt.org/z/8TzvPPE3Y

    It will give the same diagnostic even if you don't add cosnt (which you should) and misform the template
    https://godbolt.org/z/xredxYY3x

    And on a correctly formed full explicit specialization
    https://godbolt.org/z/3e3veWxsc

    funnily enough if you remove the const (to make it malformed) it gives diagnostics that make sense
    https://godbolt.org/z/6f3h4a99c

    <source>:15:37: error: redeclaration of 'wi' with a different type: 'float [2]' vs 'const float [2]'
    float GaussLegendreValues<2,float>::wi[2] = {1.0, 1.0};
                                        ^
    <source>:15:37: note: previous declaration is here
    

    All happens without -spirv as well.

  12. devshgraphicsprogramming commented on Dec 10, 2025

    @devshgraphicsprogramming
    Author

    The fun part is that as soon as we remove the templating, we get expected behaviour

    const without static in OOL initialization
    https://godbolt.org/z/WvdKs7KnM

    However its a bit disappointing that const static on the OOL is accepted
    https://godbolt.org/z/hW1z876cW

  13. added 2 commits that reference this issue on Jan 20, 2026
    ff8759c
    d4940f5
  14. added a commit that references this issue on Jan 26, 2026
    df50f51
  15. devshgraphicsprogramming commented on Feb 24, 2026

    @devshgraphicsprogramming
    Author

    Nathan Gauër (@Keenuts) I don't get why the issue is closed if all the repros I gave still fail ?

  16. Keenuts commented on Feb 24, 2026

    @Keenuts
    Collaborator

    They do? In this case we can re-open.

  17. devshgraphicsprogramming commented on Feb 24, 2026

    @devshgraphicsprogramming
    Author

    They do? In this case we can re-open.

    first thing is that DXC wants me to use static const at the OOL definition site, when it should be just const
    #5823 (comment)

    second is that I can't OOL define members of a templated struct, even with explicit instantiation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugspirv

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions