All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christopher Li <sparse@chrisli.org>,
	Dibyendu Majumdar <mobile@majumdar.org.uk>,
	Linux-Sparse <linux-sparse@vger.kernel.org>
Subject: Re: [PATCH 07/13] llvm: fix output OP_ADD mixed with pointers
Date: Mon, 6 Mar 2017 21:09:57 +0100	[thread overview]
Message-ID: <20170306200956.s6wqxw2mfk2lgjlu@macpro.local> (raw)
In-Reply-To: <CA+55aFx=R=Z2jrTQhT2o4oA-it+sQjKT097zMgPhgKFp0byPFw@mail.gmail.com>

On Mon, Mar 06, 2017 at 10:17:35AM -0800, Linus Torvalds wrote:
> On Mon, Mar 6, 2017 at 8:43 AM, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> >
> > With an example:
> > == C code ==
> >         void *foo(int *p) { return p + 5; }
> >
> > == linearized code ==
> >         foo:
> >         .L0:
> >                 <entry-point>
> >                 add.64      %r2 <- %arg1, $20
> >                 cast.64     %r3 <- (64) %r2
> >                 ret.64      %r3
> 
> This is correct.

Interestingly, the following code is linearized a bit differently:
== C code ==
	void *foo(int *p) { return p += 5; }
== linearized code ==
	foo:
	.L0:
	        <entry-point>
	        cast.64     %r98 <- (64) %arg1
	        add.64      %r99 <- %r98, $20
	        ptrcast.64  %r100 <- (64) %r99
	        cast.64     %r101 <- (64) %r100
	        ret.64      %r101

Where the type of %r98 & %r99 is 'unsigned long' (or more probably size_t).
This is then correctly LLVMized as:
	define i8* @foo(i32* %ARG1) {
	L18:
	  %R98 = ptrtoint i32* %ARG1 to i64
	  %R99 = add i64 %R98, 20
	  %R100 = inttoptr i64 %R99 to i32*
	  %R101 = bitcast i32* %R100 to i8*
	  ret i8* %R101
	}
 
> > == LLVM code from sparse-llvm ==
> >
> >         define i8* @foo(i32* %ARG1) {
> >         L0:
> >           %0 = getelementptr i32, i32* %ARG1, inttoptr (i64 20 to i32*)
> >           %R3 = bitcast i32* %0 to i8*
> 
> This is garbage, I'm afraid.
> 
> When sparse does the "add 20 to pointer", it adds the *byte offset* 20
> to the pointer. The LLVM module should not use "getelementptr" for
> this, because it's not element #20, it's the element at offset 20.

It's clear that there is a semantic gap between sparse's & LLVM's IR.

> I think you're supposed to either use "uglygep" with the base pointer
> cast to a simple address-unit pointer (ie unsigned char).
> 
> Or not use GEP at all.
> 
>                 Linus

I bet we'll have others problems with GEP.

Luc

  reply	other threads:[~2017-03-06 20:10 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-05 11:20 [WIP 00/13] LLVM fixes Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 01/13] llvm: add a helper to convert an integer to a ValueRef Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 02/13] llvm: fix translation of PSEUDO_VALs into a ValueRefs Luc Van Oostenryck
2017-03-07 15:11   ` Christopher Li
2017-03-07 16:18     ` Luc Van Oostenryck
2017-03-07 22:48       ` Christopher Li
2017-03-05 11:20 ` [PATCH 03/13] llvm: fix output_op_store() which modify its operand Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 04/13] llvm: fix output_op_[ptr]cast() Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 05/13] add get_nth1_arg() Luc Van Oostenryck
2017-03-06 14:40   ` Christopher Li
2017-03-06 16:52     ` Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 06/13] llvm: fix type of literal integer passed as arguments Luc Van Oostenryck
2017-03-06 14:56   ` Christopher Li
2017-03-07 15:33   ` Christopher Li
2017-03-07 16:21     ` Luc Van Oostenryck
2017-03-07 19:41     ` Dibyendu Majumdar
2017-03-10 16:08       ` Dibyendu Majumdar
2017-03-10 17:47         ` Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 07/13] llvm: fix output OP_ADD mixed with pointers Luc Van Oostenryck
2017-03-06 15:16   ` Christopher Li
2017-03-06 15:32     ` Dibyendu Majumdar
2017-03-06 16:22       ` Christopher Li
2017-03-06 16:43         ` Luc Van Oostenryck
2017-03-06 17:06           ` Dibyendu Majumdar
2017-03-06 19:50             ` Luc Van Oostenryck
2017-03-06 17:07           ` Christopher Li
2017-03-06 19:52             ` Luc Van Oostenryck
2017-03-06 21:15             ` [PATCH v2] " Luc Van Oostenryck
2017-03-06 18:17           ` [PATCH 07/13] " Linus Torvalds
2017-03-06 20:09             ` Luc Van Oostenryck [this message]
2017-03-05 11:20 ` [PATCH 08/13] llvm: add support for OP_NEG Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 09/13] give a type to OP_PHISOURCE Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 10/13] give a type to OP_SEL, always Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 11/13] llvm: remove unneeded arg 'module' Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 12/13] llvm: remove unneeded arg 'fn' Luc Van Oostenryck
2017-03-05 11:20 ` [PATCH 13/13] llvm: fix: do not mix pointers and floats when doing compares Luc Van Oostenryck
2017-03-06  1:47 ` [WIP 00/13] LLVM fixes Christopher Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170306200956.s6wqxw2mfk2lgjlu@macpro.local \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=mobile@majumdar.org.uk \
    --cc=sparse@chrisli.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.