<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Incorrect optimization: std::byte representation swap not observed in Intel® oneAPI DPC++/C++ Compiler</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/Incorrect-optimization-std-byte-representation-swap-not-observed/m-p/1717210#M4576</link>
    <description>&lt;P&gt;&lt;SPAN&gt;A trivially copyable object (struct Foo) is stored inside a std::byte buffer via placement new. Swapping two such objects by swapping their std::byte arrays (std::swap on the arrays) should exchange their object representations. The program prints the original value instead of the swapped value only with icx; other compilers (GCC, Clang, MSVC, NVHPC) produce the expected output. This suggests icx fails to treat writes through std::byte as potentially modifying the overlapped object, violating the aliasing rules that grant std::byte access to any object's representation.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://godbolt.org/z/bKsocbn6b" target="_self"&gt;Compiler Explorer link&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;cstddef&amp;gt; /* For std::byte */
#include &amp;lt;cstdio&amp;gt; /* For printf */
#include &amp;lt;memory&amp;gt; /* For std::construct_at */
#include &amp;lt;new&amp;gt; /* For std::launder */
#include &amp;lt;type_traits&amp;gt; /* For std::is_trivially_copyable_v */
#include &amp;lt;utility&amp;gt; /* For std::swap */

struct Erased {
  template &amp;lt;class T&amp;gt;
  Erased(T val) requires(std::is_trivially_copyable_v&amp;lt;T&amp;gt; &amp;amp;&amp;amp;
      sizeof(T) &amp;lt;= sizeof(void*) &amp;amp;&amp;amp; alignof(T) &amp;lt;= alignof(void*)) {
    std::construct_at(reinterpret_cast&amp;lt;T*&amp;gt;(data), val);
  }

  void Swap(Erased&amp;amp; rhs) {
    std::swap(data, rhs.data);
    std::swap(padding, rhs.padding);
  }

  alignas(alignof(void*)) std::byte data[sizeof(void*)];
  void* padding = nullptr;
};

int main() {
  struct Foo {
    const char* msg;
  };
  Erased a{Foo{"bug"}};
  Erased b{Foo{"ok"}};
  a.Swap(b);
  Foo* p = std::launder(reinterpret_cast&amp;lt;Foo*&amp;gt;(a.data));
  puts(p-&amp;gt;msg); // Expect: "ok"; Actual: "bug"
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Expected Result&lt;/STRONG&gt;: Program outputs: ok&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Actual Result&lt;/STRONG&gt;: Program outputs: bug&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Impact&lt;/STRONG&gt;: Causes silent logic errors in type‑erasure / small buffer optimization code that relies on valid object representation swaps.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Sep 2025 08:55:59 GMT</pubDate>
    <dc:creator>Mingxin_Wang</dc:creator>
    <dc:date>2025-09-16T08:55:59Z</dc:date>
    <item>
      <title>Incorrect optimization: std::byte representation swap not observed</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/Incorrect-optimization-std-byte-representation-swap-not-observed/m-p/1717210#M4576</link>
      <description>&lt;P&gt;&lt;SPAN&gt;A trivially copyable object (struct Foo) is stored inside a std::byte buffer via placement new. Swapping two such objects by swapping their std::byte arrays (std::swap on the arrays) should exchange their object representations. The program prints the original value instead of the swapped value only with icx; other compilers (GCC, Clang, MSVC, NVHPC) produce the expected output. This suggests icx fails to treat writes through std::byte as potentially modifying the overlapped object, violating the aliasing rules that grant std::byte access to any object's representation.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://godbolt.org/z/bKsocbn6b" target="_self"&gt;Compiler Explorer link&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;cstddef&amp;gt; /* For std::byte */
#include &amp;lt;cstdio&amp;gt; /* For printf */
#include &amp;lt;memory&amp;gt; /* For std::construct_at */
#include &amp;lt;new&amp;gt; /* For std::launder */
#include &amp;lt;type_traits&amp;gt; /* For std::is_trivially_copyable_v */
#include &amp;lt;utility&amp;gt; /* For std::swap */

struct Erased {
  template &amp;lt;class T&amp;gt;
  Erased(T val) requires(std::is_trivially_copyable_v&amp;lt;T&amp;gt; &amp;amp;&amp;amp;
      sizeof(T) &amp;lt;= sizeof(void*) &amp;amp;&amp;amp; alignof(T) &amp;lt;= alignof(void*)) {
    std::construct_at(reinterpret_cast&amp;lt;T*&amp;gt;(data), val);
  }

  void Swap(Erased&amp;amp; rhs) {
    std::swap(data, rhs.data);
    std::swap(padding, rhs.padding);
  }

  alignas(alignof(void*)) std::byte data[sizeof(void*)];
  void* padding = nullptr;
};

int main() {
  struct Foo {
    const char* msg;
  };
  Erased a{Foo{"bug"}};
  Erased b{Foo{"ok"}};
  a.Swap(b);
  Foo* p = std::launder(reinterpret_cast&amp;lt;Foo*&amp;gt;(a.data));
  puts(p-&amp;gt;msg); // Expect: "ok"; Actual: "bug"
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Expected Result&lt;/STRONG&gt;: Program outputs: ok&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Actual Result&lt;/STRONG&gt;: Program outputs: bug&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Impact&lt;/STRONG&gt;: Causes silent logic errors in type‑erasure / small buffer optimization code that relies on valid object representation swaps.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2025 08:55:59 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/Incorrect-optimization-std-byte-representation-swap-not-observed/m-p/1717210#M4576</guid>
      <dc:creator>Mingxin_Wang</dc:creator>
      <dc:date>2025-09-16T08:55:59Z</dc:date>
    </item>
  </channel>
</rss>

