Set the stack limit through a function attribute

Recent versions of LLVM control the stack size limit through a function
attribute.

Note that older versions of LLVM simply ignore this attribute so we
don't have to set it conditionally. The -warn-stack-size command line
option can be removed though once we use LLVM 13+ everywhere.

Bug: b/191193823
Bug: b/165000222
Change-Id: Ie5d18031c5cd13e1fb2703110974e4cf59458077
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/63410
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp
index a1dac6c..d0061e6 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -152,14 +152,6 @@
 #endif
 #if LLVM_VERSION_MAJOR <= 12
 			"-warn-stack-size=524288"  // Warn when a function uses more than 512 KiB of stack memory
-#else
-		// TODO(b/191193823): TODO(ndesaulniers): Update this after
-		// go/compilers/fc018ebb608ee0c1239b405460e49f1835ab6175
-#	if LLVM_VERSION_MAJOR <= 15
-		// Resolve TODO
-#	elif LLVM_VERSION_MAJOR < 9999
-#		warning Implement stack size checks using the "warn-stack-size" function attribute.
-#	endif
 #endif
 		};
 
@@ -744,7 +736,7 @@
 
 		for(size_t i = 0; i < count; i++)
 		{
-			auto func = funcs[i];
+			llvm::Function *func = funcs[i];
 
 			if(!func->hasName())
 			{
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index 6273115..74a83a3 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -524,6 +524,8 @@
 		func->addFnAttr(llvm::Attribute::SanitizeMemory);
 	}
 
+	func->addFnAttr("warn-stack-size", "524288");  // Warn when a function uses more than 512 KiB of stack memory
+
 	return func;
 }