[opengtl-commits] [265] * implement generateImageSampleNearest

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


Revision: 265
Author:   cyrille
Date:     2008-06-29 10:53:15 +0200 (Sun, 29 Jun 2008)

Log Message:
-----------
* implement generateImageSampleNearest
* make a function that generate a call to image_wrap_data
* wrap sampleNearest

Modified Paths:
--------------
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h


Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2008-06-29 08:53:15 UTC (rev 265)
@@ -284,11 +284,7 @@
         
         // Synchronize the output pixel with input
         // Call image_wrap_data on the result to get the pointer on destination data
-        std::vector<llvm::Value*> image_wrap_data_params;
-        image_wrap_data_params.push_back( arg_result );
-        image_wrap_data_params.push_back( iVal );
-        image_wrap_data_params.push_back( jVal );
-         llvm::Value* pointer =  new llvm::CallInst( Wrapper::image_wrap_dataFunction( _moduleData->llvmModule(), _moduleData->typeManager()->getStructure( "image" ) ), image_wrap_data_params.begin(), image_wrap_data_params.end(), "", firstBlockILoop ); // TODO get the real type of the output image (even if it doesn't matter much currently)
+        llvm::Value* pointer = callImageWrapData( generationContext, firstBlockILoop, generationContext.module()->typeManager()->getStructure( "image" ), arg_result, iVal, jVal);
         llvm::BasicBlock* lastBlockILoop = pixelToMem( generationContext, firstBlockILoop, resultVar->pointer(), pointer, arg_result );
       // }
       
@@ -307,7 +303,7 @@
 
 int memToVecId = 0;
 
-llvm::Function* CodeGenerator::generateMemToVec( Kernel* _kernel, GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription )
+llvm::Function* CodeGenerator::generateMemToVec( GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription )
 {
   int channelsNb = _pixelDescription.channels();
   // Check if all channels are floats
@@ -394,7 +390,7 @@
 
 int vecToMemId = 0;
 
-llvm::Function* CodeGenerator::generateVecToMem( Kernel* _kernel, GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription )
+llvm::Function* CodeGenerator::generateVecToMem( GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription )
 {
   int channelsNb = _pixelDescription.channels();
   // Check if all channels are floats
@@ -495,19 +491,85 @@
   return new llvm::CallInst( funcPtr, _arguments.begin(), _arguments.end(), "", _currentBlock );
 }
 
+llvm::Value* CodeGenerator::callImageWrapData( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, const GTLCore::Type* _imageType, llvm::Value* _imageWrap, llvm::Value* _x, llvm::Value* _y )
+{
+  GTL_ASSERT( _imageType );
+  std::vector<llvm::Value*> image_wrap_data_params;
+  image_wrap_data_params.push_back( _imageWrap );
+  image_wrap_data_params.push_back( _x );
+  image_wrap_data_params.push_back( _y );
+  
+  llvm::Function* func = Wrapper::image_wrap_dataFunction( _gc.llvmModule(), _imageType );
+#ifndef _NDEBUG_
+  {
+    const llvm::FunctionType *FTy =
+      llvm::cast<llvm::FunctionType>(llvm::cast<llvm::PointerType>(func->getType())->getElementType());
+    
+    GTL_ASSERT( image_wrap_data_params.size() == FTy->getNumParams() or
+            (FTy->isVarArg() and image_wrap_data_params.size() > FTy->getNumParams()) );
+    
+    for (unsigned i = 0; i < image_wrap_data_params.size(); ++i) {
+      if( i < FTy->getNumParams() and (FTy->getParamType(i) != image_wrap_data_params[i]->getType()) )
+      {
+        GTL_DEBUG( "Wrong parameter " << i << " : " << FTy->getParamType(i) << " => " << *FTy->getParamType(i) << " but got " << image_wrap_data_params[i] << " => " << *image_wrap_data_params[i]->getType() );
+      } else {
+        GTL_DEBUG( "Parameter " << i << " : " << FTy->getParamType(i) << " => " << *FTy->getParamType(i) << " but got " << image_wrap_data_params[i]->getType() << " => " << *image_wrap_data_params[i]->getType() );
+      }
+    }
+  }
+#endif
+
+  
+  return new llvm::CallInst(
+      func, image_wrap_data_params.begin(), image_wrap_data_params.end(), "", _currentBlock ); // TODO get the real type of the output image (even if it doesn't matter much currently)
+  
+}
+
 int imageSampleNearestId = 0;
 
-llvm::Function* CodeGenerator::generateImageSampleNearest(GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType )
+llvm::Function* CodeGenerator::generateImageSampleNearest(GTLCore::ModuleData* _moduleData, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType )
 {
-  //  pixelX sampleNearest( ImageWrap self, float2 pt )
+  //  pixelX* sampleNearest( ImageWrap* self, float2 pt )
   //  {
   //    int x = pt[0] + 0.5;
   //    int y = pt[1] + 0.5;
-  //    pixelX px = memToPixel( image_wrap_data(self, x, y ) );
+  //    pixelX* px = new pixelX;
+  //    memToPixel( image, px, image_wrap_data(self, x, y ) );
   //    px.x = pt[0];
   //    px.y = pt[1];
-  //    return px;
+  //    return pixelX;
   //  }
   
-  return 0;
+  GTLCore::CodeGenerator codeGenerator( _moduleData );
+    
+  llvm::Function* func = codeGenerator.createFunction(
+      Wrapper::image_wrap_sample_nearest_type( _moduleData->typeManager(), _imageType, _pixelType),
+      "image_wrap_sample_nearest" + GTLCore::String::number( ++imageSampleNearestId) );
+  GTLCore::GenerationContext generationContext( &codeGenerator, func, 0, _moduleData );
+  // Get the args.
+    llvm::Function::arg_iterator arg_it = func->arg_begin();
+    //   ImageWrap* self = first arg;
+    llvm::Value* arg_self = arg_it;
+    //   float2 _pt = second arg;
+    ++arg_it;
+    llvm::Value* arg_pt = arg_it;
+  //  {
+    llvm::BasicBlock* currentBlock = new llvm::BasicBlock;
+    func->getBasicBlockList().push_back( currentBlock );
+  //    int x = pt[0] + 0.5;
+    llvm::Value* x_f = GTLCore::CodeGenerator::vectorValueAt( currentBlock, arg_pt,
+                          GTLCore::CodeGenerator::integerToConstant( 0 ) );
+    llvm::Value* x_i = GTLCore::CodeGenerator::createRound( currentBlock, x_f );
+    llvm::Value* y_f = GTLCore::CodeGenerator::vectorValueAt( currentBlock, arg_pt,
+                          GTLCore::CodeGenerator::integerToConstant( 1 ) );
+    llvm::Value* y_i = GTLCore::CodeGenerator::createRound( currentBlock, y_f );
+  //    pixelX* px = new pixelX;
+    llvm::Value* px_var = new llvm::MallocInst( _pixelType->d->type(), GTLCore::CodeGenerator::integerToConstant( 1 ), "", currentBlock );
+  //    memToPixel( image, px, image_wrap_data(self, x, y ) );
+    currentBlock = memToPixel( generationContext, currentBlock,
+                               callImageWrapData( generationContext, currentBlock, _imageType, arg_self, x_i, y_i ),
+                               px_var, arg_self );
+    setPixelCoordinates( generationContext, currentBlock, px_var, x_f, y_f );
+    new llvm::ReturnInst( px_var, currentBlock);
+  return func;
 }

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2008-06-29 08:53:15 UTC (rev 265)
@@ -51,12 +51,12 @@
       static llvm::BasicBlock* memToPixel( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _dataPointer, llvm::Value* _pixel, llvm::Value* _image );
       static llvm::BasicBlock* pixelToMem( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel, llvm::Value* _dataPointer, llvm::Value* _image );
       static llvm::Function* generateEvaluatePixeles( const std::vector<const GTLCore::Type*>& _inputTypes, const GTLCore::Type* _outputType, Kernel* _kernel, GTLCore::ModuleData* _moduleData, int _channels );
-      static llvm::Function* generateMemToVec( Kernel* _kernel, GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription );
-      static llvm::Function* generateVecToMem( Kernel* _kernel, GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription );
+      static llvm::Function* generateMemToVec( GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription );
+      static llvm::Function* generateVecToMem( GTLCore::ModuleData* _moduleData, const GTLCore::PixelDescription& _pixelDescription );
       /**
        * Generate a sample nearest function.
        */
-      llvm::Function* generateImageSampleNearest(GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, const GTLCore::Type* _imageType, const GTLCore::Type* _type );
+      static llvm::Function* generateImageSampleNearest( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _imageType, const GTLCore::Type* _type );
       /**
        * @return a pointer to the data pointer of a pixel (as a pointer to a vector of float)
        */
@@ -74,6 +74,7 @@
        * Call a "virtual" member of a structure, aka, a member with a pointer.
        */
       static llvm::Value* callVirtualMember( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, int _member_index , std::vector<llvm::Value*> arguments);
+      static llvm::Value* callImageWrapData( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, const GTLCore::Type* _imageType, llvm::Value* _imageWrap, llvm::Value* _x, llvm::Value* _y );
     private:
       static int s_evaluatePixelesId;
   };

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp	2008-06-29 08:53:15 UTC (rev 265)
@@ -124,7 +124,7 @@
   if(d->source.empty()) return;
   cleanup();
   d->moduleData = new GTLCore::ModuleData(new llvm::Module(d->name));
-  Wrapper::fillTypeManager( d->moduleData->llvmModule(), d->moduleData->typeManager(), d->count_channels_generic );
+  Wrapper::fillTypeManager( d->moduleData, d->moduleData->typeManager(), d->count_channels_generic );
   Compiler c;
   GTLCore::String nameSpace;
   bool result = c.compile( d->source, d->name, d->moduleData, nameSpace );

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2008-06-29 08:53:15 UTC (rev 265)
@@ -75,6 +75,8 @@
 
 llvm::BasicBlock* PixelVisitor::set( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType, bool _allocatedInMemory) const
 {
+  SHIVA_DEBUG( *_pointerType << " " << *_valueType );
+  SHIVA_DEBUG( *_pointer << " " << *_value );
   new llvm::StoreInst( _value,
                        CodeGenerator::accessPixelDataPtr( _generationContext, _currentBlock, _pointer ),
                        "",

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2008-06-29 08:53:15 UTC (rev 265)
@@ -24,10 +24,12 @@
 
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
+#include <llvm/Module.h>
 
 #include "GTLCore/Function_p.h"
 #include "GTLCore/Macros.h"
 #include "GTLCore/Macros_p.h"
+#include "GTLCore/ModuleData_p.h"
 #include "GTLCore/Parameter.h"
 #include "GTLCore/PixelDescription.h"
 #include "GTLCore/Type.h"
@@ -82,9 +84,9 @@
     // Wrap functions doesn't exist yet, generate them
     WrappedFunctions wf;
     wf.memToVec = GTLCore::VirtualMachine::instance()->getPointerToFunction(
-        CodeGenerator::generateMemToVec( d->kernel, d->moduleData, _abstractImage->pixelDescription()  ) );
+        CodeGenerator::generateMemToVec( d->moduleData, _abstractImage->pixelDescription()  ) );
     wf.vecToMem = GTLCore::VirtualMachine::instance()->getPointerToFunction(
-        CodeGenerator::generateVecToMem( d->kernel, d->moduleData, _abstractImage->pixelDescription()  ) );
+        CodeGenerator::generateVecToMem( d->moduleData, _abstractImage->pixelDescription()  ) );
     d->imageFunctions[ _abstractImage->pixelDescription() ] = wf;
     owrap->memToVec = wf.memToVec;
     owrap->vecToMem = wf.vecToMem;
@@ -96,21 +98,21 @@
   return owrap;
 }
 
-void Wrapper::fillTypeManager( llvm::Module* _module, GTLCore::TypeManager* _typeManager, int _channels )
+void Wrapper::fillTypeManager( GTLCore::ModuleData* _module, GTLCore::TypeManager* _typeManager, int _channels )
 {
   SHIVA_DEBUG("fillTypeManager");
   for( int i = 1; i <= 5; ++i)
   {
     // Create Image type
-    createImageType( _module, _typeManager, GTLCore::String::number( i ), i );
-    createPixelType( _module, _typeManager, GTLCore::String::number( i ), i );
+    const GTLCore::Type* pixelType = createPixelType( _module->llvmModule(), _typeManager, GTLCore::String::number( i ), i );
+    createImageType( _module, _typeManager, GTLCore::String::number( i ), i, pixelType );
   }
-  createImageType( _module, _typeManager, "", _channels );
-  createPixelType( _module, _typeManager, "", _channels );
-  createRegionType( _module, _typeManager );
+  const GTLCore::Type* pixelType = createPixelType( _module->llvmModule(), _typeManager, "", _channels );
+  createImageType( _module, _typeManager, "", _channels, pixelType );
+  createRegionType( _module->llvmModule(), _typeManager );
 }
 
-void Wrapper::createPixelType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels )
+const GTLCore::Type* Wrapper::createPixelType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels )
 {
   //---------------------- WARNING ----------------------//
   // Whenever the following structure is edited,         //
@@ -125,6 +127,7 @@
   
   const GTLCore::Type* type = _typeManager->d->createStructure( "pixel" + _suffix, pixelDataMembers );
   type->d->setVisitor( PixelVisitor::instance() );
+  return type;
 }
 
 void Wrapper::createRegionType( llvm::Module* _module, GTLCore::TypeManager* _typeManager )
@@ -176,15 +179,16 @@
 
 llvm::Function* Wrapper::image_wrap_dataFunction( llvm::Module* _module, const GTLCore::Type* _imageType )
 {
+  GTL_ASSERT( _imageType );
   std::vector<const llvm::Type*> functionArgs;
   functionArgs.push_back(llvm::PointerType::get( _imageType->d->type(), 0));
   functionArgs.push_back(llvm::IntegerType::get(32));
   functionArgs.push_back(llvm::IntegerType::get(32));
   llvm::FunctionType* functionTy = llvm::FunctionType::get( llvm::PointerType::get(llvm::IntegerType::get(8), 0), functionArgs, false);
-  return new llvm::Function( functionTy, llvm::GlobalValue::ExternalLinkage, "image_wrap_data", _module);
+  return (llvm::Function*)_module->getOrInsertFunction( "image_wrap_data", functionTy);
 }
 
-llvm::FunctionType* Wrapper::image_wrap_sample_nearest( GTLCore::TypeManager* _typeManager, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType )
+llvm::FunctionType* Wrapper::image_wrap_sample_nearest_type( GTLCore::TypeManager* _typeManager, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType )
 {
   std::vector<const llvm::Type*> functionArgs;
   functionArgs.push_back( _imageType->d->pointerType() );
@@ -208,7 +212,7 @@
   return llvm::FunctionType::get( llvm::Type::VoidTy, functionArgs, false);
 }
 
-void Wrapper::createImageType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels )
+void Wrapper::createImageType( GTLCore::ModuleData* _moduleData, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels, const GTLCore::Type* _pixelType )
 {
   //---------------------- WARNING ----------------------//
   // Whenever the following structure is edited,         //
@@ -225,7 +229,14 @@
         "vecToMem",
         GTLCore::Type::Private::createArbitraryType(
             llvm::PointerType::get( image_wrap_vec_float_to_mem_type( _typeManager, _channels ), 0 ) ) ) );
-  *_typeManager->d->createStructure( "image" + _suffix, imageDataMembers )->d->type();
+  const GTLCore::Type* type = _typeManager->d->createStructure( "image" + _suffix, imageDataMembers );
+  
+  type->d->addFunctionMember( GTLCore::Type::StructFunctionMember(
+          GTLCore::Function::Private::createInternalFunction(
+                _moduleData->llvmModule(), "sampleNearest",
+                CodeGenerator::generateImageSampleNearest( _moduleData, type, _pixelType ),
+                _pixelType, 2,
+                type, _moduleData->typeManager()->getVector( GTLCore::Type::Float, 2 ) ) ) );
 }
 
 const GTLCore::Type* Wrapper::vectorType( GTLCore::TypeManager* _typeManager, int _channels )

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h	2008-06-29 08:50:27 UTC (rev 264)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h	2008-06-29 08:53:15 UTC (rev 265)
@@ -54,9 +54,9 @@
       struct Private;
       Private* const d;
     public:
-      static void fillTypeManager( llvm::Module* _module, GTLCore::TypeManager*, int _channels );
+      static void fillTypeManager( GTLCore::ModuleData* _module, GTLCore::TypeManager*, int _channels );
       static llvm::Function* image_wrap_dataFunction( llvm::Module* _module, const GTLCore::Type* _imageType );
-      static llvm::FunctionType* image_wrap_sample_nearest( GTLCore::TypeManager* _typeManager, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType );
+      static llvm::FunctionType* image_wrap_sample_nearest_type( GTLCore::TypeManager* _typeManager, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType );
       static llvm::FunctionType* image_wrap_mem_to_vec_float_type( GTLCore::TypeManager* _typeManager, int _channels );
       static llvm::FunctionType* image_wrap_vec_float_to_mem_type( GTLCore::TypeManager* _typeManager, int _channels );
       /**
@@ -65,8 +65,8 @@
       static const GTLCore::Type* vectorType( GTLCore::TypeManager* _typeManager, int _channels );
     private:
       static void createRegionType( llvm::Module* _module, GTLCore::TypeManager* _typeManager );
-      static void createPixelType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels );
-      static void createImageType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels );
+      static const GTLCore::Type* createPixelType( llvm::Module* _module, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels );
+      static void createImageType( GTLCore::ModuleData* _moduleData, GTLCore::TypeManager* _typeManager, const GTLCore::String& _suffix, int _channels, const GTLCore::Type* _pixelType );
   };
 }
 


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