summaryrefslogtreecommitdiff
blob: 43b7925a9ce799ea371856d76c3259d2827b65bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
This patch is from:
 https://raw.githubusercontent.com/archlinux/svntogit-community/packages/julia/trunk/julia-llvm13.patch

From 9daa25a5f331a7e1c0f0b222373a853c2ce1462d Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Tue, 12 Oct 2021 11:56:19 -0400
Subject: [PATCH 2/6] WIP: add Type to ByVal attribute

---
 src/abi_aarch64.cpp |  2 +-
 src/abi_arm.cpp     |  2 +-
 src/abi_llvm.cpp    |  2 +-
 src/abi_ppc64le.cpp |  6 +++++-
 src/abi_win32.cpp   |  6 +++++-
 src/abi_win64.cpp   |  9 +++++++--
 src/abi_x86.cpp     |  8 ++++++--
 src/abi_x86_64.cpp  | 11 ++++++++++-
 src/ccall.cpp       |  4 ++--
 9 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/src/abi_aarch64.cpp b/src/abi_aarch64.cpp
index ce94cc66f0..7ffe107d61 100644
--- a/src/abi_aarch64.cpp
+++ b/src/abi_aarch64.cpp
@@ -194,7 +194,7 @@ Type *isHFAorHVA(jl_datatype_t *dt, size_t &nele) const
     return NULL;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     // B.2
     //   If the argument type is an HFA or an HVA, then the argument is used
diff --git a/src/abi_arm.cpp b/src/abi_arm.cpp
index 1a5d3d0651..ed846dfafb 100644
--- a/src/abi_arm.cpp
+++ b/src/abi_arm.cpp
@@ -23,7 +23,7 @@
 
 struct ABI_ARMLayout : AbiLayout {
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     return false;
 }
diff --git a/src/abi_llvm.cpp b/src/abi_llvm.cpp
index 1ab30da1b2..dba8f4aa8a 100644
--- a/src/abi_llvm.cpp
+++ b/src/abi_llvm.cpp
@@ -45,7 +45,7 @@ bool use_sret(jl_datatype_t *ty) override
     return false;
 }
 
-bool needPassByRef(jl_datatype_t *ty, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *ty, AttrBuilder &ab, Type *Ty) override
 {
     return false;
 }
diff --git a/src/abi_ppc64le.cpp b/src/abi_ppc64le.cpp
index dd6f927d9c..35e444ef77 100644
--- a/src/abi_ppc64le.cpp
+++ b/src/abi_ppc64le.cpp
@@ -101,12 +101,16 @@ bool use_sret(jl_datatype_t *dt) override
     return false;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     jl_datatype_t *ty0 = NULL;
     bool hva = false;
     if (jl_datatype_size(dt) > 64 && isHFA(dt, &ty0, &hva) > 8) {
+#if JL_LLVM_VERSION < 120000
         ab.addAttribute(Attribute::ByVal);
+#else
+        ab.addByValAttr(Ty);
+#endif
         return true;
     }
     return false;
diff --git a/src/abi_win32.cpp b/src/abi_win32.cpp
index af16a0310b..0b34f840e4 100644
--- a/src/abi_win32.cpp
+++ b/src/abi_win32.cpp
@@ -49,11 +49,15 @@ bool use_sret(jl_datatype_t *dt) override
     return true;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     // Use pass by reference for all structs
     if (dt->layout->nfields > 0) {
+#if JL_LLVM_VERSION < 120000
         ab.addAttribute(Attribute::ByVal);
+#else
+        ab.addByValAttr(Ty);
+#endif
         return true;
     }
     return false;
diff --git a/src/abi_win64.cpp b/src/abi_win64.cpp
index 16e46a9703..f47802edf1 100644
--- a/src/abi_win64.cpp
+++ b/src/abi_win64.cpp
@@ -56,14 +56,19 @@ bool use_sret(jl_datatype_t *dt) override
     return true;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     nargs++;
     size_t size = jl_datatype_size(dt);
     if (win64_reg_size(size))
         return false;
-    if (nargs <= 4)
+    if (nargs <= 4) {
+#if JL_LLVM_VERSION < 120000
         ab.addAttribute(Attribute::ByVal);
+#else
+        ab.addByValAttr(Ty);
+#endif
+    }
     return true;
 }
 
diff --git a/src/abi_x86.cpp b/src/abi_x86.cpp
index 7a65de028e..c6c0282602 100644
--- a/src/abi_x86.cpp
+++ b/src/abi_x86.cpp
@@ -67,12 +67,16 @@ bool use_sret(jl_datatype_t *dt) override
     return true;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     size_t size = jl_datatype_size(dt);
     if (is_complex64(dt) || is_complex128(dt) || (jl_is_primitivetype(dt) && size <= 8))
         return false;
-    ab.addAttribute(Attribute::ByVal);
+#if JL_LLVM_VERSION < 120000
+        ab.addAttribute(Attribute::ByVal);
+#else
+        ab.addByValAttr(Ty);
+#endif
     return true;
 }
 
diff --git a/src/abi_x86_64.cpp b/src/abi_x86_64.cpp
index ac28af3011..5f8256dee4 100644
--- a/src/abi_x86_64.cpp
+++ b/src/abi_x86_64.cpp
@@ -178,11 +178,15 @@ bool use_sret(jl_datatype_t *dt) override
     return sret;
 }
 
-bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
+bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab, Type *Ty) override
 {
     Classification cl = classify(dt);
     if (cl.isMemory) {
+#if JL_LLVM_VERSION < 120000
         ab.addAttribute(Attribute::ByVal);
+#else
+        ab.addByValAttr(Ty);
+#endif
         return true;
     }
 
@@ -202,7 +206,12 @@ bool needPassByRef(jl_datatype_t *dt, AttrBuilder &ab) override
     else if (jl_is_structtype(dt)) {
         // spill to memory even though we would ordinarily pass
         // it in registers
+#if JL_LLVM_VERSION < 120000
         ab.addAttribute(Attribute::ByVal);
+#else
+        Type* Ty = preferred_llvm_type(dt, false);
+        ab.addByValAttr(Ty);
+#endif
         return true;
     }
     return false;
diff --git a/src/ccall.cpp b/src/ccall.cpp
index 66ab84c264..e21c717d41 100644
--- a/src/ccall.cpp
+++ b/src/ccall.cpp
@@ -291,7 +291,7 @@ class AbiLayout {
 public:
     virtual ~AbiLayout() {}
     virtual bool use_sret(jl_datatype_t *ty) = 0;
-    virtual bool needPassByRef(jl_datatype_t *ty, AttrBuilder&) = 0;
+    virtual bool needPassByRef(jl_datatype_t *ty, AttrBuilder&, Type* llvm_t) = 0;
     virtual Type *preferred_llvm_type(jl_datatype_t *ty, bool isret) const = 0;
 };
 
@@ -1077,7 +1077,7 @@ std::string generate_func_sig(const char *fname)
         }
 
         // Whether or not LLVM wants us to emit a pointer to the data
-        bool byRef = abi->needPassByRef((jl_datatype_t*)tti, ab);
+        bool byRef = abi->needPassByRef((jl_datatype_t*)tti, ab, t);
 
         if (jl_is_cpointer_type(tti)) {
             pat = t;

From 1fe19a197ebbe33c9e60b2ca7d30c2573772b476 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Thu, 21 Oct 2021 20:32:39 -0400
Subject: [PATCH 4/6] [LLVM/Win32] Force stack alignment on module

---
 src/aotcompile.cpp |  3 +++
 src/ccall.cpp      |  3 +++
 src/codegen.cpp    | 11 ++++++++++-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp
index ffd43ee8d48d..d3dfc744fcd0 100644
--- a/src/aotcompile.cpp
+++ b/src/aotcompile.cpp
@@ -550,6 +550,9 @@ void jl_dump_native_impl(void *native_code,
     std::unique_ptr<Module> sysimage(new Module("sysimage", Context));
     sysimage->setTargetTriple(data->M->getTargetTriple());
     sysimage->setDataLayout(data->M->getDataLayout());
+#if JL_LLVM_VERSION >= 130000
+    sysimage->setOverrideStackAlignment(data->M->getOverrideStackAlignment());
+#endif
     data->M.reset(); // free memory for data->M
 
     if (sysimg_data) {
diff --git a/src/ccall.cpp b/src/ccall.cpp
index fb70e53e2814..dd7626c918d0 100644
--- a/src/ccall.cpp
+++ b/src/ccall.cpp
@@ -891,6 +891,9 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
     // copy module properties that should always match
     Mod->setTargetTriple(jl_Module->getTargetTriple());
     Mod->setDataLayout(jl_Module->getDataLayout());
+#if JL_LLVM_VERSION >= 130000
+    Mod->setOverrideStackAlignment(jl_Module->getOverrideStackAlignment());
+#endif
 
     // verify the definition
     Function *def = Mod->getFunction(ir_name);
diff --git a/src/codegen.cpp b/src/codegen.cpp
index b09eb8db04e0..bc4ea7711a04 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -1707,6 +1707,14 @@ static void jl_setup_module(Module *m, const jl_cgparams_t *params = &jl_default
             llvm::DEBUG_METADATA_VERSION);
     m->setDataLayout(jl_data_layout);
     m->setTargetTriple(jl_TargetMachine->getTargetTriple().str());
+
+#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_) && JL_LLVM_VERSIOn >= 130000
+    // tell Win32 to assume the stack is always 16-byte aligned,
+    // and to ensure that it is 16-byte aligned for out-going calls,
+    // to ensure compatibility with GCC codes
+    m->setOverrideStackAlignment(16;)
+#endif
+
 }
 
 Module *jl_create_llvm_module(StringRef name)
@@ -8235,10 +8243,11 @@ extern "C" void jl_init_llvm(void)
 
     TargetOptions options = TargetOptions();
     //options.PrintMachineCode = true; //Print machine code produced during JIT compiling
-#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_)
+#if defined(_OS_WINDOWS_) && !defined(_CPU_X86_64_) && JL_LLVM_VERSION <= 120000
     // tell Win32 to assume the stack is always 16-byte aligned,
     // and to ensure that it is 16-byte aligned for out-going calls,
     // to ensure compatibility with GCC codes
+    // In LLVM 13 and onwards this has turned into a module option
     options.StackAlignmentOverride = 16;
 #endif
 #ifdef JL_DEBUG_BUILD

From 7a320647976eb97fdd169b5e85397b9e493e4569 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Fri, 22 Oct 2021 15:35:38 -0400
Subject: [PATCH 5/6] StackProtector is now a module flag

---
 src/aotcompile.cpp | 1 +
 src/ccall.cpp      | 1 +
 src/codegen.cpp    | 6 ++++--
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp
index d3dfc744fcd0..05fcca50c4ee 100644
--- a/src/aotcompile.cpp
+++ b/src/aotcompile.cpp
@@ -551,6 +551,7 @@ void jl_dump_native_impl(void *native_code,
     sysimage->setTargetTriple(data->M->getTargetTriple());
     sysimage->setDataLayout(data->M->getDataLayout());
 #if JL_LLVM_VERSION >= 130000
+    sysimage->setStackProtectorGuard(data->M->getStackProtectorGuard());
     sysimage->setOverrideStackAlignment(data->M->getOverrideStackAlignment());
 #endif
     data->M.reset(); // free memory for data->M
diff --git a/src/ccall.cpp b/src/ccall.cpp
index dd7626c918d0..647735edb37d 100644
--- a/src/ccall.cpp
+++ b/src/ccall.cpp
@@ -892,6 +892,7 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
     Mod->setTargetTriple(jl_Module->getTargetTriple());
     Mod->setDataLayout(jl_Module->getDataLayout());
 #if JL_LLVM_VERSION >= 130000
+    Mod->setStackProtectorGuard(jl_Module->getStackProtectorGuard());
     Mod->setOverrideStackAlignment(jl_Module->getOverrideStackAlignment());
 #endif
 
diff --git a/src/codegen.cpp b/src/codegen.cpp
index bc4ea7711a04..1a7017d3aeb7 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -1714,7 +1714,9 @@ static void jl_setup_module(Module *m, const jl_cgparams_t *params = &jl_default
     // to ensure compatibility with GCC codes
     m->setOverrideStackAlignment(16;)
 #endif
-
+#if defined(JL_DEBUG_BUILD) && JL_LLVM_VERSION >= 130000
+    m->setStackProtectorGuard("global");
+#endif
 }
 
 Module *jl_create_llvm_module(StringRef name)

From e323fc8f7be4ce053dec613076d7dd7517515134 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Sun, 24 Oct 2021 15:18:23 -0400
Subject: [PATCH 6/6] Cleanup MachineObjectFileInfo handling in disassembly

---
 src/disasm.cpp | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/disasm.cpp b/src/disasm.cpp
index 73b394b77d0b..25e7841bde85 100644
--- a/src/disasm.cpp
+++ b/src/disasm.cpp
@@ -860,21 +860,21 @@ static void jl_dump_asm_internal(
     std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TheTriple.str()));
     assert(MRI && "Unable to create target register info!");
 
-    std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
-#if JL_LLVM_VERSION >= 130000
-    MCSubtargetInfo *MSTI = TheTarget->createMCSubtargetInfo(TheTriple.str(), cpu, features);
-    assert(MSTI && "Unable to create subtarget info!");
+    std::unique_ptr<llvm::MCSubtargetInfo> STI(
+      TheTarget->createMCSubtargetInfo(TheTriple.str(), cpu, features));
+    assert(STI && "Unable to create subtarget info!");
 
-    MCContext Ctx(TheTriple, MAI.get(), MRI.get(), MSTI, &SrcMgr);
-    MOFI->initMCObjectFileInfo(Ctx, /* PIC */ false, /* LargeCodeModel */ false);
+#if JL_LLVM_VERSION >= 130000
+    MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
+    std::unique_ptr<MCObjectFileInfo> MOFI(
+      TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false, /*LargeCodeModel=*/ false));
+    Ctx.setObjectFileInfo(MOFI.get());
 #else
+    std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
     MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
     MOFI->InitMCObjectFileInfo(TheTriple, /* PIC */ false, Ctx);
 #endif
 
-    // Set up Subtarget and Disassembler
-    std::unique_ptr<MCSubtargetInfo>
-        STI(TheTarget->createMCSubtargetInfo(TheTriple.str(), cpu, features));
     std::unique_ptr<MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI, Ctx));
     if (!DisAsm) {
         rstream << "ERROR: no disassembler for target " << TheTriple.str();