Reland “Make Reactor buildable with LLVM 18”

Relevant LLVM API changes:
https://github.com/llvm/llvm-project/pull/66295
https://github.com/llvm/llvm-project/pull/71029
https://github.com/llvm/llvm-project/pull/74261

This reverts commit fefba0024aa6cecf17fb26fe44c0f59aa1c59734.

Change-Id: I2389e8a777d010eb4200b5c4d97b88834d066a43
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/75428
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Shahbaz Youssefi <syoussefi@google.com>
Reviewed-by: Geoff Lang <geofflang@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@google.com>
Commit-Queue: Shahbaz Youssefi <syoussefi@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp
index 262c69e..6db6ca1 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -47,7 +47,11 @@
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Support/CommandLine.h"
+#if LLVM_VERSION_MAJOR >= 18
+#include "llvm/TargetParser/Host.h"
+#else
 #include "llvm/Support/Host.h"
+#endif
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Transforms/InstCombine/InstCombine.h"
 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
@@ -73,6 +77,12 @@
 #	include "llvm/Transforms/IPO.h"
 #endif
 
+#if LLVM_VERSION_MAJOR < 18
+using LLVMOptLevel = llvm::CodeGenOpt::Level;
+#else
+using LLVMOptLevel = llvm::CodeGenOptLevel;
+#endif
+
 #ifdef _MSC_VER
     __pragma(warning(pop))
 #endif
@@ -190,7 +200,7 @@
 private:
 	JITGlobals(llvm::orc::JITTargetMachineBuilder &&jitTargetMachineBuilder, llvm::DataLayout &&dataLayout);
 
-	static llvm::CodeGenOpt::Level toLLVM(int level);
+	static LLVMOptLevel toLLVM(int level);
 
 	const llvm::orc::JITTargetMachineBuilder jitTargetMachineBuilder;
 	const llvm::DataLayout dataLayout;
@@ -303,26 +313,30 @@
 {
 }
 
-llvm::CodeGenOpt::Level JITGlobals::toLLVM(int level)
+LLVMOptLevel JITGlobals::toLLVM(int level)
 {
+#if LLVM_VERSION_MAJOR < 18
+#define CodeGenOptLevel CodeGenOpt
+#endif
 	// TODO(b/173257647): MemorySanitizer instrumentation produces IR which takes
 	// a lot longer to process by the machine code optimization passes. Disabling
 	// them has a negligible effect on code quality but compiles much faster.
 	if(__has_feature(memory_sanitizer))
 	{
-		return llvm::CodeGenOpt::None;
+		return llvm::CodeGenOptLevel::None;
 	}
 
 	switch(level)
 	{
-	case 0: return llvm::CodeGenOpt::None;
-	case 1: return llvm::CodeGenOpt::Less;
-	case 2: return llvm::CodeGenOpt::Default;
-	case 3: return llvm::CodeGenOpt::Aggressive;
+	case 0: return llvm::CodeGenOptLevel::None;
+	case 1: return llvm::CodeGenOptLevel::Less;
+	case 2: return llvm::CodeGenOptLevel::Default;
+	case 3: return llvm::CodeGenOptLevel::Aggressive;
 	default: UNREACHABLE("Unknown Optimization Level %d", int(level));
 	}
 
-	return llvm::CodeGenOpt::Default;
+	return llvm::CodeGenOptLevel::Default;
+#undef CodeGenOptLevel
 }
 
 class MemoryMapper final : public llvm::SectionMemoryManager::MemoryMapper
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index dda6d91..62b4009 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -59,6 +59,12 @@
 }  // namespace llvm
 #endif
 
+#if LLVM_VERSION_MAJOR >= 18
+#define our_getInt8PtrTy llvm::PointerType::getUnqual
+#else
+#define our_getInt8PtrTy llvm::Type::getInt8PtrTy
+#endif
+
 namespace {
 
 // Used to automatically invoke llvm_shutdown() when driver is unloaded
@@ -3782,7 +3788,7 @@
 void VPrintf(const std::vector<Value *> &vals)
 {
 	auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
-	auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
+	auto i8PtrTy = our_getInt8PtrTy(*jit->context);
 	auto funcTy = llvm::FunctionType::get(i32Ty, { i8PtrTy }, true);
 	auto func = jit->module->getOrInsertFunction("rr::DebugPrintf", funcTy);
 	jit->builder->CreateCall(func, V(vals));
@@ -3851,7 +3857,7 @@
 	auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
 	auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
 	auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
-	auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
+	auto i8PtrTy = our_getInt8PtrTy(*jit->context);
 	auto promiseTy = jit->coroutine.yieldType;
 	auto promisePtrTy = promiseTy->getPointerTo();
 
@@ -4016,7 +4022,7 @@
 	// coroutine.
 	auto voidTy = llvm::Type::getVoidTy(*jit->context);
 	auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
-	auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
+	auto i8PtrTy = our_getInt8PtrTy(*jit->context);
 	auto handleTy = i8PtrTy;
 	auto boolTy = i1Ty;
 	auto promiseTy = T(YieldType);