Skip to content

[HLSL] Support HLSL matrix initializers #159434

@farzonl

Description

@farzonl

In HLSL matrixs are matrix_type in all respects except that they
support a constructor style syntax for initializing matrices. This
change should add a translation of matrix constructor arguments into
initializer lists.

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

Example of support needed:

https://godbolt.org/z/fGxEqxaef

//------------------------------------------------------------------------------
// 2) Initialization using vectors, scalars, or a single splatted scalar
//------------------------------------------------------------------------------
float2 a2 = float2(1, 2);

// “By scalars” (row-major textual layout, still column-major by default storage)
float2x2 A_scalar = float2x2(1, 2,
                             3, 4);

// “By vectors” (concatenated)
float2x2 A_vecs = float2x2(a2, float2(3, 4));

// “Single splatted scalar”
float2x2 A_splat = (float2x2)2.5;   // every element = 2.5

//Ignore this function this is just to keep the 
// matrices from getting optimized away
export float2x2 fn() {
    return A_scalar * A_vecs * A_splat;
        
}

Some Sema Error testing

// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -fsyntax-only -verify %s


typedef matrix<float, 2, 1> float2x1;
typedef matrix<float, 3, 1> float3x1;


struct S { float f; };
struct S2 { float f; int i; };

[numthreads(1,1,1)]
void entry() {
  float2x1 LilMat = float2x1(1.0, 2.0);
  float2x1 BrokenMat = float2x1(1.0, 2.0, 3.0); // expected-error{{excess elements in matrix initializer}}
  float3x1 NormieMat = float3x1(LilMat, 3.0, 4.0); // expected-error{{excess elements in matrix initializer}}
  float3x1 BrokenNormie = float3x1(3.0, 4.0); // expected-error{{too few elements in matrix initialization (expected 3 elements, have 2)}}
  float3x1 OverwhemledNormie = float3x1(3.0, 4.0, 5.0, 6.0); // expected-error{{excess elements in matrix initializer}}

  // These _should_ work in HLSL but aren't yet supported.
  S s;
  float2x1 GettingStrange = float2x1(s, s); // expected-error{{no viable conversion from 'S' to 'float'}} expected-error{{no viable conversion from 'S' to 'float'}}
}

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions