[CRASHED] Assert at 'Ptr must be a pointer to Val type' when use LLVMBuildStore c-api

Hello hackers,

There is a simple situation that i used llvm c-api to build some code into IR module.
LLVM-version : release/13 on X86-ubuntu22.04

Case like bellow:

LLVMTypeRef TypeSizeT;
typedef uintptr_t Datum;

typedef struct Op
{
  Datum	   *resvalue;
  uint32_t currentExprId;
} Op;

static inline LLVMValueRef
l_ptr_const(void *ptr, LLVMTypeRef type)
{
	LLVMValueRef c = LLVMConstInt(TypeSizeT, (uintptr_t) ptr, false);

	return LLVMConstIntToPtr(c, type);
}

void
BuildArgs( Op *op)
{
  LLVMBuilderRef b;
  LLVMValueRef v_currentExprId_p;
  LLVMValueRef v_currentExprId;
  LLVMValueRef v_resvaluep;

  b = LLVMCreateBuilder();
  

  // after this, v_resvaluep's type : i64* --> PointerType
  v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));

  // after this, v_currentExprId_p's type : i32* --> PointerType
  v_currentExprId_p = l_ptr_const(&op->currentExprId,
							 l_ptr(LLVMInt32Type()));

  // after this, v_currentExprId's type : i32
  v_currentExprId = LLVMBuildLoad(b, v_currentExprId_p, "v_currentExprId");

  // Will crash at AssertOK, that  v_currentExprId's type and v_resvaluep's type not same.
  LLVMBuildStore(b, v_currentExprId, v_resvaluep);
  ...
}

In our situation, we checked upon crash in reality gdb:


we find v_resvaluep is normal PointerType, but after call LLVMBuildLoad, the v_currentExprId has been changed to non-pointertype, it’s really wired. This problem caused an ASSERT at LLVMBuildStore.

Any one, who can give some suggestion?

So you’re trying to store an i32 to an i64*? That’s the inconsistency being detected here.

1 Like

Thanks for your reply.
I have find the problem, i should keep the element-type same with v_resvaluep which is TypeSizeT.