File llvm-set-of-custom-patches.patch of Package julia

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
openSUSE Build Service is sponsored by