Skip to content

HLSL initializer list support #56067

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

Closed
Tracked by #21 ...
llvm-beanz opened this issue Jun 16, 2022 · 4 comments · Fixed by #123141
Closed
Tracked by #21 ...

HLSL initializer list support #56067

llvm-beanz opened this issue Jun 16, 2022 · 4 comments · Fixed by #123141
Assignees
Labels
HLSL HLSL Language Support

Comments

@llvm-beanz
Copy link
Collaborator

llvm-beanz commented Jun 16, 2022

HLSL initializer lists are... odd...

Basically HLSL flattens any structures or vector arguments to an InitListExpr, and flattens the shape of the target variable. As long as they then agree on how many elements need to be assigned, the assignment is valid.

The following are valid HLSL assignments:

struct A {
  float X;
  float Y;
};
struct B {
  A Arr[2];
  int Z;
};

A ah = {1.0, 2.0};
float2 F2 = {1.0, 2.0};
A aah = {F2};
B bah = { {1, 2}, {3, 4}, 5};
B bahh = {1,2,3,4,5};
B bahhh = {F2, 1,2,3};
B bahhhh = {{F2}, {F2}, 3};
B bahhhhh = {1,2,3,F2};

And more...

We want to deprecate this syntax in the future, however we believe that there is enough existing code that depends on this that we will need to have clang support it.

AC:

  • clang supports DXC-style flattened initializer lists
@llvm-beanz llvm-beanz added the HLSL HLSL Language Support label Jun 16, 2022
@damyanp
Copy link
Contributor

damyanp commented Jul 23, 2024

If we accept https://github.com/microsoft/hlsl-specs/blob/main/proposals/0005-strict-initializer-lists.md then we don't need to do this.

Note follow ups for that proposal:

  • clang emits fixits/warnings when it detects code relying on this, with the plan being that a future language version doesn't accept it
  • refactoring pattern added to allow clangd to update code

@damyanp
Copy link
Contributor

damyanp commented Jul 23, 2024

Set to medium priority since we're targeting building a collection of shaders that rely on this behavior, but we also have the option of updating these shaders.

@llvm-beanz
Copy link
Collaborator Author

Assigning this to myself to start working on the language for the spec.

llvm-beanz added a commit to llvm-beanz/llvm-project that referenced this issue Sep 10, 2024
HLSL's initialization lists are _extremely_ generous about allowing
conversions. This change demotes the C++11 warning to the legacy
warning when in HLSL mode.

Required for llvm#56067
llvm-beanz added a commit that referenced this issue Sep 12, 2024
HLSL's initialization lists are _extremely_ generous about allowing
conversions. This change demotes the C++11 warning to the legacy warning
when in HLSL mode.

Required for #56067
@damyanp damyanp moved this to Active in HLSL Support Oct 9, 2024
@damyanp damyanp moved this from Active to Reviewing in HLSL Support Oct 10, 2024
@damyanp damyanp moved this from Reviewing to Active in HLSL Support Oct 10, 2024
@spall
Copy link
Contributor

spall commented Nov 14, 2024

https://godbolt.org/z/bhYeMYsq4
Array Initialization that doesn't currently work.

llvm-beanz added a commit to llvm-beanz/llvm-project that referenced this issue Jan 15, 2025
This PR implements HLSL's initialization list behvaior as specified in
the draft language specifcation under
[*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html
#Decl.Init.Agg).

This behavior is a bit unusual for C/C++ because intermediate braces in
initializer lists are ignored and a whole array of additional
conversions occur unintuitively to how initializaiton works in C.

The implementaiton in this PR generates a valid C/C++ initialization
list AST for the HLSL initializer so that there are no changes required
to Clang's CodeGen to support this. This design will also allow us to
use Clang's rewrite to convert HLSL initializers to valid C/C++
initializers that are equivalent. It does have the downside that it
will generate often redundant accesses during codegen. The IR optimizer
is extremely good at eliminating those so this will have no impact on
the final executable performance.

There is some opportunity for optimizing the initializer list
generation that we could consider in subsequent commits. One notable
opportunity would be to identify aggregate objects that occur in the
same place in both initializers and do not require converison, those
aggregates could be initialized as aggregates rather than fully
scalarized.

Closes llvm#56067
llvm-beanz added a commit to llvm-beanz/llvm-project that referenced this issue Feb 15, 2025
This PR implements HLSL's initialization list behvaior as specified in
the draft language specifcation under
[*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html
#Decl.Init.Agg).

This behavior is a bit unusual for C/C++ because intermediate braces in
initializer lists are ignored and a whole array of additional
conversions occur unintuitively to how initializaiton works in C.

The implementaiton in this PR generates a valid C/C++ initialization
list AST for the HLSL initializer so that there are no changes required
to Clang's CodeGen to support this. This design will also allow us to
use Clang's rewrite to convert HLSL initializers to valid C/C++
initializers that are equivalent. It does have the downside that it
will generate often redundant accesses during codegen. The IR optimizer
is extremely good at eliminating those so this will have no impact on
the final executable performance.

There is some opportunity for optimizing the initializer list
generation that we could consider in subsequent commits. One notable
opportunity would be to identify aggregate objects that occur in the
same place in both initializers and do not require converison, those
aggregates could be initialized as aggregates rather than fully
scalarized.

Closes llvm#56067
llvm-beanz added a commit that referenced this issue Feb 15, 2025
This PR implements HLSL's initialization list behvaior as specified in
the draft language specifcation under

[*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Decl.Init.Agg).

This behavior is a bit unusual for C/C++ because intermediate braces in
initializer lists are ignored and a whole array of additional
conversions occur unintuitively to how initializaiton works in C.

The implementaiton in this PR generates a valid C/C++ initialization
list AST for the HLSL initializer so that there are no changes required
to Clang's CodeGen to support this. This design will also allow us to
use Clang's rewrite to convert HLSL initializers to valid C/C++
initializers that are equivalent. It does have the downside that it will
generate often redundant accesses during codegen. The IR optimizer is
extremely good at eliminating those so this will have no impact on the
final executable performance.

There is some opportunity for optimizing the initializer list generation
that we could consider in subsequent commits. One notable opportunity
would be to identify aggregate objects that occur in the same place in
both initializers and do not require converison, those aggregates could
be initialized as aggregates rather than fully scalarized.

Closes #56067

---------

Co-authored-by: Finn Plummer <[email protected]>
Co-authored-by: Helena Kotas <[email protected]>
Co-authored-by: Justin Bogner <[email protected]>
@github-project-automation github-project-automation bot moved this from Active to Closed in HLSL Support Feb 15, 2025
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this issue Feb 24, 2025
This PR implements HLSL's initialization list behvaior as specified in
the draft language specifcation under

[*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Decl.Init.Agg).

This behavior is a bit unusual for C/C++ because intermediate braces in
initializer lists are ignored and a whole array of additional
conversions occur unintuitively to how initializaiton works in C.

The implementaiton in this PR generates a valid C/C++ initialization
list AST for the HLSL initializer so that there are no changes required
to Clang's CodeGen to support this. This design will also allow us to
use Clang's rewrite to convert HLSL initializers to valid C/C++
initializers that are equivalent. It does have the downside that it will
generate often redundant accesses during codegen. The IR optimizer is
extremely good at eliminating those so this will have no impact on the
final executable performance.

There is some opportunity for optimizing the initializer list generation
that we could consider in subsequent commits. One notable opportunity
would be to identify aggregate objects that occur in the same place in
both initializers and do not require converison, those aggregates could
be initialized as aggregates rather than fully scalarized.

Closes llvm#56067

---------

Co-authored-by: Finn Plummer <[email protected]>
Co-authored-by: Helena Kotas <[email protected]>
Co-authored-by: Justin Bogner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HLSL HLSL Language Support
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants