Work around stack probe issue.

LLVM fails to resolve the __chkstck function for probing the stack when
it's larger than 4 kB. We don't need it for SwiftShader because newly
created threads have 1 MB of committed stack memory.

Bug b/115344057
Bug swiftshader:25

Change-Id: Ic6097376052b07ed6af950dfdac8499b5fc83053
Reviewed-on: https://swiftshader-review.googlesource.com/20688
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index ba4aeb9..e033a18 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -919,6 +919,17 @@
 		::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "", ::module);
 		::function->setCallingConv(llvm::CallingConv::C);
 
+		#if defined(_WIN32) && SWIFTSHADER_LLVM_VERSION >= 7
+			// FIXME(capn):
+			// On Windows, stack memory is committed in increments of 4 kB pages, with the last page
+			// having a trap which allows the OS to grow the stack. For functions with a stack frame
+			// larger than 4 kB this can cause an issue when a variable is accessed beyond the guard
+			// page. Therefore the compiler emits a call to __chkstk in the function prolog to probe
+			// the stack and ensure all pages have been committed. This is currently broken in LLVM
+			// JIT, but we can prevent emitting the stack probe call:
+			::function->addFnAttr("stack-probe-size", "1048576");
+		#endif
+
 		::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function));
 	}