Skip to content

Commit f5a30f1

Browse files
authored
[Clang][MicrosoftMangle] Implement mangling for ConstantMatrixType (llvm#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> ```
1 parent 7afbffb commit f5a30f1

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3535,7 +3535,22 @@ void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
35353535

35363536
void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
35373537
Qualifiers quals, SourceRange Range) {
3538-
Error(Range.getBegin(), "matrix type") << Range;
3538+
QualType EltTy = T->getElementType();
3539+
const BuiltinType *ET = EltTy->getAs<BuiltinType>();
3540+
3541+
llvm::SmallString<64> TemplateMangling;
3542+
llvm::raw_svector_ostream Stream(TemplateMangling);
3543+
MicrosoftCXXNameMangler Extra(Context, Stream);
3544+
3545+
Stream << "?$";
3546+
3547+
Extra.mangleSourceName("__matrix");
3548+
Extra.mangleType(EltTy, Range, QMM_Escape);
3549+
3550+
Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumRows()));
3551+
Extra.mangleIntegerLiteral(llvm::APSInt::getUnsigned(T->getNumColumns()));
3552+
3553+
mangleArtificialTagType(TagTypeKind::Struct, TemplateMangling, {"__clang"});
35393554
}
35403555

35413556
void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// RUN: %clang_cc1 -fenable-matrix -fms-extensions -fcxx-exceptions -ffreestanding -target-feature +avx -emit-llvm %s -o - -triple=i686-pc-win32 | FileCheck %s
2+
// 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
3+
4+
typedef float __attribute__((matrix_type(4, 4))) m4x4f;
5+
typedef float __attribute__((matrix_type(2, 2))) m2x2f;
6+
7+
typedef int __attribute__((matrix_type(4, 4))) m4x4i;
8+
typedef int __attribute__((matrix_type(2, 2))) m2x2i;
9+
10+
void thow(int i) {
11+
switch (i) {
12+
case 0: throw m4x4f();
13+
// CHECK: ??_R0U?$__matrix@M$03$03@__clang@@@8
14+
// CHECK: _CT??_R0U?$__matrix@M$03$03@__clang@@@864
15+
// CHECK: _CTA1U?$__matrix@M$03$03@__clang@@
16+
// CHECK: _TI1U?$__matrix@M$03$03@__clang@@
17+
case 1: throw m2x2f();
18+
// CHECK: ??_R0U?$__matrix@M$01$01@__clang@@@8
19+
// CHECK: _CT??_R0U?$__matrix@M$01$01@__clang@@@816
20+
// CHECK: _CTA1U?$__matrix@M$01$01@__clang@@
21+
// CHECK: _TI1U?$__matrix@M$01$01@__clang@@
22+
case 2: throw m4x4i();
23+
// CHECK: ??_R0U?$__matrix@H$03$03@__clang@@@8
24+
// CHECK: _CT??_R0U?$__matrix@H$03$03@__clang@@@864
25+
// CHECK: _CTA1U?$__matrix@H$03$03@__clang@@
26+
// CHECK: _TI1U?$__matrix@H$03$03@__clang@@
27+
case 3: throw m2x2i();
28+
// CHECK: ??_R0U?$__matrix@H$01$01@__clang@@@8
29+
// CHECK: _CT??_R0U?$__matrix@H$01$01@__clang@@@816
30+
// CHECK: _CTA1U?$__matrix@H$01$01@__clang@@
31+
// CHECK: _TI1U?$__matrix@H$01$01@__clang@@
32+
}
33+
}
34+
35+
void foo44f(m4x4f) {}
36+
// CHECK: define dso_local void @"?foo44f@@YAXU?$__matrix@M$03$03@__clang@@@Z"
37+
38+
m4x4f rfoo44f() { return m4x4f(); }
39+
// CHECK: define dso_local noundef <16 x float> @"?rfoo44f@@YAU?$__matrix@M$03$03@__clang@@XZ"
40+
41+
void foo22f(m2x2f) {}
42+
// CHECK: define dso_local void @"?foo22f@@YAXU?$__matrix@M$01$01@__clang@@@Z"
43+
44+
m2x2f rfoo22f() { return m2x2f(); }
45+
// CHECK: define dso_local noundef <4 x float> @"?rfoo22f@@YAU?$__matrix@M$01$01@__clang@@XZ"
46+
47+
void foo44i(m4x4i) {}
48+
// CHECK: define dso_local void @"?foo44i@@YAXU?$__matrix@H$03$03@__clang@@@Z"
49+
50+
m4x4i rfoo44i() { return m4x4i(); }
51+
// CHECK: define dso_local noundef <16 x i32> @"?rfoo44i@@YAU?$__matrix@H$03$03@__clang@@XZ"
52+
53+
void foo22i(m2x2i) {}
54+
// CHECK: define dso_local void @"?foo22i@@YAXU?$__matrix@H$01$01@__clang@@@Z"
55+
56+
m2x2i rfoo22i() { return m2x2i(); }
57+
// CHECK: define dso_local noundef <4 x i32> @"?rfoo22i@@YAU?$__matrix@H$01$01@__clang@@XZ"

0 commit comments

Comments
 (0)