Commit 78254b65124d40642c7f6f4e4f437dcd4b0b9d78
1 parent
3ae7332d
substantial LLVM progress
Showing
2 changed files
with
65 additions
and
17 deletions
sdk/jitcv/jitcv.h
| ... | ... | @@ -3,9 +3,7 @@ |
| 3 | 3 | |
| 4 | 4 | #include <stddef.h> |
| 5 | 5 | #include <stdint.h> |
| 6 | - | |
| 7 | -namespace jitcv | |
| 8 | -{ | |
| 6 | +#include <openbr.h> | |
| 9 | 7 | |
| 10 | 8 | /*! |
| 11 | 9 | * \brief jitcv matrix |
| ... | ... | @@ -83,16 +81,22 @@ struct Matrix |
| 83 | 81 | inline uint32_t bytes() const { return bits() / 8 * elements(); } |
| 84 | 82 | }; |
| 85 | 83 | |
| 84 | +#ifdef __cplusplus | |
| 85 | +extern "C" { | |
| 86 | +#endif | |
| 87 | + | |
| 86 | 88 | typedef void (*UnaryFunction)(const Matrix *src, Matrix *dst); |
| 87 | 89 | typedef void (*BinaryFunction)(const Matrix *srcA, const Matrix *srcB, Matrix *dst); |
| 88 | -UnaryFunction makeUnaryFunction(const char *description); | |
| 89 | -BinaryFunction makeBinaryFunction(const char *description); | |
| 90 | +BR_EXPORT UnaryFunction makeUnaryFunction(const char *description); | |
| 91 | +BR_EXPORT BinaryFunction makeBinaryFunction(const char *description); | |
| 90 | 92 | |
| 91 | 93 | typedef void (*UnaryKernel)(const Matrix *src, Matrix *dst, uint32_t size); |
| 92 | 94 | typedef void (*BinaryKernel)(const Matrix *srcA, const Matrix *srcB, Matrix *dst, uint32_t size); |
| 93 | -UnaryKernel makeUnaryKernel(const char *description, const Matrix *src); | |
| 94 | -BinaryKernel makeBinaryKernel(const char *description, const Matrix *srcA, const Matrix *srcB); | |
| 95 | +BR_EXPORT UnaryKernel makeUnaryKernel(const char *description, const Matrix *src); | |
| 96 | +BR_EXPORT BinaryKernel makeBinaryKernel(const char *description, const Matrix *srcA, const Matrix *srcB); | |
| 95 | 97 | |
| 98 | +#ifdef __cplusplus | |
| 96 | 99 | } |
| 100 | +#endif | |
| 97 | 101 | |
| 98 | 102 | #endif // __JITCV_H | ... | ... |
sdk/plugins/llvm.cpp
| ... | ... | @@ -25,7 +25,6 @@ |
| 25 | 25 | |
| 26 | 26 | using namespace br; |
| 27 | 27 | using namespace cv; |
| 28 | -using namespace jitcv; | |
| 29 | 28 | using namespace llvm; |
| 30 | 29 | |
| 31 | 30 | static Module *TheModule = NULL; |
| ... | ... | @@ -33,6 +32,8 @@ static ExecutionEngine *TheExecutionEngine = NULL; |
| 33 | 32 | static FunctionPassManager *TheFunctionPassManager = NULL; |
| 34 | 33 | static FunctionPassManager *TheExtraFunctionPassManager = NULL; |
| 35 | 34 | static StructType *TheMatrixStruct = NULL; |
| 35 | +static Function *makeUnaryKernelFunction = NULL; | |
| 36 | +static Function *makeBinaryKernelFunction = NULL; | |
| 36 | 37 | |
| 37 | 38 | static QString MatrixToString(const Matrix &m) |
| 38 | 39 | { |
| ... | ... | @@ -326,12 +327,21 @@ namespace br |
| 326 | 327 | class UnaryTransform : public UntrainableMetaTransform |
| 327 | 328 | { |
| 328 | 329 | Q_OBJECT |
| 329 | - | |
| 330 | + uint32_t fileIndex; | |
| 330 | 331 | UnaryKernel kernel; |
| 331 | - quint16 hash; | |
| 332 | + uint16_t hash; | |
| 332 | 333 | |
| 333 | 334 | public: |
| 334 | - UnaryTransform() : kernel(NULL), hash(0) {} | |
| 335 | + static QHash<uint32_t, File> fileTable; | |
| 336 | + | |
| 337 | + UnaryTransform() : fileIndex(0), kernel(NULL), hash(0) {} | |
| 338 | + | |
| 339 | + void init() | |
| 340 | + { | |
| 341 | + fileIndex = fileTable.size()+1; | |
| 342 | + fileTable.insert(fileIndex, file); | |
| 343 | + } | |
| 344 | + | |
| 335 | 345 | virtual int preallocate(const Matrix &src, Matrix &dst) const = 0; /*!< Preallocate destintation matrix based on source matrix. */ |
| 336 | 346 | virtual Value *buildPreallocate(const MatrixBuilder &src, const MatrixBuilder &dst) const { (void) src; (void) dst; return MatrixBuilder::constant(0); } |
| 337 | 347 | virtual void build(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const = 0; /*!< Build the kernel. */ |
| ... | ... | @@ -375,9 +385,6 @@ private: |
| 375 | 385 | |
| 376 | 386 | Function *compile(const Matrix &m) const |
| 377 | 387 | { |
| 378 | - Function *kernel = compileKernel(m); | |
| 379 | - optimize(kernel); | |
| 380 | - | |
| 381 | 388 | Constant *c = TheModule->getOrInsertFunction(qPrintable(mangledName()), |
| 382 | 389 | Type::getVoidTy(getGlobalContext()), |
| 383 | 390 | PointerType::getUnqual(TheMatrixStruct), |
| ... | ... | @@ -419,7 +426,10 @@ private: |
| 419 | 426 | builder.CreateCondBr(hashTest, getKernel, preallocate); |
| 420 | 427 | |
| 421 | 428 | builder.SetInsertPoint(getKernel); |
| 422 | - builder.CreateStore(kernel, kernelFunction); | |
| 429 | + builder.CreateStore(builder.CreateCall2(makeUnaryKernelFunction, | |
| 430 | + builder.CreateIntToPtr(MatrixBuilder::constant(fileIndex, 32), Type::getInt8PtrTy(getGlobalContext())), | |
| 431 | + src), | |
| 432 | + kernelFunction); | |
| 423 | 433 | builder.CreateStore(mb.hash(), kernelHash); |
| 424 | 434 | builder.CreateBr(preallocate); |
| 425 | 435 | builder.SetInsertPoint(preallocate); |
| ... | ... | @@ -486,6 +496,8 @@ private: |
| 486 | 496 | } |
| 487 | 497 | }; |
| 488 | 498 | |
| 499 | +QHash<uint32_t, File> UnaryTransform::fileTable; | |
| 500 | + | |
| 489 | 501 | /*! |
| 490 | 502 | * \brief LLVM Binary Kernel |
| 491 | 503 | * \author Josh Klontz \cite jklontz |
| ... | ... | @@ -1017,6 +1029,34 @@ class LLVMInitializer : public Initializer |
| 1017 | 1029 | Type::getInt16Ty(getGlobalContext()), // hash |
| 1018 | 1030 | NULL); |
| 1019 | 1031 | |
| 1032 | + std::vector<Type*> unaryKernelParams; | |
| 1033 | + unaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1034 | + unaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1035 | + unaryKernelParams.push_back(Type::getInt32Ty(getGlobalContext())); | |
| 1036 | + Type *unaryKernelReturn = Type::getVoidTy(getGlobalContext()); | |
| 1037 | + Type *unaryKernelType = PointerType::getUnqual(FunctionType::get(unaryKernelReturn, unaryKernelParams, false)); | |
| 1038 | + std::vector<Type*> makeUnaryKernelParams; | |
| 1039 | + makeUnaryKernelParams.push_back(Type::getInt8PtrTy(getGlobalContext())); | |
| 1040 | + makeUnaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1041 | + FunctionType* makeUnaryKernelType = FunctionType::get(unaryKernelType, makeUnaryKernelParams, false); | |
| 1042 | + makeUnaryKernelFunction = Function::Create(makeUnaryKernelType, GlobalValue::ExternalLinkage, "makeUnaryKernel", TheModule); | |
| 1043 | + makeUnaryKernelFunction->setCallingConv(CallingConv::C); | |
| 1044 | + | |
| 1045 | + std::vector<Type*> binaryKernelParams; | |
| 1046 | + binaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1047 | + binaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1048 | + binaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1049 | + binaryKernelParams.push_back(Type::getInt32Ty(getGlobalContext())); | |
| 1050 | + Type *binaryKernelReturn = Type::getVoidTy(getGlobalContext()); | |
| 1051 | + Type *binaryKernelType = PointerType::getUnqual(FunctionType::get(binaryKernelReturn, binaryKernelParams, false)); | |
| 1052 | + std::vector<Type*> makeBinaryKernelParams; | |
| 1053 | + makeBinaryKernelParams.push_back(Type::getInt8PtrTy(getGlobalContext())); | |
| 1054 | + makeBinaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1055 | + makeBinaryKernelParams.push_back(PointerType::getUnqual(TheMatrixStruct)); | |
| 1056 | + FunctionType* makeBinaryKernelType = FunctionType::get(binaryKernelType, makeBinaryKernelParams, false); | |
| 1057 | + makeBinaryKernelFunction = Function::Create(makeBinaryKernelType, GlobalValue::ExternalLinkage, "makeBinaryKernel", TheModule); | |
| 1058 | + makeBinaryKernelFunction->setCallingConv(CallingConv::C); | |
| 1059 | + | |
| 1020 | 1060 | QSharedPointer<Transform> kernel(Transform::make("add(1)", NULL)); |
| 1021 | 1061 | |
| 1022 | 1062 | Template src, dst; |
| ... | ... | @@ -1082,7 +1122,8 @@ BR_REGISTER(Initializer, LLVMInitializer) |
| 1082 | 1122 | |
| 1083 | 1123 | UnaryFunction makeUnaryFunction(const char *description) |
| 1084 | 1124 | { |
| 1085 | - QScopedPointer<UnaryTransform> unaryTransform(Factory<UnaryTransform>::make(description)); | |
| 1125 | + QScopedPointer<UnaryTransform> unaryTransform(dynamic_cast<UnaryTransform*>(Transform::make(description, NULL))); | |
| 1126 | + if (unaryTransform == NULL) qFatal("makeUnaryFunction NULL transform!"); | |
| 1086 | 1127 | return unaryTransform->getFunction(NULL); |
| 1087 | 1128 | } |
| 1088 | 1129 | |
| ... | ... | @@ -1094,7 +1135,10 @@ BinaryFunction makeBinaryFunction(const char *description) |
| 1094 | 1135 | |
| 1095 | 1136 | UnaryKernel makeUnaryKernel(const char *description, const Matrix *src) |
| 1096 | 1137 | { |
| 1097 | - QScopedPointer<UnaryTransform> unaryTransform(Factory<UnaryTransform>::make(description)); | |
| 1138 | + if (description == NULL) qFatal("makeUnaryKernel NULL description!"); | |
| 1139 | + const File f = long(description) < 1000 ? UnaryTransform::fileTable[long(description)] : File(description); | |
| 1140 | + QScopedPointer<UnaryTransform> unaryTransform(dynamic_cast<UnaryTransform*>(Transform::make(f, NULL))); | |
| 1141 | + if (unaryTransform == NULL) qFatal("makeUnaryKernel NULL transform!"); | |
| 1098 | 1142 | return unaryTransform->getKernel(src); |
| 1099 | 1143 | } |
| 1100 | 1144 | ... | ... |