Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6142] structs are incorrectly exploded with $@convention(c) on some targets #48697

Closed
compnerd opened this issue Oct 13, 2017 · 9 comments
Closed
Assignees
Labels
affects ABI Flag: Affects ABI bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation Linux Platform: Linux

Comments

@compnerd
Copy link
Collaborator

Previous ID SR-6142
Radar None
Original Reporter @compnerd
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AffectsABI, IRGen, Linux
Assignee @compnerd
Priority Medium

md5: 74f9d9208c50e942836c6b8c51917f6b

Issue Description:

IRGenCall currently explodes fields of structures when doing a FFI call into a function with the C calling convention. This would break on at least Linux ARM (hard float ABI), Windows ARM, PowerPC64, S390x all of which pass arguments differently. Linux ARM and Windows ARM pass the argument as a first class structure type (struct {float, float}) and PowerPC64 and s390x pass it as a single i64 type.

@compnerd
Copy link
Collaborator Author

reduced.swift:

// RUN: %swiftc -frontend -target thumbv7-unknown-windows-msvc -parse-stdlib -parse-as-library -I Inputs/usr/include -S -o - %s

import SRoA

public func g(_ s: S) {
{{ return f(s)}}
}

Inputs/usr/include/SRoA.h:

typedef struct S {
{{ float f;}}
{{ float g;}}
} S;

void f(S);

Inputs/usr/include/module.map:

module SRoA {
{{ header "SRoA.h"}}
}

@belkadan
Copy link
Contributor

cc @rjmccall, aschwaighofer@apple.com (JIRA User)

@aschwaighofer
Copy link
Member

Currently, IRGen (in GenCall.cpp) will unconditionally explode llvm::StructTypes if the clang::CodeGen::ABIArgInfo type is Direct.

John explained to me that clang::CodeGen::ABIArgInfo has extra state whether this expansion is allowed to happen (ABIArgInfo::getCanBeFlattened).

I can see three places in GenCall.cpp that need to be modified to respect this flag: emitDirectForeignParameter, expandExternalSignatureTypes, emitDirectExternalArgument.

@compnerd
Copy link
Collaborator Author

Yeah, I was using `getCanBeFlattened`. However, I am having some issues with the threading of the `ArgInfo`. Note that this also causes some issues since I saw cases of `getCanBeFlattened` returning `true` for `i8*`.

@aschwaighofer
Copy link
Member

'i8*' is an llvm::PointerType not an llvm::StructType. clang's lib/CodeGen/CGCall.cpp is instructive.

  llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
  if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
    ...

@compnerd
Copy link
Collaborator Author

Agreed; I figured that out later, I just meant that we need a bit more checking 🙂

@compnerd
Copy link
Collaborator Author

BTW, changing just one site was sufficient; however, that would still cause issues in `visitFullApplySite`. IRGenSIL is where I was missing access to the the `ArgABIInfo`.

@compnerd
Copy link
Collaborator Author

#12525 should address this issue, but there are a couple of failing tests it seems.

@aschwaighofer
Copy link
Member

Thank you!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects ABI Flag: Affects ABI bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself IRGen LLVM IR generation Linux Platform: Linux
Projects
None yet
Development

No branches or pull requests

3 participants