Skip to content

[LLVM] bug: branch with a single target and different arguments #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangweixuan opened this issue Jun 20, 2023 · 1 comment
Closed

Comments

@wangweixuan
Copy link

I believe Koopa IR allows br instructions where both branches target the same basic block. I am interested when they carry different arguments:

fun @foo() {
%entry:
  br 0, %end(1), %end(2)

%end(%r: i32):
  ret %r       // returns 2
}

libkoopa cannot faithfully lower it to LLVM IR:

define i32 @foo() {
$entry:
  %$0 = icmp ne i32 0, 0
  br i1 %$0, label %$end, label %$end

$end:
  %$r = phi i32 [1, %$entry]
  ret i32 %$r       ; won't compile using `llc', returns 1 using `clang'
}
@MaxXSoft
Copy link
Member

I believe Koopa IR allows br instructions where both branches target the same basic block.

True.

I am interested when they carry different arguments:

I actually overlooked this issue before, my fault.

This can be handled in two ways:

  1. When there is a branch instruction with two identical target basic blocks and two sets of BB parameters, it performs a selection first, passes selected values as BB parameters, and then transfers the control flow to the target block:
br %cond, %target(%a0, %a1, ...), %target(%b0, %b1, ...)

is actually:

%p0 = select %cond, %a0, %b0
%p1 = select %cond, %a1, %b1
...
jump %target(%p0, %p1, ...)
  1. This kind of usage is illegal.

From the example you provided, it seems that your understanding is based on the first approach. However, since the basic block parameter is an alternative way of representing Phi functions, I would prefer to disable this usage for the sake of IR semantic orthogonality.

Thanks for your feedback. I'll add some additional checks in koopa/libkoopa later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants