[llvm-dev] inttoptr and noalias returns

Joseph Tremoulet via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 31 11:08:37 PDT 2021


Hi,

I'm a bit confused about the interaction between inttoptr and noalias, and would like to better understand our model.

I realize there's a bunch of in-flight work around restrict modeling and that ptrtoint was on the agenda for last week's AA call.  I'm interested in understanding both the current state and the thinking/plans for the future.  And I'm happy for pointers to anywhere this is already written down, I didn't find it from skimming the AA call minutes or the mailing list archive, but I could easily have overlooked it, and haven't really dug into the set of restrict patches (nor do I know where to get a list of those).

I also realize that with aliasing questions there can always be a gap between what the model says we can infer and how aggressive analyses and optimizations are about actually making use of those inferences.  Again I'm interested in both answers (and happy for either).

In the LangRef section on pointer aliasing rules [1], I see

An integer constant other than zero or a pointer value returned from a function not defined within LLVM may be associated with address ranges allocated through mechanisms other than those provided by LLVM. Such ranges shall not overlap with any ranges of addresses allocated by mechanisms provided by LLVM.

And I'm curious what "mechanisms provided by LLVM" for allocation means.  Alloca, presumably.  Global variables?  Certain intrinsics?  Any function with a noalias return value?

In the LangRef description of the noalias attribute [2], I see

This indicates that memory locations accessed via pointer values based on the argument or return value are not also accessed, during the execution of the function, via pointer values not based on the argument or return value ...  On function return values, the noalias attribute indicates that the function acts like a system memory allocation function, returning a pointer to allocated storage disjoint from the storage for any other object accessible to the caller.

The phrase "the storage for any other object accessible to the caller" in the noalias description sounds like a broader category than the phrase "mechanisms provided by LLVM" from the pointer aliasing section, so I would expect that if the pointer returned from a call to a function with return attribute noalias does not escape, then loads/stores through it would not alias loads/stores through a pointer produced by inttoptr.  Am I interpreting that correctly?

I wrote some snippets [3] to see what the optimizer would do.  Each case has a store of value 86 via pointer %p that I'd expect dead store elimination to remove if we think it does not alias the subsequent load via pointer %q (because immediately after that is another store to %p).
In each case, %q is the result of a call to a function whose return value is annotated noalias.

When %p is a pointer parameter, I indeed see the optimizer removing the dead store:
define i8 @test1(i8* %p) {

    %q = call i8* @allocate()
    store i8 86, i8* %p ; <-- this gets removed
    %result = load i8, i8* %q
    store i8 0, i8* %p
    ret i8 %result
}

When %p is the result of inttoptr, I do not see the store being removed, and I'm wondering if this is because of a subtle aliasing rule or an intentional conservativism in the optimizer or just a blind spot in the analysis:
define i8 @test2(i64 %p_as_int) {
    %p = inttoptr i64 %p_as_int to i8*

    %q = call i8* @allocate()
    store i8 86, i8* %p ; <-- this does not get removed
    %result = load i8, i8* %q
    store i8 0, i8* %p
    ret i8 %result
}

When I outline the inttoptr into a separate function, I again see the optimizer remove the dead store, which again I'm wondering if the difference between this and the previous case is an intentional subtle point or what.
define i8* @launder(i64 %int) noinline {
  %ptr = inttoptr i64 %int to i8*
  ret i8* %ptr
}

define i8 @test3(i64 %p_as_int) {
    %p = call i8* @launder(i64 %p_as_int)

    %q = call i8* @allocate()
    store i8 86, i8* %p ; <-- this gets removed
    %result = load i8, i8* %q
    store i8 0, i8* %p
    ret i8 %result
}



Happy for any insights you can share.

Thanks,
-Joseph

1 - https://llvm.org/docs/LangRef.html#pointeraliasing
2 - https://llvm.org/docs/LangRef.html#parameter-attributes
3 - https://godbolt.org/z/x8e41G33Y


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210331/235f7e64/attachment.html>


More information about the llvm-dev mailing list