title: Passing an array to an external function
body: <p>I am new to LLVM, and I am learning how to use LLVM for profiling. I need to pass an array to an external method, and insert a call instruction to the method in the code. I am currently using the following code, which on execution gives a segmentation fault.</p>
  
  <pre><code>std::vector&lt;Value*&gt; Args(1);
  
  //Vector with array values
  SmallVector&lt;Constant*, 2&gt; counts;
  
  counts.push_back(ConstantInt::get(Type::getInt32Ty(BB-&gt;getContext()),32, false));
                          counts.push_back(ConstantInt::get(Type::getInt32Ty(BB-&gt;getContext()),12, false));
  
  //Array with 2 integers
  Args[0]= ConstantArray::get(llvm::ArrayType::get(llvm::Type::getInt32Ty(BI-&gt;getContext()),2), counts);
  </code></pre>
  
  <p>Here, the external function 'hook' is defined as <code>M.getOrInsertFunction("hook", Type::getVoidTy(M.getContext()),
                                                      llvm::ArrayType::get(llvm::Type::getInt32Ty(BI-&gt;getContext()),2)
                                                      (Type*)0);</code></p>
  
  <p>After reading a few source files, I've tried using GetElementPtrInst to pass the array</p>
  
  <pre><code>std::vector&lt;Value*&gt; ids(1);
  ids.push_back(ConstantInt::get(Type::getInt32Ty(BB-&gt;getContext()),0));
  Constant* array = ConstantArray::get(llvm::ArrayType::get(llvm::Type::getInt32Ty(BI-&gt;getContext()),2), counts);
  Args[0] = ConstantExpr::getGetElementPtr(&amp;(*array), ids, false);
  </code></pre>
  
  <p>but it fails with</p>
  
  <pre><code>7  opt 0x00000000006c59f5 bool llvm::isa&lt;llvm::Constant, llvm::Value*&gt;(llvm::Value* const&amp;) + 24
  
  8  opt 0x00000000006c5a0f llvm::cast_retty&lt;llvm::Constant, llvm::Value*&gt;::ret_type llvm::cast&lt;llvm::Constant, llvm::Value*&gt;(llvm::Value* const&amp;) + 24
  
  9  opt 0x0000000000b2b22f
  
  10 opt 0x0000000000b2a4fe llvm::ConstantFoldGetElementPtr(llvm::Constant*, bool, llvm::ArrayRef&lt;llvm::Value*&gt;) + 55
  
  11 opt 0x0000000000b33df2 llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, llvm::ArrayRef&lt;llvm::Value*&gt;, bool) + 82
  </code></pre>
  
  <p>Also, in this case, 'hook' is defined as   <code>M.getOrInsertFunction("hook", Type::getVoidTy(M.getContext()),
                                              PointerType::get(Type::getInt32PtrTy(M.getContext()),0),  //when using GEP
                                                      (Type*)0);</code></p>
  
  <p>Could someone kindly keep me a few pointers on passing arrays to an external function (say with the signature <code>void hook(int abc[])</code> ). I am probably wrong all the way through, and would really appreciate some help.</p>