================
@@ -2329,15 +2329,39 @@ LValue CodeGenFunction::EmitMatrixElementExpr(const 
MatrixElementExpr *E) {
       E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
 
   // Encode the element access list into a vector of unsigned indices.
+  // getEncodedElementAccess returns row-major linearized indices.
   SmallVector<uint32_t, 4> Indices;
   E->getEncodedElementAccess(Indices);
 
+  // getEncodedElementAccess returns row-major linearized indices
+  // If the matrix memory layout is column-major, convert indices
+  // to column-major indices.
+  bool IsColMajor = getLangOpts().getDefaultMatrixMemoryLayout() ==
+                    LangOptions::MatrixMemoryLayout::MatrixColMajor;
+  if (IsColMajor) {
+    const auto *MT = E->getBase()->getType()->castAs<ConstantMatrixType>();
+    unsigned NumCols = MT->getNumColumns();
+    for (uint32_t &Idx : Indices) {
+      // Decompose row-major index: Row = Idx / NumCols, Col = Idx % NumCols
+      unsigned Row = Idx / NumCols;
+      unsigned Col = Idx % NumCols;
+      // Re-linearize as column-major
+      Idx = MT->getColumnMajorFlattenedIndex(Row, Col);
+    }
+  }
+
----------------
inbelic wrote:

nit:
```suggestion
  } else {
    assert(getLangOpts().getDefaultMatrixMemoryLayout() ==
              LangOptions::MatrixMemoryLayout::MatrixRowMajor);
  }
```

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

Reply via email to