llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[flang] Error when lowering to FIR (Block construct)

Open ceseo opened this issue 3 years ago • 3 comments

flang-new version 17.0.0 (fc748408c6436c2a80e372156d53e15b014c6105)

This issue was found while building SPEC CPU 2017 with Flang. A reduced testcase follows:

subroutine foo(mystr, myvar)                                                    
    character(*), optional :: mystr                                             
    integer :: myvar                                                            
                                                                                
    if (present(mystr)) then                                                    
      select case (trim(mystr))                                                 
        case ('my_test')                                                        
          myvar = 77                                                            
        case default                                                            
          myvar = 78                                                            
      end select                                                                
    else                                                                        
      myvar = 79                                                                
    end if                                                                      
                                                                                
  end subroutine

This generates:

error: loc("/tmp/tests/test.f90":11:7): operation with block successors must terminate its parent block
error: verification of lowering to FIR failed

Analyzing the bbc output:

// -----// IR Dump Before Fortran::lower::VerifierPass () //----- //
"builtin.module"() ({
  "func.func"() <{arg_attrs = [{fir.bindc_name = "mystr", fir.optional}, {fir.bindc_name = "myvar"}], function_type = (!fir.boxchar<1>, !fir.ref<i32>) -> (), sym_name = "_QPfoo"}> ({
  ^bb0(%arg0: !fir.boxchar<1>, %arg1: !fir.ref<i32>):
    %0 = "fir.alloca"() {in_type = !fir.box<!fir.heap<!fir.char<1,?>>>, operand_segment_sizes = array<i32: 0, 0>} : () -> !fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>
    %1:2 = "fir.unboxchar"(%arg0) : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
    %2 = "fir.is_present"(%1#0) : (!fir.ref<!fir.char<1,?>>) -> i1
    "cf.cond_br"(%2)[^bb1, ^bb6] <{operand_segment_sizes = array<i32: 1, 0, 0>}> : (i1) -> ()
  ^bb1:  // pred: ^bb0
    %3 = "fir.embox"(%1#0, %1#1) {operand_segment_sizes = array<i32: 1, 0, 0, 1, 0>} : (!fir.ref<!fir.char<1,?>>, index) -> !fir.box<!fir.char<1,?>>
    %4 = "fir.zero_bits"() : () -> !fir.heap<!fir.char<1,?>>
    %5 = "arith.constant"() <{value = 0 : index}> : () -> index
    %6 = "fir.embox"(%4, %5) {operand_segment_sizes = array<i32: 1, 0, 0, 1, 0>} : (!fir.heap<!fir.char<1,?>>, index) -> !fir.box<!fir.heap<!fir.char<1,?>>>
    "fir.store"(%6, %0) : (!fir.box<!fir.heap<!fir.char<1,?>>>, !fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>) -> ()
    %7 = "fir.address_of"() {symbol = @_QQcl.dc297055db257c9785cb588cabc17e6c} : () -> !fir.ref<!fir.char<1,36>>
    %8 = "arith.constant"() <{value = 6 : i32}> : () -> i32
    %9 = "fir.convert"(%0) : (!fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>) -> !fir.ref<!fir.box<none>>
    %10 = "fir.convert"(%3) : (!fir.box<!fir.char<1,?>>) -> !fir.box<none>
    %11 = "fir.convert"(%7) : (!fir.ref<!fir.char<1,36>>) -> !fir.ref<i8>
    %12 = "fir.call"(%9, %10, %11, %8) {callee = @_FortranATrim, fastmath = #arith.fastmath<contract>} : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none
    %13 = "fir.load"(%0) : (!fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>) -> !fir.box<!fir.heap<!fir.char<1,?>>>
    %14 = "fir.box_elesize"(%13) : (!fir.box<!fir.heap<!fir.char<1,?>>>) -> index
    %15 = "fir.box_addr"(%13) : (!fir.box<!fir.heap<!fir.char<1,?>>>) -> !fir.heap<!fir.char<1,?>>
    %16 = "fir.convert"(%15) : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,?>>
    %17 = "fir.address_of"() {symbol = @_QQcl.6D795F74657374} : () -> !fir.ref<!fir.char<1,7>>
    %18 = "arith.constant"() <{value = 7 : index}> : () -> index
    %19 = "fir.convert"(%17) : (!fir.ref<!fir.char<1,7>>) -> !fir.ref<!fir.char<1,?>>
    %20 = "fir.convert"(%16) : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
    %21 = "fir.convert"(%19) : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
    %22 = "fir.convert"(%14) : (index) -> i64
    %23 = "fir.convert"(%18) : (index) -> i64
    %24 = "fir.call"(%20, %21, %22, %23) {callee = @_FortranACharacterCompareScalar1, fastmath = #arith.fastmath<contract>} : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
    %25 = "arith.constant"() <{value = 0 : i32}> : () -> i32
    %26 = "arith.cmpi"(%24, %25) <{predicate = 0 : i64}> : (i32, i32) -> i1
    "cf.cond_br"(%26)[^bb3, ^bb2] <{operand_segment_sizes = array<i32: 1, 0, 0>}> : (i1) -> ()
  ^bb2:  // pred: ^bb1
    "cf.br"()[^bb4] : () -> ()
  ^bb3:  // pred: ^bb1
    %27 = "arith.constant"() <{value = 77 : i32}> : () -> i32
    "fir.store"(%27, %arg1) : (i32, !fir.ref<i32>) -> ()
    "cf.br"()[^bb5] : () -> ()
  ^bb4:  // pred: ^bb2
    %28 = "arith.constant"() <{value = 78 : i32}> : () -> i32
    "fir.store"(%28, %arg1) : (i32, !fir.ref<i32>) -> ()
    "cf.br"()[^bb5] : () -> ()
  ^bb5:  // 2 preds: ^bb3, ^bb4
    "cf.br"()[^bb7] : () -> ()
    "fir.freemem"(%15) : (!fir.heap<!fir.char<1,?>>) -> ()
    "cf.br"()[^bb7] : () -> ()
  ^bb6:  // pred: ^bb0
    %29 = "arith.constant"() <{value = 79 : i32}> : () -> i32
    "fir.store"(%29, %arg1) : (i32, !fir.ref<i32>) -> ()
    "cf.br"()[^bb7] : () -> ()
  ^bb7:  // 3 preds: ^bb5, ^bb5, ^bb6
    "func.return"() : () -> ()
  }) : () -> ()
  "func.func"() <{function_type = (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none, sym_name = "_FortranATrim", sym_visibility = "private"}> ({
  }) {fir.runtime} : () -> ()
  "fir.global"() ({
    %0 = "fir.string_lit"() {size = 36 : i64, value = "/home/carlos.seo/Downloads/test.f90\00"} : () -> !fir.char<1,36>
    "fir.has_value"(%0) : (!fir.char<1,36>) -> ()
  }) {constant, linkName = "internal", sym_name = "_QQcl.dc297055db257c9785cb588cabc17e6c", symref = @_QQcl.dc297055db257c9785cb588cabc17e6c, type = !fir.char<1,36>} : () -> ()
  "fir.global"() ({
    %0 = "fir.string_lit"() {size = 7 : i64, value = "my_test"} : () -> !fir.char<1,7>
    "fir.has_value"(%0) : (!fir.char<1,7>) -> ()
  }) {constant, linkName = "internal", sym_name = "_QQcl.6D795F74657374", symref = @_QQcl.6D795F74657374, type = !fir.char<1,7>} : () -> ()
  "func.func"() <{function_type = (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32, sym_name = "_FortranACharacterCompareScalar1", sym_visibility = "private"}> ({
  }) {fir.runtime} : () -> ()
}) {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "aarch64-unknown-linux-gnu"} : () -> ()


error: loc("/tmp/tests/test.f90":11:7): operation with block successors must terminate its parent block
oops, pass manager reported failure

This particular bit is interesting:

  ^bb5:  // 2 preds: ^bb3, ^bb4
    "cf.br"()[^bb7] : () -> ()
    "fir.freemem"(%15) : (!fir.heap<!fir.char<1,?>>) -> ()
    "cf.br"()[^bb7] : () -> ()
  ^bb6:  // pred: ^bb0
    %29 = "arith.constant"() <{value = 79 : i32}> : () -> i32
    "fir.store"(%29, %arg1) : (i32, !fir.ref<i32>) -> ()
    "cf.br"()[^bb7] : () -> ()
  ^bb7:  // 3 preds: ^bb5, ^bb5, ^bb6
    "func.return"() : () -> ()

When closing the SELECT CASE statement (^bb5), it is generating two unconditional branches.

My investigation shows that this problem started after patch 2c1433453d1670f668220670b8f2df60f9dc9949. Apparently, this new block construct design doesn't handle well these nested blocks (btw, if I remove either the trim or present calls, it works fine).

I'm working on a fix for this.

ceseo avatar May 15 '23 23:05 ceseo

@llvm/issue-subscribers-flang-ir

llvmbot avatar May 16 '23 00:05 llvmbot

Which benchmark in Spec2017 is this?

kiranchandramohan avatar May 16 '23 10:05 kiranchandramohan

@kiranchandramohan 628.pop2_s

ceseo avatar May 16 '23 13:05 ceseo