OSDN Git Service

Implement Pointer<> support for Subzero.
authorNicolas Capens <capn@google.com>
Tue, 27 Sep 2016 18:18:07 +0000 (14:18 -0400)
committerNicolas Capens <capn@google.com>
Thu, 29 Sep 2016 01:55:17 +0000 (01:55 +0000)
Bug swiftshader:11

Change-Id: I794ef54a7c2ecde71ee6344c63955d2f838ff189
Reviewed-on: https://swiftshader-review.googlesource.com/7331
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
src/Reactor/LLVMReactor.cpp
src/Reactor/Main.cpp
src/Reactor/Nucleus.hpp
src/Reactor/Reactor.hpp
src/Reactor/SubzeroReactor.cpp

index 4069523..47518ec 100644 (file)
@@ -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
index afdc73f..ae1b7fa 100644 (file)
@@ -23,9 +23,10 @@ int main()
        Routine *routine = nullptr;
 
        {
-               Function<Int(Int, Int)> function;
+               Function<Int(Pointer<Int>, Int)> function;
                {
-                       Int x = function.Arg<0>();
+                       Pointer<Int> 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);
                }
        }
index 9483465..afc5d52 100644 (file)
@@ -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);
index c9ecdbb..ba935ec 100644 (file)
@@ -60,8 +60,6 @@ namespace sw
                {
                        return true;
                }
-
-               typedef void ctype;
        };
 
        template<class T>
@@ -86,6 +84,7 @@ namespace sw
                Value *getAddress(Value *index) const;
 
        protected:
+               Type *const type;
                Value *address;
        };
 
@@ -2373,7 +2372,7 @@ namespace sw
        template<class T>
        RValue<T> Reference<T>::operator=(const Reference<T> &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<T>(tmp);
@@ -2388,7 +2387,7 @@ namespace sw
        template<class T>
        Value *Reference<T>::loadValue() const
        {
-               return Nucleus::createLoad(address, false, alignment);
+               return Nucleus::createLoad(address, T::getType(), false, alignment);
        }
 
        template<class T>
@@ -2754,7 +2753,7 @@ namespace sw
        template<class T>
        void Return(const Pointer<T> &ret)
        {
-               Nucleus::createRet(Nucleus::createLoad(ret.address));
+               Nucleus::createRet(Nucleus::createLoad(ret.address, Pointer<T>::getType()));
                Nucleus::setInsertBlock(Nucleus::createBasicBlock());
        }
 
index 9adc3fb..a27b986 100644 (file)
@@ -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