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

Predefine version FreeStanding when targeting bare-metal #3608

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,9 @@ void registerPredefinedTargetVersions() {
VersionCondition::addPredefinedGlobalIdent("Android");
} else {
llvm::StringRef osName = triple.getOSName();
if (!osName.empty() && osName != "unknown" && osName != "none") {
if (osName.empty() || osName == "unknown" || osName == "none") {
VersionCondition::addPredefinedGlobalIdent("FreeStanding");
} else {
warning(Loc(), "unknown target OS: %s", osName.str().c_str());
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/codegen/unknown_critical_section_size.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// RUN: %ldc -mtriple=x86_64 -c -w %s
// RUN: not %ldc -mtriple=x86_64 -c -w -d-version=WithSynchronized %s 2>&1 | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

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

hm, I always thought that specifying a triple without the 2nd/3rd parts would select the default for the host OS, instead of selecting "no OS"....

Copy link
Member

Choose a reason for hiding this comment

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

See Clang's documentation https://clang.llvm.org/docs/CrossCompilation.html#target-triple
Clang also uses the default when a triple part is empty, instead of assuming none.
-ffreestanding is what we should implement, in my opinion.
See also: https://chromium.googlesource.com/external/llvm.org/clang/+/google/stable/test/CodeGen/PR3589-freestanding-libcalls.c

Copy link
Member Author

Choose a reason for hiding this comment

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

Clang also uses the default when a triple part is empty, instead of assuming none.

Doesn't seem to be the case: https://cpp.godbolt.org/z/ThW5jW. I think -march=x86-64 is to be used when only wanting to target another arch, with the same host OS and environment.

Copy link
Member

Choose a reason for hiding this comment

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

Ah OK. Indeed.
I presume FreeStanding can be used to disable all parts of druntime that depend on a host OS. So it'd be good to be able to still set the arch without changing OS.
Is LDCs behavior changed now? (e.g. that before -mtriple=x86_64 would still compile-in all Linux/Windows/... parts.)
Don't think so right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing has changed (FreeStanding isn't used anywhere in druntime/Phobos yet); Windows, linux, OSX etc. haven't been predefined before either. I think it just might make disabling parts you've mentioned easier.


// no target OS
version (FreeStanding) {} else static assert(0);

void foo() {}

version (WithSynchronized)
void bar()
{
__gshared int global;
// CHECK: unknown_critical_section_size.d(12): Error: unknown critical section size for the selected target
Copy link
Member

Choose a reason for hiding this comment

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

fyi, FileCheck has @LINE for such cases: ...size.d([[@LINE+1]]): Error...
http://llvm.org/docs/CommandGuide/FileCheck.html#filecheck-pseudo-numeric-variables

// CHECK: unknown_critical_section_size.d(15): Error: unknown critical section size for the selected target
synchronized
{
global += 10;
Expand Down