This is an archive of the discontinued LLVM Phabricator instance.

[HLSL] Support HLSL vector initializers
ClosedPublic

Authored by beanz on Jun 14 2022, 2:50 PM.

Details

Summary

In HLSL vectors are ext_vectors in all respects except that they
support a constructor style syntax for initializing vectors. This
change adds a translation of vector constructor arguments into
initializer lists.

This supports two oddities of HLSL syntax:
(1) HLSL vectors support constructor syntax
(2) HLSL vectors are expanded to constituate components in constructors

Diff Detail

Event Timeline

beanz created this revision.Jun 14 2022, 2:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2022, 2:50 PM
beanz requested review of this revision.Jun 14 2022, 2:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2022, 2:50 PM
python3kgae added inline comments.Jun 14 2022, 3:04 PM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
9

Could we have test case for array/struct when initialize vector?
Like
float a[2] = {1.2, 3.2};
float4 arrayVec = float4(a, 1.0, 2.0);

beanz added inline comments.Jun 15 2022, 6:53 AM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
9

This change does not support all of HLSL's odd initialization features. It only supports initializing vectors from scalars and vectors.

aaron.ballman added inline comments.Jun 15 2022, 8:13 AM
clang/lib/Sema/SemaInit.cpp
5984

Huh, interesting -- I didn't realize we had AST nodes that didn't use ::Create(). :-D

clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
9

I'm surprised to see float(1.0, 2.0) work -- that's not list initialization, it's using round parens instead of curly braces so I'm pretty sure that's a function-style cast/constructor. Is that intentional?

beanz added inline comments.Jun 15 2022, 8:20 AM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
9

Yea, it's a bit gross. HLSL supports function cast for vector construction, by converting the parameters inside to an initialization list.

HLSL also has some super odd initialization list behaviors that I'm trying to figure out a reasonable way to handle (while also hoping to remove them from the language).

aaron.ballman added inline comments.Jun 15 2022, 8:55 AM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
9

Ahhhh okay, I was confused -- I thought the goal was to allow direct init list initialization. Thanks for clarifying.

13

How do these examples behave:

float f = 1.0f, g = 2.0f;
float2 foo0 = float2(f, g); // Non-literal

int i = 1, j = 2;
float2 foo1 = float2(1, 2); // Integer literals
float2 foo2 = float2(i, j); // Integer non-literal

struct S { float f; } s;
float2 foo3 = float2(s, s); // No potential type conversion possible
float2 foo4 = float2(s.f, s.f); // Should work though?

// and for good measure
struct T {
  operator float() const;
} t;
float2 foo5 = float2(t, t); // Should work though?

typedef float2 second_level_of_typedefs;
second_level_of_typedefs foo6 = float2(1.0f, 2.0f); // Should work though?
float2 foo7 = second_level_of_typedefs(1.0f, 2.0f); // Should work though?
clang/test/SemaHLSL/BuiltIns/vector-constructors.hlsl
1 ↗(On Diff #436953)

I don't feel strongly, but -ast-dump tests often go under clang/test/AST because they're not really testing semantics so much as what information we've dumped out to represent those semantics.

beanz added inline comments.Jun 15 2022, 8:58 AM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
13

I'll extend the test coverage to catch these cases.

struct S { float f; } s;
float2 foo3 = float2(s, s); // No potential type conversion possible

If you really want to regret reading this review... this actually is valid in HLSL. It isn't supported by this patch, but this is one of those odd behaviors I want to change in the language.

aaron.ballman added inline comments.Jun 15 2022, 9:04 AM
clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl
13

struct S { float f; } s;
float2 foo3 = float2(s, s); // No potential type conversion possible

If you really want to regret reading this review... this actually is valid in HLSL. It isn't supported by this patch, but this is one of those odd behaviors I want to change in the language.

Macro thisisfine: This is fine

beanz updated this revision to Diff 437670.Jun 16 2022, 12:30 PM

Updates based on feedback from @aaron.ballman.

Thank you for all the test suggestions, it caught an extra-odd AST generation. Fix included :).

This revision is now accepted and ready to land.Jun 21 2022, 9:13 AM
This revision was landed with ongoing or failed builds.Jun 21 2022, 10:34 AM
This revision was automatically updated to reflect the committed changes.