<div dir="ltr">I'm implementing c style "sizeof()", and I did as <a href="http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt">http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt</a> illuarstrated, and it works find, here's an example of my implementation:<div>auto *p = builder.CreateGEP(structTy,<br>                              llvm::ConstantPointerNull::get(pointerTy),<br>                              constint1);<br><br>  auto *size = builder.CreatePtrToInt(p, llvm::IntegerType::get(context, 64));<br></div><div><br></div><div>and type definitions:</div><div><div>auto *constint1 = llvm::ConstantInt::get(context, llvm::APInt(64, 1));<br>auto *int64Ty = llvm::IntegerType::get(context, 64);<br>auto *doubleTy = llvm::Type::getDoubleTy(context);<br>auto *structTy = llvm::StructType::create(context, "Foo");<br>structTy->setBody({int64Ty, doubleTy});<br></div><div>auto *pointerTy = llvm::PointerType::get(structTy, 0);</div></div><div><br></div><div>take care that the "size" is a "llvm::Value" type, not a "llvm::constantInt" type, this leads to a problem:  definitions like "int i[sizeof(Foo)]" will fail (please ignore that this definition makes no sense), because in my implementation, sizeof() should get the result on compile time, but the current implementation seems calculate at runtime( or at least an llvm::Value, not an llvm::ConstantInt)</div><div><br></div><div>and I noticed this in the tutorial:</div><div><pre style="color:rgb(0,0,0);white-space:pre-wrap">Note that in both of these cases, the expression will be evaluated to a
constant at code generation time, so there is no runtime overhead to using this
technique.</pre></div><div>But how can he cast an llvm::Value to llvm::ConstantInt?</div></div>