Skip to content

powf not inlining or optimizing in builtin implementation (cf. Rust, clang) #12032

@wallabra

Description

@wallabra

Zig Version

0.9.0 (Godbolt)

Steps to Reproduce

This behaviour can be verified e.g. by implementing the generic Minkowski distance and compiling the instances with order 1 (equivalent to Manhattan distance) and order 2 (equivalent to Euclidean distance). Make sure to enable optimizations with fast math.

It can be done in Godbolt:

In this case, compare the deassembled outputs.

Expected Behavior

std.math.powf should be inlined and optimized out where applicable (i.e. where the compiler can guarantee safe behaviour, and thus simply use the equivalent of libm's powf).

The first- and second-order instances of the generic Minkowski function should be automatically optimized into specific algorithms by the compiler. This can be most easily verified by checking that the assembly versions of both cases are short and don't contain call, and that only the 2nd-order case contains vsqrtss (since in the first-order case, all powf calls are passed an exponent of 1 and can be safely optimized out).

Actual Behavior

In Zig 0.9.0, the Minkowski function is neither inlined, much less optimized, in the specific cases of orders 1 and 2, as a result of the compiler failing to inline and optimize out std.math.pow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions