From e12780d57478a3d0e349a29169df73ae4a8ba684 Mon Sep 17 00:00:00 2001 From: Nicolas Capens Date: Tue, 27 Sep 2016 14:18:07 -0400 Subject: [PATCH] Implement Pointer<> support for Subzero. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Bug swiftshader:11 Change-Id: I794ef54a7c2ecde71ee6344c63955d2f838ff189 Reviewed-on: https://swiftshader-review.googlesource.com/7331 Tested-by: Nicolas Capens Reviewed-by: Alexis Hétu Reviewed-by: Nicolas Capens --- src/Reactor/LLVMReactor.cpp | 7 ++++--- src/Reactor/Main.cpp | 10 ++++++---- src/Reactor/Nucleus.hpp | 2 +- src/Reactor/Reactor.hpp | 9 ++++----- src/Reactor/SubzeroReactor.cpp | 40 +++++++++++++++++++++++++++++----------- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp index 4069523a9..47518ecea 100644 --- a/src/Reactor/LLVMReactor.cpp +++ b/src/Reactor/LLVMReactor.cpp @@ -448,8 +448,9 @@ namespace sw return V(::builder->CreateNot(v)); } - Value *Nucleus::createLoad(Value *ptr, bool isVolatile, unsigned int align) + Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align) { + assert(ptr->getType()->getContainedType(0) == type); return V(::builder->Insert(new LoadInst(ptr, "", isVolatile, align))); } @@ -809,14 +810,14 @@ namespace sw return T(llvm::Type::getVoidTy(*::context)); } - LValue::LValue(Type *type, int arraySize) + LValue::LValue(Type *type, int arraySize) : type(type) { address = Nucleus::allocateStackVariable(type, arraySize); } Value *LValue::loadValue(unsigned int alignment) const { - return Nucleus::createLoad(address, false, alignment); + return Nucleus::createLoad(address, type, false, alignment); } Value *LValue::storeValue(Value *value, unsigned int alignment) const diff --git a/src/Reactor/Main.cpp b/src/Reactor/Main.cpp index afdc73f6e..ae1b7fa3c 100644 --- a/src/Reactor/Main.cpp +++ b/src/Reactor/Main.cpp @@ -23,9 +23,10 @@ int main() Routine *routine = nullptr; { - Function function; + Function, Int)> function; { - Int x = function.Arg<0>(); + Pointer p = function.Arg<0>(); + Int x = *p; Int y = function.Arg<1>(); Int sum = x + y; @@ -37,8 +38,9 @@ int main() if(routine) { - int (*add)(int, int) = (int(*)(int,int))routine->getEntry(); - int result = add(1, 2); + int (*add)(int*, int) = (int(*)(int*,int))routine->getEntry(); + int one = 1; + int result = add(&one, 2); assert(result == 3); } } diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp index 9483465a0..afc5d52bd 100644 --- a/src/Reactor/Nucleus.hpp +++ b/src/Reactor/Nucleus.hpp @@ -93,7 +93,7 @@ namespace sw static Value *createNot(Value *V); // Memory instructions - static Value *createLoad(Value *ptr, bool isVolatile = false, unsigned int align = 0); + static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); static Value *createStore(Value *value, Value *ptr, bool isVolatile = false, unsigned int align = 0); static Value *createStore(Constant *constant, Value *ptr, bool isVolatile = false, unsigned int align = 0); static Value *createGEP(Value *ptr, Value *index); diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp index c9ecdbb94..ba935ec3b 100644 --- a/src/Reactor/Reactor.hpp +++ b/src/Reactor/Reactor.hpp @@ -60,8 +60,6 @@ namespace sw { return true; } - - typedef void ctype; }; template @@ -86,6 +84,7 @@ namespace sw Value *getAddress(Value *index) const; protected: + Type *const type; Value *address; }; @@ -2373,7 +2372,7 @@ namespace sw template RValue Reference::operator=(const Reference &ref) const { - Value *tmp = Nucleus::createLoad(ref.address, false, ref.alignment); + Value *tmp = Nucleus::createLoad(ref.address, T::getType(), false, ref.alignment); Nucleus::createStore(tmp, address, false, alignment); return RValue(tmp); @@ -2388,7 +2387,7 @@ namespace sw template Value *Reference::loadValue() const { - return Nucleus::createLoad(address, false, alignment); + return Nucleus::createLoad(address, T::getType(), false, alignment); } template @@ -2754,7 +2753,7 @@ namespace sw template void Return(const Pointer &ret) { - Nucleus::createRet(Nucleus::createLoad(ret.address)); + Nucleus::createRet(Nucleus::createLoad(ret.address, Pointer::getType())); Nucleus::setInsertBlock(Nucleus::createBasicBlock()); } diff --git a/src/Reactor/SubzeroReactor.cpp b/src/Reactor/SubzeroReactor.cpp index 9adc3fbc0..a27b98645 100644 --- a/src/Reactor/SubzeroReactor.cpp +++ b/src/Reactor/SubzeroReactor.cpp @@ -245,13 +245,24 @@ namespace sw Value *Nucleus::allocateStackVariable(Type *t, int arraySize) { + assert(arraySize == 0 && "UNIMPLEMENTED"); + Ice::Type type = T(t); - Ice::Variable *value = ::function->makeVariable(type); - assert(type == Ice::IceType_i32 && arraySize == 0 && "UNIMPLEMENTED"); - auto bytes = Ice::ConstantInteger32::create(::context, type, 4); - auto alloca = Ice::InstAlloca::create(::function, value, bytes, 4); - ::function->getEntryNode()->appendInst(alloca); - return V(value); + + int32_t size = 0; + switch(type) + { + case Ice::IceType_i32: size = 4; break; + case Ice::IceType_i64: size = 8; break; + default: assert(false && "UNIMPLEMENTED" && type); + } + + auto bytes = Ice::ConstantInteger32::create(::context, type, size); + auto address = ::function->makeVariable(T(getPointerType(t))); + auto alloca = Ice::InstAlloca::create(::function, address, bytes, size); + ::function->getEntryNode()->getInsts().push_front(alloca); + + return V(address); } BasicBlock *Nucleus::createBasicBlock() @@ -425,9 +436,9 @@ namespace sw assert(false && "UNIMPLEMENTED"); return nullptr; } - Value *Nucleus::createLoad(Value *ptr, bool isVolatile, unsigned int align) + Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align) { - Ice::Variable *value = ::function->makeVariable(ptr->getType()); + Ice::Variable *value = ::function->makeVariable(T(type)); auto load = Ice::InstLoad::create(::function, value, ptr, align); ::basicBlock->appendInst(load); return V(value); @@ -687,7 +698,14 @@ namespace sw Type *Nucleus::getPointerType(Type *ElementType) { - assert(false && "UNIMPLEMENTED"); return nullptr; + if(sizeof(void*) == 8) + { + return T(Ice::IceType_i64); + } + else + { + return T(Ice::IceType_i32); + } } Constant *Nucleus::createNullValue(Type *Ty) @@ -755,14 +773,14 @@ namespace sw return T(Ice::IceType_void); } - LValue::LValue(Type *type, int arraySize) + LValue::LValue(Type *type, int arraySize) : type(type) { address = Nucleus::allocateStackVariable(type, arraySize); } Value *LValue::loadValue(unsigned int alignment) const { - return Nucleus::createLoad(address, false, alignment); + return Nucleus::createLoad(address, type, false, alignment); } Value *LValue::storeValue(Value *value, unsigned int alignment) const -- 2.11.0