Commit 843c7e157dd90f27bd530638b2e31162ee693be6

Authored by Josh Klontz
1 parent 5c84b9d1

llvm abstraction fix, bug remains

Showing 1 changed file with 58 additions and 36 deletions
sdk/plugins/llvm.cpp
@@ -105,26 +105,41 @@ struct MatrixBuilder @@ -105,26 +105,41 @@ struct MatrixBuilder
105 Constant *autoConstant(double value) const { return m->isFloating() ? ((m->bits() == 64) ? constant(value) : constant(float(value))) : constant(int(value), m->bits()); } 105 Constant *autoConstant(double value) const { return m->isFloating() ? ((m->bits() == 64) ? constant(value) : constant(float(value))) : constant(int(value), m->bits()); }
106 AllocaInst *autoAlloca(double value, const Twine &name = "") const { AllocaInst *alloca = b->CreateAlloca(ty(), 0, name); b->CreateStore(autoConstant(value), alloca); return alloca; } 106 AllocaInst *autoAlloca(double value, const Twine &name = "") const { AllocaInst *alloca = b->CreateAlloca(ty(), 0, name); b->CreateStore(autoConstant(value), alloca); return alloca; }
107 107
108 - Value *data(bool cast = true) const { LoadInst *data = b->CreateLoad(b->CreateStructGEP(v, 0), name+"_data"); return cast ? b->CreatePointerCast(data, ptrTy()) : data; }  
109 - Value *channels() const { return m->singleChannel() ? static_cast<Value*>(one()) : static_cast<Value*>(b->CreateLoad(b->CreateStructGEP(v, 1), name+"_channels")); }  
110 - Value *columns() const { return m->singleColumn() ? static_cast<Value*>(one()) : static_cast<Value*>(b->CreateLoad(b->CreateStructGEP(v, 2), name+"_columns")); }  
111 - Value *rows() const { return m->singleRow() ? static_cast<Value*>(one()) : static_cast<Value*>(b->CreateLoad(b->CreateStructGEP(v, 3), name+"_rows")); }  
112 - Value *frames() const { return m->singleFrame() ? static_cast<Value*>(one()) : static_cast<Value*>(b->CreateLoad(b->CreateStructGEP(v, 4), name+"_frames")); }  
113 - Value *hash() const { return b->CreateLoad(b->CreateStructGEP(v, 5), name+"_hash"); }  
114 -  
115 - void setData(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 0)); }  
116 - void setChannels(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 1)); }  
117 - void setColumns(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 2)); }  
118 - void setRows(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 3)); }  
119 - void setFrames(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 4)); }  
120 - void setHash(Value *value) const { b->CreateStore(value, b->CreateStructGEP(v, 5)); }  
121 -  
122 - void copyHeader(const MatrixBuilder &other) const {  
123 - setChannels(other.channels());  
124 - setColumns(other.columns());  
125 - setRows(other.rows());  
126 - setFrames(other.frames());  
127 - setHash(other.hash()); 108 + Value *data(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 0), name+"_data"); }
  109 + Value *data(Value *matrix, Type *type, const Twine &name = "") const { return b->CreatePointerCast(data(matrix, name), type); }
  110 + Value *channels(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 1), name+"_channels"); }
  111 + Value *columns(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 2), name+"_columns"); }
  112 + Value *rows(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 3), name+"_rows"); }
  113 + Value *frames(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 4), name+"_frames"); }
  114 + Value *hash(Value *matrix, const Twine &name = "") const { return b->CreateLoad(b->CreateStructGEP(matrix, 5), name+"_hash"); }
  115 +
  116 + Value *data(bool cast = true) const { return cast ? data(v, ptrTy(), name) : data(v, name); }
  117 + Value *channels() const { return m->singleChannel() ? static_cast<Value*>(one()) : channels(v, name); }
  118 + Value *columns() const { return m->singleColumn() ? static_cast<Value*>(one()) : columns(v, name); }
  119 + Value *rows() const { return m->singleRow() ? static_cast<Value*>(one()) : rows(v, name); }
  120 + Value *frames() const { return m->singleFrame() ? static_cast<Value*>(one()) : frames(v, name); }
  121 + Value *hash() const { return hash(v, name); }
  122 +
  123 + void setData(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 0)); }
  124 + void setChannels(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 1)); }
  125 + void setColumns(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 2)); }
  126 + void setRows(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 3)); }
  127 + void setFrames(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 4)); }
  128 + void setHash(Value *matrix, Value *value) const { b->CreateStore(value, b->CreateStructGEP(matrix, 5)); }
  129 +
  130 + void setData(Value *value) const { setData(v, value); }
  131 + void setChannels(Value *value) const { setChannels(v, value); }
  132 + void setColumns(Value *value) const { setColumns(v, value); }
  133 + void setRows(Value *value) const { setRows(v, value); }
  134 + void setFrames(Value *value) const { setFrames(v, value); }
  135 + void setHash(Value *value) const { setHash(v, value); }
  136 +
  137 + void copyHeaderTo(Value *matrix) const {
  138 + setChannels(matrix, channels());
  139 + setColumns(matrix, columns());
  140 + setRows(matrix, rows());
  141 + setFrames(matrix, frames());
  142 + setHash(matrix, hash());
128 } 143 }
129 144
130 void deallocate() const { 145 void deallocate() const {
@@ -222,7 +237,12 @@ struct MatrixBuilder @@ -222,7 +237,12 @@ struct MatrixBuilder
222 deindex(rem, c, x, y); 237 deindex(rem, c, x, y);
223 } 238 }
224 239
  240 + LoadInst *load(Value *matrix, Type *type, Value *i) const { return b->CreateLoad(b->CreateGEP(data(matrix, type), i)); }
225 LoadInst *load(Value *i) const { return b->CreateLoad(b->CreateGEP(data(), i)); } 241 LoadInst *load(Value *i) const { return b->CreateLoad(b->CreateGEP(data(), i)); }
  242 + StoreInst *store(Value *matrix, Type *type, Value *i, Value *value) const { Value *d = data(matrix, type);
  243 + d->dump(); i->dump();
  244 + Value *idx = b->CreateGEP(d, i);
  245 + return b->CreateStore(value, idx); }
226 StoreInst *store(Value *i, Value *value) const { return b->CreateStore(value, b->CreateGEP(data(), i)); } 246 StoreInst *store(Value *i, Value *value) const { return b->CreateStore(value, b->CreateGEP(data(), i)); }
227 247
228 Value *cast(Value *i, const MatrixBuilder &dst) const { return (m->type() == dst.m->type()) ? i : b->CreateCast(CastInst::getCastOpcode(i, m->isSigned(), dst.ty(), dst.m->isSigned()), i, dst.ty()); } 248 Value *cast(Value *i, const MatrixBuilder &dst) const { return (m->type() == dst.m->type()) ? i : b->CreateCast(CastInst::getCastOpcode(i, m->isSigned(), dst.ty(), dst.m->isSigned()), i, dst.ty()); }
@@ -322,8 +342,8 @@ public: @@ -322,8 +342,8 @@ public:
322 fileTable.insert(fileIndex, file); 342 fileTable.insert(fileIndex, file);
323 } 343 }
324 344
325 - virtual Value *preallocation(const MatrixBuilder &src, const MatrixBuilder &dst) const = 0; /*!< Allocate the destintation matrix given the source matrix. */  
326 - virtual void kernel(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const = 0; /*!< Run the computation given the source matrix. */ 345 + virtual Value *preallocation(const MatrixBuilder &src, Value *dst) const = 0; /*!< Allocate the destintation matrix given the source matrix. */
  346 + virtual void kernel(const MatrixBuilder &src, Value *dst, PHINode *i) const = 0; /*!< Run the computation given the source matrix. */
327 347
328 void optimize(Function *f) const 348 void optimize(Function *f) const
329 { 349 {
@@ -460,10 +480,9 @@ public: @@ -460,10 +480,9 @@ public:
460 BasicBlock *entry = BasicBlock::Create(getGlobalContext(), "entry", function); 480 BasicBlock *entry = BasicBlock::Create(getGlobalContext(), "entry", function);
461 IRBuilder<> builder(entry); 481 IRBuilder<> builder(entry);
462 MatrixBuilder mb(m, src, &builder, function, "src"); 482 MatrixBuilder mb(m, src, &builder, function, "src");
463 - MatrixBuilder nb(NULL, dst, &builder, function, "dst");  
464 483
465 - Value *kernelSize = preallocation(mb, nb);  
466 - nb.setData(builder.CreateCall(malloc, nb.bytes())); 484 + Value *kernelSize = preallocation(mb, dst);
  485 + mb.setData(dst, builder.CreateCall(malloc, kernelSize));
467 builder.CreateRet(kernelSize); 486 builder.CreateRet(kernelSize);
468 487
469 optimize(function); 488 optimize(function);
@@ -500,8 +519,7 @@ public: @@ -500,8 +519,7 @@ public:
500 BasicBlock *loop, *exit; 519 BasicBlock *loop, *exit;
501 PHINode *i = MatrixBuilder::beginLoop(builder, function, entry, loop, exit, len, "i"); 520 PHINode *i = MatrixBuilder::beginLoop(builder, function, entry, loop, exit, len, "i");
502 521
503 - Matrix n;  
504 - kernel(MatrixBuilder(m, src, &builder, function, "src"), MatrixBuilder(&n, dst, &builder, function, "dst"), i); 522 + kernel(MatrixBuilder(m, src, &builder, function, "src"), dst, i);
505 523
506 MatrixBuilder::endLoop(builder, loop, exit); 524 MatrixBuilder::endLoop(builder, loop, exit);
507 builder.CreateRetVoid(); 525 builder.CreateRetVoid();
@@ -643,18 +661,18 @@ class StitchableTransform : public UnaryTransform @@ -643,18 +661,18 @@ class StitchableTransform : public UnaryTransform
643 Q_OBJECT 661 Q_OBJECT
644 662
645 public: 663 public:
646 - virtual Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const = 0; /*!< A simplification of Kernel::build() for stitchable kernels. */ 664 + virtual Value *stitch(const MatrixBuilder &src, Value *val) const = 0; /*!< A simplification of Kernel::build() for stitchable kernels. */
647 665
648 - virtual Value *preallocation(const MatrixBuilder &src, const MatrixBuilder &dst) const 666 + virtual Value *preallocation(const MatrixBuilder &src, Value *dst) const
649 { 667 {
650 - dst.copyHeader(src);  
651 - return dst.elements(); 668 + src.copyHeaderTo(dst);
  669 + return src.elements();
652 } 670 }
653 671
654 private: 672 private:
655 - void kernel(const MatrixBuilder &src, const MatrixBuilder &dst, PHINode *i) const 673 + void kernel(const MatrixBuilder &src, Value *dst, PHINode *i) const
656 { 674 {
657 - dst.store(i, stitch(src, dst, src.load(i))); 675 + src.store(dst, src.ty(), i, stitch(src, src.load(i)));
658 } 676 }
659 }; 677 };
660 678
@@ -949,10 +967,9 @@ class addTransform : public StitchableTransform @@ -949,10 +967,9 @@ class addTransform : public StitchableTransform
949 Q_PROPERTY(double b READ get_b WRITE set_b RESET reset_b STORED false) 967 Q_PROPERTY(double b READ get_b WRITE set_b RESET reset_b STORED false)
950 BR_PROPERTY(double, b, 0) 968 BR_PROPERTY(double, b, 0)
951 969
952 - Value *stitch(const MatrixBuilder &src, const MatrixBuilder &dst, Value *val) const 970 + Value *stitch(const MatrixBuilder &src, Value *val) const
953 { 971 {
954 - (void) src;  
955 - return dst.add(val, dst.autoConstant(b)); 972 + return src.add(val, src.autoConstant(b));
956 } 973 }
957 }; 974 };
958 975
@@ -1064,6 +1081,11 @@ class LLVMInitializer : public Initializer @@ -1064,6 +1081,11 @@ class LLVMInitializer : public Initializer
1064 Type::getInt16Ty(getGlobalContext()), // hash 1081 Type::getInt16Ty(getGlobalContext()), // hash
1065 NULL); 1082 NULL);
1066 1083
  1084 +// Matrix m(1, 2, 3, 4, Matrix::f32);
  1085 +// UnaryAllocation allocation = makeUnaryAllocation("add(1)", &m);
  1086 +// Matrix n;
  1087 +// allocation(&m, &n);
  1088 +// qDebug() << n.channels << n.rows << n.columns << n.frames << n.hash;
1067 QSharedPointer<Transform> kernel(Transform::make("add(1)", NULL)); 1089 QSharedPointer<Transform> kernel(Transform::make("add(1)", NULL));
1068 1090
1069 Template src, dst; 1091 Template src, dst;