Skip to content

Commit 22c34e6

Browse files
committed
[WebAssembly] Support the new "Lime1" CPU
This adds WebAssembly support for the new [Lime1 CPU]. First, this defines some new target features. These are subsets of existing features that reflect implementation concerns: - "call-indirect-overlong" - implied by "reference-types"; just the overlong encoding for the `call_indirect` immediate, and not the actual reference types. - "bulk-memory-opt" - implied by "bulk-memory": just `memory.copy` and `memory.fill`, and not the other instructions in the bulk-memory proposal. Next, this defines a new target CPU, "lime1", which enables mutable-globals, bulk-memory-opt, multivalue, sign-ext, nontrapping-fptoint, extended-const, and call-indirect-overlong. Unlike the default "generic" CPU, "lime1" is meant to be frozen, and followed up by "lime2" and so on when new features are desired. [Lime1 CPU]: https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1
1 parent 668f2c7 commit 22c34e6

33 files changed

+217
-64
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,15 @@ and `-mbulk-memory` flags, which correspond to the [Bulk Memory Operations]
854854
and [Non-trapping float-to-int Conversions] language features, which are
855855
[widely implemented in engines].
856856

857+
A new Lime1 target CPU is added, -mcpu=lime1. This CPU follows the definition of
858+
the Lime1 CPU [here], and enables -mmultivalue, -mmutable-globals,
859+
-mcall-indirect-overlong, -msign-ext, -mbulk-memory-opt, -mnontrapping-fptoint,
860+
and -mextended-const.
861+
857862
[Bulk Memory Operations]: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
858863
[Non-trapping float-to-int Conversions]: https://github.com/WebAssembly/spec/blob/master/proposals/nontrapping-float-to-int-conversion/Overview.md
859864
[widely implemented in engines]: https://webassembly.org/features/
865+
[here]: https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1
860866

861867
AVR Support
862868
^^^^^^^^^^^

clang/lib/Basic/Targets/WebAssembly.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
4747
return llvm::StringSwitch<bool>(Feature)
4848
.Case("atomics", HasAtomics)
4949
.Case("bulk-memory", HasBulkMemory)
50+
.Case("bulk-memory-opt", HasBulkMemoryOpt)
51+
.Case("call-indirect-overlong", HasCallIndirectOverlong)
5052
.Case("exception-handling", HasExceptionHandling)
5153
.Case("extended-const", HasExtendedConst)
5254
.Case("fp16", HasFP16)
@@ -79,6 +81,8 @@ void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
7981
Builder.defineMacro("__wasm_atomics__");
8082
if (HasBulkMemory)
8183
Builder.defineMacro("__wasm_bulk_memory__");
84+
if (HasBulkMemoryOpt)
85+
Builder.defineMacro("__wasm_bulk_memory_opt__");
8286
if (HasExceptionHandling)
8387
Builder.defineMacro("__wasm_exception_handling__");
8488
if (HasExtendedConst)
@@ -155,12 +159,25 @@ bool WebAssemblyTargetInfo::initFeatureMap(
155159
const std::vector<std::string> &FeaturesVec) const {
156160
auto addGenericFeatures = [&]() {
157161
Features["bulk-memory"] = true;
162+
Features["bulk-memory-opt"] = true;
163+
Features["call-indirect-overlong"] = true;
158164
Features["multivalue"] = true;
159165
Features["mutable-globals"] = true;
160166
Features["nontrapping-fptoint"] = true;
161167
Features["reference-types"] = true;
162168
Features["sign-ext"] = true;
163169
};
170+
auto addLime1Features = [&]() {
171+
// Lime1:
172+
// <https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1>
173+
Features["multivalue"] = true;
174+
Features["mutable-globals"] = true;
175+
Features["call-indirect-overlong"] = true;
176+
Features["sign-ext"] = true;
177+
Features["bulk-memory-opt"] = true;
178+
Features["nontrapping-fptoint"] = true;
179+
Features["extended-const"] = true;
180+
};
164181
auto addBleedingEdgeFeatures = [&]() {
165182
addGenericFeatures();
166183
Features["atomics"] = true;
@@ -174,6 +191,8 @@ bool WebAssemblyTargetInfo::initFeatureMap(
174191
};
175192
if (CPU == "generic") {
176193
addGenericFeatures();
194+
} else if (CPU == "lime1") {
195+
addLime1Features();
177196
} else if (CPU == "bleeding-edge") {
178197
addBleedingEdgeFeatures();
179198
}
@@ -200,6 +219,14 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
200219
HasBulkMemory = false;
201220
continue;
202221
}
222+
if (Feature == "+bulk-memory-opt") {
223+
HasBulkMemoryOpt = true;
224+
continue;
225+
}
226+
if (Feature == "-bulk-memory-opt") {
227+
HasBulkMemoryOpt = false;
228+
continue;
229+
}
203230
if (Feature == "+exception-handling") {
204231
HasExceptionHandling = true;
205232
continue;
@@ -265,6 +292,14 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
265292
HasReferenceTypes = false;
266293
continue;
267294
}
295+
if (Feature == "+call-indirect-overlong") {
296+
HasCallIndirectOverlong = true;
297+
continue;
298+
}
299+
if (Feature == "-call-indirect-overlong") {
300+
HasCallIndirectOverlong = false;
301+
continue;
302+
}
268303
if (Feature == "+relaxed-simd") {
269304
SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
270305
continue;
@@ -310,6 +345,18 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
310345
<< Feature << "-target-feature";
311346
return false;
312347
}
348+
349+
// The reference-types feature included the change to `call_indirect`
350+
// encodings to support overlong immediates.
351+
if (HasReferenceTypes) {
352+
HasCallIndirectOverlong = true;
353+
}
354+
355+
// bulk-memory-opt is a subset of bulk-memory.
356+
if (HasBulkMemory) {
357+
HasBulkMemoryOpt = true;
358+
}
359+
313360
return true;
314361
}
315362

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
5555

5656
bool HasAtomics = false;
5757
bool HasBulkMemory = false;
58+
bool HasBulkMemoryOpt = false;
5859
bool HasExceptionHandling = false;
5960
bool HasExtendedConst = false;
6061
bool HasFP16 = false;
@@ -63,6 +64,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
6364
bool HasMutableGlobals = false;
6465
bool HasNontrappingFPToInt = false;
6566
bool HasReferenceTypes = false;
67+
bool HasCallIndirectOverlong = false;
6668
bool HasSignExt = false;
6769
bool HasTailCall = false;
6870
bool HasWideArithmetic = false;

lld/test/wasm/compress-relocs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: llc -filetype=obj %s -o %t.o
2-
; RUN: llvm-mc -mattr=+reference-types -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/call-indirect.s -o %t2.o
2+
; RUN: llvm-mc -mattr=+call-indirect-overlong -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/call-indirect.s -o %t2.o
33
; RUN: wasm-ld --export-dynamic -o %t.wasm %t2.o %t.o
44
; RUN: obj2yaml %t.wasm | FileCheck %s
55
; RUN: wasm-ld --export-dynamic -O2 -o %t-opt.wasm %t2.o %t.o

lld/test/wasm/import-table-explicit.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: llvm-mc -mattr=+reference-types -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
1+
# RUN: llvm-mc -mattr=+call-indirect-overlong -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
22
# RUN: wasm-ld --import-table -o %t.wasm %t.o
33
# RUN: obj2yaml %t.wasm | FileCheck %s
44

lld/test/wasm/invalid-mvp-table-use.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
22
#
33
# If any table is defined or declared besides the __indirect_function_table,
4-
# the compilation unit should be compiled with -mattr=+reference-types,
4+
# the compilation unit should be compiled with -mattr=+call-indirect-overlong,
55
# causing symbol table entries to be emitted for all tables.
66
# RUN: not wasm-ld --no-entry %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
77

lld/test/wasm/lto/Inputs/libcall-archive.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ define void @memcpy() #0 {
55
ret void
66
}
77

8-
attributes #0 = { "target-features"="-bulk-memory" }
8+
attributes #0 = { "target-features"="-bulk-memory,-bulk-memory-opt" }

lld/test/wasm/lto/libcall-archive.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ entry:
1616

1717
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture, i64, i1)
1818

19-
attributes #0 = { "target-features"="-bulk-memory" }
19+
attributes #0 = { "target-features"="-bulk-memory,-bulk-memory-opt" }
2020

2121
; CHECK: - Type: CUSTOM
2222
; CHECK-NEXT: Name: name

lld/test/wasm/lto/stub-library-libcall.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t_main.o %t/main.s
33
# RUN: llvm-as %S/Inputs/foo.ll -o %t_foo.o
44
# RUN: llvm-as %S/Inputs/libcall.ll -o %t_libcall.o
5-
# RUN: wasm-ld -mllvm -mattr=-bulk-memory %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
5+
# RUN: wasm-ld -mllvm -mattr=-bulk-memory,-bulk-memory-opt %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
66
# RUN: obj2yaml %t.wasm | FileCheck %s
77

88
# The function `func_with_libcall` will generate an undefined reference to
@@ -12,7 +12,7 @@
1212
# If %t_foo.o is not included in the link we get an undefined symbol reported
1313
# to the dependency of memcpy on the foo export:
1414

15-
# RUN: not wasm-ld -mllvm -mattr=-bulk-memory %t_main.o %t_libcall.o %p/Inputs/stub.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING %s
15+
# RUN: not wasm-ld -mllvm -mattr=-bulk-memory,-bulk-memory-opt %t_main.o %t_libcall.o %p/Inputs/stub.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING %s
1616
# MISSING: stub.so: undefined symbol: foo. Required by memcpy
1717

1818
#--- main.s

lld/test/wasm/multi-table.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ call_indirect_explicit_tables:
2626
call_indirect table_b, () -> ()
2727
end_function
2828

29-
# RT-MVP: wasm-ld: error: object file not built with 'reference-types' feature conflicts with import of table table_a by file
29+
# RT-MVP: wasm-ld: error: object file not built with 'call-indirect-overlong' feature conflicts with import of table table_a by file
3030

3131
# CHECK: --- !WASM
3232
# CHECK-NEXT: FileHeader:

0 commit comments

Comments
 (0)