Skip to content

[HLSL][Matrix] Failure to initalize vector via cast of a matrix to vector #169561

@farzonl

Description

@farzonl

The code below is failing in clang/lib/Sema/SemaInit.cpp via a FK_ConversionFailed in clang when it works fine in DXC.

https://godbolt.org/z/r9G7fYzcj

export float4 fn(float2x2 m) {
    float4 v = m;
    return v;
}

Note the explicit cast version crashes today, but wont after: #168915

export float4 fn(float2x2 m) {
    float4 v = (float4)m;
    return v;
}
error: cannot initialize a variable of type 'float4' (aka 'vector<float, 4>') with an rvalue of type 'float2x2' (aka 'matrix<float, 2, 2>')
    2 |     float4 v = (float4)m;
      |            ^   ~~~~~~~~~~~

The bug is happening because we are not hitting this block in InitializationSequence::InitializeFrom

// For HLSL ext vector types we allow list initialization behavior for C++
  // functional cast expressions which look like constructor syntax. This is
  // accomplished by converting initialization arguments to InitListExpr.
  if (S.getLangOpts().HLSL && Args.size() > 1 &&
      (DestType->isExtVectorType() || DestType->isConstantMatrixType()) &&
      (SourceType.isNull() ||
       !Context.hasSameUnqualifiedType(SourceType, DestType))) {
    InitListExpr *ILE = new (Context)
        InitListExpr(S.getASTContext(), Args.front()->getBeginLoc(), Args,
                     Args.back()->getEndLoc());
    ILE->setType(DestType);
    Args[0] = ILE;
    TryListInitialization(S, Entity, Kind, ILE, *this,
                          TreatUnavailableAsInvalid);
    return;
  }

We can see if we did every thing would work fine:
https://godbolt.org/z/GMej3Kvvr

export float4 fn(float2x2 m) {
    float4 v = {m};
    return v;
}

Ie if we did vector initalization via matrix inside an initalizer list everthing would work just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    HLSLHLSL Language Support

    Type

    Projects

    Status

    Planning

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions