Skip to content

Commit 758289d

Browse files
committed
Support for wasm32-wali-linux-musl target in clang, lld, and libcxx
1 parent 917f022 commit 758289d

File tree

12 files changed

+154
-25
lines changed

12 files changed

+154
-25
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,8 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
687687
}
688688
case llvm::Triple::wasm32:
689689
if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
690-
Triple.getVendor() != llvm::Triple::UnknownVendor ||
690+
(!Triple.isWALI() &&
691+
Triple.getVendor() != llvm::Triple::UnknownVendor) ||
691692
!Triple.isOSBinFormatWasm())
692693
return nullptr;
693694
switch (os) {
@@ -697,6 +698,10 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
697698
case llvm::Triple::Emscripten:
698699
return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
699700
Triple, Opts);
701+
// WALI OS target
702+
case llvm::Triple::Linux:
703+
return std::make_unique<WALITargetInfo<WebAssembly32TargetInfo>>(Triple,
704+
Opts);
700705
case llvm::Triple::UnknownOS:
701706
return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
702707
Triple, Opts);

clang/lib/Basic/Targets/OSTargets.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,23 @@ class LLVM_LIBRARY_VISIBILITY WASITargetInfo
936936
using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
937937
};
938938

939+
// WALI target
940+
template <typename Target>
941+
class LLVM_LIBRARY_VISIBILITY WALITargetInfo
942+
: public WebAssemblyOSTargetInfo<Target> {
943+
void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
944+
MacroBuilder &Builder) const final {
945+
WebAssemblyOSTargetInfo<Target>::getOSDefines(Opts, Triple, Builder);
946+
// Linux defines; list based off of gcc output
947+
DefineStd(Builder, "unix", Opts);
948+
DefineStd(Builder, "linux", Opts);
949+
Builder.defineMacro("__wali__");
950+
}
951+
952+
public:
953+
using WebAssemblyOSTargetInfo<Target>::WebAssemblyOSTargetInfo;
954+
};
955+
939956
// Emscripten target
940957
template <typename Target>
941958
class LLVM_LIBRARY_VISIBILITY EmscriptenTargetInfo

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
8888
LongDoubleWidth = LongDoubleAlign = 128;
8989
LongDoubleFormat = &llvm::APFloat::IEEEquad();
9090
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
91-
// size_t being unsigned long for both wasm32 and wasm64 makes mangled names
92-
// more consistent between the two.
93-
SizeType = UnsignedLong;
94-
PtrDiffType = SignedLong;
95-
IntPtrType = SignedLong;
9691
HasUnalignedAccess = true;
92+
if (T.isWALI()) {
93+
// WALI ABI requires 64-bit longs on both wasm32 and wasm64
94+
LongAlign = LongWidth = 64;
95+
SizeType = UnsignedInt;
96+
PtrDiffType = SignedInt;
97+
IntPtrType = SignedInt;
98+
} else {
99+
// size_t being unsigned long for both wasm32 and wasm64 makes mangled
100+
// names more consistent between the two.
101+
SizeType = UnsignedLong;
102+
PtrDiffType = SignedLong;
103+
IntPtrType = SignedLong;
104+
}
97105
}
98106

99107
StringRef getABI() const override;

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6833,6 +6833,8 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
68336833
TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
68346834
else if (Target.isOHOSFamily())
68356835
TC = std::make_unique<toolchains::OHOS>(*this, Target, Args);
6836+
else if (Target.isWALI())
6837+
TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
68366838
else
68376839
TC = std::make_unique<toolchains::Linux>(*this, Target, Args);
68386840
break;

libunwind/src/assembly.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ aliasname: \
249249
#define WEAK_ALIAS(name, aliasname)
250250
#define NO_EXEC_STACK_DIRECTIVE
251251

252+
#elif defined(__wasm__)
253+
#define NO_EXEC_STACK_DIRECTIVE
254+
252255
// clang-format on
253256
#else
254257

lld/wasm/Config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ struct Ctx {
199199
// Function that initializes passive data segments during instantiation.
200200
DefinedFunction *initMemory;
201201

202+
// __wasm_memory_grow
203+
// Function to perform memory.grow. Serves as a hook to
204+
// relieve engine APIs from performing this internally
205+
DefinedFunction *memoryGrow;
206+
207+
// __wasm_memory_size
208+
// Function to perform memory.size. Serves as a hook to
209+
// relieve engine APIs from performing this internally
210+
DefinedFunction *memorySize;
211+
202212
// __wasm_call_ctors
203213
// Function that directly calls all ctors in priority order.
204214
DefinedFunction *callCtors;

lld/wasm/Symbols.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ uint32_t DefinedFunction::getExportedFunctionIndex() const {
285285
return function->getFunctionIndex();
286286
}
287287

288+
void DefinedFunction::setExportNoWrap(bool v) { exportNoWrap = v; }
289+
290+
bool DefinedFunction::getExportNoWrap() const { return exportNoWrap; }
291+
288292
uint64_t DefinedData::getVA(bool absolute) const {
289293
LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n");
290294
// TLS symbols (by default) are relative to the start of the TLS output

lld/wasm/Symbols.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,20 @@ class DefinedFunction : public FunctionSymbol {
229229
return s->kind() == DefinedFunctionKind;
230230
}
231231

232+
// Get/set the exportNoWrap
233+
void setExportNoWrap(bool v);
234+
bool getExportNoWrap() const;
235+
232236
// Get the function index to be used when exporting. This only applies to
233237
// defined functions and can be differ from the regular function index for
234238
// weakly defined functions (that are imported and used via one index but
235239
// defined and exported via another).
236240
uint32_t getExportedFunctionIndex() const;
237241

238242
InputFunction *function;
243+
244+
protected:
245+
bool exportNoWrap = false;
239246
};
240247

241248
class UndefinedFunction : public FunctionSymbol {

lld/wasm/Writer.cpp

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Writer {
6060

6161
void createSyntheticInitFunctions();
6262
void createInitMemoryFunction();
63+
void createMemoryGrowFunction();
64+
void createMemorySizeFunction();
6365
void createStartFunction();
6466
void createApplyDataRelocationsFunction();
6567
void createApplyGlobalRelocationsFunction();
@@ -888,31 +890,33 @@ void Writer::createCommandExportWrappers() {
888890
toWrap.push_back(f);
889891

890892
for (auto *f : toWrap) {
891-
auto funcNameStr = (f->getName() + ".command_export").str();
892-
commandExportWrapperNames.push_back(funcNameStr);
893-
const std::string &funcName = commandExportWrapperNames.back();
893+
if (!(f->getExportNoWrap())) {
894+
auto funcNameStr = (f->getName() + ".command_export").str();
895+
commandExportWrapperNames.push_back(funcNameStr);
896+
const std::string &funcName = commandExportWrapperNames.back();
894897

895-
auto func = make<SyntheticFunction>(*f->getSignature(), funcName);
896-
if (f->function->getExportName())
897-
func->setExportName(f->function->getExportName()->str());
898-
else
899-
func->setExportName(f->getName().str());
898+
auto func = make<SyntheticFunction>(*f->getSignature(), funcName);
899+
if (f->function->getExportName())
900+
func->setExportName(f->function->getExportName()->str());
901+
else
902+
func->setExportName(f->getName().str());
900903

901-
DefinedFunction *def =
902-
symtab->addSyntheticFunction(funcName, f->flags, func);
903-
def->markLive();
904+
DefinedFunction *def =
905+
symtab->addSyntheticFunction(funcName, f->flags, func);
906+
def->markLive();
904907

905-
def->flags |= WASM_SYMBOL_EXPORTED;
906-
def->flags &= ~WASM_SYMBOL_VISIBILITY_HIDDEN;
907-
def->forceExport = f->forceExport;
908+
def->flags |= WASM_SYMBOL_EXPORTED;
909+
def->flags &= ~WASM_SYMBOL_VISIBILITY_HIDDEN;
910+
def->forceExport = f->forceExport;
908911

909-
f->flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
910-
f->flags &= ~WASM_SYMBOL_EXPORTED;
911-
f->forceExport = false;
912+
f->flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
913+
f->flags &= ~WASM_SYMBOL_EXPORTED;
914+
f->forceExport = false;
912915

913-
out.functionSec->addFunction(func);
916+
out.functionSec->addFunction(func);
914917

915-
createCommandExportWrapper(f->getFunctionIndex(), def);
918+
createCommandExportWrapper(f->getFunctionIndex(), def);
919+
}
916920
}
917921
}
918922

@@ -1136,6 +1140,8 @@ void Writer::createSyntheticInitFunctions() {
11361140
return;
11371141

11381142
static WasmSignature nullSignature = {{}, {}};
1143+
static WasmSignature memoryGrowSignature = {{ValType::I32}, {ValType::I32}};
1144+
static WasmSignature memorySizeSignature = {{ValType::I32}, {}};
11391145

11401146
createApplyDataRelocationsFunction();
11411147

@@ -1156,6 +1162,25 @@ void Writer::createSyntheticInitFunctions() {
11561162
}
11571163
}
11581164

1165+
// Memory grow/size export hooks
1166+
auto memoryGrowFunc =
1167+
make<SyntheticFunction>(memoryGrowSignature, "__wasm_memory_grow");
1168+
memoryGrowFunc->setExportName("__wasm_memory_grow");
1169+
ctx.sym.memoryGrow = symtab->addSyntheticFunction(
1170+
"__wasm_memory_grow",
1171+
WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED, memoryGrowFunc);
1172+
ctx.sym.memoryGrow->markLive();
1173+
ctx.sym.memoryGrow->setExportNoWrap(true);
1174+
1175+
auto memorySizeFunc =
1176+
make<SyntheticFunction>(memorySizeSignature, "__wasm_memory_size");
1177+
memorySizeFunc->setExportName("__wasm_memory_size");
1178+
ctx.sym.memorySize = symtab->addSyntheticFunction(
1179+
"__wasm_memory_size",
1180+
WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED, memorySizeFunc);
1181+
ctx.sym.memorySize->markLive();
1182+
ctx.sym.memorySize->setExportNoWrap(true);
1183+
11591184
if (ctx.arg.sharedMemory) {
11601185
if (out.globalSec->needsTLSRelocations()) {
11611186
ctx.sym.applyGlobalTLSRelocs = symtab->addSyntheticFunction(
@@ -1200,6 +1225,36 @@ void Writer::createSyntheticInitFunctions() {
12001225
}
12011226
}
12021227

1228+
void Writer::createMemoryGrowFunction() {
1229+
LLVM_DEBUG(dbgs() << "createMemoryGrowFunction\n");
1230+
assert(ctx.sym.memoryGrow);
1231+
std::string bodyContent;
1232+
{
1233+
raw_string_ostream os(bodyContent);
1234+
writeUleb128(os, 0, "num locals");
1235+
writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
1236+
writeUleb128(os, 0, "local 0");
1237+
writeU8(os, WASM_OPCODE_MEMORY_GROW, "memory grow");
1238+
writeUleb128(os, 0, "reserved memory byte");
1239+
writeU8(os, WASM_OPCODE_END, "END");
1240+
}
1241+
createFunction(ctx.sym.memoryGrow, bodyContent);
1242+
}
1243+
1244+
void Writer::createMemorySizeFunction() {
1245+
LLVM_DEBUG(dbgs() << "createMemorySizeFunction\n");
1246+
assert(ctx.sym.memorySize);
1247+
std::string bodyContent;
1248+
{
1249+
raw_string_ostream os(bodyContent);
1250+
writeUleb128(os, 0, "num locals");
1251+
writeU8(os, WASM_OPCODE_MEMORY_SIZE, "memory size");
1252+
writeUleb128(os, 0, "reserved memory byte");
1253+
writeU8(os, WASM_OPCODE_END, "END");
1254+
}
1255+
createFunction(ctx.sym.memorySize, bodyContent);
1256+
}
1257+
12031258
void Writer::createInitMemoryFunction() {
12041259
LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
12051260
assert(ctx.sym.initMemory);
@@ -1788,6 +1843,12 @@ void Writer::run() {
17881843
if (ctx.sym.initMemory) {
17891844
createInitMemoryFunction();
17901845
}
1846+
if (ctx.sym.memoryGrow) {
1847+
createMemoryGrowFunction();
1848+
}
1849+
if (ctx.sym.memorySize) {
1850+
createMemorySizeFunction();
1851+
}
17911852
createStartFunction();
17921853

17931854
createCallCtorsFunction();

llvm/include/llvm/BinaryFormat/Wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ enum : unsigned {
135135
WASM_OPCODE_BR_TABLE = 0x0e,
136136
WASM_OPCODE_RETURN = 0x0f,
137137
WASM_OPCODE_DROP = 0x1a,
138+
WASM_OPCODE_MEMORY_SIZE = 0x3f,
139+
WASM_OPCODE_MEMORY_GROW = 0x40,
138140
WASM_OPCODE_MISC_PREFIX = 0xfc,
139141
WASM_OPCODE_MEMORY_INIT = 0x08,
140142
WASM_OPCODE_MEMORY_FILL = 0x0b,

0 commit comments

Comments
 (0)