| Issue |
243701
|
| Summary |
[flang] Win64: INTEGER(16) values passed to and returned from flang-rt use a different convention than the runtime
|
| Labels |
platform:windows,
flang:codegen,
flang-rt
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
On x86_64 Windows, flang passes and returns 128-bit integers to flang-rt in a different way than the runtime (built by clang, where `common::int128_t` is `__int128`) expects them.
**Arguments.** flang passes an `i128` by value in two general-purpose registers; clang passes `__int128` by pointer on Win64. For example, `PRINT` of an INTEGER(16) value:
```fortran
subroutine print16(i16)
integer(16), intent(in) :: i16
print '(z0)', i16
end subroutine
```
```console
$ flang --target=x86_64-pc-windows-msvc -O1 -S print16.f90 -o - | grep -B3 OutputInteger128
movq (%rsi), %rdx
movq 8(%rsi), %r8
movq %rax, %rcx
callq _FortranAioOutputInteger128
```
The runtime side, compiled by clang for the same target, takes the argument by pointer:
```c
#include <stdbool.h>
bool callee16(void *cookie, __int128 n) { return n == 31; }
```
```console
$ clang --target=x86_64-pc-windows-msvc -O1 -S callee16.c -o - | grep -A2 '^callee16:'
callee16: # @callee16
# %bb.0: # %entry
movdqa (%rdx), %xmm0
```
So `_FortranAioOutputInteger128` dereferences the low half of the value as an address. This crashed `flang-rt/test/Driver/boz-input-field.f90` on the flang-x86_64-windows buildbot with an access violation (0xc0000005); that change was reverted in 243306 and relanded in 243678 with the INTEGER(16) case excluded on Windows. `CheckUnitNumberInRange128` takes an `int128_t` first, so a WRITE to an INTEGER(16) unit number takes the same path.
**Returns.** flang reads an `i128` result from RAX:RDX; clang returns `__int128` in XMM0 on Win64. For example, IALL of an INTEGER(16) array calls `_FortranAIAll16`:
```fortran
subroutine f(a, r)
integer(16), intent(in) :: a(:)
integer(16), intent(out) :: r
r = iall(a)
end subroutine
```
```console
$ flang --target=x86_64-pc-windows-msvc -O2 -S iall16.f90 -o - | grep -A2 'callq.*IAll16'
callq _FortranAIAll16
movq %rdx, 8(%rsi)
movq %rax, (%rsi)
```
```c
__int128 ret128(long long lo) { return (__int128)lo << 64 | 7; }
```
```console
$ clang --target=x86_64-pc-windows-msvc -O1 -S ret128.c -o - | grep -A4 '^ret128:'
ret128: # @ret128
# %bb.0: # %entry
movq %rcx, %xmm1
movdqa __xmm@00000000000000000000000000000007(%rip), %xmm0 # xmm0 = [7,u]
punpcklqdq %xmm1, %xmm0 # xmm0 = xmm0[0],xmm1[0]
```
So the caller reads RAX:RDX, which the callee never set: a wrong value rather than a crash.
**Where.** `TargetX86_64Win` in `flang/lib/Optimizer/CodeGen/Target.cpp` overrides only the COMPLEX hooks, so integer arguments take the generic by-value path. `TargetRewrite` converts integer results with `integerArgumentType`, and `integerReturnType` is not used. When flang-rt is built by cl.exe, `int128_t` is the `SignedInt128` class instead of `__int128` (`flang/include/flang/Common/uint128.h`), so the return convention also depends on which compiler built the runtime; a runtime interface that returns 128-bit results through a reference argument would avoid that dependence.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs