Commit daa5d910 by Nicolas Capens

Abstract the Routine class.

Bug swiftshader:10 Change-Id: I29b1de8c1adb67449a380c307d12e2aea21f32cc Reviewed-on: https://swiftshader-review.googlesource.com/7251Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent aff3ad41
# Ignored folders # # Ignored folders #
/lib/ /lib/
/obj/ /obj/
/bin/ /bin/
.vs
# Ignored files #
*.obj # Ignored files #
*.lib *.obj
*.log *.lib
*.tlog *.log
*.exe *.tlog
*.ilk *.exe
*.pdb *.ilk
*.sbr *.pdb
*.bsc *.sbr
*.dll *.bsc
*.res *.dll
*.idb *.res
*.sdf *.idb
*.suo *.sdf
*.o *.suo
*.depend *.o
*.layout *.depend
*.opensdf *.layout
*.aps *.opensdf
*.aps
*.opendb
*.db
...@@ -605,8 +605,10 @@ set(REACTOR_LIST ...@@ -605,8 +605,10 @@ set(REACTOR_LIST
${SOURCE_DIR}/Reactor/Nucleus.hpp ${SOURCE_DIR}/Reactor/Nucleus.hpp
${SOURCE_DIR}/Reactor/Routine.cpp ${SOURCE_DIR}/Reactor/Routine.cpp
${SOURCE_DIR}/Reactor/Routine.hpp ${SOURCE_DIR}/Reactor/Routine.hpp
${SOURCE_DIR}/Reactor/RoutineManager.cpp ${SOURCE_DIR}/Reactor/LLVMRoutine.cpp
${SOURCE_DIR}/Reactor/RoutineManager.hpp ${SOURCE_DIR}/Reactor/LLVMRoutine.hpp
${SOURCE_DIR}/Reactor/LLVMRoutineManager.cpp
${SOURCE_DIR}/Reactor/LLVMRoutineManager.hpp
) )
file(GLOB_RECURSE EGL_LIST file(GLOB_RECURSE EGL_LIST
......
...@@ -38,7 +38,8 @@ COMMON_SRC_FILES += \ ...@@ -38,7 +38,8 @@ COMMON_SRC_FILES += \
COMMON_SRC_FILES += \ COMMON_SRC_FILES += \
Reactor/Nucleus.cpp \ Reactor/Nucleus.cpp \
Reactor/Routine.cpp \ Reactor/Routine.cpp \
Reactor/RoutineManager.cpp Reactor/LLVMRoutine.cpp \
Reactor/LLVMRoutineManager.cpp
COMMON_SRC_FILES += \ COMMON_SRC_FILES += \
Renderer/Blitter.cpp \ Renderer/Blitter.cpp \
......
...@@ -178,8 +178,10 @@ ...@@ -178,8 +178,10 @@
<Unit filename="../../Reactor/Reactor.hpp" /> <Unit filename="../../Reactor/Reactor.hpp" />
<Unit filename="../../Reactor/Routine.cpp" /> <Unit filename="../../Reactor/Routine.cpp" />
<Unit filename="../../Reactor/Routine.hpp" /> <Unit filename="../../Reactor/Routine.hpp" />
<Unit filename="../../Reactor/RoutineManager.cpp" /> <Unit filename="../../Reactor/LLVMRoutine.cpp" />
<Unit filename="../../Reactor/RoutineManager.hpp" /> <Unit filename="../../Reactor/LLVMRoutine.hpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
<Unit filename="../../Reactor/x86.hpp" /> <Unit filename="../../Reactor/x86.hpp" />
<Unit filename="../../Renderer/Blitter.cpp" /> <Unit filename="../../Renderer/Blitter.cpp" />
<Unit filename="../../Renderer/Blitter.hpp" /> <Unit filename="../../Renderer/Blitter.hpp" />
......
...@@ -177,8 +177,10 @@ ...@@ -177,8 +177,10 @@
<Unit filename="../../Reactor/Reactor.hpp" /> <Unit filename="../../Reactor/Reactor.hpp" />
<Unit filename="../../Reactor/Routine.cpp" /> <Unit filename="../../Reactor/Routine.cpp" />
<Unit filename="../../Reactor/Routine.hpp" /> <Unit filename="../../Reactor/Routine.hpp" />
<Unit filename="../../Reactor/RoutineManager.cpp" /> <Unit filename="../../Reactor/LLVMRoutine.cpp" />
<Unit filename="../../Reactor/RoutineManager.hpp" /> <Unit filename="../../Reactor/LLVMRoutine.hpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
<Unit filename="../../Reactor/x86.hpp" /> <Unit filename="../../Reactor/x86.hpp" />
<Unit filename="../../Renderer/Blitter.cpp" /> <Unit filename="../../Renderer/Blitter.cpp" />
<Unit filename="../../Renderer/Blitter.hpp" /> <Unit filename="../../Renderer/Blitter.hpp" />
......
...@@ -43,7 +43,8 @@ source_set("swiftshader_reactor") { ...@@ -43,7 +43,8 @@ source_set("swiftshader_reactor") {
sources = [ sources = [
"Nucleus.cpp", "Nucleus.cpp",
"Routine.cpp", "Routine.cpp",
"RoutineManager.cpp", "LLVMRoutine.cpp",
"LLVMRoutineManager.cpp",
] ]
if (is_win) { if (is_win) {
......
// Copyright 2016 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 "LLVMRoutine.hpp"
#include "../Common/Memory.hpp"
#include "../Common/Thread.hpp"
#include "../Common/Types.hpp"
namespace sw
{
LLVMRoutine::LLVMRoutine(int bufferSize) : bufferSize(bufferSize)
{
void *memory = allocateExecutable(bufferSize);
buffer = memory;
entry = memory;
functionSize = bufferSize; // Updated by LLVMRoutineManager::endFunctionBody
}
LLVMRoutine::~LLVMRoutine()
{
deallocateExecutable(buffer, bufferSize);
}
const void *LLVMRoutine::getEntry()
{
return entry;
}
int LLVMRoutine::getCodeSize()
{
return functionSize - static_cast<int>((uintptr_t)entry - (uintptr_t)buffer);
}
}
// Copyright 2016 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.
#ifndef sw_LLVMRoutine_hpp
#define sw_LLVMRoutine_hpp
#include "Routine.hpp"
namespace sw
{
class LLVMRoutineManager;
class LLVMRoutine : public Routine
{
friend class LLVMRoutineManager;
public:
LLVMRoutine(int bufferSize);
//LLVMRoutine(void *memory, int bufferSize, int offset);
virtual ~LLVMRoutine();
//void setFunctionSize(int functionSize);
//const void *getBuffer();
const void *getEntry();
//int getBufferSize();
//int getFunctionSize(); // Includes constants before the entry point
int getCodeSize(); // Executable code only
//bool isDynamic();
private:
void *buffer;
const void *entry;
int bufferSize;
int functionSize;
//const bool dynamic; // Generated or precompiled
};
}
#endif // sw_LLVMRoutine_hpp
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "RoutineManager.hpp" #include "LLVMRoutineManager.hpp"
#include "Routine.hpp" #include "LLVMRoutine.hpp"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "../Common/Memory.hpp" #include "../Common/Memory.hpp"
#include "../Common/Thread.hpp" #include "../Common/Thread.hpp"
...@@ -24,30 +24,30 @@ namespace sw ...@@ -24,30 +24,30 @@ namespace sw
{ {
using namespace llvm; using namespace llvm;
volatile int RoutineManager::averageInstructionSize = 4; volatile int LLVMRoutineManager::averageInstructionSize = 4;
RoutineManager::RoutineManager() LLVMRoutineManager::LLVMRoutineManager()
{ {
routine = 0; routine = nullptr;
} }
RoutineManager::~RoutineManager() LLVMRoutineManager::~LLVMRoutineManager()
{ {
delete routine; delete routine;
} }
void RoutineManager::AllocateGOT() void LLVMRoutineManager::AllocateGOT()
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
uint8_t *RoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment) uint8_t *LLVMRoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
return 0; return nullptr;
} }
uint8_t *RoutineManager::startFunctionBody(const llvm::Function *function, uintptr_t &actualSize) uint8_t *LLVMRoutineManager::startFunctionBody(const llvm::Function *function, uintptr_t &actualSize)
{ {
if(actualSize == 0) // Estimate size if(actualSize == 0) // Estimate size
{ {
...@@ -69,52 +69,52 @@ namespace sw ...@@ -69,52 +69,52 @@ namespace sw
actualSize = (actualSize + pageSize - 1) & ~(pageSize - 1); actualSize = (actualSize + pageSize - 1) & ~(pageSize - 1);
delete routine; delete routine;
routine = new Routine(static_cast<int>(actualSize)); routine = new LLVMRoutine(static_cast<int>(actualSize));
return (uint8_t*)routine->getBuffer(); return (uint8_t*)routine->buffer;
} }
void RoutineManager::endFunctionBody(const llvm::Function *function, uint8_t *functionStart, uint8_t *functionEnd) void LLVMRoutineManager::endFunctionBody(const llvm::Function *function, uint8_t *functionStart, uint8_t *functionEnd)
{ {
routine->setFunctionSize(static_cast<int>(static_cast<ptrdiff_t>(functionEnd - functionStart))); routine->functionSize = static_cast<int>(static_cast<ptrdiff_t>(functionEnd - functionStart));
} }
uint8_t *RoutineManager::startExceptionTable(const llvm::Function* F, uintptr_t &ActualSize) uint8_t *LLVMRoutineManager::startExceptionTable(const llvm::Function* F, uintptr_t &ActualSize)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
return 0; return nullptr;
} }
void RoutineManager::endExceptionTable(const llvm::Function *F, uint8_t *TableStart, uint8_t *TableEnd, uint8_t* FrameRegister) void LLVMRoutineManager::endExceptionTable(const llvm::Function *F, uint8_t *TableStart, uint8_t *TableEnd, uint8_t* FrameRegister)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
uint8_t *RoutineManager::getGOTBase() const uint8_t *LLVMRoutineManager::getGOTBase() const
{ {
ASSERT(!HasGOT); ASSERT(!HasGOT);
return 0; return nullptr;
} }
uint8_t *RoutineManager::allocateSpace(intptr_t Size, unsigned Alignment) uint8_t *LLVMRoutineManager::allocateSpace(intptr_t Size, unsigned Alignment)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
return 0; return nullptr;
} }
uint8_t *RoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment) uint8_t *LLVMRoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
return 0; return nullptr;
} }
void RoutineManager::deallocateFunctionBody(void *Body) void LLVMRoutineManager::deallocateFunctionBody(void *Body)
{ {
delete routine; delete routine;
routine = 0; routine = nullptr;
} }
void RoutineManager::deallocateExceptionTable(void *ET) void LLVMRoutineManager::deallocateExceptionTable(void *ET)
{ {
if(ET) if(ET)
{ {
...@@ -122,26 +122,26 @@ namespace sw ...@@ -122,26 +122,26 @@ namespace sw
} }
} }
void RoutineManager::setMemoryWritable() void LLVMRoutineManager::setMemoryWritable()
{ {
} }
void RoutineManager::setMemoryExecutable() void LLVMRoutineManager::setMemoryExecutable()
{ {
markExecutable(routine->buffer, routine->bufferSize); markExecutable(routine->buffer, routine->bufferSize);
} }
void RoutineManager::setPoisonMemory(bool poison) void LLVMRoutineManager::setPoisonMemory(bool poison)
{ {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
Routine *RoutineManager::acquireRoutine(void *entry) LLVMRoutine *LLVMRoutineManager::acquireRoutine(void *entry)
{ {
routine->entry = entry; routine->entry = entry;
Routine *result = routine; LLVMRoutine *result = routine;
routine = 0; routine = nullptr;
return result; return result;
} }
......
...@@ -12,22 +12,22 @@ ...@@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#ifndef sw_RoutineManager_hpp #ifndef sw_LLVMRoutineManager_hpp
#define sw_RoutineManager_hpp #define sw_LLVMRoutineManager_hpp
#include "llvm/GlobalValue.h" #include "llvm/GlobalValue.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h" #include "llvm/ExecutionEngine/JITMemoryManager.h"
namespace sw namespace sw
{ {
class Routine; class LLVMRoutine;
class RoutineManager : public llvm::JITMemoryManager class LLVMRoutineManager : public llvm::JITMemoryManager
{ {
public: public:
RoutineManager(); LLVMRoutineManager();
virtual ~RoutineManager(); virtual ~LLVMRoutineManager();
virtual void AllocateGOT(); virtual void AllocateGOT();
...@@ -45,13 +45,13 @@ namespace sw ...@@ -45,13 +45,13 @@ namespace sw
virtual void setMemoryExecutable(); virtual void setMemoryExecutable();
virtual void setPoisonMemory(bool poison); virtual void setPoisonMemory(bool poison);
Routine *acquireRoutine(void *entry); LLVMRoutine *acquireRoutine(void *entry);
private: private:
Routine *routine; LLVMRoutine *routine;
static volatile int averageInstructionSize; static volatile int averageInstructionSize;
}; };
} }
#endif // sw_RoutineManager_hpp #endif // sw_LLVMRoutineManager_hpp
...@@ -29,8 +29,8 @@ ...@@ -29,8 +29,8 @@
#include "llvm/Support/TargetSelect.h" #include "llvm/Support/TargetSelect.h"
#include "../lib/ExecutionEngine/JIT/JIT.h" #include "../lib/ExecutionEngine/JIT/JIT.h"
#include "Routine.hpp" #include "LLVMRoutine.hpp"
#include "RoutineManager.hpp" #include "LLVMRoutineManager.hpp"
#include "x86.hpp" #include "x86.hpp"
#include "CPUID.hpp" #include "CPUID.hpp"
#include "Thread.hpp" #include "Thread.hpp"
...@@ -60,7 +60,7 @@ namespace llvm ...@@ -60,7 +60,7 @@ namespace llvm
namespace namespace
{ {
sw::RoutineManager *routineManager = nullptr; sw::LLVMRoutineManager *routineManager = nullptr;
llvm::ExecutionEngine *executionEngine = nullptr; llvm::ExecutionEngine *executionEngine = nullptr;
llvm::IRBuilder<> *builder = nullptr; llvm::IRBuilder<> *builder = nullptr;
llvm::LLVMContext *context = nullptr; llvm::LLVMContext *context = nullptr;
...@@ -118,7 +118,7 @@ namespace sw ...@@ -118,7 +118,7 @@ namespace sw
} }
::module = new Module("", *::context); ::module = new Module("", *::context);
::routineManager = new RoutineManager(); ::routineManager = new LLVMRoutineManager();
#if defined(__x86_64__) #if defined(__x86_64__)
const char *architecture = "x86-64"; const char *architecture = "x86-64";
...@@ -205,7 +205,7 @@ namespace sw ...@@ -205,7 +205,7 @@ namespace sw
} }
void *entry = ::executionEngine->getPointerToFunction(::function); void *entry = ::executionEngine->getPointerToFunction(::function);
Routine *routine = ::routineManager->acquireRoutine(entry); LLVMRoutine *routine = ::routineManager->acquireRoutine(entry);
if(CodeAnalystLogJITCode) if(CodeAnalystLogJITCode)
{ {
......
...@@ -34,6 +34,7 @@ namespace sw ...@@ -34,6 +34,7 @@ namespace sw
class Value; class Value;
class Constant; class Constant;
class BasicBlock; class BasicBlock;
class Routine;
enum Optimization enum Optimization
{ {
...@@ -53,10 +54,6 @@ namespace sw ...@@ -53,10 +54,6 @@ namespace sw
extern Optimization optimization[10]; extern Optimization optimization[10];
class Routine;
class RoutineManager;
class Builder;
class Nucleus class Nucleus
{ {
public: public:
......
...@@ -266,15 +266,17 @@ ...@@ -266,15 +266,17 @@
</ProjectReference> </ProjectReference>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="LLVMRoutine.cpp" />
<ClCompile Include="LLVMRoutineManager.cpp" />
<ClCompile Include="Nucleus.cpp" /> <ClCompile Include="Nucleus.cpp" />
<ClCompile Include="Routine.cpp" /> <ClCompile Include="Routine.cpp" />
<ClCompile Include="RoutineManager.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="LLVMRoutine.hpp" />
<ClInclude Include="LLVMRoutineManager.hpp" />
<ClInclude Include="Nucleus.hpp" /> <ClInclude Include="Nucleus.hpp" />
<ClInclude Include="Reactor.hpp" /> <ClInclude Include="Reactor.hpp" />
<ClInclude Include="Routine.hpp" /> <ClInclude Include="Routine.hpp" />
<ClInclude Include="RoutineManager.hpp" />
<ClInclude Include="x86.hpp" /> <ClInclude Include="x86.hpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
...@@ -18,10 +18,13 @@ ...@@ -18,10 +18,13 @@
<ClCompile Include="Nucleus.cpp"> <ClCompile Include="Nucleus.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="RoutineManager.cpp"> <ClCompile Include="Routine.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Routine.cpp"> <ClCompile Include="LLVMRoutineManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LLVMRoutine.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
...@@ -35,10 +38,13 @@ ...@@ -35,10 +38,13 @@
<ClInclude Include="x86.hpp"> <ClInclude Include="x86.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="RoutineManager.hpp"> <ClInclude Include="Routine.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Routine.hpp"> <ClInclude Include="LLVMRoutineManager.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LLVMRoutine.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
......
...@@ -14,74 +14,17 @@ ...@@ -14,74 +14,17 @@
#include "Routine.hpp" #include "Routine.hpp"
#include "../Common/Memory.hpp"
#include "../Common/Thread.hpp" #include "../Common/Thread.hpp"
#include "../Common/Types.hpp"
#include <cassert>
namespace sw namespace sw
{ {
Routine::Routine(int bufferSize) : bufferSize(bufferSize), dynamic(true) Routine::Routine()
{
void *memory = allocateExecutable(bufferSize);
buffer = memory;
entry = memory;
functionSize = bufferSize; // Updated by RoutineManager::endFunctionBody
bindCount = 0;
}
Routine::Routine(void *memory, int bufferSize, int offset) : bufferSize(bufferSize), functionSize(bufferSize), dynamic(false)
{ {
buffer = (unsigned char*)memory - offset;
entry = memory;
bindCount = 0; bindCount = 0;
} }
Routine::~Routine()
{
if(dynamic)
{
deallocateExecutable(buffer, bufferSize);
}
}
void Routine::setFunctionSize(int functionSize)
{
this->functionSize = functionSize;
}
const void *Routine::getBuffer()
{
return buffer;
}
const void *Routine::getEntry()
{
return entry;
}
int Routine::getBufferSize()
{
return bufferSize;
}
int Routine::getFunctionSize()
{
return functionSize;
}
int Routine::getCodeSize()
{
return functionSize - static_cast<int>((uintptr_t)entry - (uintptr_t)buffer);
}
bool Routine::isDynamic()
{
return dynamic;
}
void Routine::bind() void Routine::bind()
{ {
atomicIncrement(&bindCount); atomicIncrement(&bindCount);
...@@ -96,4 +39,9 @@ namespace sw ...@@ -96,4 +39,9 @@ namespace sw
delete this; delete this;
} }
} }
Routine::~Routine()
{
assert(bindCount == 0);
}
} }
...@@ -17,38 +17,21 @@ ...@@ -17,38 +17,21 @@
namespace sw namespace sw
{ {
class RoutineManager;
class Routine class Routine
{ {
friend class RoutineManager;
public: public:
Routine(int bufferSize); Routine();
Routine(void *memory, int bufferSize, int offset);
~Routine(); virtual ~Routine();
void setFunctionSize(int functionSize); virtual const void *getEntry() = 0;
const void *getBuffer();
const void *getEntry();
int getBufferSize();
int getFunctionSize(); // Includes constants before the entry point
int getCodeSize(); // Executable code only
bool isDynamic();
// Reference counting
void bind(); void bind();
void unbind(); void unbind();
private: private:
void *buffer;
const void *entry;
int bufferSize;
int functionSize;
volatile int bindCount; volatile int bindCount;
const bool dynamic; // Generated or precompiled
}; };
} }
......
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