-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[MC][WebAssembly] Fix importing tables with limits #172992
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
base: main
Are you sure you want to change the base?
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-lld-wasm @llvm/pr-subscribers-llvm-mc Author: None (daxpedda) ChangesThe additional parameter was unnecessary when the information is already readily available in the Fixes #172868. Pinging @sbc100 as the target maintainer. Full diff: https://github.com/llvm/llvm-project/pull/172992.diff 2 Files Affected:
diff --git a/lld/test/wasm/import-table-explicit.s b/lld/test/wasm/import-table-explicit.s
index 701b7a1dc3e16..34dfea4466ebd 100644
--- a/lld/test/wasm/import-table-explicit.s
+++ b/lld/test/wasm/import-table-explicit.s
@@ -5,11 +5,18 @@
.globl __indirect_function_table
.tabletype __indirect_function_table, funcref
+.globl __table_with_limits
+.import_module __table_with_limits, env
+.import_name __table_with_limits, __table_with_limits
+.tabletype __table_with_limits, funcref, 2, 10
+
.globl _start
_start:
.functype _start () -> ()
i32.const 1
call_indirect __indirect_function_table, () -> ()
+ i32.const 1
+ call_indirect __table_with_limits, () -> ()
end_function
# Verify the --import-table flag creates a table import
@@ -24,3 +31,14 @@ _start:
# CHECK-NEXT: ElemType: FUNCREF
# CHECK-NEXT: Limits:
# CHECK-NEXT: Minimum: 0x1
+
+# CHECK: - Module: env
+# CHECK-NEXT: Field: __table_with_limits
+# CHECK-NEXT: Kind: TABLE
+# CHECK-NEXT: Table:
+# CHECK-NEXT: Index: 0
+# CHECK-NEXT: ElemType: FUNCREF
+# CHECK-NEXT: Limits:
+# CHECK-NEXT: Flags: [ HAS_MAX ]
+# CHECK-NEXT: Minimum: 0x2
+# CHECK-NEXT: Maximum: 0xA
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 15590b31fd07f..f0ad3bf97a2b4 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -323,8 +323,8 @@ class WasmObjectWriter : public MCObjectWriter {
void writeValueType(wasm::ValType Ty) { W->OS << static_cast<char>(Ty); }
void writeTypeSection(ArrayRef<wasm::WasmSignature> Signatures);
- void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint64_t DataSize,
- uint32_t NumElements);
+ void writeImportSection(ArrayRef<wasm::WasmImport> Imports,
+ uint64_t DataSize);
void writeFunctionSection(ArrayRef<WasmFunction> Functions);
void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
void writeElemSection(const MCSymbolWasm *IndirectFunctionTable,
@@ -823,8 +823,7 @@ void WasmObjectWriter::writeTypeSection(
}
void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
- uint64_t DataSize,
- uint32_t NumElements) {
+ uint64_t DataSize) {
if (Imports.empty())
return;
@@ -855,7 +854,9 @@ void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
case wasm::WASM_EXTERNAL_TABLE:
W->OS << char(Import.Table.ElemType);
encodeULEB128(Import.Table.Limits.Flags, W->OS);
- encodeULEB128(NumElements, W->OS); // initial
+ encodeULEB128(Import.Table.Limits.Minimum, W->OS);
+ if (Import.Table.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
+ encodeULEB128(Import.Table.Limits.Maximum, W->OS);
break;
case wasm::WASM_EXTERNAL_TAG:
W->OS << char(0); // Reserved 'attribute' field
@@ -1901,7 +1902,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
uint32_t CodeSectionIndex, DataSectionIndex;
if (Mode != DwoMode::DwoOnly) {
writeTypeSection(Signatures);
- writeImportSection(Imports, DataSize, TableElems.size());
+ writeImportSection(Imports, DataSize);
writeFunctionSection(Functions);
writeTableSection(Tables);
// Skip the "memory" section; we import the memory instead.
|
|
@llvm/pr-subscribers-backend-webassembly Author: None (daxpedda) ChangesThe additional parameter was unnecessary when the information is already readily available in the Fixes #172868. Pinging @sbc100 as the target maintainer. Full diff: https://github.com/llvm/llvm-project/pull/172992.diff 2 Files Affected:
diff --git a/lld/test/wasm/import-table-explicit.s b/lld/test/wasm/import-table-explicit.s
index 701b7a1dc3e16..34dfea4466ebd 100644
--- a/lld/test/wasm/import-table-explicit.s
+++ b/lld/test/wasm/import-table-explicit.s
@@ -5,11 +5,18 @@
.globl __indirect_function_table
.tabletype __indirect_function_table, funcref
+.globl __table_with_limits
+.import_module __table_with_limits, env
+.import_name __table_with_limits, __table_with_limits
+.tabletype __table_with_limits, funcref, 2, 10
+
.globl _start
_start:
.functype _start () -> ()
i32.const 1
call_indirect __indirect_function_table, () -> ()
+ i32.const 1
+ call_indirect __table_with_limits, () -> ()
end_function
# Verify the --import-table flag creates a table import
@@ -24,3 +31,14 @@ _start:
# CHECK-NEXT: ElemType: FUNCREF
# CHECK-NEXT: Limits:
# CHECK-NEXT: Minimum: 0x1
+
+# CHECK: - Module: env
+# CHECK-NEXT: Field: __table_with_limits
+# CHECK-NEXT: Kind: TABLE
+# CHECK-NEXT: Table:
+# CHECK-NEXT: Index: 0
+# CHECK-NEXT: ElemType: FUNCREF
+# CHECK-NEXT: Limits:
+# CHECK-NEXT: Flags: [ HAS_MAX ]
+# CHECK-NEXT: Minimum: 0x2
+# CHECK-NEXT: Maximum: 0xA
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 15590b31fd07f..f0ad3bf97a2b4 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -323,8 +323,8 @@ class WasmObjectWriter : public MCObjectWriter {
void writeValueType(wasm::ValType Ty) { W->OS << static_cast<char>(Ty); }
void writeTypeSection(ArrayRef<wasm::WasmSignature> Signatures);
- void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint64_t DataSize,
- uint32_t NumElements);
+ void writeImportSection(ArrayRef<wasm::WasmImport> Imports,
+ uint64_t DataSize);
void writeFunctionSection(ArrayRef<WasmFunction> Functions);
void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
void writeElemSection(const MCSymbolWasm *IndirectFunctionTable,
@@ -823,8 +823,7 @@ void WasmObjectWriter::writeTypeSection(
}
void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
- uint64_t DataSize,
- uint32_t NumElements) {
+ uint64_t DataSize) {
if (Imports.empty())
return;
@@ -855,7 +854,9 @@ void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
case wasm::WASM_EXTERNAL_TABLE:
W->OS << char(Import.Table.ElemType);
encodeULEB128(Import.Table.Limits.Flags, W->OS);
- encodeULEB128(NumElements, W->OS); // initial
+ encodeULEB128(Import.Table.Limits.Minimum, W->OS);
+ if (Import.Table.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
+ encodeULEB128(Import.Table.Limits.Maximum, W->OS);
break;
case wasm::WASM_EXTERNAL_TAG:
W->OS << char(0); // Reserved 'attribute' field
@@ -1901,7 +1902,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
uint32_t CodeSectionIndex, DataSectionIndex;
if (Mode != DwoMode::DwoOnly) {
writeTypeSection(Signatures);
- writeImportSection(Imports, DataSize, TableElems.size());
+ writeImportSection(Imports, DataSize);
writeFunctionSection(Functions);
writeTableSection(Tables);
// Skip the "memory" section; we import the memory instead.
|
sbc100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
lgtm % testing comment
a63a97c to
158f4f4
Compare
sbc100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Thanks for working on this.
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.MC/WebAssembly/global-ctor-dtor.llLLVM.MC/WebAssembly/reloc-pic.sLLVM.MC/WebAssembly/reloc-pic64.sLLVM.MC/WebAssembly/weak-alias.sIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
🪟 Windows x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.MC/WebAssembly/global-ctor-dtor.llLLVM.MC/WebAssembly/reloc-pic.sLLVM.MC/WebAssembly/reloc-pic64.sLLVM.MC/WebAssembly/weak-alias.sIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
|
Looks like a few test failures. |
|
Ran the full MC Wasm test suite locally now to confirm. |
| // Update minimum size of `__indirect_function_table` table. | ||
| auto It = llvm::find_if(Imports, [](const wasm::WasmImport &I) { | ||
| return I.Field == "__indirect_function_table"; | ||
| }); | ||
|
|
||
| if (It != Imports.end()) { | ||
| It->Table.Limits.Minimum = TableElems.size(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure why TableElems exists in the first place. I would assume that the correct data should be present in the symbol to begin with, but seems that it isn't and that its supposed to be different.
I placed it here because its after TableElems has been modified, but I'm happy to move it elsewhere.
The additional parameter was unnecessary when the information is already readily available in the
WasmTableType.Fixes: #172868