Skip to content

Failure to fuse adjacent loops #117938

@Kmeakin

Description

@Kmeakin

src calculates the sum of a slice of integers, then the product. tgt does the same in a single loop. LLVM should fuse the two loops into one, since they are side effect free

https://godbolt.org/z/Kdd7z4c88

#include <stddef.h>
#include <stdint.h>

typedef uint32_t u32;

struct tuple {
    u32 sum;
    u32 prod;
};

struct tuple src(u32* p, size_t len) {
    u32 sum = 0;
    u32 prod = 1;

    for (size_t i = 0; i < len; i++) {
        sum += p[i];
    }

    for (size_t i = 0; i < len; i++) {
        prod *= p[i];
    }

    return (struct tuple){.sum = sum, .prod = prod};
}

struct tuple tgt(u32* p, size_t len) {
    u32 sum = 0;
    u32 prod = 1;

    for (size_t i = 0; i < len; i++) {
        sum += p[i];
        prod *= p[i];
    }

    return (struct tuple){.sum = sum, .prod = prod};
}

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