llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Miscompilation on x86_64

Open malte0811 opened this issue 5 months ago • 4 comments

The assertion in the following code should pass, but fails with -O1 and above (Godbolt link). As far as I can tell there should not be any undefined behavior in the code. This seems to happen since Clang 18, and still happens with "clang (trunk)" on Godbolt. The issue seems to be purely in test_main, the other two functions are only necessary to demonstrate the problem. It looks like the value of a is stored in the lower 8 bits of a register (r13 in the Godbolt example) whose upper 56 bits still contain some other value. Clang then uses the whole 64-bit register in a lea, leading to an incorrect value of b.second.

// Compile with "clang++ -O1 -std=c++20 main.cpp"
#include <vector>
#include <cassert>

struct StructA {
   signed char value;
};

struct StructB {
   StructA first;
   int second;
};

__attribute__((noinline)) int test_inner(int const&, StructB b) { return b.second; }

__attribute__((noinline)) std::vector<int> test_main(std::vector<StructA> const& as)
{
   std::vector<int> track_keys;
   for (StructA a : as) {
      track_keys.emplace_back(test_inner(13, StructB{a, 2}));
   }
   return track_keys;
}

int main()
{
   assert(test_main({StructA{0}}).front() == 2);
}

malte0811 avatar Jun 23 '26 08:06 malte0811

@llvm/issue-subscribers-backend-x86

Author: malte0811

The assertion in the following code should pass, but fails with `-O1` and above ([Godbolt link](https://godbolt.org/z/sxWxarvsf)). As far as I can tell there should not be any undefined behavior in the code. This seems to happen since Clang 18, and still happens with "clang (trunk)" on Godbolt. The issue seems to be purely in `test_main`, the other two functions are only necessary to demonstrate the problem. It looks like the value of `a` is stored in the lower 8 bits of a register (`r13` in the Godbolt example) whose upper 56 bits still contain some other value. Clang then uses the whole 64-bit register in a `lea`, leading to an incorrect value of `b.second`.
// Compile with "clang++ -O1 -std=c++20 main.cpp"
#include &lt;vector&gt;
#include &lt;cassert&gt;

struct StructA {
   signed char value;
};

struct StructB {
   StructA first;
   int second;
};

__attribute__((noinline)) int test_inner(int const&amp;, StructB b) { return b.second; }

__attribute__((noinline)) std::vector&lt;int&gt; test_main(std::vector&lt;StructA&gt; const&amp; as)
{
   std::vector&lt;int&gt; track_keys;
   for (StructA a : as) {
      track_keys.emplace_back(test_inner(13, StructB{a, 2}));
   }
   return track_keys;
}

int main()
{
   assert(test_main({StructA{0}}).front() == 2);
}

Bisected to https://github.com/llvm/llvm-project/commit/f44079db22036d0ade2cf3d2e5a24bde5d378efd. The assertion passes with those patterns reverted.


The description in the bug is correct, we end up with the upper bits of the register having junk. We can tweak one of the tests frominsert.ll for a more minimal reproducer. (Goldbolt link)

; *out = (uint64_t)((res & ~0xFF) | *byte);
define void @sub8_32_store_ptr(i32 noundef %res, ptr %byte, ptr %out) {
entry:
  %and = and i32 %res, -256
  %d = load i8, ptr %byte, align 1
  %conv2 = zext i8 %d to i32
  %or = or i32 %and, %conv2
  %z = zext i32 %or to i64
  store i64 %z, ptr %out
  ret void
}

define void @caller(i64 %arg_i64, ptr %byte, ptr %out) {
entry:
  ; Truncate to 32 bits. No-op because of ABI passing 64-bit to 32-bit args.
  %arg_i32 = trunc i64 %arg_i64 to i32
  call void @sub8_32_store_ptr(i32 %arg_i32, ptr %byte, ptr %out)
  ret void
}

emits

sub8_32_store_ptr:                      # @sub8_32_store_ptr
        mov     dil, byte ptr [rsi]
        mov     qword ptr [rdx], rdi
        ret
caller:                                 # @caller
        push    rax
        call    sub8_32_store_ptr@PLT
        pop     rax
        ret

The top 4 junk bytes in arg_i64 / rdi will end up getting written to the out / rdx ptr.

/cc @david-xl @topperc @RKSimon @KanRobert reviewers/authors on the original PR https://github.com/llvm/llvm-project/pull/75978

I opened https://github.com/llvm/llvm-project/pull/205600 as a potential fix that tweaks the pattern match to use def32, not sure if it's the best solution.

ammaraskar avatar Jun 24 '26 17:06 ammaraskar

It looks like this was fixed by https://github.com/llvm/llvm-project/pull/205600 (merged 2026-07-09).

I rechecked the minimal IR repro from the discussion on current tip with assertions-enabled llc (x86_64-unknown-linux-gnu, -O1) and it now emits the zero-extending form:

movb (%rsi), %dil
movl %edi, %eax
movq %rax, (%rdx)

instead of storing/using the full potentially-dirty wide register. Unless there is a remaining variant not covered by #205600, this issue can probably be closed.

agourakis82 avatar Sep 22 '26 01:09 agourakis82

I believe this just fixed by with https://github.com/llvm/llvm-project/pull/205600, I'll close this for now.

PaulkaToast avatar Oct 07 '26 22:10 PaulkaToast