Skip to content

release/20.x: [Clang][MicrosoftMangle] Implement mangling for ConstantMatrixType (#134930) #138017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/20.x
Choose a base branch
from

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented Apr 30, 2025

Backport f5a30f1

Requested by: @tstellar

…lvm#134930)

This pull request implements mangling for ConstantMatrixType, allowing
matrices to be used on Windows.

Related issues: llvm#53158, llvm#127127

This example code:
```cpp
#include <typeinfo>
#include <stdio.h>

typedef float Matrix4 __attribute__((matrix_type(4, 4)));

int main()
{
  printf("%s\n", typeid(Matrix4).name());
}
```
Outputs this:
```
struct __clang::__matrix<float,4,4>
```

(cherry picked from commit f5a30f1)
@llvmbot
Copy link
Member Author

llvmbot commented Apr 30, 2025

@rnk What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from rnk April 30, 2025 19:44
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 30, 2025
@llvmbot
Copy link
Member Author

llvmbot commented Apr 30, 2025

@llvm/pr-subscribers-clang

Author: None (llvmbot)

Changes

Backport f5a30f1

Requested by: @tstellar


Full diff: https://github.com/llvm/llvm-project/pull/138017.diff

2 Files Affected:

  • (modified) clang/lib/AST/MicrosoftMangle.cpp (+16-1)
  • (added) clang/test/CodeGenCXX/mangle-ms-matrix.cpp (+57)
diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp
index 42b735ccf4a2c..74c995f2f97f0 100644
--- a/clang/lib/AST/MicrosoftMangle.cpp
+++ b/clang/lib/AST/MicrosoftMangle.cpp
@@ -3552,7 +3552,22 @@ void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
 
 void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
                                          Qualifiers quals, SourceRange Range) {
-  Error(Range.getBegin(), "matrix type") << Range;
+  QualType EltTy = T->getElementType();
+  const BuiltinType *ET = EltTy->getAs<BuiltinType>();
+
+  llvm::SmallString<64> TemplateMangling;
+  llvm::raw_svector_ostream Stream(TemplateMangling);
+  MicrosoftCXXNameMangler Extra(Context, Stream);
+
+  Stream << "?$";
+
+  Extra.mangleSourceName("__matrix");
+  Extra.mangleType(EltTy, Range, QMM_Escape);
+
+  Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumRows()));
+  Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumColumns()));
+
+  mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
 }
 
 void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T,
diff --git a/clang/test/CodeGenCXX/mangle-ms-matrix.cpp b/clang/test/CodeGenCXX/mangle-ms-matrix.cpp
new file mode 100644
index 0000000000000..b244aa6e33cfa
--- /dev/null
+++ b/clang/test/CodeGenCXX/mangle-ms-matrix.cpp
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -fenable-matrix -fms-extensions -fcxx-exceptions -ffreestanding -target-feature +avx -emit-llvm %s -o - -triple=i686-pc-win32 | FileCheck %s
+// RUN: %clang_cc1 -fenable-matrix -fms-extensions -fcxx-exceptions -ffreestanding -target-feature +avx -emit-llvm %s -o - -triple=i686-pc-win32 -fexperimental-new-constant-interpreter | FileCheck %s
+
+typedef float __attribute__((matrix_type(4, 4))) m4x4f;
+typedef float __attribute__((matrix_type(2, 2))) m2x2f;
+
+typedef int __attribute__((matrix_type(4, 4))) m4x4i;
+typedef int __attribute__((matrix_type(2, 2))) m2x2i;
+
+void thow(int i) {
+  switch (i) {
+    case 0: throw m4x4f();
+    // CHECK: ??_R0U?$__matrix@M$03$03@__clang@@@8
+    // CHECK: _CT??_R0U?$__matrix@M$03$03@__clang@@@864
+    // CHECK: _CTA1U?$__matrix@M$03$03@__clang@@
+    // CHECK: _TI1U?$__matrix@M$03$03@__clang@@
+    case 1: throw m2x2f();
+    // CHECK: ??_R0U?$__matrix@M$01$01@__clang@@@8
+    // CHECK: _CT??_R0U?$__matrix@M$01$01@__clang@@@816
+    // CHECK: _CTA1U?$__matrix@M$01$01@__clang@@
+    // CHECK: _TI1U?$__matrix@M$01$01@__clang@@
+    case 2: throw m4x4i();
+    // CHECK: ??_R0U?$__matrix@H$03$03@__clang@@@8
+    // CHECK: _CT??_R0U?$__matrix@H$03$03@__clang@@@864
+    // CHECK: _CTA1U?$__matrix@H$03$03@__clang@@
+    // CHECK: _TI1U?$__matrix@H$03$03@__clang@@
+    case 3: throw m2x2i();
+    // CHECK: ??_R0U?$__matrix@H$01$01@__clang@@@8
+    // CHECK: _CT??_R0U?$__matrix@H$01$01@__clang@@@816
+    // CHECK: _CTA1U?$__matrix@H$01$01@__clang@@
+    // CHECK: _TI1U?$__matrix@H$01$01@__clang@@
+  }
+}
+
+void foo44f(m4x4f) {}
+// CHECK: define dso_local void @"?foo44f@@YAXU?$__matrix@M$03$03@__clang@@@Z"
+
+m4x4f rfoo44f() { return m4x4f(); }
+// CHECK: define dso_local noundef <16 x float> @"?rfoo44f@@YAU?$__matrix@M$03$03@__clang@@XZ"
+
+void foo22f(m2x2f) {}
+// CHECK: define dso_local void @"?foo22f@@YAXU?$__matrix@M$01$01@__clang@@@Z"
+
+m2x2f rfoo22f() { return m2x2f(); }
+// CHECK: define dso_local noundef <4 x float> @"?rfoo22f@@YAU?$__matrix@M$01$01@__clang@@XZ"
+
+void foo44i(m4x4i) {}
+// CHECK: define dso_local void @"?foo44i@@YAXU?$__matrix@H$03$03@__clang@@@Z"
+
+m4x4i rfoo44i() { return m4x4i(); }
+// CHECK: define dso_local noundef <16 x i32> @"?rfoo44i@@YAU?$__matrix@H$03$03@__clang@@XZ"
+
+void foo22i(m2x2i) {}
+// CHECK: define dso_local void @"?foo22i@@YAXU?$__matrix@H$01$01@__clang@@@Z"
+
+m2x2i rfoo22i() { return m2x2i(); }
+// CHECK: define dso_local noundef <4 x i32> @"?rfoo22i@@YAU?$__matrix@H$01$01@__clang@@XZ"
\ No newline at end of file

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's cherry pick it.

@@ -3552,7 +3552,22 @@ void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,

void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
Qualifiers quals, SourceRange Range) {
Error(Range.getBegin(), "matrix type") << Range;
QualType EltTy = T->getElementType();
const BuiltinType *ET = EltTy->getAs<BuiltinType>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also backport ccdd55c or manually delete this unused variable to keep the release branch CI green.

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Status: Needs Merge
Development

Successfully merging this pull request may close these issues.

3 participants