summarylogtreecommitdiffstats
path: root/21d4c2f1.patch
blob: 89125af3d9aeffb94ad34d1e708fbcf51f85cdb4 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
From 921f1b9d5e9389756826898d6907c0a2829efa51 Mon Sep 17 00:00:00 2001
From: Prem Chintalapudi <prem.chintalapudi@gmail.com>
Date: Wed, 10 May 2023 09:58:04 -0400
Subject: [PATCH 1/7] Fix remarks emissions from simdloop pass

Co-authored-by: Valentin Churavy <v.churavy@gmail.com>
---
 src/llvm-simdloop.cpp | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/llvm-simdloop.cpp b/src/llvm-simdloop.cpp
index 3c94b226ad7b8..233f61c9fea6b 100644
--- a/src/llvm-simdloop.cpp
+++ b/src/llvm-simdloop.cpp
@@ -165,11 +165,13 @@ static bool markLoopInfo(Module &M, Function *marker, function_ref<LoopInfo &(Fu
         Instruction *I = cast<Instruction>(U);
         ToDelete.push_back(I);
 
-        LoopInfo &LI = GetLI(*I->getParent()->getParent());
-        Loop *L = LI.getLoopFor(I->getParent());
-        I->removeFromParent();
-        if (!L)
+        BasicBlock *B = I->getParent();
+        LoopInfo &LI = GetLI(*B->getParent());
+        Loop *L = LI.getLoopFor(B);
+        if (!L) {
+            I->removeFromParent();
             continue;
+        }
 
         LLVM_DEBUG(dbgs() << "LSL: loopinfo marker found\n");
         bool simd = false;
@@ -258,6 +260,8 @@ static bool markLoopInfo(Module &M, Function *marker, function_ref<LoopInfo &(Fu
             }
         }
 
+        I->removeFromParent();
+
         Changed = true;
     }
 
From b2273d39542fe803f7d9da03ef57af7e815db68c Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Sun, 30 Apr 2023 20:19:08 -0400
Subject: [PATCH 3/7] Do not yet mandate opaque pointers for LLVM 15

---
 src/codegen.cpp    | 11 +++++++++++
 src/jitlayers.cpp  |  3 ---
 src/llvm-version.h |  2 +-
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/codegen.cpp b/src/codegen.cpp
index a9d2cb0c60333..2e3f7eb2bf7bb 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -9083,6 +9083,17 @@ extern "C" void jl_init_llvm(void)
     if (clopt && clopt->getNumOccurrences() == 0)
         cl::ProvidePositionalOption(clopt, "4", 1);
 
+#if JL_LLVM_VERSION >= 150000
+    clopt = llvmopts.lookup("opaque-pointers");
+    if (clopt && clopt->getNumOccurrences() == 0) {
+#ifdef JL_LLVM_OPAQUE_POINTERS
+        cl::ProvidePositionalOption(clopt, "true", 1);
+#else
+        cl::ProvidePositionalOption(clopt, "false", 1);
+#endif
+    }
+#endif
+
     jl_ExecutionEngine = new JuliaOJIT();
 
     bool jl_using_gdb_jitevents = false;
diff --git a/src/jitlayers.cpp b/src/jitlayers.cpp
index 643f0468457ae..ef7e98bb7852a 100644
--- a/src/jitlayers.cpp
+++ b/src/jitlayers.cpp
@@ -1306,9 +1306,6 @@ JuliaOJIT::JuliaOJIT()
     JD(ES.createBareJITDylib("JuliaOJIT")),
     ContextPool([](){
         auto ctx = std::make_unique<LLVMContext>();
-#ifdef JL_LLVM_OPAQUE_POINTERS
-        ctx->setOpaquePointers(true);
-#endif
         return orc::ThreadSafeContext(std::move(ctx));
     }),
 #ifdef JL_USE_JITLINK
diff --git a/src/llvm-version.h b/src/llvm-version.h
index a3f3774b6dc15..819ec1c88976b 100644
--- a/src/llvm-version.h
+++ b/src/llvm-version.h
@@ -14,7 +14,7 @@
     #error Only LLVM versions >= 12.0.0 are supported by Julia
 #endif
 
-#if JL_LLVM_VERSION >= 150000
+#if JL_LLVM_VERSION >= 160000
 #define JL_LLVM_OPAQUE_POINTERS 1
 #endif
 

From 190f84180883eb498cb7b7ed27e10af9a6c62863 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Wed, 26 Apr 2023 20:48:45 -0400
Subject: [PATCH 4/7] Upgrade Julia to LLVM 15.0.7+5

Co-authored-by: Gabriel Baraldi <baraldigabriel@gmail.com>
---
 Make.inc                        |   2 +-
 deps/checksums/clang            | 224 ++++++++--------
 deps/checksums/lld              | 224 ++++++++--------
 deps/checksums/llvm             | 452 +++++++++++++++-----------------
 deps/clang.version              |   2 +-
 deps/lld.version                |   2 +-
 deps/llvm-tools.version         |   4 +-
 deps/llvm.version               |   9 +-
 stdlib/LLD_jll/Project.toml     |   4 +-
 stdlib/libLLVM_jll/Project.toml |   2 +-
 10 files changed, 446 insertions(+), 479 deletions(-)

diff --git a/Make.inc b/Make.inc
index 4d564f057a3da..35b0657de5aa2 100644
--- a/Make.inc
+++ b/Make.inc
@@ -480,7 +480,7 @@ FC := $(CROSS_COMPILE)gfortran
 ifeq ($(OS), Darwin)
 APPLE_ARCH := $(shell uname -m)
 ifneq ($(APPLE_ARCH),arm64)
-MACOSX_VERSION_MIN := 10.10
+MACOSX_VERSION_MIN := 10.14
 else
 MACOSX_VERSION_MIN := 11.0
 endif
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 2e3f7eb2bf7bb..07e7b15afc165 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -9169,7 +9169,9 @@ extern "C" JL_DLLEXPORT void jl_init_codegen_impl(void)
 extern "C" JL_DLLEXPORT void jl_teardown_codegen_impl() JL_NOTSAFEPOINT
 {
     // output LLVM timings and statistics
-    jl_ExecutionEngine->printTimers();
+    // Guard against exits before we have initialized the ExecutionEngine
+    if (jl_ExecutionEngine)
+        jl_ExecutionEngine->printTimers();
     PrintStatistics();
 }
 
diff --git a/src/jitlayers.h b/src/jitlayers.h
index bbbcbe73f1e54..4c6921cd42dab 100644
--- a/src/jitlayers.h
+++ b/src/jitlayers.h
@@ -97,10 +97,8 @@ struct OptimizationOptions {
 };
 
 // LLVM's new pass manager is scheduled to replace the legacy pass manager
-// for middle-end IR optimizations. However, we have not qualified the new
-// pass manager on our optimization pipeline yet, so this remains an optional
-// define
-#if defined(HAS_SANITIZER) && JL_LLVM_VERSION >= 150000
+// for middle-end IR optimizations.
+#if JL_LLVM_VERSION >= 150000
 #define JL_USE_NEW_PM
 #endif
 
diff --git a/src/pipeline.cpp b/src/pipeline.cpp
index 4403653a9d8e4..7e61171d288e6 100644
--- a/src/pipeline.cpp
+++ b/src/pipeline.cpp
@@ -361,7 +361,8 @@ static void buildFullPipeline(ModulePassManager &MPM, PassBuilder *PB, Optimizat
     {
         FunctionPassManager FPM;
         FPM.addPass(SROAPass());
-        FPM.addPass(InstSimplifyPass());
+        // SROA can duplicate PHI nodes which can block LowerSIMD
+        FPM.addPass(InstCombinePass());
         FPM.addPass(JumpThreadingPass());
         FPM.addPass(CorrelatedValuePropagationPass());
         FPM.addPass(ReassociatePass());
@@ -384,7 +385,7 @@ static void buildFullPipeline(ModulePassManager &MPM, PassBuilder *PB, Optimizat
 #endif
             LPM2.addPass(LICMPass(LICMOptions()));
             JULIA_PASS(LPM2.addPass(JuliaLICMPass()));
-            LPM2.addPass(SimpleLoopUnswitchPass(true, true));
+            LPM2.addPass(SimpleLoopUnswitchPass(false, true));
             LPM2.addPass(LICMPass(LICMOptions()));
             JULIA_PASS(LPM2.addPass(JuliaLICMPass()));
             //LICM needs MemorySSA now, so we must use it
@@ -397,11 +398,11 @@ static void buildFullPipeline(ModulePassManager &MPM, PassBuilder *PB, Optimizat
             LPM.addPass(LoopIdiomRecognizePass());
             LPM.addPass(IndVarSimplifyPass());
             LPM.addPass(LoopDeletionPass());
+            LPM.addPass(LoopFullUnrollPass());
             invokeLoopOptimizerEndCallbacks(LPM, PB, O);
             //We don't know if the loop end callbacks support MSSA
             FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM), /*UseMemorySSA = */false));
         }
-        FPM.addPass(LoopUnrollPass(LoopUnrollOptions().setRuntime(false)));
         JULIA_PASS(FPM.addPass(AllocOptPass()));
         FPM.addPass(SROAPass());
         FPM.addPass(InstSimplifyPass());

From 2ddbb5abb93045eeb4513e223c86e9c25fa774a4 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Wed, 26 Apr 2023 20:49:16 -0400
Subject: [PATCH 6/7] Fix tests and static analyzer for LLVM 15

Co-authored-by: Gabriel Baraldi <baraldigabriel@gmail.com>
Co-authored-by: Prem Chintalapudi <prem.chintalapudi@gmail.com>
---
 src/llvm-alloc-opt.cpp                   |  3 ++
 src/llvm-late-gc-lowering.cpp            |  1 +
 src/llvm-lower-handlers.cpp              |  1 +
 src/llvm-multiversioning.cpp             |  3 ++
 src/llvm-ptls.cpp                        |  2 +
 test/clangsa/MissingRoots.c              |  3 ++
 test/cmdlineargs.jl                      | 10 ++--
 test/llvmpasses/pipeline-o2-broadcast.jl | 68 ++++++++++++++----------
 test/llvmpasses/pipeline-o2.jl           |  6 +--
 9 files changed, 62 insertions(+), 35 deletions(-)

diff --git a/src/llvm-alloc-opt.cpp b/src/llvm-alloc-opt.cpp
index 1a524cbe8d419..bb6de67f347ff 100644
--- a/src/llvm-alloc-opt.cpp
+++ b/src/llvm-alloc-opt.cpp
@@ -1138,9 +1138,12 @@ void Optimizer::splitOnStack(CallInst *orig_inst)
                     ref->setOrdering(AtomicOrdering::NotAtomic);
                     operands.push_back(ref);
                 }
+#ifndef __clang_analyzer__
+                // FIXME: SA finds "Called C++ object pointer is null" inside the LLVM code.
                 auto new_call = builder.CreateCall(pass.gc_preserve_begin_func, operands);
                 new_call->takeName(call);
                 call->replaceAllUsesWith(new_call);
+#endif
                 call->eraseFromParent();
                 return;
             }
diff --git a/src/llvm-late-gc-lowering.cpp b/src/llvm-late-gc-lowering.cpp
index a836ff1361768..ac70685e7431b 100644
--- a/src/llvm-late-gc-lowering.cpp
+++ b/src/llvm-late-gc-lowering.cpp
@@ -1262,6 +1262,7 @@ static bool isLoadFromConstGV(LoadInst *LI, bool &task_local, PhiSet *seen)
     // We only emit single slot GV in codegen
     // but LLVM global merging can change the pointer operands to GEPs/bitcasts
     auto load_base = LI->getPointerOperand()->stripInBoundsOffsets();
+    assert(load_base); // Static analyzer
     auto gv = dyn_cast<GlobalVariable>(load_base);
     if (isTBAA(LI->getMetadata(LLVMContext::MD_tbaa),
                {"jtbaa_immut", "jtbaa_const", "jtbaa_datatype"})) {
diff --git a/src/llvm-lower-handlers.cpp b/src/llvm-lower-handlers.cpp
index 919128769019b..39a36bfc3ba76 100644
--- a/src/llvm-lower-handlers.cpp
+++ b/src/llvm-lower-handlers.cpp
@@ -8,6 +8,7 @@
 
 #include <llvm/ADT/DepthFirstIterator.h>
 #include <llvm/ADT/Statistic.h>
+#include <llvm/ADT/Triple.h>
 #include <llvm/Analysis/CFG.h>
 #include <llvm/IR/BasicBlock.h>
 #include <llvm/IR/Constants.h>
diff --git a/src/llvm-multiversioning.cpp b/src/llvm-multiversioning.cpp
index 21a090724802a..cdba03047a4b7 100644
--- a/src/llvm-multiversioning.cpp
+++ b/src/llvm-multiversioning.cpp
@@ -14,11 +14,13 @@
 #include <llvm/Pass.h>
 #include <llvm/ADT/BitVector.h>
 #include <llvm/ADT/Statistic.h>
+#include <llvm/ADT/Triple.h>
 #include <llvm/IR/Module.h>
 #include <llvm/IR/LegacyPassManager.h>
 #include <llvm/IR/Function.h>
 #include <llvm/IR/Instructions.h>
 #include <llvm/IR/Constants.h>
+#include <llvm/IR/Dominators.h>
 #include <llvm/IR/LLVMContext.h>
 #include <llvm/Analysis/LoopInfo.h>
 #include <llvm/Analysis/CallGraph.h>
@@ -779,6 +781,7 @@ static Value *rewrite_inst_use(const Stack& stack, Type *T_size, Value *replace,
             replace = inst;
             continue;
         }
+        assert(val);
         unsigned nargs = val->getNumOperands();
         args.resize(nargs);
         for (unsigned j = 0; j < nargs; j++) {
diff --git a/src/llvm-ptls.cpp b/src/llvm-ptls.cpp
index 8174832b3cebf..a628710916327 100644
--- a/src/llvm-ptls.cpp
+++ b/src/llvm-ptls.cpp
@@ -9,6 +9,7 @@
 #include <llvm-c/Types.h>
 
 #include <llvm/Pass.h>
+#include <llvm/ADT/Triple.h>
 #include <llvm/IR/Module.h>
 #include <llvm/IR/LegacyPassManager.h>
 #include <llvm/IR/Function.h>
@@ -161,6 +162,7 @@ void LowerPTLS::fix_pgcstack_use(CallInst *pgcstack, Function *pgcstack_getter,
         SmallVector<uint32_t, 2> Weights{9, 1};
         TerminatorInst *fastTerm;
         TerminatorInst *slowTerm;
+        assert(pgcstack->getType()); // Static analyzer
         auto cmp = new ICmpInst(phi, CmpInst::ICMP_NE, pgcstack, Constant::getNullValue(pgcstack->getType()));
         SplitBlockAndInsertIfThenElse(cmp, phi, &fastTerm, &slowTerm,
                                       MDB.createBranchWeights(Weights));
diff --git a/test/clangsa/MissingRoots.c b/test/clangsa/MissingRoots.c
index f0b32c54bc7b8..0ff5e633622ce 100644
--- a/test/clangsa/MissingRoots.c
+++ b/test/clangsa/MissingRoots.c
@@ -352,6 +352,9 @@ void assoc_exact_broken(jl_value_t **args, size_t n, int8_t offs, size_t world)
 }
 */
 
+// declare
+jl_typemap_level_t *jl_new_typemap_level(void);
+
 void assoc_exact_ok(jl_value_t *args1, jl_value_t **args, size_t n, int8_t offs, size_t world) {
     jl_typemap_level_t *cache = jl_new_typemap_level();
     JL_GC_PUSH1(&cache);
diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl
index 389b195d97935..1d04926ef23af 100644
--- a/test/cmdlineargs.jl
+++ b/test/cmdlineargs.jl
@@ -188,10 +188,12 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
         @test contains(v[2], r"enable-tail-merge + = 1")
         @test isempty(v[3])
     end
-    @testset let v = readchomperrors(setenv(`$exename -e 0`, "JULIA_LLVM_ARGS" => "-print-options -enable-tail-merge=1 -enable-tail-merge=1", "HOME" => homedir()))
-        @test !v[1]
-        @test isempty(v[2])
-        @test v[3] == "julia: for the --enable-tail-merge option: may only occur zero or one times!"
+    if Base.libllvm_version < v"15" #LLVM over 15 doesn't care for multiple options
+        @testset let v = readchomperrors(setenv(`$exename -e 0`, "JULIA_LLVM_ARGS" => "-print-options -enable-tail-merge=1 -enable-tail-merge=1", "HOME" => homedir()))
+            @test !v[1]
+            @test isempty(v[2])
+            @test v[3] == "julia: for the --enable-tail-merge option: may only occur zero or one times!"
+        end
     end
 end
 
diff --git a/test/llvmpasses/pipeline-o2.jl b/test/llvmpasses/pipeline-o2.jl
index 2996a44de62b3..fcb2161de7614 100644
--- a/test/llvmpasses/pipeline-o2.jl
+++ b/test/llvmpasses/pipeline-o2.jl
@@ -78,21 +78,21 @@ end
 # COM: memset checks
 
 # COM: INT64
-# ALL-LABEL: define nonnull {} addrspace(10)* @julia_zeros
+# ALL: define {{.*}} @julia_zeros
 # ALL-NOT: bounds_error
 # COM: memset is not used with bounds checks on (too late in the pipeline)
 # BC_OFF: llvm.memset
 # BC_AUTO: llvm.memset
 
 # COM: INT32
-# ALL-LABEL: define nonnull {} addrspace(10)* @julia_zeros
+# ALL: define {{.*}} @julia_zeros
 # ALL-NOT: bounds_error
 # COM: memset is not used with bounds checks on (too late in the pipeline)
 # BC_OFF: llvm.memset
 # BC_AUTO: llvm.memset
 
 # COM: INT16
-# ALL-LABEL: define nonnull {} addrspace(10)* @julia_zeros
+# ALL: define {{.*}} @julia_zeros
 # ALL-NOT: bounds_error
 # COM: memset is not used with bounds checks on (too late in the pipeline)
 # BC_OFF: llvm.memset

From 77c13ad59364189386114b546a7482dbe2edf233 Mon Sep 17 00:00:00 2001
From: Valentin Churavy <v.churavy@gmail.com>
Date: Wed, 10 May 2023 10:51:16 -0400
Subject: [PATCH 7/7] Reenable NonTrivial Loop Unswitch

---
 src/codegen.cpp  | 3 ---
 src/pipeline.cpp | 2 +-
 2 files changed, 1 insertion(+), 4 deletions(-)

diff --git a/src/codegen.cpp b/src/codegen.cpp
index 07e7b15afc165..ae306d3d1cdb5 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -9073,9 +9073,6 @@ extern "C" void jl_init_llvm(void)
     clopt = llvmopts.lookup("unswitch-threshold");
     if (clopt->getNumOccurrences() == 0)
         cl::ProvidePositionalOption(clopt, "100", 1);
-    clopt = llvmopts.lookup("enable-unswitch-cost-multiplier");
-    if (clopt->getNumOccurrences() == 0)
-        cl::ProvidePositionalOption(clopt, "false", 1);
 #endif
     // if the patch adding this option has been applied, lower its limit to provide
     // better DAGCombiner performance.
diff --git a/src/pipeline.cpp b/src/pipeline.cpp
index 7e61171d288e6..6e6a9a3c37d02 100644
--- a/src/pipeline.cpp
+++ b/src/pipeline.cpp
@@ -385,7 +385,7 @@ static void buildFullPipeline(ModulePassManager &MPM, PassBuilder *PB, Optimizat
 #endif
             LPM2.addPass(LICMPass(LICMOptions()));
             JULIA_PASS(LPM2.addPass(JuliaLICMPass()));
-            LPM2.addPass(SimpleLoopUnswitchPass(false, true));
+            LPM2.addPass(SimpleLoopUnswitchPass(/*NonTrivial*/true, true));
             LPM2.addPass(LICMPass(LICMOptions()));
             JULIA_PASS(LPM2.addPass(JuliaLICMPass()));
             //LICM needs MemorySSA now, so we must use it