Skip to content

[LLVM][BranchFolding] Infinite loop happens in BranchFolding with BPF jumptable support #149712

@yonghong-song

Description

@yonghong-song

The following commit is the anticipated bpf jump table support.
yonghong-song@1890225

But with the above commit, I found BranchFolding has an infinite loop.
The following is the reproducer:

  1. Check out the above commit and do a build.

  2. Source code:

$ cat test2.c
  int foo(int a, int b) {
    __label__ l1, l2, l3, l4;
    void *jt1[] = {[0]=&&l1, [1]=&&l2};
    void *jt2[] = {[0]=&&l3, [1]=&&l4};
    int ret = 0;

    goto *jt1[a % 2];
    l1: ret += 1;
    l2: ret += 3;
    goto *jt2[b % 2];
    l3: ret += 5;
    l4: ret += 7;
    return ret;
  }
  1. The following clang compilation has a hang:
$ clang --target=bpf -O2 -S test2.c
^C
  1. Then I added '-mllvm -debug-only=branch-folder'
$ clang --target=bpf -O2 -S test2.c -mllvm -debug-only=branch-folder >& log
^C
$ cat log
TryTailMergeBlocks: %bb.0, %bb.2
  with successor %bb.1
  which has fall-through from %bb.0
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.0 is 1

TryTailMergeBlocks: %bb.0, %bb.1
  with successor %bb.2
  which has fall-through from %bb.1
Looking for common tails of at least 3 instructions
Common tail length of %bb.1 and %bb.0 is 1

TryTailMergeBlocks: %bb.0, %bb.1, %bb.2
  with successor %bb.3
  which has fall-through from %bb.2
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.1 is 3
Common tail length of %bb.2 and %bb.0 is 1
Common tail length of %bb.1 and %bb.0 is 1

Using common tail in %bb.2 for %bb.1
Common tail length of %bb.2 and %bb.0 is 1

TryTailMergeBlocks: %bb.0, %bb.2
  with successor %bb.4
  which has fall-through from %bb.3
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.0 is 1

TryTailMergeBlocks: %bb.4, %bb.3
  with successor %bb.5
  which has fall-through from %bb.4
Looking for common tails of at least 3 instructions

TryTailMergeBlocks: %bb.0, %bb.2
  with successor %bb.1
  which has fall-through from %bb.0
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.0 is 1

TryTailMergeBlocks: %bb.0, %bb.1
  with successor %bb.2
  which has fall-through from %bb.1
Looking for common tails of at least 3 instructions

TryTailMergeBlocks: %bb.0, %bb.2
  with successor %bb.4
  which has fall-through from %bb.2
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.0 is 1

TryTailMergeBlocks: %bb.4, %bb.3
  with successor %bb.5
  which has fall-through from %bb.4
Looking for common tails of at least 3 instructions

TryTailMergeBlocks: %bb.0, %bb.2
  with successor %bb.3
  which has fall-through from %bb.5
Looking for common tails of at least 3 instructions
Common tail length of %bb.2 and %bb.0 is 1

...

There are infinite TryTailMergeBlocks ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    hangCompiler hang (infinite loop)invalidResolved as invalid, i.e. not a bugllvm:optimizations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions