[clang] [Clang][MicrosoftMangle] Implement mangling for ConstantMatrixType (PR #134930)

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 19 12:58:44 PDT 2025


https://github.com/Losy001 updated https://github.com/llvm/llvm-project/pull/134930

>From 4a3b446a8081636219c704d0a2bbba5af567f5ca Mon Sep 17 00:00:00 2001
From: Losy001 <64610343+Losy001 at users.noreply.github.com>
Date: Tue, 8 Apr 2025 22:41:03 +0200
Subject: [PATCH 1/2] Implement mangling for matrix types

---
 clang/lib/AST/MicrosoftMangle.cpp | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp
index 4d14614fc1ec7..5ffe8b5d3d3f9 100644
--- a/clang/lib/AST/MicrosoftMangle.cpp
+++ b/clang/lib/AST/MicrosoftMangle.cpp
@@ -3526,7 +3526,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,

>From 5a81c54986d85b045588529ecdd63f2967f6c931 Mon Sep 17 00:00:00 2001
From: Losy001 <64610343+Losy001 at users.noreply.github.com>
Date: Sat, 19 Apr 2025 21:58:25 +0200
Subject: [PATCH 2/2] Add tests

---
 clang/test/CodeGenCXX/mangle-ms-matrix.cpp | 57 ++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 clang/test/CodeGenCXX/mangle-ms-matrix.cpp

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 at M$03$03 at __clang@@@8
+    // CHECK: _CT??_R0U?$__matrix at M$03$03 at __clang@@@864
+    // CHECK: _CTA1U?$__matrix at M$03$03 at __clang@@
+    // CHECK: _TI1U?$__matrix at M$03$03 at __clang@@
+    case 1: throw m2x2f();
+    // CHECK: ??_R0U?$__matrix at M$01$01 at __clang@@@8
+    // CHECK: _CT??_R0U?$__matrix at M$01$01 at __clang@@@816
+    // CHECK: _CTA1U?$__matrix at M$01$01 at __clang@@
+    // CHECK: _TI1U?$__matrix at M$01$01 at __clang@@
+    case 2: throw m4x4i();
+    // CHECK: ??_R0U?$__matrix at H$03$03 at __clang@@@8
+    // CHECK: _CT??_R0U?$__matrix at H$03$03 at __clang@@@864
+    // CHECK: _CTA1U?$__matrix at H$03$03 at __clang@@
+    // CHECK: _TI1U?$__matrix at H$03$03 at __clang@@
+    case 3: throw m2x2i();
+    // CHECK: ??_R0U?$__matrix at H$01$01 at __clang@@@8
+    // CHECK: _CT??_R0U?$__matrix at H$01$01 at __clang@@@816
+    // CHECK: _CTA1U?$__matrix at H$01$01 at __clang@@
+    // CHECK: _TI1U?$__matrix at H$01$01 at __clang@@
+  }
+}
+
+void foo44f(m4x4f) {}
+// CHECK: define dso_local void @"?foo44f@@YAXU?$__matrix at M$03$03 at __clang@@@Z"
+
+m4x4f rfoo44f() { return m4x4f(); }
+// CHECK: define dso_local noundef <16 x float> @"?rfoo44f@@YAU?$__matrix at M$03$03 at __clang@@XZ"
+
+void foo22f(m2x2f) {}
+// CHECK: define dso_local void @"?foo22f@@YAXU?$__matrix at M$01$01 at __clang@@@Z"
+
+m2x2f rfoo22f() { return m2x2f(); }
+// CHECK: define dso_local noundef <4 x float> @"?rfoo22f@@YAU?$__matrix at M$01$01 at __clang@@XZ"
+
+void foo44i(m4x4i) {}
+// CHECK: define dso_local void @"?foo44i@@YAXU?$__matrix at H$03$03 at __clang@@@Z"
+
+m4x4i rfoo44i() { return m4x4i(); }
+// CHECK: define dso_local noundef <16 x i32> @"?rfoo44i@@YAU?$__matrix at H$03$03 at __clang@@XZ"
+
+void foo22i(m2x2i) {}
+// CHECK: define dso_local void @"?foo22i@@YAXU?$__matrix at H$01$01 at __clang@@@Z"
+
+m2x2i rfoo22i() { return m2x2i(); }
+// CHECK: define dso_local noundef <4 x i32> @"?rfoo22i@@YAU?$__matrix at H$01$01 at __clang@@XZ"
\ No newline at end of file



More information about the cfe-commits mailing list