[opengtl-commits] [303] allocate complex structures in memory

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 303
Author:   cyrille
Date:     2008-07-22 17:37:30 +0200 (Tue, 22 Jul 2008)

Log Message:
-----------
allocate complex structures in memory

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp
    trunk/OpenGTL/OpenCTL/OpenCTL/ctlstdlib.ctl
    trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
    trunk/OpenGTL/OpenShiva/tests/convolution/blur.shiva


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp	2008-07-22 15:37:30 UTC (rev 303)
@@ -186,7 +186,7 @@
     {
       moduleData->appendFunction( (*it)->function()->name(), (*it)->function());
     }
-//     OCTL_DEBUG( *d->module );
+    OCTL_DEBUG( *d->module );
     // Success
     // Optimization taken from the Stacker example
     llvm::PassManager Passes;

Modified: trunk/OpenGTL/OpenCTL/OpenCTL/ctlstdlib.ctl
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/ctlstdlib.ctl	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/ctlstdlib.ctl	2008-07-22 15:37:30 UTC (rev 303)
@@ -149,6 +149,10 @@
   {
     if( table[i][0] <= p && p < table[i+1][0] )
     {
+      if( i < 1 || i >= table.size-2 )
+      {
+        return interpolate1D( table, p );
+      }
       float s = (p - table[i][0]) / (table[i+1][0] - table[i][0]);
       float dx0 = (table[i][0] - table[i-1][0]);
       float dx1 = (table[i+1][0] - table[i][0]);

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp	2008-07-22 15:37:30 UTC (rev 303)
@@ -215,3 +215,27 @@
   type->d->setType( _type );
   return type;
 }
+
+bool Type::Private::isComplexStructure()
+{
+  if( dataType != Type::STRUCTURE )
+  {
+    return false;
+  }
+  GTL_ASSERT( structDataMembers );
+  for( std::vector<StructDataMember>::iterator it = structDataMembers->begin();
+       it != structDataMembers->end(); ++it )
+  {
+    if( it->type()->dataType() == Type::STRUCTURE or it->type()->dataType() == Type::ARRAY )
+    {
+      return true;
+    }
+  }
+  return false;
+}
+
+bool Type::Private::isNestedArray()
+{
+  GTL_ASSERT( dataType != Type::ARRAY or arrayType );
+  return dataType == Type::ARRAY and (arrayType->dataType() == Type::ARRAY or arrayType->dataType() == Type::STRUCTURE);
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h	2008-07-22 15:37:30 UTC (rev 303)
@@ -105,6 +105,24 @@
        * OpenShiva's wrappers.
        */
       static Type* createArbitraryType( const llvm::Type* _type);
+      /**
+       * @return true if this a complex structure (a structure where one of its element
+       *         is an array or an other structure)
+       * 
+       * @code
+       * Type* type1 = new Type( struct { int a; int b; } );
+       * Type* type2 = new Type( struct { int a; int b[2]; } );
+       * Type* type3 = new Type( struct { int a; struct { int b; } } );
+       * type1->d->isComplexStructure() == false;
+       * type2->d->isComplexStructure() == true;
+       * type3->d->isComplexStructure() == true;
+       * @endcode
+       */
+      bool isComplexStructure();
+      /**
+       * @return true if this a "nested" array (ie int a[2][3]; is nested, while int a[2]; isn't)
+       */
+      bool isNestedArray();
     private:
       void setType( const llvm::Type* _type);
       const llvm::Type* m_type;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp	2008-07-22 15:37:30 UTC (rev 303)
@@ -52,6 +52,10 @@
   d->pointer = 0;
   d->allocatedInMemory = false;
   d->constantPointer = false;
+  if( _type->d->isNestedArray() or _type->d->isComplexStructure() )
+  {
+    setAllocateInMemory( true );
+  }
 }
 
 VariableNG::~VariableNG()
@@ -150,10 +154,11 @@
 
 llvm::BasicBlock* VariableNG::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _donttouch )
 {
-  if( d->allocatedInMemory )
+  if( d->allocatedInMemory and not d->constantPointer )
   {
-    return d->visitor->cleanUp( _generationContext, _currentBlock, d->pointer, d->type, _donttouch, d->allocatedInMemory );
-    // TODO DESALLOCATE
+    llvm::BasicBlock* bb = d->visitor->cleanUp( _generationContext, _currentBlock, d->pointer, d->type, _donttouch, d->allocatedInMemory );
+    new llvm::FreeInst( d->pointer, bb);
+    return bb;
   }
   return _currentBlock;
 }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-07-22 15:37:30 UTC (rev 303)
@@ -160,13 +160,18 @@
     // Check if size are identical or update the size of this array
     {
       llvm::Value* test= _generationContext.codeGenerator()->createDifferentExpression( _currentBlock, getSize( _generationContext, _currentBlock, _pointer ), Type::Integer32, getSize( _generationContext, _currentBlock, _value ), Type::Integer32);
-      llvm::BasicBlock* ifContent = llvm::BasicBlock::Create();
+      llvm::BasicBlock* ifContent = llvm::BasicBlock::Create( "ifContent");
       _generationContext.llvmFunction()->getBasicBlockList().push_back( ifContent );
-      setSize( _generationContext, ifContent, _pointer, _pointerType, getSize( _generationContext, ifContent, _value ), _allocatedInMemory, true );
-    
+      std::list<llvm::Value*> sizes;
+      sizes.push_back( getSize( _generationContext, ifContent, _value ) );
+      
+      llvm::BasicBlock* endIfContent = cleanUp(_generationContext, ifContent, _pointer, _pointerType, 0, _allocatedInMemory );
+      
+      endIfContent = initialise( _generationContext, endIfContent, _pointer, _pointerType, sizes, _allocatedInMemory );
+      
       llvm::BasicBlock* afterIf = llvm::BasicBlock::Create();
       _generationContext.llvmFunction()->getBasicBlockList().push_back( afterIf);
-      _generationContext.codeGenerator()->createIfStatement( _currentBlock, test, Type::Boolean, ifContent, ifContent, afterIf );
+      _generationContext.codeGenerator()->createIfStatement( _currentBlock, test, Type::Boolean, ifContent, endIfContent, afterIf );
       _currentBlock = afterIf;
     
     }
@@ -201,7 +206,7 @@
                     endBodyBlock );
 }
 
-void ArrayVisitor::setSize(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory, bool _allreadyInitialised) const
+llvm::BasicBlock* ArrayVisitor::setSize(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory) const
 {
   GTL_ASSERT( _pointerType->dataType() == Type::ARRAY );
   GTL_DEBUG( *_size );
@@ -221,16 +226,13 @@
     {
       array = new llvm::MallocInst(
                     _pointerType->embeddedType()->d->type(), _size, "", _currentBlock);
-      if( _allreadyInitialised )
-      {
-        new llvm::FreeInst( ptr, _currentBlock );
-      }
     } else {
       array = new llvm::AllocaInst(
                     _pointerType->embeddedType()->d->type(), _size, "", _currentBlock);
     }
     new llvm::StoreInst( array, ptr, "", _currentBlock);
   }
+  return _currentBlock;
 }
 
 llvm::Value* ArrayVisitor::getSize(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* _pointer ) const
@@ -249,7 +251,7 @@
   if( not _sizes.empty())
   {
     llvm::Value* currentSize = _sizes.front();
-    setSize( _generationContext, _currentBlock, _pointer, _pointerType, currentSize, _allocatedInMemory, false );
+    _currentBlock = setSize( _generationContext, _currentBlock, _pointer, _pointerType, currentSize, _allocatedInMemory );
     std::list< llvm::Value*> sizeAfter = _sizes;
     sizeAfter.pop_front();
     //   int i = 0;
@@ -278,14 +280,48 @@
                     Type::Integer32,
                     bodyBlock,
                     endBodyBlock );
+  } else {
+    std::vector<llvm::Value*> indexes;
+    indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the structure of the array
+    indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the size of the array
+    // Init the size
+    llvm::Value* ptr = llvm::GetElementPtrInst::Create( _pointer, indexes.begin(), indexes.end(), "", _currentBlock);
+    new llvm::StoreInst( GTLCore::CodeGenerator::integerToConstant( 0 ), ptr, "", _currentBlock);
   }
   return _currentBlock;
 }
 
 llvm::BasicBlock* ArrayVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
 {
-  
-  return _currentBlock;
+    //   int i = 0;
+    VariableNG* index = new VariableNG( Type::Integer32, false);
+    index->initialise( _generationContext, _currentBlock, ExpressionResult( _generationContext.codeGenerator()->integerToConstant(0), Type::Integer32), std::list<llvm::Value*>());
+    
+    // Construct the body of the for loop
+    llvm::BasicBlock* bodyBlock = llvm::BasicBlock::Create("bodyBlock");
+    _generationContext.llvmFunction()->getBasicBlockList().push_back( bodyBlock);
+    GTL_DEBUG( " value = " << *_pointer << " type = " << *_pointer->getType() << " " << *_pointerType->embeddedType() << " " << *_pointerType );
+    const Visitor* visitor = Visitor::getVisitorFor( _pointerType->embeddedType() );
+    llvm::BasicBlock* endBodyBlock = visitor->cleanUp( 
+          _generationContext,
+          bodyBlock, 
+          _generationContext.codeGenerator()->accessArrayValue( bodyBlock, _pointer, index->get( _generationContext, bodyBlock  ) ),
+          _pointerType->embeddedType(), _donttouch, _allocatedInMemory );
+    std::vector<llvm::Value*> indexes;
+    indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the structure of the array
+    indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); // Access the size of the array
+    // Init the size
+    llvm::Value* ptrToData = llvm::GetElementPtrInst::Create( _pointer, indexes.begin(), indexes.end(), "", endBodyBlock);
+    new llvm::FreeInst( new llvm::LoadInst( ptrToData, "", endBodyBlock ), endBodyBlock );
+    // Create the for statement
+    return CodeGenerator::createIterationForStatement(
+                    _generationContext,
+                    _currentBlock,
+                    index,
+                    getSize( _generationContext, _currentBlock, _pointer),
+                    Type::Integer32,
+                    bodyBlock,
+                    endBodyBlock );
 }
 
 //--------- VectorVisitor ---------///

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-07-22 15:37:30 UTC (rev 303)
@@ -154,7 +154,7 @@
        * @param _pointer a pointer toward the data
        * @param _size the new size of the array
        */
-      void setSize(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory, bool _allreadyInitialised ) const;
+      llvm::BasicBlock* setSize(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory) const;
   };
 
   class VectorVisitor : public Visitor {

Modified: trunk/OpenGTL/OpenShiva/tests/convolution/blur.shiva
===================================================================
--- trunk/OpenGTL/OpenShiva/tests/convolution/blur.shiva	2008-07-16 11:53:53 UTC (rev 302)
+++ trunk/OpenGTL/OpenShiva/tests/convolution/blur.shiva	2008-07-22 15:37:30 UTC (rev 303)
@@ -11,14 +11,11 @@
     float2 point3;
     point3[0] = result.x + 1;
     point3[1] = result.y + 1;
-    pixel v1 = img.sampleNearest( point1 );
-    pixel v2 = img.sampleNearest( point2 );
-    pixel v3 = img.sampleNearest( point3 );
-    pixel v4 = v1 + v2 + v3;
-    for(int i = 0; i < 3; ++i)
-    {
-      result[i] = v4 / 3;
-//       result[i] = (v1[i] + v2[i] + v3[i]) / 3;
-    }
+//     pixel v1 = img.sampleNearest( point1 );
+//     pixel v2 = img.sampleNearest( point2 );
+//     pixel v3 = img.sampleNearest( point3 );
+//     pixel v4 = v1 + v2 + v3;
+    pixel v4 = img.sampleNearest( point1 ) + img.sampleNearest( point2 ) + img.sampleNearest( point3 );
+    result = v4 / 3.0;
   }
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/