C++ Logo

std-proposals

Advanced search

[std-proposals] [[false_path_is_defined_behavior]] attribute to if-thens

From: Matthew Kolbe <matthew.kolbe_at_[hidden]>
Date: Thu, 31 Oct 2024 12:40:01 -0600
I am proposing an attribute to aid compilers in vectorizing conditional
code more effectively. In current scenarios, compilers sometimes hesitate
to vectorize code containing conditional branches, as they cannot guarantee
that accessing data on the “false path” won’t trigger undefined behavior
(UB). This results in missed vectorization opportunities, particularly when
working with SIMD operations, where executing both branches and blending
the result is often optimal.

Here's an example

```cpp
int a;
if (condition) {
    a = array[i];
} else {
    a = array[i+1]
}
```

In this code, if the compiler knew that accessing array[i + 1] on the false
path of condition was safe—even if that path isn’t executed—it could
vectorize this operation by evaluating both paths and blending the results.
However, the lack of clarity regarding UB in these cases can prevent this
optimization.

*Proposed Solution:*

To resolve this, I propose a new attribute,
[[false_path_is_defined_behavior]], which would guarantee that accessing
the false path’s value does not invoke UB, thereby enabling compilers to
vectorize more confidently.

Example with the proposed attribute:

```cpp
int a;
if (condition) [[false_path_is_defined_behavior]] {
    a = array[i];
} else {
    a = array[i+1]
}
```

This attribute would provide compilers the information needed to safely
perform vectorization in SIMD-friendly loops, improving performance without
additional programmer intervention.

I would be grateful for any feedback from the committee on this concept and
its feasibility.

Thank you very much for your time and consideration.

Best,


-- 
Matthew P. Kolbe
(312) 218-6595
matthew.kolbe_at_[hidden]

Received on 2024-10-31 18:40:15