450 Commits

Author SHA1 Message Date
Hood Chatham
15b03687ff
[WebAssembly,clang] Add __builtin_wasm_test_function_pointer_signature (#150201)
Tests if the runtime type of the function pointer matches the static
type. If this returns false, calling the function pointer will trap.
Uses `@llvm.wasm.ref.test.func` added in #147486.

Also adds a "gc" wasm feature to gate the use of the ref.test
instruction.
2025-07-25 16:52:39 -07:00
Hood Chatham
42d079e16e
[WebAssembly,MC] Add ref.test_func handling to AsmParser (#139642)
This will allow us to eliminate the hand-coded webassembly here:

https://github.com/python/cpython/blob/main/Python/emscripten_trampoline.c#L138
2025-07-07 13:31:57 -07:00
Heejin Ahn
4d1c827423
[WebAssembly] Support parsing .lto_set_conditional (#126546)
In the split-LTO-unit mode in ThinLTO, a compilation module is split
into two and global variables that meet a specific criteria is moved to
the split module.
d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L315-L366)

And if there is an originally local-linkage global value defined in the
original module and referenced in the split module or the vice versa,
that value is _promoted_ by attaching a module ID to their names in
order to prevent name clashes because now they can be referenced from
other modules.
d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L46-L100)

And when that promoted global value is a function, a
`.lto_set_conditional` entry is written to the original module to avoid
breaking references from inline assembly:

d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L84-L91)

The syntax of this is, if the original function name is `symbolA` and
the module ID is `123`,
```ll
module asm ".lto_set_conditional symbolA,symbolA.123"
```
These symbols are parsed here:

648981f913/llvm/lib/MC/MCParser/AsmParser.cpp (L6467)

The first function symbol in this `.lto_set_conditional` do not exist as
a function in the bitcode anymore because it was renamed to the second.
So they are not assigned as function symbols but they are not really
data either, so the object writer crashes here:
5b9e6c7993/llvm/lib/MC/WasmObjectWriter.cpp (L1820)

This PR makes the object writer just skip those symbols.

---

This problem was discovered when I was testing with
`-fwhole-program-vtables`. The reason we didn't have this problem before
with ThinLTO was because `-fsplit-lto-unit`, which splits LTO units when
possible, defaults to false, but it defaults to true when
`-fwhole-program-vtables` is used.
2025-04-02 03:15:29 +09:00
Fangrui Song
4f7ff6bb38 [MC] Clean up tests that implicit .text MCAsmStreamer 2024-12-22 18:11:50 -08:00
George Stagg
ed91843d43
[WebAssembly] Handle symbols in .init_array sections (#119127)
Follow on from #111008.
2024-12-10 08:28:18 -08:00
George Stagg
ac5dd455ca
[WebAssembly] Support multiple .init_array fragments when writing Wasm objects (#111008) 2024-12-04 13:12:15 -08:00
Dan Gohman
c3536b263f
[WebAssembly] Define call-indirect-overlong and bulk-memory-opt features (#117087)
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.

This is split out from https://github.com/llvm/llvm-project/pull/112035.

---------

Co-authored-by: Heejin Ahn <aheejin@gmail.com>
2024-12-02 17:08:07 -08:00
Dan Gohman
1bc2cd98c5
[WebAssembly] Enable nontrapping-fptoint and bulk-memory by default. (#112049)
We were prepared to enable these features [back in February], but they
got pulled for what appear to be unrelated reasons. So let's have
another try at enabling them!

Another motivation here is that it'd be convenient for the [Lime1
proposal] if "lime1" is close to a subset of "generic" (missing only
for extended-const).

[back in February]:
https://github.com/WebAssembly/tool-conventions/issues/158#issuecomment-1931119512
[Lime1 proposal]: https://github.com/llvm/llvm-project/pull/112035
2024-10-25 13:52:51 -07:00
Heejin Ahn
991adff462
[WebAssembly] Allow try_table to target loops in AsmTypeCheck (#111432) 2024-10-07 15:50:59 -07:00
Heejin Ahn
a98466ad89
[WebAssembly] Merge eh-assembly-legacy.s into eh-assembly.s (#111411)
Now that we don't need to specify `--no-type-check` on `eh-assembly.s`,
we can test the two modes in the same file.
2024-10-07 13:24:13 -07:00
Heejin Ahn
69577b2454
[WebAssembly] Support type checker for new EH (#111069)
This adds supports for the new EH instructions (`try_table` and
`throw_ref`) to the type checker.

One thing I'd like to improve on is the locations in the errors for
`catch_***` clauses. Currently they just point to the starting column of
`try_table` instruction itself. But to figure out where catch clauses
start you need to traverse `OperandVector` and check
`WebAssemblyOperand::isCatchList` on them to see which one is the catch
list operand, but `WebAssemblyOperand` class is in AsmParser and
AsmTypeCheck does not have access to it:

cdfdc857cb/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp (L43-L204)
And even if AsmTypeCheck has access to it, currently it treats the list
of catch clauses as a single `WebAssemblyOperand` so there is no way to
get the starting location of each `catch_***` clause in the current
structure.

This also renames `valTypeToStackType` to `valTypesToStackTypes`, given
that it takes two type lists.
2024-10-07 10:29:26 -07:00
Heejin Ahn
643df0573d
[WebAssembly] Enable type checker in annotations.s (#111079) 2024-10-04 10:32:10 -07:00
Heejin Ahn
83e3779473
[WebAssembly] Add intentations to type-checker-error.s (#111071) 2024-10-03 17:32:43 -07:00
Heejin Ahn
1753d169d5
[WebAssembly] Print instructions with type checking errors (#111067)
When there was a type checking error, we didn't run `InstPrinter`. This
can be confusing because when there is an error in, say, block parameter
type, `InstPrinter` doesn't run even if it has nothing to do with block
parameter types, and all those updates to `ControlFlowStack` or
`TryStack` do not happen:

c20b90ab85/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyInstPrinter.cpp (L135-L151)

For example,
```wast
block (i32) -> () ;; Block input parameter error
end_block         ;; Now this errors out as "End marker mismatch"
```
This is confusing because there is a `block` and the `end_block` is not
a mismatch. Only that `block` has a type checking error, but that's not
an end marker mismatch.

I think we can just print the instruction whether we had a type checking
error or not, and this will be less confusing.
2024-10-03 17:32:29 -07:00
Heejin Ahn
a268bda0da
[WebAssembly] Handle block and polymorphic stack in AsmTypeCheck (#110770)
This makes the type checker handle blocks with input parameters and
return types, branches, and polymorphic stacks correctly.

We maintain the stack of "block info", which contains its input
parameter type, return type, and whether it is a loop or not. And this
is used when checking the validity of the value stack at the `end`
marker and all branches targeting the block.

`StackType` now supports a new variant `Polymorphic`, which indicates
the stack is in the polymorphic state. `Polymorphic`s are not popped
even when `popType` is executed; they are only popped when the current
block ends.

When popping from the value stack, we ensure we don't pop more than we
are allowed to at the given block level and print appropriate error
messages instead. Also after a block ends, the value stack is guaranteed
to have the right types based on the block return type. For example,
```wast
block i32
  unreachable
end_block
;; You can expect to have an i32 on the stack here
```

This also adds handling for `br_if`. Previously only `br`s were checked.

`checkEnd` and `checkBr` were removed and their contents have been
inlined to the main `typeCheck` function, because they are called only
from a single callsite.

This also fixes two existing bugs in AsmParser, which were required to
make the tests passing. I added Github comments about them inline.

This modifies several existing invalid tests, those that passed
(incorrectly) before but do not pass with the new type checker anymore.

Fixes #107524.
2024-10-02 16:48:21 -07:00
Heejin Ahn
7b2346829f
[WebAssembly] Use 'any' type in more cases in AsmTypeCheck (#110403)
Now that we support 'any' type in the value stack in the checker, this
uses it in more places.

When an instruction pops multiple values, rather than popping in one by
one and generating multiple error messages, it adds them to a vector and
pops them at once. When the type to be popped is not clear, it pops
'any', at least makes sure there are correct number of values on the
stack. So for example, in case of `table.fill`, which expects `[i32 t
i32]` (where t is the type of the elements in the table), it pops them
at once, generating an error message like
```console
error: type mismatch, expected [i32, externref, i32] but got [...]
```
In case the table is invalid so we don't know the type, it tries to pop
an 'any' instead, popping whatever value there is:
```console
error: type mismatch, expected [i32, any, i32] but got [...]
```

Checks done on other instructions based on the register info are already
popping and pushing types in vectors, after #110094:
a52251675f/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp (L515-L536)

This also pushes 'any' in case the type to push is unclear. For example,
`local/global.set` pushes a value of the type specified in the local or
global, but in case that local or global is invalid, we push 'any'
instead, which will match with whatever type.

The objective of all these is not to make one instruction's error
propragate continuously into subsequent instructions. This also matches
Wabt's behavior.

This also renames `checkAndPopTypes` to just `popTypes`, to be
consistent with a single-element version `popType`. `popType(s)` also
does type checks.
2024-09-30 15:37:44 -07:00
Heejin Ahn
d1cd2c3a26
[WebAssembly] Unify type checking in AsmTypeCheck (#110094)
This unifies the way we check types in various places in AsmTypeCheck.
The objectives of this PR are:

- We now use `checkTypes` for all type checking and `checkAndPopTypes`
for type checking + popping. All other functions are helper functions to
call these two functions.

- We now support comparisons of types between vectors. This lets us
printing error messages in more readable way. When an instruction takes
[i32, i64] but the stack top is [f32, f64], now instead of
  ```console
  error: type mismatch, expected i64 but got f64
  error: type mismatch, expected i32 but got f32
  ```
  we can print this
  ```console
  error: type mismatch, expected [i32, i64] but got [f32, f64]
  ```
which is also the format Wabt checker prints. This also helps printing
more meaningful messages when there are superfluous values on the stack
at the end of the function, such as:
  ```console
  error: type mismatch, expected [] but got [i32, exnref]
  ```
Actually, many instructions are not utilizing this batch printing now,
which still causes multiple error messages to be printed for a single
instruction. This will be improved in a follow-up.

- The value stack now supports `Any` and `Ref`. There are instructions
that requires the type to be anything. Also instructions like
`ref.is_null` requires the type to be any reference types. Type
comparison function will handle this types accordingly, meaning
`match(I32, Any)` or `match(externref, Ref)` will succeed.

The changes in `type-checker-errors.s` are mostly the message format
changes. One downside of the new message format is that it doesn't have
instruction names in it. I plan to improve that in a potential
follow-up.

This also made some modifications in the instructions in
`type-checker-errors.s`. Currently, except for a few functions I've
recently added at the end, each function tests for a single error,
because the type checker used to bail out after the first error until
#109705. But many functions included multiple errors anyway, which I
don't think was the intention of the original writer. So I added some
instructions to remove the other errors which are not being tested. (In
some cases I added more error checking lines instead, when I felt that
could be relevant.)

Thanks to the new `ExactMatch` option in `checkTypes` function family,
we now can distinguish the cases when to check against only the top of
the value stack and when to check against the whole stack (e.g. to check
whether we have any superfluous values remaining at the end of the
function). `return` or `return_call(_indirect)` can set `ExactMatch` to
`false` because they don't care about the superfluous values. This makes
`type-checker-return.s` succeed and I was able to remove the `FIXME`.

This is the basis of the PR that fixes block parameter/return type
handling in the checker, but does not yet include the actual
block-related functionality, which will be submitted separately after
this PR.
2024-09-26 18:25:43 -07:00
Heejin Ahn
b62075e029
[WebAssembly] Allow AsmTypeCheck detect multiple errors in function (#109705)
This allows multiple errors to be reported within a function, rather
than returning on the first error and not looking at the rest of the
function.

I think the rationale for the previous behavior was that upon
encountering the first error, the value stack was not in the correct
status anymore and the rest of the function checking was not very
meaningful. But this patch makes the instruction push the correct result
type upon its completion, so the while the possibility of previous error
affecting later instructions is not zero, I think this can be more
helpful to assembly hand-writers. This also allows us to write multiple
error test cases without creating as many functions.

This is what Wabt and Binaryen wast checker/validator do as well.

Also this makes sure we return a value (true/false) within an `if` for
each instruction, removing the need for the long `if`-`else if`-`else
if` chain and making them all just `if`s. I also added newlines between
the `if`s, which I feel is easier to read.
2024-09-24 16:56:24 -07:00
Heejin Ahn
08bba6503b
[WebAssembly] Support binary generation for new EH (#109027)
This adds support for binary generation for the new EH proposal.

So far the only case that we emitted variable immediate operands in
binary has been `br_table`'s destinations. (Other `variable_ops` uses in
TableGen files are register operands, such as the operands of `call`, so
they don't get emitted in binary as a part of the same instruction.)

With this PR, variable immediate operands can include `try_table`'s
operands:
- The number of of catch clauses
- catch clauses sub-opcodes
  - `catch`: 0x00
  - `catch_ref`: 0x01
  - `catch_all`: 0x02
  - `catch_all_ref`: 0x03
- catch clauses' destinations

With `try_table`, we now have variable expr operands for `try_table`'s
catch clauses' tags. We treat their fixups in the same way we do for
tags in other instructions such as in `throw`.

Diff without whitespace will be easier to view.
2024-09-17 14:58:19 -07:00
Heejin Ahn
38333f4161
[WebAssembly] Support annotation for try_table (#109029)
This adds support for annotations (`down to labelN`) for `try_table`.
2024-09-17 14:57:57 -07:00
Heejin Ahn
defb8fb2c6
[WebAssembly] Support assembly parsing for new EH (#108668)
This adds assembly parsing support for the new EH (exnref) proposal.

`try_table` parsing is a little tricky because catch clause lists use
`()` and the multivalue block return types also use `()`. This handles
all combinations below:
- No return type (void) + no catch list
- No return type (void) + catch list
- Single return type + no catch list
- Single return type + catch list
- Multivalue return type + no catch list
- Multivalue return type + catch list

This does not include AsmTypeCheck support yet. That's the reason why
this adds a new test file and use `--no-type-check` in the command line.
After the type checker is added as a follow-up, I plan to merge
https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/WebAssembly/eh-assembly-legacy.s
with this file. (Turning on `-mattr=+exception-handling` adds support
for all legacy and new EH instructions in the assembly.
`-wasm-enable-exnref` in `llc` only controls which instructions to
generate and it doesn't affect `llvm-mc` and assembly parsing.)
2024-09-17 11:35:32 -07:00
Heejin Ahn
dbc590073d
[WebAssembly] Fix .functype directives in tests (#108748)
For defined functions, it appears `.functype` directive should be after
the function label. Otherwise binary generation does not seem to work
correctly. Also this fixes a case that the `.functype` directive's name
is incorrect.
2024-09-16 15:14:36 -07:00
Heejin Ahn
4b4dbaaede
[WebAssembly] Rename eh-assembly.s to -legacy.s (#108747)
The plan was to make `eh-assembly.s` contain both the legacy and the new
tests, but the new tests require `--no-type-check` because the type
checker for the new EH is in progress. In case this drags on further
than expected, this renames the current file to `-legacy.s` in order to
follow the current naming scheme in `test/CodeGen/WebAssembly`.

After landing this first, `eh-assembly-new.s` in #108668 will be renamed
to `eh-assembly.s`.
2024-09-16 11:29:24 -07:00
Heejin Ahn
d8ee96c500
[WebAssembly] Add indentations to annotations.s (#108790)
Given that the instructions here are all control flow instructions,
adding indentations seem to make it easier to read.
2024-09-16 11:29:09 -07:00
Heejin Ahn
20645572e4
[WebAssembly] Add more EH assembly test cases (#108654)
This adds assembly tests for a catch-less `try` and a `try` with a
multivalue return.
2024-09-13 23:20:44 -07:00
Heejin Ahn
d6d4a48e0e
[WebAssembly] Add type checking for 'throw' (#108641)
This was previously missing.
2024-09-13 18:15:10 -07:00
Heejin Ahn
acf90fd03f
[WebAssembly] Create separate file for EH assembly tests (#108472)
Create `eh-assembly.s` that contains EH tests and remove EH tests from
`basic-assembly.s`, given that it's easier to manage. (We can have many
different tests, including the legacy EH and the new exnref, and with
nesting for readability)
2024-09-13 12:20:43 -07:00
Heejin Ahn
c8ed2b8733
[WebAssembly] Add a colon to type error message (#107980) 2024-09-10 11:01:33 -07:00
Brendan Dahl
923a1c1fc3
[WebAssembly] Update FP16 opcodes to match current spec. (#106759)
f267a3d544/proposals/half-precision/Overview.md (binary-format)
2024-08-30 13:01:16 -07:00
Brendan Dahl
7d373cef49
[WebAssembly] Change half-precision feature name to fp16. (#105434)
This better aligns with how the feature is being referred to and what
runtimes (V8) are calling it.
2024-08-22 09:44:33 -07:00
Pavel Labath
0e4c7fabd3
[DebugInfo] (Always) include the dwo name in the hash (#100375)
Since ce0c205813c74b4225180ac8a6e40fd52ea88229, we are doing that if a
single (LTO) compilation contains more than one compile unit, but the
same thing can happen if the non-lto and single-cu lto compilations,
typically when the CU ends up (nearly) empty. In my case, this happened
when LTO emptied two compile units.

Note that the source file name is already a part of the hash, so this
can only happen when a single file is compiled and linked twice into the
same application (most likely with different preprocessor defintiions).
While not exactly common, this pattern is used by some C code to
implement "templates".

The 2017 patch already hinted at the possibility of doing this
unconditionally, and this patch implements that. While the DWARF spec
hints at the option of using the type signature hashing algorithm for
the DWO_id purposes, AFAICT it does not actually require it, so I
believe this change is still conforming.

The relevant section of the spec is in Section 3.1.2 "Skeleton
Compilation Unit Entries" (in non-normative text):

```
The means of determining a compilation unit ID does not need to be
similar or related to the means of determining a type unit signature.
However, it should be suitable for detecting file version skew or other
kinds of mismatched files and for looking up a full split unit in a
DWARF package file (see Section 7.3.5 on page 190).
```
2024-08-05 16:38:08 +02:00
Brendan Dahl
0dbd72d6ab
[WebAssembly] Implement f16x8.replace_lane instruction. (#99388)
Use a builtin and intrinsic until half types are better supported for
instruction selection.
2024-07-24 11:55:36 -07:00
Heejin Ahn
fb6e024f49
[WebAssembly] Update generic and bleeding-edge CPUs (#96584)
This updates the list of features in 'generic' and 'bleeding-edge' CPUs
in the backend to match

4e0a0eae58/clang/lib/Basic/Targets/WebAssembly.cpp (L150-L178)

This updates existing CodeGen tests in a way that, if a test has
separate RUN lines for a reference-types test and a non-reference-types
test, I added -mattr=-reference-types to the no-reftype test's RUN
command line. I didn't delete existing -mattr=+reference-types lines in
reftype tests because having it helps readability.

Also, when tests is not really about reference-types but they have to
updated because they happen to contain call_indirect lines because now
call_indirect will take __indirect_function_table as an argument, I just
added the table argument to the expected output.

`target-features-cpus.ll` has been updated reflecting the newly added
features.
2024-07-01 19:12:01 -07:00
Brendan Dahl
928b780840
[WebAssembly] Implement trunc_sat and convert instructions for f16x8. (#95180)
These instructions can be generated using regular LL intrinsics.

Specified at:

29a9b9462c/proposals/half-precision/Overview.md
2024-06-25 10:39:05 -07:00
Brendan Dahl
3ab6d12625
[WebAssembly] Implement f16x8 madd and nmadd instructions. (#95151)
Implemented with intrinsics and builtins.

Specified at:

https://github.com/WebAssembly/half-precision/blob/main/proposals/half-precision/Overview.md
2024-06-11 16:10:00 -07:00
Sam Clegg
554a2fa4b2
[WebAssembly] Fix element segments in wasm64 object files (#94617)
Followup to #94487
2024-06-06 07:45:38 -07:00
Sam Clegg
c2244f8284
[WebAssembly] Set IS_64 flag correctly on __indirect_function_table in object files (#94487)
Follow up to #92042
2024-06-05 20:28:51 -07:00
Brendan Dahl
dfd1a2f081
[WebAssembly] Implement all f16x8 unary instructions. (#94063)
All of these instructions can be generated using regular LL intrinsics.

Specified at:

29a9b9462c/proposals/half-precision/Overview.md
2024-06-04 13:06:16 -04:00
Brendan Dahl
8aa8019975
[WebAssembly] Implement all f16x8 relation instructions. (#93751)
All of these instructions can be generated using regular LL
instructions.

Specified at:

29a9b9462c/proposals/half-precision/Overview.md
2024-05-30 09:02:17 -07:00
Brendan Dahl
60bce6eab4
[WebAssembly] Implement all f16x8 binary instructions. (#93360)
This reuses most of the code that was created for f32x4 and f64x2 binary
instructions and tries to follow how they were implemented.

add/sub/mul/div - use regular LL instructions
min/max - use the minimum/maximum intrinsic, and also have builtins
pmin/pmax - use the wasm.pmax/pmin intrinsics and also have builtins

Specified at:

29a9b9462c/proposals/half-precision/Overview.md
2024-05-28 16:33:20 -07:00
Heejin Ahn
c179d50fd3
[WebAssembly] Add exnref type (#93586)
This adds (back) the exnref type restored in the new EH proposal adopted
in Oct 2023 CG meeting:

https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md:x
2024-05-28 16:10:11 -07:00
Brendan Dahl
4ebe9bba59
[WebAssembly] Implement prototype f16x8.extract_lane instruction. (#93272)
Specified at:

29a9b9462c/proposals/half-precision/Overview.md

Note: the current spec has f16x8.extract_lane as opcode 0x124, but this
is incorrect and will be changed to 0x121 soon.
2024-05-24 08:31:07 -07:00
Brendan Dahl
09c5525610
[WebAssembly] Implement prototype f16x8.splat instruction. (#93228)
Adds a builtin and intrinsic for the f16x8.splat instruction.

Specified at:

29a9b9462c/proposals/half-precision/Overview.md

Note: the current spec has f16x8.splat as opcode 0x123, but this is
incorrect and will be changed to 0x120 soon.
2024-05-23 20:05:22 -07:00
Brendan Dahl
8a3277acbc
[WebAssembly] Implement prototype f32.store_f16 instruction. (#91545)
Adds a builtin and intrinsic for the f32.store_f16 instruction.

The instruction stores an f32 value as an f16 memory. Specified at:

29a9b9462c/proposals/half-precision/Overview.md

Note: the current spec has f32.store_f16 as opcode 0xFD0121, but this is
incorrect and will be changed to 0xFC31 soon.
2024-05-09 15:38:13 -07:00
Brendan Dahl
1a2a1fbd7c
[WebAssembly] Implement prototype f32.load_f16 instruction. (#90906)
Adds a builtin and intrinsic for the f32.load_f16 instruction.

The instruction loads an f16 value from memory and puts it in an f32.
Specified at:

29a9b9462c/proposals/half-precision/Overview.md

Note: the current spec has f32.load_f16 as opcode 0xFD0120, but this is
incorrect and will be changed to 0xFC30 soon.
2024-05-07 11:33:10 -07:00
Congcong Cai
1a46229636
Revert "Revert "[WebAssembly] remove instruction after builtin trap" (#90354)" (#90366)
`llvm.trap` will be convert as unreachable which is terminator.
Instruction after terminator will cause validation failed.
This PR introduces a pass to clean instruction after terminator.
Fixes: https://github.com/llvm/llvm-project/issues/68770
Reapply: #90207
2024-04-28 10:13:02 +08:00
Mehdi Amini
38a2051c52
Revert "[WebAssembly] remove instruction after builtin trap" (#90354)
Reverts llvm/llvm-project#90207

LLD Bots are broken.
2024-04-27 21:14:46 +02:00
Congcong Cai
ff03f23be8
[WebAssembly] remove instruction after builtin trap (#90207)
`llvm.trap` will be convert as `unreachable` which is terminator.
Instruction after terminator will cause validation failed.
This PR introduces a pass to clean instruction after terminator.
Fixes: #68770.
2024-04-27 22:11:47 +08:00
Tim Neumann
f792f14b01
[WebAssembly] Allocate MCSymbolWasm data on MCContext (#85866)
Fixes #85578, a use-after-free caused by some `MCSymbolWasm` data being
freed too early.

Previously, `WebAssemblyAsmParser` owned the data that is moved to
`MCContext` by this PR, which caused problems when handling module ASM,
because the ASM parser was destroyed after parsing the module ASM, but
the symbols persisted.

The added test passes locally with an LLVM build with AddressSanitizer
enabled.

Implementation notes:

* I've called the added method
<code>allocate<b><i>Generic</i></b>String</code> and added the second
paragraph of its documentation to maybe guide people a bit on when to
use this method (based on my (limited) understanding of the `MCContext`
class). We could also just call it `allocateString` and remove that
second paragraph.
* The added `createWasmSignature` method does not support taking the
return and parameter types as arguments: Specifying them afterwards is
barely any longer and prevents them from being accidentally specified in
the wrong order.
* This removes a _"TODO: Do the uniquing of Signatures here instead of
ObjectFileWriter?"_ since the field it's attached to is also removed.
Let me know if you think that TODO should be preserved somewhere.
2024-04-02 10:59:29 -07:00
Yuta Saito
ba3c1f9ce3
[WebAssembly] Add segment RETAIN flag to support private retained data (#81539)
In WebAssembly, we have `WASM_SYMBOL_NO_STRIP` symbol flag to mark the
referenced content as retained. However, the flag is not enough to
express retained data that is not referenced by any symbol. This patch
adds a new segment flag`WASM_SEG_FLAG_RETAIN` to support "private"
linkage data that is retained by llvm.used.

This kind of data that is not referenced but must be retained is usually
used with encapsulation symbols (__start/__stop). Swift runtime uses
this technique and depends on the fact "all metadata sections in live
objects are retained", which was not guaranteed with `--gc-sections`
before this patch.

This is a revised version of https://reviews.llvm.org/D126950 (has been
reverted) based on @MaskRay's comments
2024-02-21 03:35:36 +09:00