Skip to content

[flang][ICE] Unexpected interaction between two subroutines #97325

Closed
@pawosm-arm

Description

@pawosm-arm

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. Also, replacing the LOGICAL and INTEGER types in something1 with matching types (e.g. with both LOGICAL or both INTEGER) makes the problem go away.

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

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorflang:ir

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions