<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/97325>97325</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang][ICE] Unexpected interaction between two subroutines
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          pawosm-arm
      </td>
    </tr>
</table>

<pre>
    Hello,

I've found a piece of Fortran code which exposes a puzzling behavior of the flang-new compiler:

```
MODULE minimal

  USE, INTRINSIC :: ISO_C_BINDING, ONLY: c_ptr, c_null_ptr, c_size_t

  INTERFACE
 FUNCTION c_malloc(size) BIND(C, name = 'malloc')
      IMPORT :: c_ptr, c_size_t
      IMPLICIT NONE
      INTEGER(c_size_t), VALUE :: size
 TYPE(c_ptr) :: c_malloc
    END FUNCTION
  END INTERFACE

CONTAINS
  SUBROUTINE something1(la, ia)
    LOGICAL, INTENT(OUT) :: la(1)
    INTEGER, INTENT(IN) :: ia(1)

    la = ia
  END SUBROUTINE

  SUBROUTINE something2()
    TYPE(c_ptr) :: ptr

    ptr = c_malloc(1)
  END SUBROUTINE
END MODULE
```

Trying to compile this results in an ICE:

```
error: loc("minimal.f90":19:5): 'llvm.call' op result type mismatch: '!llvm.ptr' != 'i64'
error: Lowering to LLVM IR failed
flang-new: llvm-project/llvm/lib/IR/Instructions.cpp:2444: void llvm::InsertValueInst::init(llvm::Value*, llvm::Value*, llvm::ArrayRef<unsigned int>, const llvm::Twine&): Assertion `ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == Val->getType() && "Inserted value must match indexed type!"' failed.
```

It is sufficient to either replace the `ptr = c_malloc(1)` line with the `ptr = c_null_ptr` line in the `something2` subroutine or remove the `la = ia` line in the `something1` subroutine to make this problem go away.

I've also tried the explicit bbc -> tco -> opt -> llc path, and the ICE did not occur.

</pre>
<img width="1px" height="1px"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