Skip to content

Support !range metadata for by-value function arguments #76628

Closed
@AngelicosPhosphoros

Description

@AngelicosPhosphoros

It can be useful for cases like Rusts NonZeroInt types or subrange types like in Pascal,

For example, in Rust, when passing non-zero integer function arguments as references, compiler emits !range metadata on loads so optimizer can exploit this to remove unnecessary checks for zero (and generated branches).
However, when arguments are passed by value (which is optimal in sense of stack memory usage), frontend cannot emit range metadata for function arguments so LLVM optimizer fails to remove zero checks despite them being unnecessary.

E. g. this optimizes well

pub fn is_zero(a: &NonZeroU32)->bool {
   a.get() == 0
}

Output:

define noundef zeroext i1 @is_zero(i32 noundef %_a) unnamed_addr #0 !dbg !14 {
  ret i1 false, !dbg !15
}

and this optimizes badly

pub fn is_zero(a: NonZeroU32)->bool {
   a.get() == 0
}

Output:

define noundef zeroext i1 @is_zero(i32 noundef %a) unnamed_addr #0 !dbg !7 {
  %_0 = icmp eq i32 %a, 0, !dbg !12
  ret i1 %_0, !dbg !13
}

Frontend cannot solve this because even if it would put such the arguments into stack and then load them with metadata specified, first SROA or mem2reg would remove this operations so later optimizations wouldn't see that values have limited range.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions