CMake: Add build rules for google benchmark.

Add a single Reactor Coroutine benchmark so there's something to compile.

Bug: b/148660286
Change-Id: Icbc4c44dd5c0f1ab495c3b8eddce6ea90199e987
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/40809
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 73cf966..caf9097 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -110,6 +110,7 @@
 
 option_if_not_defined(SWIFTSHADER_BUILD_SAMPLES "Build sample programs" 1)
 option_if_not_defined(SWIFTSHADER_BUILD_TESTS "Build test programs" 1)
+option_if_not_defined(SWIFTSHADER_BUILD_BENCHMARKS "Build benchmarks" 0)
 
 option_if_not_defined(SWIFTSHADER_MSAN "Build with memory sanitizer" 0)
 option_if_not_defined(SWIFTSHADER_ASAN "Build with address sanitizer" 0)
@@ -160,17 +161,32 @@
 # Initialize submodules
 ###########################################################
 
-if (NOT TARGET gtest)
-    if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/.git)
-        message(WARNING "
-      third_party/googletest submodule missing.
-      Running 'git submodule update --init' to download it:
-        ")
+if (SWIFTSHADER_BUILD_TESTS)
+    if (NOT TARGET gtest)
+        if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/.git)
+            message(WARNING "
+        third_party/googletest submodule missing.
+        Running 'git submodule update --init' to download it:
+            ")
 
-        execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest)
+            execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest)
+        endif()
     endif()
 endif()
 
+if(SWIFTSHADER_BUILD_BENCHMARKS)
+    if (NOT TARGET benchmark::benchmark)
+        if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark/.git)
+            message(WARNING "
+        third_party/benchmark submodule missing.
+        Running 'git submodule update --init' to download it:
+            ")
+
+            execute_process(COMMAND git submodule update --init ${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark)
+        endif()
+    endif()
+endif(SWIFTSHADER_BUILD_BENCHMARKS)
+
 if (NOT TARGET libbacktrace)
     if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libbacktrace/src/.git)
         message(WARNING "
@@ -2302,6 +2318,25 @@
     endif()
 endif(SWIFTSHADER_BUILD_TESTS)
 
+if(SWIFTSHADER_BUILD_BENCHMARKS)
+    if (NOT TARGET benchmark::benchmark)
+        set(BENCHMARK_ENABLE_TESTING FALSE CACHE BOOL FALSE FORCE)
+        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/benchmark)
+    endif()
+
+    set(REACTOR_BENCHMARK_LIST
+        ${SOURCE_DIR}/Reactor/ReactorBenchmarks.cpp
+    )
+
+    add_executable(ReactorBenchmarks ${REACTOR_BENCHMARK_LIST})
+    target_link_libraries(ReactorBenchmarks benchmark::benchmark ${Reactor})
+
+    set_target_properties(ReactorBenchmarks PROPERTIES
+        COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS}"
+        FOLDER "Benchmarks"
+    )
+endif(SWIFTSHADER_BUILD_BENCHMARKS)
+
 if(SWIFTSHADER_BUILD_TESTS AND SWIFTSHADER_BUILD_VULKAN)
     set(VK_UNITTESTS_LIST
         ${CMAKE_CURRENT_SOURCE_DIR}/tests/VulkanUnitTests/Device.cpp
diff --git a/src/Reactor/ReactorBenchmarks.cpp b/src/Reactor/ReactorBenchmarks.cpp
new file mode 100644
index 0000000..613ffde
--- /dev/null
+++ b/src/Reactor/ReactorBenchmarks.cpp
@@ -0,0 +1,63 @@
+// Copyright 2020 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Coroutine.hpp"
+#include "Reactor.hpp"
+
+#include "benchmark/benchmark.h"
+
+BENCHMARK_MAIN();
+
+class Coroutines : public benchmark::Fixture
+{
+public:
+	void SetUp(const ::benchmark::State &state) {}
+
+	void TearDown(const ::benchmark::State &state) {}
+};
+
+BENCHMARK_F(Coroutines, Fibonacci)
+(benchmark::State &state)
+{
+	using namespace rr;
+
+	if(!Caps.CoroutinesSupported)
+	{
+		state.SkipWithError("Coroutines are not supported");
+		return;
+	}
+
+	Coroutine<int()> function;
+	{
+		Yield(Int(0));
+		Yield(Int(1));
+		Int current = 1;
+		Int next = 1;
+		While(true)
+		{
+			Yield(next);
+			auto tmp = current + next;
+			current = next;
+			next = tmp;
+		}
+	}
+
+	auto coroutine = function();
+
+	int out = 0;
+	for(auto _ : state)
+	{
+		coroutine->await(out);
+	}
+}