[llvm-dev] verifyFunction fails validation with Terminator found in the middle of a basic block

229 views
Skip to first unread message

Vijay via llvm-dev

unread,
Oct 20, 2021, 3:49:32 PM10/20/21
to llvm...@lists.llvm.org
Hi,

I'm generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then

The resulting IR still works fine but I'm not sure if the code generation should be done in a better way for the if/then block below.

Thanks in advance,
Vijay

define double @fact(double %n) {
entry:
  %n1 = alloca double, align 8
  store double %n, double* %n1, align 8
  %n2 = load double, double* %n1, align 8
  %cmptmp = fcmp ule double %n2, 1.000000e+00
  %booltmp = uitofp i1 %cmptmp to double
  %ifcond = fcmp one double %booltmp, 0.000000e+00
  br i1 %ifcond, label %then, label %ifcont

then:                                             ; preds = %entry
  ret double 1.000000e+00
  br label %ifcont

ifcont:                                           ; preds = %then, %entry
  %n3 = load double, double* %n1, align 8
  %n4 = load double, double* %n1, align 8
  %subtmp = fsub double %n4, 1.000000e+00
  %calltmp = call double @fact(double %subtmp)
  %multmp = fmul double %n3, %calltmp
  ret double %multmp
}
Validating function...
Terminator found in the middle of a basic block!
label %then
Validation failed

Min-Yih Hsu via llvm-dev

unread,
Oct 20, 2021, 4:18:31 PM10/20/21
to Vijay, llvm-dev
On Wed, Oct 20, 2021 at 12:52 PM Vijay via llvm-dev <llvm...@lists.llvm.org> wrote:
Hi,

I'm generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then

Because there shouldn't be a terminator instruction (in this case the return instruction) in the middle of a basic block. I see no problem with the verifier's decision here.
I think the %then block should only contain the return instruction. The `br label %ifcont` instruction is redundant because the control flow will not proceed anyway beyond the return instruction once %then block is taken.

-Min
 
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


--
Min-Yih Hsu
Ph.D Student in ICS Department, University of California, Irvine (UCI).

Vijay via llvm-dev

unread,
Oct 21, 2021, 9:51:45 PM10/21/21
to llvm...@lists.llvm.org
Hi,

I'm generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then

Michael Kruse via llvm-dev

unread,
Oct 22, 2021, 2:00:49 PM10/22/21
to Vijay, llvm-dev
Am Do., 21. Okt. 2021 um 20:51 Uhr schrieb Vijay via llvm-dev
<llvm...@lists.llvm.org>:

>
> Hi,
>
> I'm generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then
>
> The resulting IR still works fine but I'm not sure if the code generation should be done in a better way for the if/then block below.
>
> Thanks in advance,
> Vijay
>
> define double @fact(double %n) {
> entry:
> %n1 = alloca double, align 8
> store double %n, double* %n1, align 8
> %n2 = load double, double* %n1, align 8
> %cmptmp = fcmp ule double %n2, 1.000000e+00
> %booltmp = uitofp i1 %cmptmp to double
> %ifcond = fcmp one double %booltmp, 0.000000e+00
> br i1 %ifcond, label %then, label %ifcont
>
> then: ; preds = %entry
> ret double 1.000000e+00
> br label %ifcont

Nothing is executed after a ret instruction, violating the definition
of a basic block (executes all instructions from beginning to end).
Passes are not expecting this situation; if something still works, it
is pure luck.

Just remove the br instruction, it's not executed anyway.

Michael


> ifcont: ; preds = %then, %entry
> %n3 = load double, double* %n1, align 8
> %n4 = load double, double* %n1, align 8
> %subtmp = fsub double %n4, 1.000000e+00
> %calltmp = call double @fact(double %subtmp)
> %multmp = fmul double %n3, %calltmp
> ret double %multmp
> }
> Validating function...
> Terminator found in the middle of a basic block!
> label %then
> Validation failed

Reply all
Reply to author
Forward
0 new messages