llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-modules

Author: Farzon Lotfi (farzonl)

<details>
<summary>Changes</summary>

fixes #<!-- -->213996
fixes #<!-- -->211977
fixes https://godbolt.org/z/rhTYx1KGf

Keep matrix layout metadata on noncanonical matrix types without affecting type 
identity, overload resolution, deduction, or mangling.

Normalize matrix values to column-major register representation when loading 
from memory, and convert them back to the destination layout when storing. 
Preserve layout metadata through typedefs, arrays, records, resources, 
serialization, AST import, and template substitution.

---

Patch is 177.15 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/225519.diff


32 Files Affected:

- (modified) clang/include/clang/AST/ASTContext.h (+6-2) 
- (modified) clang/include/clang/AST/MatrixUtils.h (+5-20) 
- (modified) clang/include/clang/AST/PropertiesBase.td (+7) 
- (modified) clang/include/clang/AST/TypeBase.h (+15-4) 
- (modified) clang/include/clang/AST/TypeProperties.td (+4-1) 
- (modified) clang/include/clang/Sema/SemaHLSL.h (-1) 
- (modified) clang/lib/AST/ASTContext.cpp (+34-7) 
- (modified) clang/lib/AST/ASTImporter.cpp (+1-1) 
- (modified) clang/lib/AST/Type.cpp (+8-6) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+16-1) 
- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+7-18) 
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+1-35) 
- (modified) clang/lib/CodeGen/CodeGenTypes.cpp (+5) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+4) 
- (modified) clang/lib/Sema/SemaHLSL.cpp (-31) 
- (modified) clang/lib/Sema/SemaStmt.cpp (-5) 
- (modified) clang/lib/Sema/SemaType.cpp (+9-2) 
- (modified) clang/lib/Sema/TreeTransform.h (+9) 
- (modified) clang/test/AST/HLSL/matrix_layout_attr.hlsl (+21) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/MatrixElementTypeCast.hlsl 
(+329-150) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/MatrixExplicitTruncation.hlsl 
(+251-110) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/MatrixImplicitTruncation.hlsl 
(+222-96) 
- (modified) 
clang/test/CodeGenHLSL/BasicFeatures/MatrixInitializerListOrder.hlsl (+13-21) 
- (modified) 
clang/test/CodeGenHLSL/BasicFeatures/MatrixToAndFromVectorConstructors.hlsl 
(+33-15) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/VectorElementwiseCast.hlsl 
(+21-6) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/matrix-type-indexing.hlsl 
(+8-10) 
- (modified) clang/test/CodeGenHLSL/builtins/mul.hlsl (+27-23) 
- (modified) clang/test/CodeGenHLSL/builtins/transpose.hlsl (+9-4) 
- (modified) clang/test/CodeGenHLSL/matrix-layout-attr-overrides-default.hlsl 
(+34-48) 
- (added) clang/test/CodeGenHLSL/matrix-layout-register-representation.hlsl 
(+37) 
- (modified) clang/test/CodeGenHLSL/resources/MatrixElement_cbuffer.hlsl (+2-1) 
- (modified) clang/test/SemaHLSL/matrix_layout_attr.hlsl (+12) 


``````````diff
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index b2d407e412b3d9..d5ec38c5f2bd24 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1906,8 +1906,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
   ///
   /// \pre \p ElementType must be a valid matrix element type (see
   /// MatrixType::isValidElementType).
-  QualType getConstantMatrixType(QualType ElementType, unsigned NumRows,
-                                 unsigned NumColumns) const;
+  QualType getConstantMatrixType(
+      QualType ElementType, unsigned NumRows, unsigned NumColumns,
+      std::optional<MatrixType::LayoutKind> Layout = std::nullopt) const;
 
   /// Return the unique reference to the matrix type of the specified element
   /// type and size
@@ -1915,6 +1916,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
                                        Expr *ColumnExpr,
                                        SourceLocation AttrLoc) const;
 
+  QualType getMatrixTypeWithLayout(QualType T,
+                                   MatrixType::LayoutKind Layout) const;
+
   QualType getDependentAddressSpaceType(QualType PointeeType,
                                         Expr *AddrSpaceExpr,
                                         SourceLocation AttrLoc) const;
diff --git a/clang/include/clang/AST/MatrixUtils.h 
b/clang/include/clang/AST/MatrixUtils.h
index ef6cbba6ba7c07..81aed613074a21 100644
--- a/clang/include/clang/AST/MatrixUtils.h
+++ b/clang/include/clang/AST/MatrixUtils.h
@@ -15,32 +15,17 @@
 #define LLVM_CLANG_AST_MATRIXUTILS_H
 
 #include "clang/AST/Type.h"
-#include "clang/Basic/AttrKinds.h"
 #include "clang/Basic/LangOptions.h"
 
 namespace clang {
 /// Returns true if matrices of \p T should be laid out in row-major order.
 ///
-/// In HLSL mode, an `HLSLRowMajor` / `HLSLColumnMajor` AttributedType anywhere
-/// in the sugar chain of \p T (imprinted by Sema when a source decl carries
-/// `[[hlsl::row_major]]` / `[[hlsl::column_major]]`) takes precedence over the
-/// `-fmatrix-memory-layout=` default carried in \p LangOpts. Otherwise the
-/// LangOptions default is used.
+/// An explicit layout stored on the matrix type takes precedence over the
+/// `-fmatrix-memory-layout=` default carried in \p LangOpts.
 inline bool isMatrixRowMajor(const LangOptions &LangOpts, QualType T) {
-  if (LangOpts.HLSL && !T.isNull()) {
-    QualType Cur = T;
-    while (const auto *AT = Cur->getAs<AttributedType>()) {
-      switch (AT->getAttrKind()) {
-      case attr::HLSLRowMajor:
-        return true;
-      case attr::HLSLColumnMajor:
-        return false;
-      default:
-        break;
-      }
-      Cur = AT->getModifiedType();
-    }
-  }
+  if (const auto *MT = T.isNull() ? nullptr : T->getAs<ConstantMatrixType>();
+      LangOpts.HLSL && MT && MT->getLayout())
+    return MT->getLayout() == MatrixType::LayoutKind::RowMajor;
   return LangOpts.getDefaultMatrixMemoryLayout() ==
          LangOptions::MatrixMemoryLayout::MatrixRowMajor;
 }
diff --git a/clang/include/clang/AST/PropertiesBase.td 
b/clang/include/clang/AST/PropertiesBase.td
index 347f58d45ffa86..0e76302dcc13fd 100644
--- a/clang/include/clang/AST/PropertiesBase.td
+++ b/clang/include/clang/AST/PropertiesBase.td
@@ -130,6 +130,13 @@ def LValuePathSerializationHelper :
     PropertyType<"APValue::LValuePathSerializationHelper"> {
   let BufferElementTypes = [ LValuePathEntry ];
 }
+def MatrixLayoutKind : EnumPropertyType<"MatrixType::LayoutKind"> {
+  let PackOptional =
+    "value.value_or(static_cast<MatrixType::LayoutKind>(2))";
+  let UnpackOptional =
+    "value == static_cast<MatrixType::LayoutKind>(2) ? std::nullopt : "
+    "std::optional<MatrixType::LayoutKind>(value)";
+}
 def NestedNameSpecifier : PropertyType<"NestedNameSpecifier">;
 def NestedNameSpecifierKind : EnumPropertyType<"NestedNameSpecifier::Kind">;
 def OverloadedOperatorKind : EnumPropertyType;
diff --git a/clang/include/clang/AST/TypeBase.h 
b/clang/include/clang/AST/TypeBase.h
index 28f102fdaf534c..cc8cb69493825d 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4429,9 +4429,14 @@ class MatrixType : public Type, public 
llvm::FoldingSetNode {
 protected:
   friend class ASTContext;
 
+public:
+  enum class LayoutKind : uint8_t { RowMajor, ColumnMajor };
+
+private:
   /// The element type of the matrix.
   QualType ElementType;
 
+protected:
   MatrixType(QualType ElementTy, QualType CanonElementTy);
 
   MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy,
@@ -4482,12 +4487,15 @@ class ConstantMatrixType final : public MatrixType {
   /// Number of rows and columns.
   unsigned NumRows;
   unsigned NumColumns;
+  std::optional<LayoutKind> Layout;
 
   ConstantMatrixType(QualType MatrixElementType, unsigned NRows,
-                     unsigned NColumns, QualType CanonElementType);
+                     unsigned NColumns, QualType CanonElementType,
+                     std::optional<LayoutKind> Layout);
 
   ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows,
-                     unsigned NColumns, QualType CanonElementType);
+                     unsigned NColumns, QualType CanonElementType,
+                     std::optional<LayoutKind> Layout);
 
 public:
   /// Returns the number of rows in the matrix.
@@ -4496,6 +4504,8 @@ class ConstantMatrixType final : public MatrixType {
   /// Returns the number of columns in the matrix.
   unsigned getNumColumns() const { return NumColumns; }
 
+  std::optional<LayoutKind> getLayout() const { return Layout; }
+
   /// Returns the number of elements required to embed the matrix into a 
vector.
   unsigned getNumElementsFlattened() const {
     return getNumRows() * getNumColumns();
@@ -4541,16 +4551,17 @@ class ConstantMatrixType final : public MatrixType {
   }
 
   void Profile(llvm::FoldingSetNodeID &ID) {
-    Profile(ID, getElementType(), getNumRows(), getNumColumns(),
+    Profile(ID, getElementType(), getNumRows(), getNumColumns(), getLayout(),
             getTypeClass());
   }
 
   static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
                       unsigned NumRows, unsigned NumColumns,
-                      TypeClass TypeClass) {
+                      std::optional<LayoutKind> Layout, TypeClass TypeClass) {
     ID.AddPointer(ElementType.getAsOpaquePtr());
     ID.AddInteger(NumRows);
     ID.AddInteger(NumColumns);
+    ID.AddInteger(Layout ? llvm::to_underlying(*Layout) + 1 : 0);
     ID.AddInteger(TypeClass);
   }
 
diff --git a/clang/include/clang/AST/TypeProperties.td 
b/clang/include/clang/AST/TypeProperties.td
index dc2a45ec857296..9174382485faff 100644
--- a/clang/include/clang/AST/TypeProperties.td
+++ b/clang/include/clang/AST/TypeProperties.td
@@ -254,9 +254,12 @@ let Class = ConstantMatrixType in {
   def : Property<"numColumns", UInt32> {
     let Read = [{ node->getNumColumns() }];
   }
+  def : Property<"layout", Optional<MatrixLayoutKind>> {
+    let Read = [{ node->getLayout() }];
+  }
 
   def : Creator<[{
-    return ctx.getConstantMatrixType(elementType, numRows, numColumns);
+    return ctx.getConstantMatrixType(elementType, numRows, numColumns, layout);
   }]>;
 }
 
diff --git a/clang/include/clang/Sema/SemaHLSL.h 
b/clang/include/clang/Sema/SemaHLSL.h
index 6c0e5b52f7cb3b..b2c73b67eaa26c 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -196,7 +196,6 @@ class SemaHLSL : public SemaBase {
                                          SourceLocation Loc);
   // Re-type a layout-adapting matrix builtin call \p E with \p DestType's
   // row_major/column_major sugar so CodeGen lowers it into that layout.
-  void propagateContextualMatrixLayout(Expr *E, QualType DestType);
   bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
 
   template <typename T>
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index e74423ca8c8a1b..15252a9eac5165 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -4870,10 +4870,11 @@ ASTContext::getDependentSizedExtVectorType(QualType 
vecType,
   return QualType(New, 0);
 }
 
-QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned 
NumRows,
-                                           unsigned NumColumns) const {
+QualType ASTContext::getConstantMatrixType(
+    QualType ElementTy, unsigned NumRows, unsigned NumColumns,
+    std::optional<MatrixType::LayoutKind> Layout) const {
   llvm::FoldingSetNodeID ID;
-  ConstantMatrixType::Profile(ID, ElementTy, NumRows, NumColumns,
+  ConstantMatrixType::Profile(ID, ElementTy, NumRows, NumColumns, Layout,
                               Type::ConstantMatrix);
 
   assert(MatrixType::isValidElementType(ElementTy, getLangOpts()) &&
@@ -4886,9 +4887,9 @@ QualType ASTContext::getConstantMatrixType(QualType 
ElementTy, unsigned NumRows,
     return QualType(MTP, 0);
 
   QualType Canonical;
-  if (!ElementTy.isCanonical()) {
-    Canonical =
-        getConstantMatrixType(getCanonicalType(ElementTy), NumRows, 
NumColumns);
+  if (Layout || !ElementTy.isCanonical()) {
+    Canonical = getConstantMatrixType(getCanonicalType(ElementTy), NumRows,
+                                      NumColumns, std::nullopt);
 
     ConstantMatrixType *NewIP = MatrixTypes.lookup(ID, Token);
     assert(!NewIP && "Matrix type shouldn't already exist in the map");
@@ -4896,7 +4897,7 @@ QualType ASTContext::getConstantMatrixType(QualType 
ElementTy, unsigned NumRows,
   }
 
   auto *New = new (*this, alignof(ConstantMatrixType))
-      ConstantMatrixType(ElementTy, NumRows, NumColumns, Canonical);
+      ConstantMatrixType(ElementTy, NumRows, NumColumns, Canonical, Layout);
   MatrixTypes.insert(New, Token);
   Types.push_back(New);
   return QualType(New, 0);
@@ -4942,6 +4943,32 @@ QualType 
ASTContext::getDependentSizedMatrixType(QualType ElementTy,
   return QualType(New, 0);
 }
 
+QualType
+ASTContext::getMatrixTypeWithLayout(QualType T,
+                                    MatrixType::LayoutKind Layout) const {
+  Qualifiers Quals = T.getQualifiers();
+  const Type *Ty = T->getUnqualifiedDesugaredType();
+
+  if (const auto *MT = dyn_cast<ConstantMatrixType>(Ty))
+    return getQualifiedType(getConstantMatrixType(MT->getElementType(),
+                                                  MT->getNumRows(),
+                                                  MT->getNumColumns(), Layout),
+                            Quals);
+
+  const auto *CAT = dyn_cast<ConstantArrayType>(Ty);
+  if (!CAT)
+    return T;
+
+  QualType Result = getConstantArrayType(
+      getMatrixTypeWithLayout(CAT->getElementType(), Layout), CAT->getSize(),
+      CAT->getSizeExpr(), CAT->getSizeModifier(),
+      CAT->getIndexTypeCVRQualifiers());
+  if (isa<ArrayParameterType>(CAT))
+    Result = getArrayParameterType(Result);
+
+  return getQualifiedType(Result, Quals);
+}
+
 QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType,
                                                   Expr *AddrSpaceExpr,
                                                   SourceLocation AttrLoc) 
const {
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index bec73d820d0099..89b34aba3ec4ac 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2108,7 +2108,7 @@ ExpectedType 
clang::ASTNodeImporter::VisitConstantMatrixType(
     return ToElementTypeOrErr.takeError();
 
   return Importer.getToContext().getConstantMatrixType(
-      *ToElementTypeOrErr, T->getNumRows(), T->getNumColumns());
+      *ToElementTypeOrErr, T->getNumRows(), T->getNumColumns(), 
T->getLayout());
 }
 
 ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType(
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 5fdac154725c3c..f855f005f03221 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -496,15 +496,17 @@ MatrixType::MatrixType(TypeClass tc, QualType matrixType, 
QualType canonType,
       ElementType(matrixType) {}
 
 ConstantMatrixType::ConstantMatrixType(QualType matrixType, unsigned nRows,
-                                       unsigned nColumns, QualType canonType)
-    : ConstantMatrixType(ConstantMatrix, matrixType, nRows, nColumns,
-                         canonType) {}
+                                       unsigned nColumns, QualType canonType,
+                                       std::optional<LayoutKind> Layout)
+    : ConstantMatrixType(ConstantMatrix, matrixType, nRows, nColumns, 
canonType,
+                         Layout) {}
 
 ConstantMatrixType::ConstantMatrixType(TypeClass tc, QualType matrixType,
                                        unsigned nRows, unsigned nColumns,
-                                       QualType canonType)
+                                       QualType canonType,
+                                       std::optional<LayoutKind> Layout)
     : MatrixType(tc, matrixType, canonType), NumRows(nRows),
-      NumColumns(nColumns) {}
+      NumColumns(nColumns), Layout(Layout) {}
 
 DependentSizedMatrixType::DependentSizedMatrixType(QualType ElementType,
                                                    QualType CanonicalType,
@@ -1279,7 +1281,7 @@ struct SimpleTransformVisitor : public 
TypeVisitor<Derived, QualType> {
       return QualType(T, 0);
 
     return Ctx.getConstantMatrixType(elementType, T->getNumRows(),
-                                     T->getNumColumns());
+                                     T->getNumColumns(), T->getLayout());
   }
 
   QualType VisitOverflowBehaviorType(const OverflowBehaviorType *T) {
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index aca0415d7f5449..91c49639e40823 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -2425,6 +2425,13 @@ LValue CodeGenFunction::EmitMatrixElementExpr(const 
MatrixElementExpr *E) {
 // (VectorType).
 static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue,
                                     bool isInit, CodeGenFunction &CGF) {
+  if (CGF.getLangOpts().HLSL &&
+      isMatrixRowMajor(CGF.getLangOpts(), lvalue.getType())) {
+    const auto *MatrixTy = lvalue.getType()->castAs<ConstantMatrixType>();
+    llvm::MatrixBuilder MB(CGF.Builder);
+    value = MB.CreateColumnMajorToRowMajorTransform(
+        value, MatrixTy->getNumRows(), MatrixTy->getNumColumns());
+  }
   Address Addr = MaybeConvertMatrixAddress(lvalue.getAddress(), CGF,
                                            value->getType()->isVectorTy());
   CGF.EmitStoreOfScalar(value, Addr, lvalue.isVolatile(), lvalue.getType(),
@@ -2515,7 +2522,15 @@ static RValue EmitLoadOfMatrixLValue(LValue LV, 
SourceLocation Loc,
 
   Address Addr = MaybeConvertMatrixAddress(DestAddr, CGF);
   LV.setAddress(Addr);
-  return RValue::get(CGF.EmitLoadOfScalar(LV, Loc));
+  llvm::Value *Value = CGF.EmitLoadOfScalar(LV, Loc);
+  if (CGF.getLangOpts().HLSL &&
+      isMatrixRowMajor(CGF.getLangOpts(), LV.getType())) {
+    const auto *MatrixTy = LV.getType()->castAs<ConstantMatrixType>();
+    llvm::MatrixBuilder MB(CGF.Builder);
+    Value = MB.CreateRowMajorToColumnMajorTransform(
+        Value, MatrixTy->getNumRows(), MatrixTy->getNumColumns());
+  }
+  return RValue::get(Value);
 }
 
 RValue CodeGenFunction::EmitLoadOfAnyValue(LValue LV, AggValueSlot Slot,
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 1410433237c49e..0c310816bc268e 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2229,13 +2229,10 @@ Value 
*ScalarExprEmitter::VisitMatrixSingleSubscriptExpr(
   auto *ResultTy = llvm::FixedVectorType::get(ElemTy, NumColumns);
   Value *RowVec = llvm::PoisonValue::get(ResultTy);
 
-  bool IsMatrixRowMajor =
-      isMatrixRowMajor(CGF.getLangOpts(), E->getBase()->getType());
-
   for (unsigned Col = 0; Col != NumColumns; ++Col) {
     Value *ColVal = llvm::ConstantInt::get(RowIdx->getType(), Col);
     Value *EltIdx = MB.CreateIndex(RowIdx, ColVal, NumRows, NumColumns,
-                                   IsMatrixRowMajor, "matrix_row_idx");
+                                   /*IsRowMajor=*/false, "matrix_row_idx");
     Value *Elt =
         Builder.CreateExtractElement(FlatMatrix, EltIdx, "matrix_elem");
     Value *Lane = llvm::ConstantInt::get(Builder.getInt32Ty(), Col);
@@ -2259,9 +2256,8 @@ Value 
*ScalarExprEmitter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E) {
   Value *Idx;
   unsigned NumCols = MatrixTy->getNumColumns();
   unsigned NumRows = MatrixTy->getNumRows();
-  bool IsMatrixRowMajor =
-      isMatrixRowMajor(CGF.getLangOpts(), E->getBase()->getType());
-  Idx = MB.CreateIndex(RowIdx, ColumnIdx, NumRows, NumCols, IsMatrixRowMajor);
+  Idx = MB.CreateIndex(RowIdx, ColumnIdx, NumRows, NumCols,
+                       /*IsRowMajor=*/false);
 
   if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0)
     MB.CreateIndexAssumption(Idx, MatrixTy->getNumElementsFlattened());
@@ -2343,10 +2339,8 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr 
*E) {
 
   // For column-major matrix types, we insert elements directly at their
   // column-major positions rather than inserting sequentially and shuffling.
-  const ConstantMatrixType *ColMajorMT = nullptr;
-  if (const auto *MT = E->getType()->getAs<ConstantMatrixType>();
-      MT && !isMatrixRowMajor(CGF.getLangOpts(), E->getType()))
-    ColMajorMT = MT;
+  const ConstantMatrixType *ColMajorMT =
+      E->getType()->getAs<ConstantMatrixType>();
 
   // Loop over initializers collecting the Value for each, and remembering
   // whether the source was swizzle (ExtVectorElementExpr).  This will allow
@@ -3182,15 +3176,10 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
       assert(NumRows <= SrcMatTy->getNumRows());
       assert(NumCols <= SrcMatTy->getNumColumns());
 
-      // isMatrix[Src|Dst]RowMajor needs the full sugared QualType to find
-      // matrix layout attrs. So use E->getType() &  DestTy rather than 
SrcMatTy
-      // & MatTy b/c getAs<ConstantMatrixType>() strips the sugar.
-      bool IsSrcRowMajor = isMatrixRowMajor(CGF.getLangOpts(), E->getType());
-      bool IsDstRowMajor = isMatrixRowMajor(CGF.getLangOpts(), DestTy);
       for (unsigned R = 0; R < NumRows; R++)
         for (unsigned C = 0; C < NumCols; C++)
-          Mask[MatTy->getFlattenedIndex(R, C, IsDstRowMajor)] =
-              SrcMatTy->getFlattenedIndex(R, C, IsSrcRowMajor);
+          Mask[MatTy->getColumnMajorFlattenedIndex(R, C)] =
+              SrcMatTy->getColumnMajorFlattenedIndex(R, C);
 
       return Builder.CreateShuffleVector(Mat, Mask, "trunc");
     }
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp 
b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index bb0fe135ff8b08..ea9ce8ad0d19c7 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -1259,13 +1259,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
     bool IsMat0 = QTy0->isConstantMatrixType();
     bool IsMat1 = QTy1->isConstantMatrixType();
 
-    // The matrix multiply intrinsic only operates on column-major order
-    // matrices. Therefore matrix memory layout transforms must be inserted
-    // before and after matrix multiply intrinsics.
-    // Use whichever operand is a matrix to discover its declared layout.
-    bool IsRowMajorMat0 = IsMat0 && isMatrixRowMajor(getLangOpts(), QTy0);
-    bool IsRowMajorMat1 = IsMat1 && isMatrixRowMajor(getLangOpts(), QTy1);
-
     llvm::MatrixBuilder MB(Builder);
     if (IsVec0 && IsMat1) {
       unsigned N = QTy0->castAs<VectorType>()->getNumElements();
@@ -1273,8 +1266,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
       unsigned Rows = MatTy->getNumRows();
       unsigned Cols = MatTy->getNumColumns();
       assert(N == Rows && "vector length must match matrix row count");
-      if (IsRowMajorMat1)
-        Op1 = MB.CreateRowMajorToColumnMajorTransform(Op1, Rows, Cols);
       return MB.CreateMatrixMultiply(Op0, Op1, 1, N, Cols, "hlsl.mul");
     }
     if (IsMat0 && IsVec1) {
@@ -1283,8 +1274,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
       unsigned Cols = MatTy->getNumColumns();
       assert(QTy1->castAs<VectorType>()->getNumElements() == Cols &&
              "vector length must match matrix column count"...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/225519
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to