Yafei Liu via llvm-dev
2020-Jan-14 09:52 UTC
[llvm-dev] sizeof implementation: how to get size as a constantInt?
I'm implementing c style "sizeof()", and I did as http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt illuarstrated, and it works find, here's an example of my implementation: auto *p = builder.CreateGEP(structTy, llvm::ConstantPointerNull::get(pointerTy), constint1); auto *size = builder.CreatePtrToInt(p, llvm::IntegerType::get(context, 64)); and type definitions: auto *constint1 = llvm::ConstantInt::get(context, llvm::APInt(64, 1)); auto *int64Ty = llvm::IntegerType::get(context, 64); auto *doubleTy = llvm::Type::getDoubleTy(context); auto *structTy = llvm::StructType::create(context, "Foo"); structTy->setBody({int64Ty, doubleTy}); auto *pointerTy = llvm::PointerType::get(structTy, 0); 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) and I noticed this in the tutorial: 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. But how can he cast an llvm::Value to llvm::ConstantInt? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200114/3213e690/attachment.html>
Eli Friedman via llvm-dev
2020-Jan-14 19:10 UTC
[llvm-dev] sizeof implementation: how to get size as a constantInt?
I’d recommend just using DataLayout::getTypeAllocSize() directly to get the number you want. (If you really want to fold your Constant to a ConstantInt, llvm::ConstantFoldConstant should work, but that’s a really convoluted way to do it.) -Eli From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Yafei Liu via llvm-dev Sent: Tuesday, January 14, 2020 1:52 AM To: llvm-dev <llvm-dev at lists.llvm.org> Subject: [EXT] [llvm-dev] sizeof implementation: how to get size as a constantInt? I'm implementing c style "sizeof()", and I did as http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt illuarstrated, and it works find, here's an example of my implementation: auto *p = builder.CreateGEP(structTy, llvm::ConstantPointerNull::get(pointerTy), constint1); auto *size = builder.CreatePtrToInt(p, llvm::IntegerType::get(context, 64)); and type definitions: auto *constint1 = llvm::ConstantInt::get(context, llvm::APInt(64, 1)); auto *int64Ty = llvm::IntegerType::get(context, 64); auto *doubleTy = llvm::Type::getDoubleTy(context); auto *structTy = llvm::StructType::create(context, "Foo"); structTy->setBody({int64Ty, doubleTy}); auto *pointerTy = llvm::PointerType::get(structTy, 0); 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) and I noticed this in the tutorial: 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. But how can he cast an llvm::Value to llvm::ConstantInt? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200114/ac4586a2/attachment.html>
Yafei Liu via llvm-dev
2020-Jan-15 01:48 UTC
[llvm-dev] sizeof implementation: how to get size as a constantInt?
Hi Eli, do you know what's the difference between "DataLayout::getTypeAllocSize()" and "llvm::ConstantExpr::getSizeOf()" On Wed, Jan 15, 2020 at 3:10 AM Eli Friedman <efriedma at quicinc.com> wrote:> I’d recommend just using DataLayout::getTypeAllocSize() directly to get > the number you want. (If you really want to fold your Constant to a > ConstantInt, llvm::ConstantFoldConstant should work, but that’s a really > convoluted way to do it.) > > > > -Eli > > > > *From:* llvm-dev <llvm-dev-bounces at lists.llvm.org> *On Behalf Of *Yafei > Liu via llvm-dev > *Sent:* Tuesday, January 14, 2020 1:52 AM > *To:* llvm-dev <llvm-dev at lists.llvm.org> > *Subject:* [EXT] [llvm-dev] sizeof implementation: how to get size as a > constantInt? > > > > I'm implementing c style "sizeof()", and I did as > http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt illuarstrated, > and it works find, here's an example of my implementation: > > auto *p = builder.CreateGEP(structTy, > llvm::ConstantPointerNull::get(pointerTy), > constint1); > > auto *size = builder.CreatePtrToInt(p, llvm::IntegerType::get(context, > 64)); > > > > and type definitions: > > auto *constint1 = llvm::ConstantInt::get(context, llvm::APInt(64, 1)); > auto *int64Ty = llvm::IntegerType::get(context, 64); > auto *doubleTy = llvm::Type::getDoubleTy(context); > auto *structTy = llvm::StructType::create(context, "Foo"); > structTy->setBody({int64Ty, doubleTy}); > > auto *pointerTy = llvm::PointerType::get(structTy, 0); > > > > 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) > > > > and I noticed this in the tutorial: > > 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. > > But how can he cast an llvm::Value to llvm::ConstantInt? >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200115/7e779d1a/attachment-0001.html>
Maybe Matching Threads
- [LLVMdev] Recursive Types using the llvm support library
- [LLVMdev] Recursive Types using the llvm support library
- [LLVMdev] generating function declarations in c frontend
- [LLVMdev] Dynamic Creation of a simple program
- [LLVMdev] Recursive Types using the llvm support library