Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 2291ee4

Browse files
Emit call to allocator in JITed code
1 parent 2653289 commit 2291ee4

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

main.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Expected<std::string> codegenIR(Module &module, unsigned items) {
2525
LLVMContext &ctx = module.getContext();
2626
IRBuilder<> B(ctx);
2727

28-
auto name = "forabssub";
28+
auto name = "allocforabssub";
2929
auto intTy = Type::getInt32Ty(ctx);
3030
auto intPtrTy = intTy->getPointerTo();
31-
auto signature = FunctionType::get(intPtrTy, {intPtrTy, intPtrTy, intPtrTy}, false);
31+
auto signature = FunctionType::get(intPtrTy, {intPtrTy, intPtrTy}, false);
3232
auto linkage = Function::ExternalLinkage;
3333

3434
auto fn = Function::Create(signature, linkage, name, module);
@@ -37,14 +37,19 @@ Expected<std::string> codegenIR(Module &module, unsigned items) {
3737
{
3838
Argument *argX = fn->arg_begin();
3939
Argument *argY = fn->arg_begin() + 1;
40-
Argument *argR = fn->arg_begin() + 2;
4140
argX->setName("xs_ptr");
4241
argY->setName("ys_ptr");
43-
argR->setName("rs_ptr");
4442

4543
auto absSig = FunctionType::get(intTy, {intTy}, false);
4644
FunctionCallee absFunction = module.getOrInsertFunction("abs", absSig);
4745

46+
auto allocSig = FunctionType::get(intPtrTy, {intTy}, false);
47+
FunctionCallee allocFunction =
48+
module.getOrInsertFunction("customIntAllocator", allocSig);
49+
50+
Value *rs_count = ConstantInt::get(intTy, items);
51+
Value *rs_ptr = B.CreateCall(allocFunction, {rs_count}, "rs_ptr");
52+
4853
for (unsigned int i = 0; i < items; i++) {
4954
Value *xi_ptr = B.CreateConstInBoundsGEP1_32(intTy, argX, i, "x_ptr");
5055
Value *yi_ptr = B.CreateConstInBoundsGEP1_32(intTy, argY, i, "y_ptr");
@@ -54,11 +59,11 @@ Expected<std::string> codegenIR(Module &module, unsigned items) {
5459
Value *difference = B.CreateSub(xi, yi, "diff");
5560
Value *absDifference = B.CreateCall(absFunction, {difference}, "dist");
5661

57-
Value *ri_ptr = B.CreateConstInBoundsGEP1_32(intTy, argR, i, "r_ptr");
62+
Value *ri_ptr = B.CreateConstInBoundsGEP1_32(intTy, rs_ptr, i, "ri_ptr");
5863
B.CreateStore(absDifference, ri_ptr);
5964
}
6065

61-
B.CreateRet(argR);
66+
B.CreateRet(rs_ptr);
6267
}
6368

6469
std::string buffer;
@@ -84,7 +89,7 @@ constexpr unsigned arrayElements(T (&)[sizeOfArray]) {
8489
}
8590

8691
// This function will be called from JITed code.
87-
int *customIntAllocator(unsigned items) {
92+
extern "C" int *customIntAllocator(unsigned items) {
8893
static int memory[100];
8994
static unsigned allocIdx = 0;
9095

@@ -101,15 +106,15 @@ int *customIntAllocator(unsigned items) {
101106
extern "C" int abs(int);
102107

103108
// Temporary global variable to replace below function step-by-step.
104-
std::function<int* (int *, int *, int *)> forabssub;
109+
std::function<int* (int *, int *)> allocforabssub;
105110

106111
// This function will be replaced by a runtime-time compiled version.
107112
template <size_t sizeOfArray>
108113
int *integerDistances(const int (&x)[sizeOfArray], int *y) {
109114
unsigned items = arrayElements(x);
110115
int *results = customIntAllocator(items);
111116

112-
results = forabssub((int *)x, y, results);
117+
results = allocforabssub((int *)x, y);
113118

114119
return results;
115120
}
@@ -143,7 +148,7 @@ int main(int argc, char **argv) {
143148
ExitOnErr(Jit.submitModule(std::move(M), std::move(C)));
144149

145150
// Request function; this compiles to machine code and links.
146-
forabssub = ExitOnErr(Jit.getFunction<int *(int *, int *, int *)>(JitedFnName));
151+
allocforabssub = ExitOnErr(Jit.getFunction<int *(int *, int *)>(JitedFnName));
147152

148153
int *z = integerDistances(x, y);
149154

0 commit comments

Comments
 (0)