Skip to content

[Matrix][HLSL] update IsSameFloatAfterCast to support Matrix type #168944

@farzonl

Description

@farzonl

The current code is:

static bool IsSameFloatAfterCast(const APValue &value,
                                 const llvm::fltSemantics &Src,
                                 const llvm::fltSemantics &Tgt) {
  if (value.isFloat())
    return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);

  if (value.isVector()) {
    for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
      if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
        return false;
    return true;
  }

  assert(value.isComplexFloat());
  return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
          IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
}

The problem here is if we don't update this function We can't properly warn about implicit conversion lose for this HLSL code:

void Half(half2x2 H);
void Case7(half2x2 H, float2x2 F, double2x2 D) {
  Half(F); // expected-warning{{implicit conversion loses floating-point precision: 'float2x2' (aka 'matrix<float, 2, 2>') to 'matrix<half, 2, 2>' (matrix of 4 'half' values)}}

  Half(D); // expected-warning{{implicit conversion loses floating-point precision: 'double2x2' (aka 'matrix<double, 2, 2>') to 'matrix<half, 2, 2>' (vector of 4 'half' values)}}
}

int Order = getASTContext().getFloatingTypeSemanticOrder(
QualType(SourceBT, 0), QualType(TargetBT, 0));
if (Order > 0) {
// Don't warn about float constants that are precisely
// representable in the target type.
Expr::EvalResult result;
if (E->EvaluateAsRValue(result, Context)) {
// Value might be a float, a float vector, or a float complex.
if (IsSameFloatAfterCast(
result.Val,
Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
return;
}
if (SourceMgr.isInSystemMacro(CC))
return;
DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision);
}

However we can't implement this ticket until we implment this one #168935

I think something like this or maybe a flattened access of the matrix APValue type:

static bool IsSameFloatAfterCast(const APValue &value,
                                 const llvm::fltSemantics &Src,
                                 const llvm::fltSemantics &Tgt) {
  if (value.isFloat())
    return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);

  if (value.isVector()) {
    for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
      if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
        return false;
    return true;
  }
+ if (value.isMatrix()) {
+     for (unsigned r = 0, e = value.getRowLength(); r != e; ++r)
+       for (unsigned c = 0, e = value.getColLength(); c != e; ++c)
+         if (!IsSameFloatAfterCast(value.getMatrixElt(r,c), Src, Tgt))
+           return false;
+     return true;
+   }
  assert(value.isComplexFloat());
  return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
          IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    Projects

    Status

    Planning

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions