Dingbao Xie
2014-Nov-05 22:04 UTC
[LLVMdev] How to lower the intrinsic function 'llvm.objectsize'?
The documentation of LLVM says that "The llvm.objectsize intrinsic is lowered to a constant representing the size of the object concerned". I'm attempting to lower this intrinsic function to a constant in a pass. Below is the code snippet that I wrote: for (BasicBlock::iterator i = b.begin(), ie = b.end(); (i != ie) && (block_split == false);) { IntrinsicInst *ii = dyn_cast<IntrinsicInst>(&*i); ++i; if(ii) { switch (ii->getIntrinsicID()) { case Intrinsic::objectsize: { IRBuilder<> builder(ii->getParent(), ii); Value *op1 = ii->getArgOperand(0); //i8* uint64_t bit_size op1->getType()->getPointerElementType()->getPrimitiveSizeInBits(); Value *result = ConstantInt::get(ii->getType(), bit_size); ii->replaceAllUsesWith(result); ii->removeFromParent(); delete ii; break; } } } I'm new to LLVM and not sure whether the implementation is correct. Can anybody tell me whether the implementation is correct? Thanks in advance. -- Dingbao Xie -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141105/8a7f40e8/attachment.html>
Matt Arsenault
2014-Nov-05 22:24 UTC
[LLVMdev] How to lower the intrinsic function 'llvm.objectsize'?
On 11/05/2014 02:04 PM, Dingbao Xie wrote:> > The documentation of LLVM says that "The llvm.objectsize intrinsic is > lowered to a constant representing the size of the object concerned". > I'm attempting to lower this intrinsic function to a constant in a > pass. Below is the code snippet that I wrote: >Why do you need to handle this yourself? This should already be handled for you (see InstCombineCalls.cpp). However, you have a few problems with this.> |for (BasicBlock::iterator i = b.begin(), ie = b.end(); > (i != ie) && (block_split == false);) { > IntrinsicInst *ii = dyn_cast<IntrinsicInst>(&*i); > ++i; > if(ii) { > switch (ii->getIntrinsicID()) { > case Intrinsic::objectsize: { > IRBuilder<> builder(ii->getParent(), ii); > Value *op1 = ii->getArgOperand(0); //i8* > uint64_t bit_size = op1->getType()->getPointerElementType()->getPrimitiveSizeInBits();|First, you can't always determine the size. Just looking at the pointer element type isn't enough. This requires finding the object definition, which can fail, and the existing handling uses llvm::getObjectSize to for. In general when looking at type sizes you don't want to use getPrimitiveSizeInBits, and should use the DataLayout for various reasons. ||> | > Value *result = ConstantInt::get(ii->getType(), bit_size); > ii->replaceAllUsesWith(result); > ii->removeFromParent(); > delete ii;||You shouldn't use delete here. You probably want ii->eraseFromParent(). |> | > break; > } > } > } > | > > I'm new to LLVM and not sure whether the implementation is correct. > Can anybody tell me whether the implementation is correct? > > Thanks in advance. > > > > -- > Dingbao Xie > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141105/407f8e57/attachment.html>
Ahmed Bougacha
2014-Nov-05 22:28 UTC
[LLVMdev] How to lower the intrinsic function 'llvm.objectsize'?
On Wed, Nov 5, 2014 at 2:04 PM, Dingbao Xie <xiedingbao at gmail.com> wrote:> The documentation of LLVM says that "The llvm.objectsize intrinsic is > lowered to a constant representing the size of the object concerned". I'm > attempting to lower this intrinsic function to a constant in a pass. Below > is the code snippet that I wrote: > > for (BasicBlock::iterator i = b.begin(), ie = b.end(); > (i != ie) && (block_split == false);) { > IntrinsicInst *ii = dyn_cast<IntrinsicInst>(&*i); > ++i; > if(ii) { > switch (ii->getIntrinsicID()) { > case Intrinsic::objectsize: { > IRBuilder<> builder(ii->getParent(), ii); > Value *op1 = ii->getArgOperand(0); //i8* > uint64_t bit_size > op1->getType()->getPointerElementType()->getPrimitiveSizeInBits(); > Value *result = ConstantInt::get(ii->getType(), bit_size); > ii->replaceAllUsesWith(result); > ii->removeFromParent(); > delete ii; > break; > } > } > } > > I'm new to LLVM and not sure whether the implementation is correct. Can > anybody tell me whether the implementation is correct?If you don't want to do it yourself you can probably just use getObjectSize, declared in include/llvm/Analysis/MemoryBuiltins.h. Also, two things regarding your implementation: - the intrinsic returns the size in bytes, not bits; generally, when dealing with memory, bytes are the relevant unit anyway. - llvm.objectsize returns the size of the "object", not just the pointee type. So for instance, if you have an i8* pointer to a 10 byte array, it would return 10, not 1. Before what you quoted, the documentation says "An object in this context means an allocation of a specific class, structure, array, or other object.". Good luck, - Ahmed> Thanks in advance. > > > > -- > Dingbao Xie > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
Dingbao Xie
2014-Nov-05 22:37 UTC
[LLVMdev] How to lower the intrinsic function 'llvm.objectsize'?
Thanks for your reply. I'm attempting to expand KLEE to support this intrinsic function. That's why I need to handle this myself. According to the reply, the correct implementation should first find the definition of the object and then determine the size of the object. BTW, can I just refer to the implementation in InstCombineCalls.cpp. On Wed, Nov 5, 2014 at 2:24 PM, Matt Arsenault <Matthew.Arsenault at amd.com> wrote:> On 11/05/2014 02:04 PM, Dingbao Xie wrote: > > The documentation of LLVM says that "The llvm.objectsize intrinsic is > lowered to a constant representing the size of the object concerned". I'm > attempting to lower this intrinsic function to a constant in a pass. Below > is the code snippet that I wrote: > > Why do you need to handle this yourself? This should already be handled > for you (see InstCombineCalls.cpp). However, you have a few problems with > this. > > for (BasicBlock::iterator i = b.begin(), ie = b.end(); > (i != ie) && (block_split == false);) { > IntrinsicInst *ii = dyn_cast<IntrinsicInst>(&*i); > ++i; > if(ii) { > switch (ii->getIntrinsicID()) { > case Intrinsic::objectsize: { > IRBuilder<> builder(ii->getParent(), ii); > Value *op1 = ii->getArgOperand(0); //i8* > uint64_t bit_size = op1->getType()->getPointerElementType()->getPrimitiveSizeInBits(); > > First, you can't always determine the size. Just looking at the pointer > element type isn't enough. This requires finding the object definition, > which can fail, and the existing handling uses llvm::getObjectSize to for. > In general when looking at type sizes you don't want to use > getPrimitiveSizeInBits, and should use the DataLayout for various reasons. > > > Value *result = ConstantInt::get(ii->getType(), bit_size); > ii->replaceAllUsesWith(result); > ii->removeFromParent(); > delete ii; > > You shouldn't use delete here. You probably want ii->eraseFromParent(). > > > break; > } > } > } > > I'm new to LLVM and not sure whether the implementation is correct. Can > anybody tell me whether the implementation is correct? > > Thanks in advance. > > > -- > Dingbao Xie > > > _______________________________________________ > LLVM Developers mailing listLLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.eduhttp://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > >-- Dingbao Xie -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141105/3da1fceb/attachment.html>
Possibly Parallel Threads
- [LLVMdev] How to lower the intrinsic function 'llvm.objectsize'?
- [LLVMdev] Question about intrinsic function llvm.objectsize
- [LLVMdev] Question about intrinsic function llvm.objectsize
- [LLVMdev] Question about intrinsic function llvm.objectsize
- [LLVMdev] Question about intrinsic function llvm.objectsize