Commit graph

601 commits

Author SHA1 Message Date
ayazhafiz
47a59c560c Make test use an i64 2022-01-29 15:04:22 -05:00
ayazhafiz
5943873654 Inline polymorphic calls at usage sites
This is a bit.. ugly, or at least seems suboptimal, but I can't think of
a better way to do it currently aside from demanding a uniform
representation, which we probably don't want to do.

Another option is something like the defunctionalization we perform
today, except also capturing potential uses of nested functions in the
closure tag of an encompassing lambda. So for example,

```
f = \x -> \y -> 1
```

would now record a lambdaset with the data `[Test.f
[TypeOfInnerClos1]]`, where `TypeOfInnerClos1` is e.g.
`[Test.f.innerClos1 I8, Test.f.innerClos1 I16]`, symbolizing that the
inner closure may be specialized to take an I8 or I16. Then at the time
that we create the capture set for `f`, we create a tag noting what
specialization should be used for the inner closure, and apply the
current defunctionalization algorithm. So effectively, the type of the
inner closure becomes a capture.

I'm not sure if this is any better, or if it has more problems.
@folkertdev any thoughts?

Closes #2322
2022-01-28 23:49:19 -05:00
Folkert
afd11e1cb1 move target -> roc_target 2022-01-26 23:33:29 +01:00
Folkert
b9c318e9fb update the tests 2022-01-26 15:59:21 +01:00
Folkert
74932a4cab phase 2 2022-01-26 14:30:37 +01:00
Folkert
9ee7198e45 and another 2022-01-25 15:23:53 +01:00
Folkert
3137e14a85 another test fix 2022-01-25 13:37:30 +01:00
Folkert
b72ad31a89 fix more tests 2022-01-25 12:05:13 +01:00
ayazhafiz
3692b38447 Disable wasm tests for now 2022-01-23 12:33:22 -05:00
ayazhafiz
b2f2fcd6a8 Collect tags from extension variables during monomorphization
Fixes #2365
2022-01-23 12:33:22 -05:00
Jan Van Bruggen
9a8a4c6ed7 Add Num.(min/max)(I/U)(8/16) builtins 2022-01-22 00:19:17 -07:00
Eric Newbury
7919cd9fee add test 2022-01-21 17:08:43 -05:00
Eric Newbury
05c01a81f5 adding List.sortAsc builtin 2022-01-21 15:34:24 -05:00
Folkert de Vries
c0ea664141
Merge pull request #2375 from rtfeldman/expression-monomorphization-inlined
Monomorphize polymorphic non-function values
2022-01-21 20:10:42 +01:00
ayazhafiz
25db1189af Monomorphize expressions in refcount test 2022-01-20 10:23:13 -05:00
ayazhafiz
8fd139ba67 Handle unspecialized symbols captured in closures 2022-01-20 00:34:11 -05:00
Richard Feldman
6d8277ad8d
Merge pull request #2367 from rtfeldman/remove_Num.minmaxInt
Remove `Num.(min/max)Int`
2022-01-20 00:19:44 -05:00
ayazhafiz
649695f6e3 Fix wasm refcount tests 2022-01-19 23:21:29 -05:00
ayazhafiz
a5de224626 Specialize polymorphic non-function expressions
This commit fixes a long-standing bug wherein bindings to polymorphic,
non-function expressions would be lowered at binding site, rather than
being specialized at the call site.

Concretely, consider the program

```
main =
    n = 1

    idU8 : U8 -> U8
    idU8 = \m -> m

    idU8 n
```

Prior to this commit, we would lower `n = 1` as part of the IR, and the
`n` at the call site `idU8 n` would reference the lowered definition.
However, at the definition site, `1` has the polymorphic type `Num *` -
it is not until the the call site that we are able to refine the type
bound by `n`, but at that point it's too late. Since the default layout
for `Num *` is a signed 64-bit int, we would generate IR like

```
procedure main():
    let App.n : Builtin(Int(I64)) = 1i64;
    ...
    let App.5 : Builtin(Int(U8)) = CallByName Add.idU8 App.n;
    ret App.5;
```

But we know `idU8` expects a `u8`; giving it an `i64` is nonsense.
Indeed this would trigger LLVM miscompilations later on.

To remedy this, we now keep a sidecar table that maps symbols to the
polymorphic expression they reference, when they do so. We then
specialize references to symbols on the fly at usage sites, similar to
how we specialize function usages.

Looking at our example, the definition `n = 1` is now never lowered to
the IR directly. We only generate code for `1` at each place `n` is
referenced. As a larger example, you can imagine that

```
main =
    n = 1

    asU8 : U8 -> U8
    asU32 : U32 -> U8

    asU8 n + asU32 n
```

is lowered to the moral equivalent of

```
main =
    asU8 : U8 -> U8
    asU32 : U32 -> U8

    asU8 1 + asU32 1
```

Moreover, transient usages of polymorphic expressions are lowered
successfully with this approach. See for example the
`monomorphized_tag_with_polymorphic_arg_and_monomorphic_arg` test in
this commit, which checks that

```
main =
    mono : U8
    mono = 15
    poly = A
    wrap = Wrapped poly mono

    useWrap1 : [Wrapped [A] U8, Other] -> U8
    useWrap1 =
        \w -> when w is
            Wrapped A n -> n
            Other -> 0

    useWrap2 : [Wrapped [A, B] U8] -> U8
    useWrap2 =
        \w -> when w is
            Wrapped A n -> n
            Wrapped B _ -> 0

    useWrap1 wrap * useWrap2 wrap
```

has proper code generated for it, in the presence of the polymorphic
`wrap` which references the polymorphic `poly`.

https://github.com/rtfeldman/roc/pull/2347 had a different approach to
this - polymorphic expressions would be converted to (possibly capturing) thunks.
This has the benefit of reducing code size if there are many polymorphic
usages, but may make the generated code slower and makes integration
with the existing IR implementation harder. In practice I think the
average number of polymorphic usages of an expression will be very
small.

Closes https://github.com/rtfeldman/roc/issues/2336
Closes https://github.com/rtfeldman/roc/issues/2254
Closes https://github.com/rtfeldman/roc/issues/2344
2022-01-19 22:52:15 -05:00
Jan Van Bruggen
8e4b6f0cab Remove Num.(min/max)Int
Replace all uses with `Num.(min/max)I64`,
since all uses expect an `I64`.
2022-01-19 09:55:06 -07:00
Jan Van Bruggen
1e9d2d1239 Remove accidental trailing spaces 2022-01-18 22:25:46 -07:00
Jan Van Bruggen
591477e77b Add most remaining Num.min/max* builtins
This skips `min/maxU128`, as they require a subtle change
to the `I128`-centric implementation of `Int`s.
2022-01-17 15:26:23 -07:00
Richard Feldman
2ade76b373
Merge pull request #2354 from rtfeldman/add_builtin_Num.minI128
Add `Num.minI128` builtin
2022-01-16 22:38:27 -05:00
Jan Van Bruggen
d7e2be306f WIP: Add Num.minI128 builtin (TODOs remain) 2022-01-15 17:49:15 -07:00
Brian Carroll
f4650654ca Wasm: cosmetic changes to fake WASI functions 2022-01-15 15:35:42 +00:00
Brian Carroll
c7da7ca689 Wasm: Parse the Name section, export init_refcount_test, and don't DCE exports 2022-01-14 18:20:52 +00:00
Brian Carroll
3d00217b53 Wasm: rename build_module_help -> build_module_without_test_wrapper 2022-01-14 18:20:52 +00:00
Brian Carroll
ca2597973e Wasm: store function_count on the ImportSection 2022-01-14 18:20:52 +00:00
Brian Carroll
7f5b8c7296 Merge branch 'trunk' of github.com:rtfeldman/roc into wasm-pre-linking 2022-01-14 18:20:24 +00:00
Folkert de Vries
e1d990896a
Merge branch 'trunk' into mono-display 2022-01-14 13:55:52 +01:00
ayazhafiz
9f78eb2e01 Fix bug that caused extraneous assignment in IR generation
Previously we would expand optional record fields to assignments when
converting record patterns to "when" expressions. This resulted in
incorrect code being generated.
2022-01-13 18:34:25 -05:00
ayazhafiz
72ee2d6327 Mark pretty_print_ir_symbols public 2022-01-13 16:35:09 -05:00
hafiz
5f216122d6 Update dev.rs 2022-01-13 16:33:26 -05:00
Brian Carroll
535927a85e Wasm: fix test_wrapper function index 2022-01-13 05:41:52 +00:00
Brian Carroll
b46124234f Wasm: Add mock functions to HTML debugger for the full set of WASI imports 2022-01-13 05:41:52 +00:00
Brian Carroll
4f15fb3967 Wasm: Adjust funciton index for test_wrapper export declaration 2022-01-13 05:41:52 +00:00
Brian Carroll
9f0e0d5099 Wasm: create a hashmap of exported functions 2022-01-13 05:41:52 +00:00
Brian Carroll
9c0abcd0da Wasm: Preload WasmModule from object file bytes 2022-01-13 05:41:52 +00:00
Brian Carroll
fd79613f0d Wasm: load platform object file in tests and pass the bytes to the backend 2022-01-13 05:41:52 +00:00
Brian Carroll
5d5e0eca96 Wasm: Convert remaining sections to store bytes, and add .size() methods 2022-01-13 05:41:52 +00:00
Brian Carroll
fb5ac05155 Wasm: Optional debug code in test_gen build script 2022-01-13 05:41:52 +00:00
Brian Carroll
56316da870 Wasm: skip linking step in tests 2022-01-13 05:41:52 +00:00
Brian Carroll
5a39002e8b Wasm: Serialize WasmModule without linking info 2022-01-13 05:41:52 +00:00
Brian Carroll
d88b86e884 Wasm: change TypeSection tests to unit tests rather than integration tests 2022-01-13 05:41:52 +00:00
Brian Carroll
5de9581b62 Wasm: pre-link app-independent code before running tests (builtins, plaform & libc) 2022-01-13 05:41:52 +00:00
Richard Feldman
d0e3c45986
Merge pull request #2328 from rtfeldman/wasm-module-refactor
Wasm module refactor
2022-01-12 11:28:12 -05:00
Richard Feldman
f83f50a9a7
Merge pull request #2337 from rtfeldman/i/2331
Use unsigned LLVM intrinsic arithmetic for unsigned integers
2022-01-12 08:29:30 -05:00
ayazhafiz
fefbb5312e Yet more quicksort bugs 2022-01-11 18:17:57 -05:00
Brian Carroll
92ec680a25 Fix clippy error on non-wasm test builds 2022-01-11 21:54:03 +00:00
Brian Carroll
54f2855349 Merge branch 'trunk' of github.com:rtfeldman/roc into wasm-module-refactor 2022-01-11 21:51:03 +00:00