<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/92161>92161</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Unexpected asm() duplication with `-O1` and `-fsanitize`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          andyhhp
      </td>
    </tr>
</table>

<pre>
    When compiling Xen with Clang (at least Clang 11, 16 and 17), with UBSAN active, the build currently fails with:
```
common/wait.c:17:9: error: invalid symbol redefinition
 ".L_wq_resume: rep movsb;"
        ^
```

I've made a minimal repro of the problem:
```c
struct cpu_info {
};

struct cpu_info *get_cpu_info(void);

struct waitqueue_vcpu {
    void *esp;
    char *stack;
};

void __prepare_to_wait(struct waitqueue_vcpu *wqv)
{
    struct cpu_info *cpu_info = get_cpu_info();
    unsigned long dummy;

    asm volatile (
        ".L_wq_resume: rep movsb;"

        : "=&S" (wqv->esp), "=&c" (dummy), "=&D" (dummy)
        : "0" (0), "1" (cpu_info), "2" (wqv->stack),
          [sz] "i" (((1L) << 12))
 : "memory", "rax", "rdx", "r8", "r9", "r10", "r11" );

    if ( wqv->esp == ((void*)0) )
    {
 }
}
```
and compiling:
```
$ clang -m64 -O1 -fsanitize=undefined -fno-sanitize=alignment -c wait-min.i -o wait.o
wait-min.i:17:9: error: invalid symbol redefinition
 ".L_wq_resume: rep movsb;"
        ^
<inline asm>:1:2: note: instantiated into assembly here
        .L_wq_resume: rep movsb;
 ^
1 error generated.
```

This function is doing some special gymnastics with asm under the hood, and it is similar to `setjmp()`. `.L_wq_resume` is a single label in the binary, jumped to from other locations.

I've found various way to avoid this bug.
1. drop `-fno-sanitize=alignment`
2. drop `-O1`
3. This diff:
```diff
@@ -21,7 +21,7 @@ void __prepare_to_wait(struct waitqueue_vcpu *wqv)
 [sz] "i" (((1L) << 12))
         : "memory", "rax", "rdx", "r8", "r9", "r10", "r11" );
 
-    if ( wqv->esp == ((void*)0) )
+    if ( 0 && wqv->esp == ((void*)0) )
     {
     }
 }
```

Sadly none of these are workarounds I can take into Xen.

It's clearly something related to the later basic block, and optimisation, and no alignment sanitisation, but I've got no clue why why the asm block would be duplicated under any circumstance.   This smells like a code-gen bug.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vk1v2zgT_jX0ZWBDovx58MGJG6BA8fbQt9jeDIoaS2z4oZCUXffXL4ZSbMdJikWL3cCxxeF8PDOceSgRgqot4prN7thsOxJdbJxfC1udmqYdla46rf9q0IJ0plVa2Rq-oYWjig3ca2FrYHwpImgUIQ6SPGf8HvI5CFtBvmB8Retk8vXuy-Z_IGRUByRhbBDKTukKZOc92qhPsBdKh6TOig3LtizbsHk2fNJSOmOcZfzhKFScSFZs8gUrNitWbAC9d54elD0IrSoIJ1M6DR4r3CuronK29wKM88mn3fFp5zF0BsnIYwvGHULJijvG-aA4_LHZhzfh9N8fGV8cEIyoEAQYZZURFLb1Dtw-Zdp6V2o0r9OS_TpE38kIsu12yu4dsMXdoLjYEqCraK9U-abGuHteM748OFVR6d-0o8I9ddjh7iDb7hKI0iRD8oehPRuTXDbCkzxEIR8vbl9BS_a7XeuxFR530e0oGuPLd2LzzfHpQEgHf1ch38jy8lxs4Sbl63TJvLOpuSvQztZQdcacbrCSlggGDk6LqDRSN98c-j9skhurIrUXK7aMz78wzsnx8ekwZsUHqms_EmcNOWj0EG82t7ebb0XKBqXsYp0PokuBnnf4S0D9gabdl76p5e_CTzbbkpUarPpP_onxFbDinhX3kPNk_gxtwGTQOH-iPPqwXvy4WlTXi-XV8-rqOc-uF0NGtz1NQNWeoMGlxNQf1CI92n4YNoyvsgT7uoqXjqNePjf1W5NOhHYmwvfYifEpyESEYzOfwvhzDuN9EEQ9P5EV284mJsIKxnvrxlc7QqvaGrQRxjKNydgoO1Ewdmk1cX2Ay85_z3zFvbJaWaSp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