From 68adc00eb162c6932e1e2ba3cde9cb3b12bd8f3b Mon Sep 17 00:00:00 2001
From: Keno Fischer <keno@juliacomputing.com>
Date: Wed, 20 Apr 2022 22:02:49 -0400
Subject: [PATCH] Enable opaque pointers for LLVM master (#45012)

Essentially a rebase of #44334 to current master. I would like to work on
some IR canonicalization improvements, but it doesn't make much sense
to that without opaque pointers, since canonical forms will change.

This bootstraps fine on LLVM master, though there are test failures
both with and without this change. I think we'll just have to address
those as part of the regular upgrade cycle as usual (though we should
probably do LLVM 14 first to catch any 13->14 regressions).
---
 src/cgutils.cpp    | 3 +++
 src/jitlayers.cpp  | 8 +++++++-
 src/llvm-version.h | 4 ++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/cgutils.cpp b/src/cgutils.cpp
index f5f378de772c..6f346b32728b 100644
--- a/src/cgutils.cpp
+++ b/src/cgutils.cpp
@@ -2035,6 +2035,8 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, MDNode *tbaa_dst, Va
     // If the types are small and simple, use load and store directly.
     // Going through memcpy can cause LLVM (e.g. SROA) to create bitcasts between float and int
     // that interferes with other optimizations.
+#ifndef JL_LLVM_OPAQUE_POINTERS
+    // TODO: Restore this for opaque pointers? Needs extra type information from the caller.
     if (sz <= 64) {
         // The size limit is arbitrary but since we mainly care about floating points and
         // machine size vectors this should be enough.
@@ -2073,6 +2075,7 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, MDNode *tbaa_dst, Va
             return;
         }
     }
+#endif
     // the memcpy intrinsic does not allow to specify different alias tags
     // for the load part (x.tbaa) and the store part (ctx.tbaa().tbaa_stack).
     // since the tbaa lattice has to be a tree we have unfortunately
diff --git a/src/llvm-version.h b/src/llvm-version.h
index 6d79abdf271f..4e15e787b7de 100644
--- a/src/llvm-version.h
+++ b/src/llvm-version.h
@@ -13,6 +13,10 @@
     #error Only LLVM versions >= 12.0.0 are supported by Julia
 #endif
 
+#if JL_LLVM_VERSION >= 150000
+#define JL_LLVM_OPAQUE_POINTERS 1
+#endif
+
 #ifdef __cplusplus
 #if defined(__GNUC__) && (__GNUC__ >= 9)
 // Added in GCC 9, this warning is annoying
