Reduce the mangler object lifetime

It's only used for mangling the names of our generated functions during
Routine construction, so don't hang on to it for the entire lifetime of
the routine.

Also, don't set Function attributes during routine compilation, set them
at creation.

Bug: b/177024837
Change-Id: I974b10430bced5d59f51e54426f7ee0aa8c96ca2
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51569
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 c7b5f30..e6e1839 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -626,7 +626,6 @@
 	llvm::orc::ExecutionSession session;
 	llvm::orc::RTDyldObjectLinkingLayer objectLayer;
 	llvm::orc::IRCompileLayer compileLayer;
-	llvm::orc::MangleAndInterner mangle;
 	llvm::orc::JITDylib &dylib;
 	std::vector<const void *> addresses;
 
@@ -644,7 +643,6 @@
 		    return std::make_unique<llvm::SectionMemoryManager>(&memoryMapper);
 	    })
 	    , compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel())))
-	    , mangle(session, JITGlobals::get()->getDataLayout())
 	    , dylib(Unwrap(session.createJITDylib("<routine>")))
 	    , addresses(count)
 	{
@@ -673,17 +671,19 @@
 
 		dylib.addGenerator(std::make_unique<ExternalSymbolGenerator>());
 
-		llvm::SmallVector<llvm::orc::SymbolStringPtr, 8> names(count);
+		llvm::SmallVector<llvm::orc::SymbolStringPtr, 8> functionNames(count);
+		llvm::orc::MangleAndInterner mangle(session, JITGlobals::get()->getDataLayout());
+
 		for(size_t i = 0; i < count; i++)
 		{
 			auto func = funcs[i];
-			func->setLinkage(llvm::GlobalValue::ExternalLinkage);
-			func->setDoesNotThrow();
+
 			if(!func->hasName())
 			{
 				func->setName("f" + llvm::Twine(i).str());
 			}
-			names[i] = mangle(func->getName());
+
+			functionNames[i] = mangle(func->getName());
 		}
 
 #ifdef ENABLE_RR_EMIT_ASM_FILE
@@ -701,7 +701,7 @@
 		// Resolve the function addresses.
 		for(size_t i = 0; i < count; i++)
 		{
-			auto symbol = session.lookup({ &dylib }, names[i]);
+			auto symbol = session.lookup({ &dylib }, functionNames[i]);
 			ASSERT_MSG(symbol, "Failed to lookup address of routine function %d: %s",
 			           (int)i, llvm::toString(symbol.takeError()).c_str());
 			addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(symbol->getAddress()));
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index 2fb46c1..7d49146 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -488,8 +488,11 @@
 {
 	llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false);
 	auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, jit->module.get());
+
+	func->setLinkage(llvm::GlobalValue::ExternalLinkage);
 	func->setDoesNotThrow();
 	func->setCallingConv(llvm::CallingConv::C);
+
 	if(__has_feature(memory_sanitizer))
 	{
 		func->addFnAttr(llvm::Attribute::SanitizeMemory);