Skip to content

noalias is not enough #53105

Open
Open
@gnzlbg

Description

@gnzlbg
Contributor

Somebody on the internet (https://blog.dend.ro/rust-and-the-case-of-the-redundant-comparison/) complained that something like this:

fn vec_clear(x: &mut i32) {
    if *x != 0 {
        *x = 0;
    }
}

generates a conditional store:

    cmpl    $0, (%rdi)
    je      .LBB0_2
    movl    $0, (%rdi)
.LBB0_2:  
    retq

on x86_64 instead of just an unconditional store movl $0, (%rdi); retq.

Taking a look at the optimized LLVM-IR:

define void @vec_clear(i32* noalias nocapture dereferenceable(4) %x) {
start:
  %0 = load i32, i32* %x, align 4
  %1 = icmp eq i32 %0, 0
  br i1 %1, label %bb2, label %bb1

bb1:
  store i32 0, i32* %x, align 4
  br label %bb2

bb2:
  ret void
}

shows the issue.

The LLVM-IR generated by rustc is loosing critical information. It marks i32* as noalias, which means, that no other pointers in vec_clear's scope will alias it. However, outside vec_clear scope, other pointers are allowed to alias that memory. That is, if *x is zero, other threads could be concurrently reading the memory and if LLVM would generate an unconditional store here, that would introduce a data-race, which means that this optimization is not safe on the LLVM-IR generated by rustc. OTOH, &mut i32` means that the pointer has unique access to the memory, that is, no other pointer can access the memory behind it as long as that pointer is alive. Therefore, transforming the code to an unconditional store does not introduce a data-race.

Therefore, I think that noalias is not enough to perform this optimization and that we would need something stronger for LLVM to be able to perform it.


This also shows that &mut T is stronger than C's restrict keyword.

Activity

RalfJung

RalfJung commented on Aug 6, 2018

@RalfJung
Member

That is, if *x is zero, other threads could be concurrently reading the memory and if LLVM would generate an unconditional store here, that would introduce a data-race, which means that this optimization is not safe on the LLVM-IR generated by rustc.

Ah, good point. Read-write data races "just" make the read yield undef, but even that would clearly be a misoptimization.

OTOH, &mut i32 means that the pointer has unique access to the memory, that is, no other pointer can access the memory behind it as long as that pointer is alive. Therefore, transforming the code to an unconditional store does not introduce a data-race.

Correct. AFAIK, noalias was never meant to express the full set of properties. It's just the strongest thing LLVM provides.

This also shows that &mut T is stronger than C's restrict keyword.

Oh yes, it is very much stronger in various ways.

varkor

varkor commented on Aug 6, 2018

@varkor
Member

Isn't this the sort of thing the noalias and alias.scopes metadata (#16515) allows one to express?

gnzlbg

gnzlbg commented on Aug 6, 2018

@gnzlbg
ContributorAuthor

@varkor what would be the scopes for the load and stores in the example?

varkor

varkor commented on Aug 6, 2018

@varkor
Member

In this example, as you point out, the aliasing is important with regards to memory accesses outside the function. So if in theory you could mark all the others... I doubt that's sufficient for LLVM though.

leonardo-m

leonardo-m commented on Aug 9, 2018

@leonardo-m

Is it a good idea to write a LLVM enhancement request?

gnzlbg

gnzlbg commented on Aug 9, 2018

@gnzlbg
ContributorAuthor

As @varkor says, we could mark all others, and we would have to mark all others for every &mut that the programs creates, and even then, this is not something that alias analysis would take into account because no sane language front-end will do this.

Extending LLVM to support this won't be easy either. Currently LLVM hoists memory ops from functions when profitable, but:

// T: Copy
fn foo(x: &mut T) {  
   // in this scope there is only one 
   // pointer to the value behind x
   let y = *x; 
    ... 
}
{
   let mut z = T;
   let ptr = &mut z as *mut T;
   // hoist the load from foo out here
   foo(&mut z);     
   *ptr = T;
}

so when hoisting the load (or store) from foo to the outer scope, the "invariant" that that's the only pointer to the data doesn't hold any more, because in the outer scope there might be other pointers to the data.

So all the optimizations that currently move memory across scope would need to update and be extremely careful with any attribute/metadata that we might want to use.

Maybe a minimal extension to alias analysis that allow us to specify the "opposite" / "negative" aliasing groups would be enough, but one would need to teach many pieces of the pipeline about this for the new information to result in better code gen.

steveklabnik

steveklabnik commented on Sep 25, 2020

@steveklabnik
Member

Triage: no idea what the current status of this is, to be honest. I imagine that this was never suggested upstream.

added
I-slowIssue: Problems and improvements with respect to performance of generated code.
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Sep 25, 2020
RalfJung

RalfJung commented on Sep 26, 2020

@RalfJung
Member

Therefore, I think that noalias is not enough to perform this optimization and that we would need something stronger for LLVM to be able to perform it.

If noalias really means "does not alias for the duration of this function call", then I think in fact it would be enough. Unfortunately, noalias scoping in LLVM is basically undocumented, and ti is also buggy so one cannot just go from the implementation.

However, a new round of noalias/restrict patches has been landing recently (https://lists.llvm.org/pipermail/llvm-dev/2019-March/131127.html, https://reviews.llvm.org/D69542#change-veawD9rpruA2), so maybe that new infrastructure is powerful enough to express the desired guarantee here.

FWIW, Stacked Borrows does allow the optimization.

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@nikic@RalfJung@gnzlbg@jonas-schievink

        Issue actions

          noalias is not enough · Issue #53105 · rust-lang/rust