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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] About BIND(C) attribute terminating abnormally during compilation
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          ohno-fj
      </td>
    </tr>
</table>

<pre>
    ```
Version of flang : 22.0.0(e3af202fd212a66700170717856a8fa9aa7ed426)/AArch64
```

The build of [bigdft-atlab / version:1.9.5](https://gitlab.com/l_sim/bigdft-suite/-/tree/1.9.5/atlab) results in an abnormal compilation termination.  
The cause of the above is a program that uses `BIND(C) attribute` to call `malloc function`.

Regarding this issue, I confirmed the existence of a similar issue in llvm-project's Pull requests.  
In https://github.com/llvm/llvm-project/pull/78428#issuecomment-1898026175, there is similar error to this issue, and it seems to be a similar problem when using `BIND(C) attribute`.  
Is this the same problem as `#78428`? In that case, should I wait for the correction of `#78428`?  

The following are a self-made reproducer, Flang, Gfortran and ifx compilation/execution results.

z01.f90:
```fortran
subroutine test(array, len)
  use iso_c_binding, only: c_ptr

  implicit none

  interface
    pure function my_malloc(size, file, line) bind(C, name='malloc')
      use, intrinsic :: iso_c_binding, only: c_ptr, c_char
      implicit none
      type(c_ptr) :: my_malloc
      integer(kind=8), intent(in), value :: size
      character(kind=c_char), intent(in) :: file(*)
      integer(kind=8), intent(in), value :: line
    end function my_malloc
  end interface

  integer(kind=4), dimension(:), pointer, intent(out) :: array
  integer(kind=8), intent(in) :: len
  type(c_ptr) :: ptr

  ptr = my_malloc(4 * len, 'test', 1_8)
  call c_f_pointer(ptr, array, [len])
end subroutine test
```

```
$ flang z01.f90 -c
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/bigdft-atlab/z01.f90":22:3): 'llvm.call' op incorrect number of operands (1) for callee (expecting: 4)
error: Lowering to LLVM IR failed
flang: /work/groups/ssoft/compiler/llvm/src/llvm-main/llvm/lib/IR/Instructions.cpp:756: void llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, const llvm::Twine&): Assertion `(Args.size() == FTy->getNumParams() || (FTy->isVar
Arg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +outline-atomics -target-feature +v8a -target-feature +fp-armv8 -target-feature
 +neon -resource-dir /work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/lib/clang/22 -mframe-pointer=non-leaf -o z01.o -x f95 z01.f90
 #0 0x000040003018b3b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libLLVM.so.22.0git+0x5afb3b8)
 #1 0x0000400030188960 llvm::sys::RunSignalHandlers() (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libLLVM.so.22.0git+0x5af8960)
 #2 0x0000400030188ab8 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x000040001ef207a0 (linux-vdso.so.1+0x7a0)
 #4 0x0000400034fe6274 raise (/lib64/libc.so.6+0x36274)
 #5 0x0000400034fd0a2c abort (/lib64/libc.so.6+0x20a2c)
 #6 0x0000400034fdfba0 __assert_fail_base (/lib64/libc.so.6+0x2fba0)
 #7 0x0000400034fdfc18 __assert_perror_fail (/lib64/libc.so.6+0x2fc18)
 #8 0x0000400030330388 llvm::CallInst::Create(llvm::CallInst*, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*>>, llvm::InsertPosition) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libLLVM.so.22.0git+0x5ca0388)
 #9 0x000040002a29bf5c llvm::IRBuilderBase::CreateCall(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*>>, llvm::Twine const&, llvm::MDNode*) LLVMToLLVMIRTranslation.cpp:0:0
#10 0x000040002a2b4e54 convertOperationImpl(mlir::Operation&, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) LLVMToLLVMIRTranslation.cpp:0:0
#11 0x000040002a336cf8 mlir::LLVM::ModuleTranslation::convertOperation(mlir::Operation&, llvm::IRBuilderBase&, bool) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libMLIR.so.22.0git+0x6926cf8)
#12 0x000040002a33ff78 mlir::LLVM::ModuleTranslation::convertBlockImpl(mlir::Block&, bool, llvm::IRBuilderBase&, bool) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libMLIR.so.22.0git+0x692ff78)
#13 0x000040002a340e1c mlir::LLVM::ModuleTranslation::convertOneFunction(mlir::LLVM::LLVMFuncOp) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libMLIR.so.22.0git+0x6930e1c)
#14 0x000040002a341920 mlir::LLVM::ModuleTranslation::convertFunctions() (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libMLIR.so.22.0git+0x6931920)
#15 0x000040002a349770 mlir::translateModuleToLLVMIR(mlir::Operation*, llvm::LLVMContext&, llvm::StringRef, bool) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libMLIR.so.22.0git+0x6939770)
#16 0x0000000005243ed8 Fortran::frontend::CodeGenAction::generateLLVMIR() (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x5243ed8)
#17 0x0000000005246e2c Fortran::frontend::CodeGenAction::executeAction() (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x5246e2c)
#18 0x000000000522dfac Fortran::frontend::FrontendAction::execute() (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x522dfac)
#19 0x000000000521e184 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x521e184)
#20 0x0000000005235e1c Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x5235e1c)
#21 0x00000000052166ec fc1_main(llvm::ArrayRef<char const*>, char const*) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x52166ec)
#22 0x00000000052160a8 main (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x52160a8)
#23 0x0000400034fd4384 __libc_start_main (/lib64/libc.so.6+0x24384)
#24 0x000000000521456c _start (/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang+0x521456c)
flang-21: error: unable to execute command: Aborted (core dumped)
flang-21: error: flang frontend command failed due to signal (use -v to see invocation)
flang version 22.0.0git (https://github.com/llvm/llvm-project.git e3af202fd212a66700170717856a8fa9aa7ed426)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin
Build config: +assertions
flang-21: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-21: note: diagnostic msg: /tmp/z01-67447e
flang-21: note: diagnostic msg: /tmp/z01-67447e.sh
flang-21: note: diagnostic msg:

********************
$
```

```
$ cat /tmp/z01-67447e
#line "./z01.f90" 1
      subroutine test(array, len)
      use iso_c_binding, only: c_ptr

      implicit none

      interface
      pure function my_malloc(size, file, line) bind(C, name='malloc')
      use, intrinsic :: iso_c_binding, only: c_ptr, c_char
      implicit none
      type(c_ptr) :: my_malloc
      integer(kind=8), intent(in), value :: size
      character(kind=c_char), intent(in) :: file(*)
      integer(kind=8), intent(in), value :: line
      end function my_malloc
      end interface

      integer(kind=4), dimension(:), pointer, intent(out) :: array
      integer(kind=8), intent(in) :: len
      type(c_ptr) :: ptr

      ptr = my_malloc(4 * len, 'test', 1_8)
      call c_f_pointer(ptr, array, [len])
      end subroutine test
$
```

```
$ cat /tmp/z01-67447e.sh
# Crash reproducer for clang version 22.0.0git (https://github.com/llvm/llvm-project.git e3af202fd212a66700170717856a8fa9aa7ed426)
# Driver args: "z01.f90" "-c"
# Original command:  "/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang" "-fc1" "-triple" "aarch64-unknown-linux-gnu" "-emit-obj" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-target-cpu" "generic" "-target-feature" "+outline-atomics" "-target-feature" "+v8a" "-target-featur
e" "+fp-armv8" "-target-feature" "+neon" "-resource-dir" "/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/lib/clang/22" "-mframe-pointer=non-leaf" "-o" "z01.o" "-x" "f95" "z01.f90"
 "/work/groups/ssoft/compiler/llvm/aarch64/main-20250721-e3af202fd212/bin/flang" "-fc1" "-triple" "aarch64-unknown-linux-gnu" "-emit-obj" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-target-cpu" "generic" "-target-feature" "+outline-atomics" "-target-feature" "+v8a" "-target-feature" "+fp-armv8" "-tar
get-feature" "+neon" "-mframe-pointer=non-leaf" "-x" "f95" "z01-67447e"
$
```


```
$ gfortran z01.f90 -c
$
```

```
$ ifx z01.f90 -c
$
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWltz27iS_jXwC4oqEpRE6sEP8kUz3spMUo539lEFgk0JExLgAqDtnF-_1QApSrJ8S0589lSdlGOT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