r/Compilers • • 15d ago

Question about LLVM's Constraint Elimination Pass

Hi redditors, I am trying to understand how clang eliminates the dead code below:

#include <stdio.h>
#include <stdlib.h>

void foo(int M, int N, int P) {
  if (P > N)
    if (N > M)
      if (M > P)
        printf("%x\n", 0xDEADC0DE);
}

When I compile it with clang 20.1.0, I get this assembly:

foo:
        ret

If I compile it with clang 19.1.0 -O2 (-O3 makes no difference), then I get:

foo:
        cmp     edi, edx
        jle     .LBB0_3
        cmp     esi, edi
        jle     .LBB0_3
        cmp     edx, esi
        jle     .LBB0_3
        lea     rdi, [rip + .L.str]
        mov     esi, -559038242
        xor     eax, eax
        jmp     printf@PLT
.LBB0_3:
        ret

.L.str:
        .asciz  "%x\n"

When I do:

clang-20 -O2 -S -emit-llvm -mllvm -print-after-all -o /dev/null foo.c 2>&1

I can see that ConstraintEliminationPass is likely the one that proves that the branch is dead. However, this pass was already present in clang 19. I was wondering if some redditor that followed that implementation could explain how they improved the pass to make it able to solve that case.

Ps.: Notice that the same program would be fully optimized by gcc, already in version 7.1 -O2 (however, not in gcc 6.5, and not in gcc 7.1 -O1), using VRP.

7 Upvotes

8 comments sorted by

8

u/PudgeNikita 15d ago edited 14d ago

After about 4 hours (i have an i5-9400f) of trying to get it to compile in the first place (because of stuff like HAS_LOGF128 and stdint.h missing somewhere because of transitive header include changes) and then git bisecting llvm's opt to spit out false on the IR from

#include <stdbool.h>

bool foo(int M, int N, int P) {
  if (P > N) {
    if (N > M) {
      if (M > P) {
        return true;
      }
    }
  }
  return false;
}

which is

define i1 @foo(i32 %0, i32 %1, i32 %2) {
  %4 = icmp sgt i32 %2, %1
  %5 = icmp sgt i32 %1, %0
  %6 = and i1 %5, %4
  %7 = icmp sgt i32 %0, %2
  %8 = and i1 %7, %6
  ret i1 %8
}

with

cmake -G Ninja -S llvm -B build \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_C_COMPILER=clang \
  -DCMAKE_CXX_COMPILER=clang++ \
  -DCMAKE_CXX_FLAGS="-include cstdint" \
  -DCMAKE_CXX_FLAGS_RELEASE="-O1 -DNDEBUG -Wno-unnecessary-virtual-specifier -Wno-pass-failed" \
  -DLLVM_ENABLE_LIBCXX=OFF \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DLLVM_TARGETS_TO_BUILD="host" \
  -DLLVM_ENABLE_PROJECTS="" \
  -DLLVM_USE_LINKER=mold \
  -DLLVM_CCACHE_BUILD=ON \
  -DLLVM_INCLUDE_TESTS=OFF \
  -DLLVM_INCLUDE_BENCHMARKS=OFF \
  -DLLVM_INCLUDE_EXAMPLES=OFF \
  -DLLVM_INCLUDE_DOCS=OFF \
  -DLLVM_ENABLE_BINDINGS=OFF \
  -DBUILD_SHARED_LIBS=ON \
  -DHAS_LOGF128=0

git bisect start --term-old=alive --term-new=dead llvmorg-
20.1.0 llvmorg-19.1.0 -- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

git bisect run sh -c 'ninja -C build opt || exit 255; ! ./build/bin/opt -passes=constraint-elimination -S /tmp/repro.ll | grep -q "false"'

i think its this commit https://github.com/llvm/llvm-project/commit/0f0c0c36e3e90b4cb04004ed9c930f3863a36422
closing this https://github.com/llvm/llvm-project/issues/117107 issue

4

u/fernando_quintao 15d ago

Wow, that was exactly it, u/PudgeNikita. Very impressive: even the issue was pretty much this chain of impossible conditionals!

1

u/nzconstructionlawyer 14d ago

Yes a lot of LLVM is just special casing. It's not elegant.

3

u/regehr 15d ago

I don't know what changed to make this possible, but one nice thing about constraint elimination is that it can tell you what it's thinking, try this with your input:

clang -O2 -S -emit-llvm -o /dev/null foo.c -mllvm -debug-only=constraint-elimination,constraint-system

(this works with an LLVM I built yesterday, possibly won't work with earlier versions)

constraint elimination is one of my favorite LLVM passes-- it does a single thing well, and since it was introduced relatively recently, its code is quite clean and easy to understand.

2

u/dumael 15d ago

Have you tried running adding -llvm -debug to the command line? It may show the some of the internal workings of the pass. It may require an asserts build or debug build of LLVM.

1

u/ha9unaka 15d ago

1

u/RemindMeBot 15d ago

I will be messaging you in 1 day on 2026-11-16 13:51:19 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

-1

u/KingOfZero 15d ago

Ask in the LLVM Discourse channels and the actual engineers will respond