@@ -25,9 +25,10 @@ Expected<std::string> codegenIR(Module &module, unsigned items) {
2525 LLVMContext &ctx = module .getContext ();
2626 IRBuilder<> B (ctx);
2727
28- auto name = " abssub " ;
28+ auto name = " forabssub " ;
2929 auto intTy = Type::getInt32Ty (ctx);
30- auto signature = FunctionType::get (intTy, {intTy, intTy}, false );
30+ auto intPtrTy = intTy->getPointerTo ();
31+ auto signature = FunctionType::get (intPtrTy, {intPtrTy, intPtrTy, intPtrTy}, false );
3132 auto linkage = Function::ExternalLinkage;
3233
3334 auto fn = Function::Create (signature, linkage, name, module );
@@ -36,15 +37,28 @@ Expected<std::string> codegenIR(Module &module, unsigned items) {
3637 {
3738 Argument *argX = fn->arg_begin ();
3839 Argument *argY = fn->arg_begin () + 1 ;
39- argX->setName (" x" );
40- argY->setName (" y" );
41- Value *difference = B.CreateSub (argX, argY, " dist" );
40+ Argument *argR = fn->arg_begin () + 2 ;
41+ argX->setName (" xs_ptr" );
42+ argY->setName (" ys_ptr" );
43+ argR->setName (" rs_ptr" );
4244
4345 auto absSig = FunctionType::get (intTy, {intTy}, false );
4446 FunctionCallee absFunction = module .getOrInsertFunction (" abs" , absSig);
45- Value *absDifference = B.CreateCall (absFunction, {difference});
4647
47- B.CreateRet (absDifference);
48+ for (unsigned int i = 0 ; i < items; i++) {
49+ Value *xi_ptr = B.CreateConstInBoundsGEP1_32 (intTy, argX, i, " x_ptr" );
50+ Value *yi_ptr = B.CreateConstInBoundsGEP1_32 (intTy, argY, i, " y_ptr" );
51+
52+ Value *xi = B.CreateLoad (xi_ptr, " x" );
53+ Value *yi = B.CreateLoad (yi_ptr, " y" );
54+ Value *difference = B.CreateSub (xi, yi, " diff" );
55+ Value *absDifference = B.CreateCall (absFunction, {difference}, " dist" );
56+
57+ Value *ri_ptr = B.CreateConstInBoundsGEP1_32 (intTy, argR, i, " r_ptr" );
58+ B.CreateStore (absDifference, ri_ptr);
59+ }
60+
61+ B.CreateRet (argR);
4862 }
4963
5064 std::string buffer;
@@ -87,17 +101,15 @@ int *customIntAllocator(unsigned items) {
87101extern " C" int abs (int );
88102
89103// Temporary global variable to replace below function step-by-step.
90- std::function<int (int , int )> abssub ;
104+ std::function<int * (int * , int *, int * )> forabssub ;
91105
92106// This function will be replaced by a runtime-time compiled version.
93107template <size_t sizeOfArray>
94108int *integerDistances (const int (&x)[sizeOfArray], int *y) {
95109 unsigned items = arrayElements (x);
96110 int *results = customIntAllocator (items);
97111
98- for (int i = 0 ; i < items; i++) {
99- results[i] = abssub (x[i], y[i]);
100- }
112+ results = forabssub ((int *)x, y, results);
101113
102114 return results;
103115}
@@ -131,7 +143,7 @@ int main(int argc, char **argv) {
131143 ExitOnErr (Jit.submitModule (std::move (M), std::move (C)));
132144
133145 // Request function; this compiles to machine code and links.
134- abssub = ExitOnErr (Jit.getFunction <int (int , int )>(JitedFnName));
146+ forabssub = ExitOnErr (Jit.getFunction <int * (int * , int *, int * )>(JitedFnName));
135147
136148 int *z = integerDistances (x, y);
137149
0 commit comments