Skip to content

TypeSystem: extend the triple sanitization for Swift #1577

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

Merged
merged 1 commit into from
Aug 3, 2020

Conversation

compnerd
Copy link
Member

The triple as computed from the executable by lldb is x86_64--linux.
While this is sufficient for many uses, it is insufficient for Swift
which requires a matching triple.

x86_64--linux is morally equivalent to x86_64-unknown-linux which is
easily achieved through normalization. However, this is insufficient on
its own. This new triple is morally equivalent to
x86_64-unknown-linux-none-elf: a freestanding environment, that is, it
is a bare-metal target. Unlike the Darwin environments, the environment
on Linux is important as that identifies the libc (GNU = glibc, uclibc =
µlibc, musl = musl). Furthermore, it contains extended ABI information,
e.g. gnueabi vs gnueabihf which indicates glibc + EABI + soft float
vs hard float (-meabi, -mfloat-abi=soft/mflot-abi=hard) which are
incompatible targets.

Fortunately, triple normalization is a very common process in clang and
the API on llvm::Triple allows us to do this as explicit operations.
The previous approach explicitly replaced the arch name spelling of
x86_64h which is the Haswell slice with the generic x86_64
architecture to handle Darwin targets. Because the Haswell slice only
enables better code generation and is ABI compatible (though not
executable across previous generations), this difference does not
matter. Re-apply this change in terms of llvm::Triple.

This code now assumes that the Linux environment is gnu which is
woefully inadequate but given that the REPL currently is not meant to
execute on android (which uses android or androideabi for the
environment and the bionic libc) and Swift is currently hosted only, it
is a stop gap until this can be addressed more thoroughly.

This change is sufficient to enable unification of the Swift resource
directory layout across Darwin and non-Darwin targets.

The triple as computed from the executable by lldb is `x86_64--linux`.
While this is sufficient for many uses, it is insufficient for Swift
which requires a matching triple.

`x86_64--linux` is morally equivalent to `x86_64-unknown-linux` which is
easily achieved through normalization.  However, this is insufficient on
its own.  This new triple is morally equivalent to
`x86_64-unknown-linux-none-elf`: a freestanding environment, that is, it
is a bare-metal target.  Unlike the Darwin environments, the environment
on Linux is important as that identifies the libc (GNU = glibc, uclibc =
µlibc, musl = musl).  Furthermore, it contains extended ABI information,
e.g. `gnueabi` vs `gnueabihf` which indicates glibc + EABI + soft float
vs hard float (`-meabi`, `-mfloat-abi=soft`/`mflot-abi=hard`) which are
incompatible targets.

Fortunately, triple normalization is a very common process in clang and
the API on `llvm::Triple` allows us to do this as explicit operations.
The previous approach explicitly replaced the arch name spelling of
`x86_64h` which is the Haswell slice with the generic `x86_64`
architecture to handle Darwin targets.  Because the Haswell slice only
enables better code generation and is ABI compatible (though not
executable across previous generations), this difference does not
matter.  Re-apply this change in terms of `llvm::Triple`.

This code now assumes that the Linux environment is `gnu` which is
woefully inadequate but given that the REPL currently is not meant to
execute on android (which uses `android` or `androideabi` for the
environment and the bionic libc) and Swift is currently hosted only, it
is a stop gap until this can be addressed more thoroughly.

This change is sufficient to enable unification of the Swift resource
directory layout across Darwin and non-Darwin targets.
@compnerd
Copy link
Member Author

@swift-ci please test

/// It also add the GNU environment for Linux. Although this is technically
/// incorrect, as the `*-unknown-linux` environment represents the bare-metal
/// environment, because Swift is currently hosted only, we can get away with
/// it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always thought GNU was a vendor, not an environment? Is it both? Can you add to the comment what the effect of this is / why this is needed?
The aim of this function is to translate the real triple into a triple that we have a stdlib for. If that is what this is enables, I'm on board with the change.

Please cherry-pick to master-rebranch and master-next, too.

Copy link
Member Author

@compnerd compnerd Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GNU isn't a vendor, its an environment - the distributions are the vendors, and they all largely use unknown as they dont have any vendor extensions. I can certainly extend the comment (most of that is in the commit message anyways). An example is SuSE which vends a toolchain as x86_64-suse-linux-gnu.

Yeap, you nailed exactly: this is translating a triple (that is computed by lldb from repl_swift) into a triple that the Swift compiler can work with because it has a standard library for that target.

Ah, the joys of the rebranching, thanks for the reminder on that!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example is SuSE which vends a toolchain as x86_64-suse-linux-gnu.

Ah that makes perfect sense. Thanks for clearing that up!

@adrian-prantl adrian-prantl self-requested a review July 31, 2020 23:03
@compnerd compnerd merged commit 59561be into swiftlang:swift/master Aug 3, 2020
@compnerd compnerd deleted the triple-fun branch August 3, 2020 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants