Skip to content

Missed optimization: references from pointers aren't treated as noalias #38941

Open
@mjbshaw

Description

@mjbshaw

The following code results in a being dereferenced again for the return value: (https://godbolt.org/z/YnrzMa3oj)

pub unsafe fn g(a: *mut i32, b: *mut i32) -> i32 {
  *a = 0;
  *b = 1;
  return *a;
}

That is to be expected. In contrast, the same function with references avoids the load: (https://godbolt.org/z/YnrzMa3oj)

pub fn g_ref(a: &mut i32, b: &mut i32) -> i32 {
  *a = 0;
  *b = 1;
  return *a;
}

However, if we change the code to the following: (https://godbolt.org/z/YnrzMa3oj)

pub fn g(a: *mut i32, b: *mut i32, x: *const i32) {
  let a = &mut *a;
  let b = &mut *b;
  *a = 10;
  *b = 11;
  return *a;
}

then the extra dereference is not optimized out as it should be, because do not emit anything like noalias inside functions.

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.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-llvmWorking group: LLVM backend code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions