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 #
/lib/
/obj/
/bin/
# Ignored files #
*.obj
*.lib
*.log
*.tlog
*.exe
*.ilk
*.pdb
*.sbr
*.bsc
*.dll
*.res
*.idb
*.sdf
*.suo
*.o
*.depend
*.layout
*.opensdf
*.aps
# Ignored folders #
/lib/
/obj/
/bin/
.vs
# Ignored files #
*.obj
*.lib
*.log
*.tlog
*.exe
*.ilk
*.pdb
*.sbr
*.bsc
*.dll
*.res
*.idb
*.sdf
*.suo
*.o
*.depend
*.layout
*.opensdf
*.aps
*.opendb
*.db
......@@ -605,8 +605,10 @@ set(REACTOR_LIST
${SOURCE_DIR}/Reactor/Nucleus.hpp
${SOURCE_DIR}/Reactor/Routine.cpp
${SOURCE_DIR}/Reactor/Routine.hpp
${SOURCE_DIR}/Reactor/RoutineManager.cpp
${SOURCE_DIR}/Reactor/RoutineManager.hpp
${SOURCE_DIR}/Reactor/LLVMRoutine.cpp
${SOURCE_DIR}/Reactor/LLVMRoutine.hpp
${SOURCE_DIR}/Reactor/LLVMRoutineManager.cpp
${SOURCE_DIR}/Reactor/LLVMRoutineManager.hpp
)
file(GLOB_RECURSE EGL_LIST
......
......@@ -38,7 +38,8 @@ COMMON_SRC_FILES += \
COMMON_SRC_FILES += \
Reactor/Nucleus.cpp \
Reactor/Routine.cpp \
Reactor/RoutineManager.cpp
Reactor/LLVMRoutine.cpp \
Reactor/LLVMRoutineManager.cpp
COMMON_SRC_FILES += \
Renderer/Blitter.cpp \
......
......@@ -178,8 +178,10 @@
<Unit filename="../../Reactor/Reactor.hpp" />
<Unit filename="../../Reactor/Routine.cpp" />
<Unit filename="../../Reactor/Routine.hpp" />
<Unit filename="../../Reactor/RoutineManager.cpp" />
<Unit filename="../../Reactor/RoutineManager.hpp" />
<Unit filename="../../Reactor/LLVMRoutine.cpp" />
<Unit filename="../../Reactor/LLVMRoutine.hpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
<Unit filename="../../Reactor/x86.hpp" />
<Unit filename="../../Renderer/Blitter.cpp" />
<Unit filename="../../Renderer/Blitter.hpp" />
......
......@@ -177,8 +177,10 @@
<Unit filename="../../Reactor/Reactor.hpp" />
<Unit filename="../../Reactor/Routine.cpp" />
<Unit filename="../../Reactor/Routine.hpp" />
<Unit filename="../../Reactor/RoutineManager.cpp" />
<Unit filename="../../Reactor/RoutineManager.hpp" />
<Unit filename="../../Reactor/LLVMRoutine.cpp" />
<Unit filename="../../Reactor/LLVMRoutine.hpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
<Unit filename="../../Reactor/x86.hpp" />
<Unit filename="../../Renderer/Blitter.cpp" />
<Unit filename="../../Renderer/Blitter.hpp" />
......
......@@ -43,7 +43,8 @@ source_set("swiftshader_reactor") {
sources = [
"Nucleus.cpp",
"Routine.cpp",
"RoutineManager.cpp",
"LLVMRoutine.cpp",
"LLVMRoutineManager.cpp",
]
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 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "RoutineManager.hpp"
#include "LLVMRoutineManager.hpp"
#include "Routine.hpp"
#include "LLVMRoutine.hpp"
#include "llvm/Function.h"
#include "../Common/Memory.hpp"
#include "../Common/Thread.hpp"
......@@ -24,30 +24,30 @@ namespace sw
{
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;
}
void RoutineManager::AllocateGOT()
void LLVMRoutineManager::AllocateGOT()
{
UNIMPLEMENTED();
}
uint8_t *RoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment)
uint8_t *LLVMRoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment)
{
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
{
......@@ -69,52 +69,52 @@ namespace sw
actualSize = (actualSize + pageSize - 1) & ~(pageSize - 1);
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();
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();
}
uint8_t *RoutineManager::getGOTBase() const
uint8_t *LLVMRoutineManager::getGOTBase() const
{
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();
return 0;
return nullptr;
}
uint8_t *RoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
uint8_t *LLVMRoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
{
UNIMPLEMENTED();
return 0;
return nullptr;
}
void RoutineManager::deallocateFunctionBody(void *Body)
void LLVMRoutineManager::deallocateFunctionBody(void *Body)
{
delete routine;
routine = 0;
routine = nullptr;
}
void RoutineManager::deallocateExceptionTable(void *ET)
void LLVMRoutineManager::deallocateExceptionTable(void *ET)
{
if(ET)
{
......@@ -122,26 +122,26 @@ namespace sw
}
}
void RoutineManager::setMemoryWritable()
void LLVMRoutineManager::setMemoryWritable()
{
}
void RoutineManager::setMemoryExecutable()
void LLVMRoutineManager::setMemoryExecutable()
{
markExecutable(routine->buffer, routine->bufferSize);
}
void RoutineManager::setPoisonMemory(bool poison)
void LLVMRoutineManager::setPoisonMemory(bool poison)
{
UNIMPLEMENTED();
}
Routine *RoutineManager::acquireRoutine(void *entry)
LLVMRoutine *LLVMRoutineManager::acquireRoutine(void *entry)
{
routine->entry = entry;
Routine *result = routine;
routine = 0;
LLVMRoutine *result = routine;
routine = nullptr;
return result;
}
......
......@@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef sw_RoutineManager_hpp
#define sw_RoutineManager_hpp
#ifndef sw_LLVMRoutineManager_hpp
#define sw_LLVMRoutineManager_hpp
#include "llvm/GlobalValue.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h"
namespace sw
{
class Routine;
class LLVMRoutine;
class RoutineManager : public llvm::JITMemoryManager
class LLVMRoutineManager : public llvm::JITMemoryManager
{
public:
RoutineManager();
LLVMRoutineManager();
virtual ~RoutineManager();
virtual ~LLVMRoutineManager();
virtual void AllocateGOT();
......@@ -45,13 +45,13 @@ namespace sw
virtual void setMemoryExecutable();
virtual void setPoisonMemory(bool poison);
Routine *acquireRoutine(void *entry);
LLVMRoutine *acquireRoutine(void *entry);
private:
Routine *routine;
LLVMRoutine *routine;
static volatile int averageInstructionSize;
};
}
#endif // sw_RoutineManager_hpp
#endif // sw_LLVMRoutineManager_hpp
......@@ -29,8 +29,8 @@
#include "llvm/Support/TargetSelect.h"
#include "../lib/ExecutionEngine/JIT/JIT.h"
#include "Routine.hpp"
#include "RoutineManager.hpp"
#include "LLVMRoutine.hpp"
#include "LLVMRoutineManager.hpp"
#include "x86.hpp"
#include "CPUID.hpp"
#include "Thread.hpp"
......@@ -60,7 +60,7 @@ namespace llvm
namespace
{
sw::RoutineManager *routineManager = nullptr;
sw::LLVMRoutineManager *routineManager = nullptr;
llvm::ExecutionEngine *executionEngine = nullptr;
llvm::IRBuilder<> *builder = nullptr;
llvm::LLVMContext *context = nullptr;
......@@ -118,7 +118,7 @@ namespace sw
}
::module = new Module("", *::context);
::routineManager = new RoutineManager();
::routineManager = new LLVMRoutineManager();
#if defined(__x86_64__)
const char *architecture = "x86-64";
......@@ -205,7 +205,7 @@ namespace sw
}
void *entry = ::executionEngine->getPointerToFunction(::function);
Routine *routine = ::routineManager->acquireRoutine(entry);
LLVMRoutine *routine = ::routineManager->acquireRoutine(entry);
if(CodeAnalystLogJITCode)
{
......
......@@ -34,6 +34,7 @@ namespace sw
class Value;
class Constant;
class BasicBlock;
class Routine;
enum Optimization
{
......@@ -53,10 +54,6 @@ namespace sw
extern Optimization optimization[10];
class Routine;
class RoutineManager;
class Builder;
class Nucleus
{
public:
......
......@@ -266,15 +266,17 @@
</ProjectReference>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="LLVMRoutine.cpp" />
<ClCompile Include="LLVMRoutineManager.cpp" />
<ClCompile Include="Nucleus.cpp" />
<ClCompile Include="Routine.cpp" />
<ClCompile Include="RoutineManager.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="LLVMRoutine.hpp" />
<ClInclude Include="LLVMRoutineManager.hpp" />
<ClInclude Include="Nucleus.hpp" />
<ClInclude Include="Reactor.hpp" />
<ClInclude Include="Routine.hpp" />
<ClInclude Include="RoutineManager.hpp" />
<ClInclude Include="x86.hpp" />
</ItemGroup>
<ItemGroup>
......
......@@ -18,10 +18,13 @@
<ClCompile Include="Nucleus.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RoutineManager.cpp">
<ClCompile Include="Routine.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Routine.cpp">
<ClCompile Include="LLVMRoutineManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LLVMRoutine.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
......@@ -35,10 +38,13 @@
<ClInclude Include="x86.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RoutineManager.hpp">
<ClInclude Include="Routine.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Routine.hpp">
<ClInclude Include="LLVMRoutineManager.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LLVMRoutine.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
......
......@@ -14,74 +14,17 @@
#include "Routine.hpp"
#include "../Common/Memory.hpp"
#include "../Common/Thread.hpp"
#include "../Common/Types.hpp"
#include <cassert>
namespace sw
{
Routine::Routine(int bufferSize) : bufferSize(bufferSize), dynamic(true)
{
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)
Routine::Routine()
{
buffer = (unsigned char*)memory - offset;
entry = memory;
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()
{
atomicIncrement(&bindCount);
......@@ -96,4 +39,9 @@ namespace sw
delete this;
}
}
Routine::~Routine()
{
assert(bindCount == 0);
}
}
......@@ -17,38 +17,21 @@
namespace sw
{
class RoutineManager;
class Routine
{
friend class RoutineManager;
public:
Routine(int bufferSize);
Routine(void *memory, int bufferSize, int offset);
Routine();
~Routine();
virtual ~Routine();
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();
virtual const void *getEntry() = 0;
// Reference counting
void bind();
void unbind();
private:
void *buffer;
const void *entry;
int bufferSize;
int functionSize;
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