[llvm-dev] How to generate IR code to dynamically compute object size ?

St├ęphane Letz via llvm-dev llvm-dev at lists.llvm.org
Thu Oct 1 03:01:09 PDT 2015


Hi,

We are using the following kind code to compile the size of a dynamically allocated object, flowing the recommendation described here :

http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt

Our code was :

Value* ptr_size = GetElementPtrInst::Create(ConstantPointerNull::get(dsp_type_ptr), genInt64(fModule, 1), "ptr_size", entry_func_llvm_create_dsp);
CastInst* size_inst = new PtrToIntInst(ptr_size, fBuilder->getInt64Ty(), "size", entry_func_llvm_create_dsp);
CallInst* call_inst1 = CallInst::Create(func_malloc, size_inst, "", entry_func_llvm_create_dsp);

With LLVM 3.7 the prototype of GetElementPtrInst::Create has changed, so we tried : 

#if defined(LLVM_37)
    ConstantPointerNull* null_ptr =  ConstantPointerNull::get(dsp_type_ptr);
    Value* ptr_size = GetElementPtrInst::Create(null_ptr->getType(), null_ptr, genInt64(fModule, 1), "ptr_size", entry_func_llvm_create_dsp);
 #else
     Value* ptr_size = GetElementPtrInst::Create(ConstantPointerNull::get(dsp_type_ptr), genInt64(fModule, 1), "ptr_size", entry_func_llvm_create_dsp);
#endif
     CastInst* size_inst = new PtrToIntInst(ptr_size, fBuilder->getInt64Ty(), "size", entry_func_llvm_create_dsp);
     CallInst* call_inst1 = CallInst::Create(func_malloc, size_inst, "", entry_func_llvm_create_dsp);


But we get the following error when running the code : 

Assertion failed: (PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()), function Create, file /opt/local/libexec/llvm-3.7/include/llvm/IR/Instructions.h, line 880.

Is there a cleaner and more official way to generate code to compute the size of a dynamically allocated object with LLVM 3.7?

Thanks.

St├ęphane Letz


More information about the llvm-dev mailing list