Only lower MSan codegen optimization for LLVM JIT

The large slowdown in codegen for MemorySanitizer instrumented Reactor
code only affects the LLVM backend, and the workaround of not performing
codegen optimizations is also LLVM specific. So only override the
optimization level for LLVM for MSan builds. Previously it was done in
Nucleus, but this abstract API affects other backends too, and this
depended on rr::Optimization::Level::None to correspond with
llvm::CodeGenOpt::None which might not always remain the case.

Bug: b/155148722
Change-Id: I2499935b1ddbd3cefd4b4497094c70ce284b170c
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/50488
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp
index a1cc35c..a742331 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -198,6 +198,14 @@
 
 llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level)
 {
+	// 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;
+	}
+
 	switch(level)
 	{
 		case rr::Optimization::Level::None: return llvm::CodeGenOpt::None;
@@ -206,6 +214,7 @@
 		case rr::Optimization::Level::Aggressive: return llvm::CodeGenOpt::Aggressive;
 		default: UNREACHABLE("Unknown Optimization Level %d", int(level));
 	}
+
 	return llvm::CodeGenOpt::Default;
 }
 
diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp
index b300d9b..4c08ddd 100644
--- a/src/Reactor/Nucleus.hpp
+++ b/src/Reactor/Nucleus.hpp
@@ -28,12 +28,6 @@
 #	undef None  // TODO(b/127920555)
 #endif
 
-// A Clang extension to determine compiler features.
-// We use it to detect Sanitizer builds (e.g. -fsanitize=memory).
-#ifndef __has_feature
-#	define __has_feature(x) 0
-#endif
-
 static_assert(sizeof(short) == 2, "Reactor's 'Short' type is 16-bit, and requires the C++ 'short' to match that.");
 static_assert(sizeof(int) == 4, "Reactor's 'Int' type is 32-bit, and requires the C++ 'int' to match that.");
 
@@ -85,14 +79,6 @@
 			this->level = Level::REACTOR_DEFAULT_OPT_LEVEL;
 		}
 #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))
-		{
-			this->level = Level::None;
-		}
 	}
 
 	Level getLevel() const { return level; }
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index 3e22ee9..05d5925 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -52,6 +52,12 @@
 }
 #endif
 
+// A Clang extension to determine compiler features.
+// We use it to detect Sanitizer builds (e.g. -fsanitize=memory).
+#ifndef __has_feature
+#	define __has_feature(x) 0
+#endif
+
 // Whether Reactor routine instrumentation is enabled for MSan builds.
 // TODO(b/155148722): Remove when unconditionally instrumenting for all build systems.
 #if !defined REACTOR_ENABLE_MEMORY_SANITIZER_INSTRUMENTATION