Skip to content

[WASM] Add minimal support for targeting wasm32-unknown-unknown-wasm. #20684

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ namespace swift {
Target.isPS4() || Target.isOSHaiku() ||
Target.getTriple().empty()) {
major = minor = revision = 0;
} else if (Target.isOSBinFormatWasm()) {
major = minor = revision = 0;
} else {
llvm_unreachable("Unsupported target OS");
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
addPlatformConditionValue(PlatformConditionKind::OS, "PS4");
else if (Target.isOSHaiku())
addPlatformConditionValue(PlatformConditionKind::OS, "Haiku");
else if (Target.isOSBinFormatWasm())
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that we should use the OSBinFormat check here. The problem is that WASM supports both ELF and WASM as object file formats, so if someone does wasm-unknown-none-elf that is going to give an error. I think that we can check if the "architecture" is wasm, and use that to indicate that we are targeting WASM.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not anymore.

But I agree that the 'os' platform condition shouldn't be determined by the file format. If we're going to present wasm as an OS, I think it should be a kind of "default" we use when the triple's arch is wasm32/64 and the OS & vendor are both unknown. It's not exactly the same, but IMO that would be closer to what clang does.

addPlatformConditionValue(PlatformConditionKind::OS, "WebAssembly");
else
UnsupportedOS = true;

Expand Down Expand Up @@ -221,6 +223,12 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::Arch, "s390x");
break;
case llvm::Triple::ArchType::wasm32:
addPlatformConditionValue(PlatformConditionKind::Arch, "wasm32");
break;
case llvm::Triple::ArchType::wasm64:
addPlatformConditionValue(PlatformConditionKind::Arch, "wasm64");
break;
default:
UnsupportedArch = true;
}
Expand Down Expand Up @@ -252,6 +260,10 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::Endianness, "big");
break;
case llvm::Triple::ArchType::wasm32:
case llvm::Triple::ArchType::wasm64:
addPlatformConditionValue(PlatformConditionKind::Endianness, "little");
break;
default:
llvm_unreachable("undefined architecture endianness");
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) {
StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
switch (triple.getOS()) {
case llvm::Triple::UnknownOS:
// NOTE: WebAssembly doesn't yet have a defined OS convention in triples.
if (triple.isOSBinFormatWasm()) {
return "wasm";
}

llvm_unreachable("unknown OS");
case llvm::Triple::Ananas:
case llvm::Triple::CloudABI:
Expand Down
5 changes: 5 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
case llvm::Triple::Haiku:
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
default:
// NOTE: WebAssembly doesn't yet have a defined OS convention in triples.
if (target.isOSBinFormatWasm()) {
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
}

Diags.diagnose(SourceLoc(), diag::error_unknown_target,
ArgList.getLastArg(options::OPT_target)->getValue());
break;
Expand Down