Commit d6c61361 by Ben Clayton

Vulkan: Build a yarn::Scheduler and pass it down to the vk::Queue.

Nothing is actually scheduled, but is a first step towards using yarn. As yarn is now actually used by SwiftShader, drop the BUILD_YARN build flags, as failing to build yarn would result in linker errors. Bug: b/139142453 Change-Id: Ibd9a69f72a248f58a62bb41eeb196c4647876e82 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/35153Tested-by: 's avatarBen Clayton <bclayton@google.com> Reviewed-by: 's avatarChris Forbes <chrisforbes@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
parent 1943a296
...@@ -79,8 +79,6 @@ if(NOT CMAKE_BUILD_TYPE) ...@@ -79,8 +79,6 @@ if(NOT CMAKE_BUILD_TYPE)
endif() endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo)
option(BUILD_YARN "Build yarn" 0)
option(BUILD_EGL "Build the EGL library" 1) option(BUILD_EGL "Build the EGL library" 1)
option(BUILD_GLESv2 "Build the OpenGL ES 2 library" 1) option(BUILD_GLESv2 "Build the OpenGL ES 2 library" 1)
option(BUILD_GLES_CM "Build the OpenGL ES 1.1 library" 1) option(BUILD_GLES_CM "Build the OpenGL ES 1.1 library" 1)
...@@ -1754,23 +1752,21 @@ file(GLOB_RECURSE VULKAN_LIST ...@@ -1754,23 +1752,21 @@ file(GLOB_RECURSE VULKAN_LIST
${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan/*.h} ${CMAKE_CURRENT_SOURCE_DIR}/include/vulkan/*.h}
) )
if(BUILD_YARN) file(GLOB YARN_FULL_LIST
file(GLOB YARN_FULL_LIST
${YARN_DIR}/*.cpp ${YARN_DIR}/*.cpp
${YARN_DIR}/*.hpp ${YARN_DIR}/*.hpp
${YARN_DIR}/*.c ${YARN_DIR}/*.c
) )
if (NOT MSVC) if (NOT MSVC)
file(GLOB YARN_ASSEMBLY_LIST ${YARN_DIR}/*.S) file(GLOB YARN_ASSEMBLY_LIST ${YARN_DIR}/*.S)
list(APPEND YARN_FULL_LIST ${YARN_ASSEMBLY_LIST}) list(APPEND YARN_FULL_LIST ${YARN_ASSEMBLY_LIST})
endif(NOT MSVC) endif(NOT MSVC)
set(YARN_LIST ${YARN_FULL_LIST}) set(YARN_LIST ${YARN_FULL_LIST})
set(YARN_TEST_LIST ${YARN_FULL_LIST}) set(YARN_TEST_LIST ${YARN_FULL_LIST})
list(FILTER YARN_LIST EXCLUDE REGEX ".*_test\\..*") list(FILTER YARN_LIST EXCLUDE REGEX ".*_test\\..*")
list(FILTER YARN_TEST_LIST INCLUDE REGEX ".*_test\\..*") list(FILTER YARN_TEST_LIST INCLUDE REGEX ".*_test\\..*")
endif(BUILD_YARN)
########################################################### ###########################################################
# Append OS specific files to lists # Append OS specific files to lists
...@@ -1860,16 +1856,14 @@ endif() ...@@ -1860,16 +1856,14 @@ endif()
# SwiftShader Targets # SwiftShader Targets
########################################################### ###########################################################
if(BUILD_YARN) add_library(Yarn STATIC ${YARN_LIST})
add_library(Yarn STATIC ${YARN_LIST}) set_target_properties(Yarn PROPERTIES
set_target_properties(Yarn PROPERTIES
POSITION_INDEPENDENT_CODE 1 POSITION_INDEPENDENT_CODE 1
FOLDER "Core" FOLDER "Core"
COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS}" COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS}"
COMPILE_DEFINITIONS "NO_SANITIZE_FUNCTION=;" COMPILE_DEFINITIONS "NO_SANITIZE_FUNCTION=;"
) )
target_link_libraries(Yarn ${OS_LIBS}) target_link_libraries(Yarn ${OS_LIBS})
endif(BUILD_YARN)
add_library(SwiftShader STATIC ${SWIFTSHADER_LIST}) add_library(SwiftShader STATIC ${SWIFTSHADER_LIST})
set_target_properties(SwiftShader PROPERTIES set_target_properties(SwiftShader PROPERTIES
...@@ -2046,7 +2040,7 @@ if(BUILD_VULKAN) ...@@ -2046,7 +2040,7 @@ if(BUILD_VULKAN)
endif() endif()
set_shared_library_export_map(libvk_swiftshader ${SOURCE_DIR}/Vulkan) set_shared_library_export_map(libvk_swiftshader ${SOURCE_DIR}/Vulkan)
target_link_libraries(libvk_swiftshader ${Reactor} ${OS_LIBS} SPIRV-Tools SPIRV-Tools-opt) target_link_libraries(libvk_swiftshader ${Reactor} Yarn ${OS_LIBS} SPIRV-Tools SPIRV-Tools-opt)
add_custom_command( add_custom_command(
TARGET libvk_swiftshader TARGET libvk_swiftshader
POST_BUILD POST_BUILD
...@@ -2167,7 +2161,8 @@ if(BUILD_TESTS) ...@@ -2167,7 +2161,8 @@ if(BUILD_TESTS)
endif() endif()
endif(BUILD_TESTS) endif(BUILD_TESTS)
if(BUILD_TESTS AND BUILD_YARN) if(BUILD_TESTS)
# Yarn unit tests
file(GLOB YARN_TEST_LIST file(GLOB YARN_TEST_LIST
${YARN_DIR}/*_test.cpp ${YARN_DIR}/*_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googletest/src/gtest-all.cc ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/googletest/src/gtest-all.cc
...@@ -2189,9 +2184,8 @@ if(BUILD_TESTS AND BUILD_YARN) ...@@ -2189,9 +2184,8 @@ if(BUILD_TESTS AND BUILD_YARN)
) )
target_link_libraries(yarn-unittests Yarn ${OS_LIBS}) target_link_libraries(yarn-unittests Yarn ${OS_LIBS})
endif(BUILD_TESTS AND BUILD_YARN)
if(BUILD_TESTS) # Math unit tests
set(MATH_UNITTESTS_LIST set(MATH_UNITTESTS_LIST
${CMAKE_CURRENT_SOURCE_DIR}/tests/MathUnitTests/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/MathUnitTests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/MathUnitTests/unittests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/MathUnitTests/unittests.cpp
......
...@@ -57,7 +57,7 @@ void Device::SamplingRoutineCache::updateConstCache() ...@@ -57,7 +57,7 @@ void Device::SamplingRoutineCache::updateConstCache()
cache.updateConstCache(); cache.updateConstCache();
} }
Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures) Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, yarn::Scheduler *scheduler)
: physicalDevice(physicalDevice), : physicalDevice(physicalDevice),
queues(reinterpret_cast<Queue*>(mem)), queues(reinterpret_cast<Queue*>(mem)),
enabledExtensionCount(pCreateInfo->enabledExtensionCount), enabledExtensionCount(pCreateInfo->enabledExtensionCount),
...@@ -76,7 +76,7 @@ Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice ...@@ -76,7 +76,7 @@ Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice
for(uint32_t j = 0; j < queueCreateInfo.queueCount; j++, queueID++) for(uint32_t j = 0; j < queueCreateInfo.queueCount; j++, queueID++)
{ {
new (&queues[queueID]) Queue(this); new (&queues[queueID]) Queue(this, scheduler);
} }
} }
......
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
namespace yarn
{
class Scheduler;
}
namespace sw namespace sw
{ {
class Blitter; class Blitter;
...@@ -37,7 +42,7 @@ class Device ...@@ -37,7 +42,7 @@ class Device
public: public:
static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; } static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; }
Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures); Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, yarn::Scheduler *scheduler);
void destroy(const VkAllocationCallbacks* pAllocator); void destroy(const VkAllocationCallbacks* pAllocator);
static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo* pCreateInfo); static size_t ComputeRequiredAllocationSize(const VkDeviceCreateInfo* pCreateInfo);
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "Pipeline/ComputeProgram.hpp" #include "Pipeline/ComputeProgram.hpp"
#include "Pipeline/SpirvShader.hpp" #include "Pipeline/SpirvShader.hpp"
#include "Yarn/Trace.hpp"
#include "spirv-tools/optimizer.hpp" #include "spirv-tools/optimizer.hpp"
#include <iostream> #include <iostream>
...@@ -257,6 +259,8 @@ std::shared_ptr<sw::SpirvShader> createShader(const vk::PipelineCache::SpirvShad ...@@ -257,6 +259,8 @@ std::shared_ptr<sw::SpirvShader> createShader(const vk::PipelineCache::SpirvShad
std::shared_ptr<sw::ComputeProgram> createProgram(const vk::PipelineCache::ComputeProgramKey& key) std::shared_ptr<sw::ComputeProgram> createProgram(const vk::PipelineCache::ComputeProgramKey& key)
{ {
YARN_SCOPED_EVENT("createProgram");
vk::DescriptorSet::Bindings descriptorSets; // FIXME(b/129523279): Delay code generation until invoke time. vk::DescriptorSet::Bindings descriptorSets; // FIXME(b/129523279): Delay code generation until invoke time.
// TODO(b/119409619): use allocator. // TODO(b/119409619): use allocator.
auto program = std::make_shared<sw::ComputeProgram>(key.getShader(), key.getLayout(), descriptorSets); auto program = std::make_shared<sw::ComputeProgram>(key.getShader(), key.getLayout(), descriptorSets);
......
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
#include "WSI/VkSwapchainKHR.hpp" #include "WSI/VkSwapchainKHR.hpp"
#include "Device/Renderer.hpp" #include "Device/Renderer.hpp"
#include "Yarn/Defer.hpp"
#include "Yarn/Scheduler.hpp"
#include "Yarn/Thread.hpp"
#include "Yarn/Trace.hpp"
#include <cstring> #include <cstring>
namespace namespace
...@@ -74,9 +79,9 @@ VkSubmitInfo* DeepCopySubmitInfo(uint32_t submitCount, const VkSubmitInfo* pSubm ...@@ -74,9 +79,9 @@ VkSubmitInfo* DeepCopySubmitInfo(uint32_t submitCount, const VkSubmitInfo* pSubm
namespace vk namespace vk
{ {
Queue::Queue(Device* device) : device(device) Queue::Queue(Device* device, yarn::Scheduler *scheduler) : device(device)
{ {
queueThread = std::thread(&Queue::taskLoop, this); queueThread = std::thread(&Queue::taskLoop, this, scheduler);
} }
Queue::~Queue() Queue::~Queue()
...@@ -155,8 +160,12 @@ void Queue::submitQueue(const Task& task) ...@@ -155,8 +160,12 @@ void Queue::submitQueue(const Task& task)
} }
} }
void Queue::taskLoop() void Queue::taskLoop(yarn::Scheduler* scheduler)
{ {
yarn::Thread::setName("Queue<%p>", this);
scheduler->bind();
defer(scheduler->unbind());
while(true) while(true)
{ {
Task task = pending.take(); Task task = pending.take();
......
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
#include "System/Synchronization.hpp" #include "System/Synchronization.hpp"
namespace yarn
{
class Scheduler;
}
namespace sw namespace sw
{ {
class Context; class Context;
...@@ -39,7 +44,7 @@ class Queue ...@@ -39,7 +44,7 @@ class Queue
VK_LOADER_DATA loaderData = { ICD_LOADER_MAGIC }; VK_LOADER_DATA loaderData = { ICD_LOADER_MAGIC };
public: public:
Queue(Device* device); Queue(Device* device, yarn::Scheduler *scheduler);
~Queue(); ~Queue();
operator VkQueue() operator VkQueue()
...@@ -64,8 +69,7 @@ private: ...@@ -64,8 +69,7 @@ private:
Type type = SUBMIT_QUEUE; Type type = SUBMIT_QUEUE;
}; };
static void TaskLoop(vk::Queue* queue); void taskLoop(yarn::Scheduler* scheduler);
void taskLoop();
void garbageCollect(); void garbageCollect();
void submitQueue(const Task& task); void submitQueue(const Task& task);
......
...@@ -64,6 +64,11 @@ ...@@ -64,6 +64,11 @@
#include "Reactor/Nucleus.hpp" #include "Reactor/Nucleus.hpp"
#include "Yarn/Scheduler.hpp"
#include "Yarn/Thread.hpp"
#include "System/CPUID.hpp"
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <string> #include <string>
...@@ -102,12 +107,33 @@ void setReactorDefaultConfig() ...@@ -102,12 +107,33 @@ void setReactorDefaultConfig()
rr::Nucleus::adjustDefaultConfig(cfg); rr::Nucleus::adjustDefaultConfig(cfg);
} }
void setCPUDefaults()
{
sw::CPUID::setEnableSSE4_1(true);
sw::CPUID::setEnableSSSE3(true);
sw::CPUID::setEnableSSE3(true);
sw::CPUID::setEnableSSE2(true);
sw::CPUID::setEnableSSE(true);
}
yarn::Scheduler* getOrCreateScheduler()
{
static auto scheduler = std::unique_ptr<yarn::Scheduler>(new yarn::Scheduler());
scheduler->setThreadInitializer([] {
sw::CPUID::setFlushToZero(true);
sw::CPUID::setDenormalsAreZero(true);
});
scheduler->setWorkerThreadCount(std::min<size_t>(yarn::Thread::numLogicalCPUs(), 16));
return scheduler.get();
}
// initializeLibrary() is called by vkCreateInstance() to perform one-off global // initializeLibrary() is called by vkCreateInstance() to perform one-off global
// initialization of the swiftshader driver. // initialization of the swiftshader driver.
void initializeLibrary() void initializeLibrary()
{ {
static bool doOnce = [] { static bool doOnce = [] {
setReactorDefaultConfig(); setReactorDefaultConfig();
setCPUDefaults();
return true; return true;
}(); }();
(void)doOnce; (void)doOnce;
...@@ -563,7 +589,8 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, c ...@@ -563,7 +589,8 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, c
(void)queueFamilyPropertyCount; // Silence unused variable warning (void)queueFamilyPropertyCount; // Silence unused variable warning
} }
return vk::DispatchableDevice::Create(pAllocator, pCreateInfo, pDevice, vk::Cast(physicalDevice), enabledFeatures); auto scheduler = getOrCreateScheduler();
return vk::DispatchableDevice::Create(pAllocator, pCreateInfo, pDevice, vk::Cast(physicalDevice), enabledFeatures, scheduler);
} }
VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
......
...@@ -15,7 +15,7 @@ if [[ -z "${REACTOR_BACKEND}" ]]; then ...@@ -15,7 +15,7 @@ if [[ -z "${REACTOR_BACKEND}" ]]; then
REACTOR_BACKEND="LLVM" REACTOR_BACKEND="LLVM"
fi fi
cmake .. "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DREACTOR_VERIFY_LLVM_IR=1" "-DBUILD_YARN=1" cmake .. "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DREACTOR_VERIFY_LLVM_IR=1"
make --jobs=$(nproc) make --jobs=$(nproc)
# Run unit tests # Run unit tests
......
...@@ -17,7 +17,7 @@ SET CONFIG=Debug ...@@ -17,7 +17,7 @@ SET CONFIG=Debug
cd %SRC%\build cd %SRC%\build
if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL! if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
cmake .. -G "Visual Studio 15 2017 Win64" -Thost=x64 "-DREACTOR_BACKEND=%REACTOR_BACKEND%" "-DREACTOR_VERIFY_LLVM_IR=1" "-DBUILD_YARN=1" cmake .. -G "Visual Studio 15 2017 Win64" -Thost=x64 "-DREACTOR_BACKEND=%REACTOR_BACKEND%" "-DREACTOR_VERIFY_LLVM_IR=1"
if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL! if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
%MSBUILD% /p:Configuration=%CONFIG% SwiftShader.sln %MSBUILD% /p:Configuration=%CONFIG% SwiftShader.sln
......
...@@ -15,7 +15,7 @@ if [[ -z "${REACTOR_BACKEND}" ]]; then ...@@ -15,7 +15,7 @@ if [[ -z "${REACTOR_BACKEND}" ]]; then
REACTOR_BACKEND="LLVM" REACTOR_BACKEND="LLVM"
fi fi
cmake .. "-DASAN=ON" "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DCMAKE_BUILD_TYPE=RelWithDebInfo" "-DREACTOR_VERIFY_LLVM_IR=1" "-DBUILD_YARN=1" cmake .. "-DASAN=ON" "-DREACTOR_BACKEND=${REACTOR_BACKEND}" "-DCMAKE_BUILD_TYPE=RelWithDebInfo" "-DREACTOR_VERIFY_LLVM_IR=1"
make -j$(sysctl -n hw.logicalcpu) make -j$(sysctl -n hw.logicalcpu)
# Run unit tests # Run unit tests
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment