Skip to content

Missed loop replace_if vectorization due to the presence of switch #48188

Closed
@davidbolvansky

Description

@davidbolvansky
Bugzilla Link 48844
Version trunk
OS Linux
CC @fhahn,@RKSimon,@nikic,@rotateright

Extended Description

#include <algorithm>
#include <vector>
#include <cstdint>

void replace_if_epi8(std::vector<int8_t>& v) {
    std::replace_if(v.begin(), v.end(), [](int8_t x){return x == 13 || x == -12; }, 42);
}

void replace_if_epi32(std::vector<int32_t>& v) {
    std::replace_if(v.begin(), v.end(), [](int8_t x){return x == 13 || x == -12; }, 42);
}

LLVM fails to vectorize this code - possibly due to presence of switch (OR cond is converted to switch).
https://godbolt.org/z/sY5oT9

Second example:

void replace_if_epi8(std::vector<int8_t>& v) {
    std::replace_if(v.begin(), v.end(), [](int8_t x){return x == 13 ; }, 42);
}

void replace_if_epi32(std::vector<int32_t>& v) {
    std::replace_if(v.begin(), v.end(), [](int8_t x){return x == 13; }, 42);
}

This code is nicely vectorized by LLVM.
https://godbolt.org/z/jx4dqo

So either simplifycfg should not create switches before vectorizers (easy fix, maybe profitable for other optimizations too?), and/or vectorizers should know how to handle them.

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