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();
