[llvm] [Bitcode] Fix nondeterministic phi instruction order (PR #151006)

Michael Hackner via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 28 21:07:14 PDT 2025


https://github.com/HackAttack updated https://github.com/llvm/llvm-project/pull/151006

>From 2a4a81e320564ed90365b0803a705060e7e9da31 Mon Sep 17 00:00:00 2001
From: Michael Hackner <mhackner at gmail.com>
Date: Mon, 28 Jul 2025 10:34:27 -0700
Subject: [PATCH] [Bitcode] Fix nondeterministic phi instruction order

Currently there can be nondeterminism of the form

```diff
- <INST_PHI op0=307 op1=2 op2=22 op3=67 op4=29 op5=111 op6=57 ...>
+ <INST_PHI op0=307 op1=2 op2=22 op3=67 op4=29 op5=157 op6=71 ...>
```

in generated bitcode. Breaking operand number ties with User solves
that.
---
 llvm/lib/Bitcode/Writer/ValueEnumerator.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index e133abe577c22..bf2905bc54812 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -211,6 +211,10 @@ static void predictValueUseListOrderImpl(const Value *V, const Function *F,
 
     // LID and RID are equal, so we have different operands of the same user.
     // Assume operands are added in order for all instructions.
+    // If operand numbers are equal, use pointer addresses for deterministic ordering.
+    if (LU->getOperandNo() == RU->getOperandNo())
+      return LU->getUser() < RU->getUser();
+
     if (LID <= ID)
       if (!IsGlobalValue) // GlobalValue uses don't get reversed.
         return LU->getOperandNo() < RU->getOperandNo();



More information about the llvm-commits mailing list