mercredi 19 octobre 2022

How can I pass array to LLVM IR Function arguments and get value by index

I want to pass an array to LLVM IR Function, and get value by index. The code as follows, but it dont't work.


  Function *FooF =
      Function::Create(FunctionType::get(Type::getInt32Ty(Context), {ArrayType::getInt32PtrTy(Context), Type::getInt32Ty(Context)}, false),
                       Function::ExternalLinkage, "foo", M);
  // Add a basic block to the FooF function.
  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FooF);
  IRBuilder<> builder(BB);
  // Get pointer to array
  Value *arg1 = FooF->arg_begin();
  // Get index
  Value *index = arg1+1;
  // Get first_element
  Value *first_element = builder.CreateExtractElement(arg1, index);
  builder.CreateRet(first_element);

  // Now we create the JIT.
  ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
  using FunctionPtr = int(*)(int32_t *, int);
  FunctionPtr func = reinterpret_cast<FunctionPtr>(EE->getFunctionAddress("foo")) ;
  int32_t array[3] = {1,2,3};
  int first = func(array, 0);

}

Aucun commentaire:

Enregistrer un commentaire