hostsupport/hostopenvg/src/sfCompiler.h
branchbug235_bringup_0
changeset 53 c2ef9095503a
parent 24 a3f46bb01be2
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and /or associated documentation files
       
     5  * (the "Materials "), to deal in the Materials without restriction,
       
     6  * including without limitation the rights to use, copy, modify, merge,
       
     7  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
     8  * and to permit persons to whom the Materials are furnished to do so,
       
     9  * subject to the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    20  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  */
       
    22 
       
    23 #ifndef __SFCOMPILER_H
       
    24 #define __SFCOMPILER_H
       
    25 
       
    26 #include <string>
       
    27 
       
    28 #ifndef __SFDYNAMICPIXELPIPE_H
       
    29 #   include "sfDynamicPixelPipe.h"
       
    30 #endif
       
    31 
       
    32 #ifndef __SFDYNAMICBLITTER_H
       
    33 #   include "sfDynamicBlitter.h"
       
    34 #endif
       
    35 
       
    36 #ifndef __RIPIXELPIPE_H
       
    37 #   include "riPixelPipe.h"
       
    38 #endif
       
    39 
       
    40 #ifndef __SFFUNCTIONCACHE_H
       
    41 #   include "sfFunctionCache.h"
       
    42 #endif
       
    43 
       
    44 #include "llvm/LLVMContext.h"
       
    45 
       
    46 // \note PPCompiler class also caches a certain amount of compiled functions.
       
    47 // It may make sense to move the cache into a separate container.
       
    48 
       
    49 // LLVM forward declarations
       
    50 namespace llvm { 
       
    51     class Type;
       
    52     class Constant;
       
    53     class Function;
       
    54     class Module;
       
    55     class ExecutionEngine;
       
    56 }
       
    57 
       
    58 namespace OpenVGRI {
       
    59 
       
    60 // Pixel-pipeline function with constant state removed:
       
    61 typedef void (*PixelPipeFunction)(const PixelPipe::PPUniforms&, PixelPipe::PPVariants&, const Span*, int);
       
    62 // Image-blitting function with constant state removed:
       
    63 typedef void (*BlitterFunction)(const DynamicBlitter::BlitUniforms&);
       
    64 
       
    65 class PPCompiler
       
    66 {
       
    67 public:
       
    68     typedef int PixelPipeHandle;
       
    69     typedef int BlitterHandle;
       
    70 private:
       
    71     // Function that generates LLVM-constant from a pixel-pipeline:
       
    72     typedef ::llvm::Constant* (*ConstantGenFunc)(const void* structure, ::llvm::LLVMContext& llvmContext, const ::llvm::Type* structType);
       
    73 
       
    74     struct PPCompilerContext {
       
    75         PPCompilerContext();
       
    76         ~PPCompilerContext();
       
    77         // Stores persistent objects related to each component (pixelpipe or blitter).
       
    78         // Note that the execution engine must be a per-process singleton for LLVM before
       
    79         // version 2.7!
       
    80         ::llvm::Module* module;
       
    81         ::llvm::Function* llvmFunction; 
       
    82     };
       
    83 
       
    84     struct PartialEvalFunc
       
    85     {
       
    86         ::llvm::Function*   llvmFunc;
       
    87         ::llvm::GlobalVariable*   llvmConst;
       
    88     };
       
    89 
       
    90 public:
       
    91     PPCompiler();
       
    92     ~PPCompiler();
       
    93 
       
    94     static PartialEvalFunc compilePixelPipeline(::llvm::LLVMContext& llvmContext, PPCompilerContext& compilerContext, ConstantGenFunc constGenFunc, const void* state, const std::string& newFuntionName);
       
    95 
       
    96     // These functions get an reserve a handle to a pixelpipe/blitter. MUST use release
       
    97     // after done with the function.
       
    98     PixelPipeHandle     compilePixelPipeline(const PixelPipe::SignatureState& state);
       
    99     BlitterHandle       compileBlitter(const DynamicBlitter::BlitSignatureState& state);
       
   100 
       
   101     PixelPipeFunction   getPixelPipePtr(PixelPipeHandle handle);
       
   102     BlitterFunction     getBlitterPtr(BlitterHandle handle);
       
   103 
       
   104     void releasePixelPipeline(PixelPipeHandle handle);
       
   105     void releaseBlitter(BlitterHandle handle);
       
   106 
       
   107     bool init();
       
   108 
       
   109     // It seems that under VS, the static init order is not correct so the compiler has to be created
       
   110     // during run-time.
       
   111     static PPCompiler&  getCompiler() { if(!s_compiler) { s_compiler = new PPCompiler(); } return *s_compiler; }
       
   112 
       
   113 private:
       
   114     bool initPPContext(PPCompilerContext& context, const unsigned char* data, size_t dataSize, const char* functionName);
       
   115     //void* compileRenderingFunction(const void* signatureState, RenderingFunctionType type);
       
   116 
       
   117     static ::llvm::Constant* createConstantStruct(const void* structure, size_t structureSize, ::llvm::LLVMContext& llvmContext, const ::llvm::Type* structType);
       
   118     static ::llvm::Constant* createPPConstant(const void* signatureState, ::llvm::LLVMContext& llvmContext, const ::llvm::Type* structType);
       
   119     static ::llvm::Constant* createBlitterConstant(const void* signatureState, ::llvm::LLVMContext& llvmContext, const ::llvm::Type* structType);
       
   120     static ::llvm::Function* findFunctionWithString(::llvm::Module* module, const char* namepart);
       
   121     static void llvmCheckPtrError(const void* ptr, std::string& err);
       
   122 
       
   123     static std::string stringOfArray(const RIuint32* arr, int nElems);
       
   124 
       
   125 private:
       
   126     //::llvm::LLVMContext& getLLVMContext() { return ::llvm::getGlobalContext(); }
       
   127     ::llvm::LLVMContext& getLLVMContext() { return m_llvmContext; }
       
   128 
       
   129     // The order is important atm. because llvm context must be destroyed last:
       
   130     //::llvm::LLVMContext& m_llvmContext;
       
   131     PPCompilerContext m_blitterContext;
       
   132     PPCompilerContext m_ppContext;
       
   133     ::llvm::ExecutionEngine* m_executionEngine;
       
   134 
       
   135     // \note Loading a system with LLVM already consumes a lot of memory, so
       
   136     // the amount of cached functions can be grown substantially depending on
       
   137     // requirements.
       
   138     enum { NUM_CACHED_PIXELPIPES = 64 };
       
   139     enum { NUM_CACHED_BLITTERS = NUM_CACHED_PIXELPIPES };
       
   140     
       
   141     FunctionCache<PixelPipeHash> m_ppCache;
       
   142     FunctionCache<BlitterHash> m_blitterCache;
       
   143     typedef FunctionCache<PixelPipeHash>::EntryHandle PixelPipeEntryHandle;
       
   144     typedef FunctionCache<BlitterHash>::EntryHandle BlitterEntryHandle;
       
   145     //std::vector<CacheEntry<BlitterHash> > blitterCache;
       
   146     
       
   147     ::llvm::LLVMContext m_llvmContext;
       
   148 
       
   149     static PPCompiler* s_compiler;
       
   150 };
       
   151 
       
   152 }
       
   153 
       
   154 
       
   155 #endif
       
   156