Skip to content

Commit e096d6f

Browse files
Compiler tweaks for Wasm & WASI (#5158)
1 parent 91eabe8 commit e096d6f

6 files changed

Lines changed: 34 additions & 5 deletions

File tree

driver/linker-gcc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ int linkObjToBinaryGcc(llvm::StringRef outputPath,
810810
// exception: invoke (ld-compatible) linker directly for WebAssembly targets
811811
std::string tool;
812812
std::unique_ptr<ArgsBuilder> argsBuilder;
813-
if (global.params.targetTriple->isOSBinFormatWasm()) {
813+
if (global.params.targetTriple->isOSBinFormatWasm() && !global.params.targetTriple->isOSWASI()) {
814814
argsBuilder = std::make_unique<LdArgsBuilder>();
815815
tool = getProgram("wasm-ld", &opts::linker);
816816
} else {

driver/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,26 @@ void registerPredefinedTargetVersions() {
908908
break;
909909
case llvm::Triple::WASI:
910910
VersionCondition::addPredefinedGlobalIdent("WASI");
911+
VersionCondition::addPredefinedGlobalIdent("WASIp1");
911912
VersionCondition::addPredefinedGlobalIdent("CRuntime_WASI");
912913
break;
914+
#if LLVM_VERSION_MAJOR >= 22
915+
case llvm::Triple::WASIp1:
916+
VersionCondition::addPredefinedGlobalIdent("WASI");
917+
VersionCondition::addPredefinedGlobalIdent("WASIp1");
918+
VersionCondition::addPredefinedGlobalIdent("CRuntime_WASI");
919+
break;
920+
case llvm::Triple::WASIp2:
921+
VersionCondition::addPredefinedGlobalIdent("WASI");
922+
VersionCondition::addPredefinedGlobalIdent("WASIp2");
923+
VersionCondition::addPredefinedGlobalIdent("CRuntime_WASI");
924+
break;
925+
case llvm::Triple::WASIp3:
926+
VersionCondition::addPredefinedGlobalIdent("WASI");
927+
VersionCondition::addPredefinedGlobalIdent("WASIp3");
928+
VersionCondition::addPredefinedGlobalIdent("CRuntime_WASI");
929+
break;
930+
#endif
913931
case llvm::Triple::Emscripten:
914932
VersionCondition::addPredefinedGlobalIdent("Emscripten");
915933
// Emscripten uses musl and libc++, so mimic a musl Linux platform:

gen/llvmhelpers.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ llvm::cl::opt<llvm::GlobalVariable::ThreadLocalMode> clThreadModel(
7171
bool isTargetWindowsMSVC() {
7272
return global.params.targetTriple->isWindowsMSVCEnvironment();
7373
}
74+
bool isTargetWasm() {
75+
return global.params.targetTriple->isWasm();
76+
}
77+
7478

7579
/******************************************************************************
7680
* Global context
@@ -289,7 +293,7 @@ void DtoCAssert(Module *M, Loc loc, LLValue *msg) {
289293
args.push_back(file);
290294
args.push_back(line);
291295
args.push_back(msg);
292-
} else if (triple.isOSSolaris() || triple.isMusl() ||
296+
} else if (triple.isOSSolaris() || triple.isMusl() || triple.isOSWASI() ||
293297
global.params.isUClibcEnvironment ||
294298
triple.isGNUEnvironment()) {
295299
const auto irFunc = gIR->func();

gen/llvmhelpers.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ import dmd.dtemplate;
2020
extern (C++) void DtoSetFuncDeclIntrinsicName(TemplateInstance ti, TemplateDeclaration td, FuncDeclaration fd);
2121

2222
extern (C++) bool isTargetWindowsMSVC();
23+
extern (C++) bool isTargetWasm();

gen/runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static const char *getCAssertFunctionName() {
358358
return "_assert";
359359
} else if (triple.isOSSolaris()) {
360360
return "__assert_c99";
361-
} else if (triple.isMusl() || triple.isGNUEnvironment()) {
361+
} else if (triple.isMusl() || triple.isGNUEnvironment() || triple.isOSWASI()) {
362362
return "__assert_fail";
363363
} else if (global.params.isNewlibEnvironment) {
364364
return "__assert_func";
@@ -372,7 +372,7 @@ static std::vector<PotentiallyLazyType> getCAssertFunctionParamTypes() {
372372
const auto uint = Type::tuns32;
373373

374374
if (triple.isOSDarwin() || triple.isOSFreeBSD() || triple.isOSSolaris() ||
375-
triple.isMusl() || global.params.isUClibcEnvironment ||
375+
triple.isMusl() || triple.isOSWASI() || global.params.isUClibcEnvironment ||
376376
triple.isGNUEnvironment()) {
377377
return {voidPtr, voidPtr, uint, voidPtr};
378378
}

tests/codegen/wasi.d

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// REQUIRES: target_WebAssembly, link_WebAssembly
22

33
// emit textual IR *and* compile & link
4-
// RUN: %ldc -mtriple=wasm32-unknown-wasi -output-ll -output-o -of=%t.wasm %s
4+
// RUN: %ldc -mtriple=wasm32-unknown-wasi --linker=lld -Xcc=-nostdlib -output-ll -output-o -of=%t.wasm %s
55
// RUN: FileCheck %s < %t.ll
6+
//
7+
// RUN: %ldc -mtriple=wasm32-unknown-wasip1 --linker=lld -Xcc=-nostdlib -output-ll -output-o -of=%t_p1.wasm %s
8+
// RUN: FileCheck %s < %t_p1.ll
9+
//
10+
// RUN: %ldc -mtriple=wasm32-unknown-wasip2 --linker=lld -Xcc=-nostdlib -output-ll -output-o -of=%t_p2.wasm %s
11+
// RUN: FileCheck %s < %t_p2.ll
612

713

814
// test predefined versions:

0 commit comments

Comments
 (0)