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

    <tr>
        <th>Summary</th>
        <td>
            [CodeGen] Assertion failure in MachineInstr::setDebugValueUndef during BranchFolder optimization
        </td>
    </tr>

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

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

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

<pre>
    ## Summary
Clang crashes with an assertion failure when building LLVM's Polly component in a self-hosted build configuration. The crash occurs during the Control Flow Optimizer pass in the BranchFolder's `HoistCommonCodeInSuccs` function.

## Bug Classification
Compiler crash bug - Backend code generator crash

## Assertion Details
```
Assertion failed: (isDebugValue() && "Must be a debug value instruction."), function setDebugValueUndef, file MachineInstr.h, line 2004.
```

## Stack Trace
```
Stack dump:
0.      Program arguments: clang [compiler flags]
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module 'polly/lib/External/isl/isl_transitive_closure.c'.
4.      Running pass 'Control Flow Optimizer' on function '@has_spurious_elements'
 #8 (anonymous namespace)::BranchRelaxation::run(llvm::MachineFunction&) (.cold.9)
 #9 llvm::MachineInstr::setDebugValueUndef()
#10 llvm::BranchFolder::HoistCommonCodeInSuccs(llvm::MachineBasicBlock*)::::operator()
#11 llvm::BranchFolder::HoistCommonCodeInSuccs(llvm::MachineBasicBlock*)
#12 llvm::BranchFolder::OptimizeFunction(...)
```

## Reproduction Steps
1. Configure LLVM build with self-hosted clang
2. Run `ninja` in build directory
3. Crash occurs when compiling `polly/lib/External/isl/isl_transitive_closure.c`
4. Also reproduces with `polly/lib/External/isl/isl_scheduler.c`

## Build Configuration
- **Host System:** macOS (Darwin 24.5.0, arm64)
- **Compiler:** clang version 22.0.0git 
- **Target:** arm64-apple-darwin24.5.0
- **Build Type:** Self-hosted build (clang building LLVM)
- **Optimization:** `-O2 -g -DNDEBUG`
- **Assertions:** Enabled (`+assertions`)

## Analysis
The assertion failure occurs when `setDebugValueUndef()` is called on a `MachineInstr` that fails the `isDebugValue()` check. This suggests that:

1. The BranchFolder optimization is incorrectly identifying a non-debug instruction as a debug instruction
2. There may be corruption in the debug metadata during the optimization pass
3. The debug instruction handling in `HoistCommonCodeInSuccs` has a logic error

This appears related to recent changes in debug instruction handling during branch folding, as there were recent reverts of patches modifying this code (commits 654c0d8 and 8d4fde5).

## Affected Files
- Files that trigger the crash: `polly/lib/External/isl/isl_transitive_closure.c`, `polly/lib/External/isl/isl_scheduler.c`
- Code location: `MachineInstr.h:2004` (`setDebugValueUndef` function)
- Optimization pass: `BranchFolder::HoistCommonCodeInSuccs`

## Workarounds
- Building with `-O1` or `-O0` may avoid the crash
- Disabling debug info (`-g`) may prevent the issue
- Excluding Polly from the build avoids the crash

## Additional Context
This crash prevents successful self-hosted LLVM builds and appears to be a regression in the debug instruction handling during backend optimization passes.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V9tu4zgS_RrmpWBBpu8PfrCdeGaA6U2jk5l9bNBUSeKEIgVekvZ-_aJIObaTdKPRiwUMJ6ZYFxbPOVUS3qvGIK7ZbMtmtzcihta69ZM7-mD71hxvDrY6rhmfMD6Bh9h1wh1ZudlpYRqQTvgWPbyo0IIwILxHF5Q1UAulo0N4adHAISpdKdPAn3_-_YnxhYfPVusjSNv11qAJoAwI8KjrUWt9wCqbgLSmVk10gnwW8NhiDglWyug8VNGR29Ai7KwJzmrYa_sC931QnfoPOuiF9-SdtmydMLLdW12hS1mwefm7VT7sbNdZs7MV_mEeopSezUuoo5EpLCs39MkV2MYGdpqqViuZ0qJi2K5XGt2Q3CE2MIKtkE9o6AwVQoMGnQh22HLlcvNatFsMQmlPj-bl8Ck3m6uiYsUmG2B8qfwtHmLzt9ARGV8yvgLG54zPgXH-KfoABwQBFW2CZ9oFyvjg4nAqzhlfMb57PSh4DGeXf5kK6_RY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