Skip to content

if statements not cleanly short-circuiting on Apple Silicon with optimizations #60695

Closed as not planned
@mckib2

Description

@mckib2

Overflows are reported for -O2 and -O3 due to evaluation of unsafe calculations in if-statements that should have been guarded by a previous condition short-circuiting. We only observe this on certain versions of MacOS, e.g., Monterrey 12.6.

Reproducer:

// main.cpp
#include <iostream>
#include <limits>
#include <cfenv>

int main(int argc, char** argv) {
    if (argc != 2) {
        return 1;
    }
    const double x = std::atof(argv[1]);
    double f1 = std::numeric_limits<double>::max();
    double res;
    if((x < 1) && (std::numeric_limits<double>::max() * x < f1)) {
        std::cout << "if branch\n";
    } else {
        std::cout << "else branch\n";
    }

    if(std::fetestexcept(FE_OVERFLOW)) {
        std::cout << "overflow reported\n";
    } else {
        std::cout << "overflow not reported\n";
    }

    return 0;
}

// output:
//     else branch
//     overflow reported

Build:

clang++ -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -O2 -std=gnu++14 main.cpp

Run:

./a.out 42.0

Version information:

% clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions