pointerscompilationocamlllvmllvm-ir

LLVM API produces invalid IR for ptr type


I am using LLVM 15 and I am trying to compile a language of my own (pretty much like Pascal in terms of syntax) to LLVM IR and I am using Ocaml. When I try to create a struct and set its body to have ptr elements, then when I get a reference to that field of the struct and store to it, it has type str* which is invalid. Here is a sample ocaml code that produces this:

let frame = named_struct_type llvm.context "frame1" in
struct_set_body frame ([| pointer_type2 llvm.ctx |]) false;
let frame_ptr = build_alloca frame ("frame_ptr") llvm.builder in
let base = build_struct_gep2 frame frame_ptr 0 "frame_elem_ptr" llvm.builder in
ignore (build_store v base llvm.builder);

where v is a function parameter called auxiliery and of type ptr.

The code above produces this IR:

  %frame = type { ptr }
  %frame_ptr = alloca %frame, align 8
  %frame_elem_ptr = getelementptr inbounds %frame, %frame* %frame_ptr, i32 0, i32 0
  store ptr %auxiliary, ptr* %frame_elem_ptr, align 8

When I try to compile this I get:

llc-15: error: llc-15: llvm_ir.ll:54:28: error: ptr* is invalid - use ptr instead

Which kiiind of makes sense to me... I mean of course, I understand that the asterisk must be removed and if I manually remove it from the IR file it should compile (and it does actually!) but :

  1. Why is this not allowed? (A pointer to a pointer is fine in C)
  2. And most importantly, does anyone have any idea how could I fix it in my Ocaml code (or even C++ api instructions will be much appreciated!) ?

Generally, opaque pointers have caused me more trouble than good... :(


Solution

  • It turns out that LLVM 15 bindings in OCaml do not support opaque pointers at all! They are disabled automatically and there is no way to get away from this. So use LLVM 16 (or higher) if you want opaque pointers.