Commit graph

1688 commits

Author SHA1 Message Date
Richard Feldman
655373dbe7
Merge remote-tracking branch 'origin/trunk' into gui-example 2022-02-23 21:25:01 -05:00
Brian Carroll
b327418a6a Merge branch 'trunk' of github.com:rtfeldman/roc into wasm-specializations 2022-02-22 23:48:30 +00:00
Brian Carroll
ce6d5b1727 mono: Fix recursive union arg layout for calls to helper functions 2022-02-22 23:42:48 +00:00
hafiz
ca9ecbea52
Merge pull request #2533 from rtfeldman/abilities!
[WIP] Spike for abilities and friends
2022-02-21 23:18:40 -05:00
ayazhafiz
90de82e295 Validation of opaques during canonicalization 2022-02-21 18:25:19 -05:00
ayazhafiz
395e0a33e0 Make sure unit structs are always units 2022-02-21 14:10:46 -05:00
ayazhafiz
dfc384aa1f Make mono test output prettier 2022-02-21 14:10:45 -05:00
ayazhafiz
e52d427ac8 Hash record field name order in generated layouts
Closes #2535

See the referenced issue for longer discussion - here's the synopsis.
Consider this program

```
app "test" provides [ nums ] to "./platform"

alpha = { a: 1, b: 2 }

nums : List U8
nums =
    [
        alpha.a,
        alpha.b,
    ]
```

Here's its IR:

```
procedure : `#UserApp.alpha` {I64, U8}
procedure = `#UserApp.alpha` ():
    let `#UserApp.5` : Builtin(Int(I64)) = 1i64;
    let `#UserApp.6` : Builtin(Int(U8)) = 2i64;
    let `#UserApp.4` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = Struct {`#UserApp.5`, `#UserApp.6`};
    ret `#UserApp.4`;

procedure : `#UserApp.nums` List U8
procedure = `#UserApp.nums` ():
    let `#UserApp.7` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = CallByName `#UserApp.alpha`;
    let `#UserApp.1` : Builtin(Int(U8)) = StructAtIndex 1 `#UserApp.7`;
    let `#UserApp.3` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = CallByName `#UserApp.alpha`;
    let `#UserApp.2` : Builtin(Int(U8)) = StructAtIndex 1 `#UserApp.3`;
    let `#UserApp.0` : Builtin(List(Builtin(Int(U8)))) = Array [`#UserApp.1`, `#UserApp.2`];
    ret `#UserApp.0`;
```

What's happening is that we need to specialize `alpha` twice - once for the
type of a narrowed to a U8, another time for the type of b narrowed to a U8.

We do the specialization for alpha.b first - record fields are sorted by
layout, so we generate a record of type {i64, u8}. But then we go to
specialize alpha.a, but this has the same layout - {i64, u8} - so we reuse
the existing one! So (at least for records), we need to include record field
order associated with the sorted layout fields, so that we don't reuse
monomorphizations like this incorrectly!
2022-02-21 14:10:45 -05:00
Brendan Hansknecht
74daec84df
Merge pull request #2472 from rtfeldman/add_Int.toInt_builtins
Add `{Int *}.to{Int *}` builtins
2022-02-21 18:03:14 +00:00
ayazhafiz
6b53692aac Canonicalize opaque types 2022-02-20 13:47:01 -05:00
ayazhafiz
13067f2908 Implement Num.toNNNChecked
Closes #2411
2022-02-19 16:20:21 -05:00
Folkert
637360fad5
clippy 2022-02-19 16:50:42 +01:00
ayazhafiz
886e4e07f6 Update alias analysis, implementation for List.mapWithIndex 2022-02-13 22:50:27 -05:00
Brian Carroll
c61a18a200 Use custom macros for static assertions 2022-02-09 17:16:43 +00:00
Brian Carroll
d8b76b317b repl: replace [u8; 8] with u64 in static assertions 2022-02-09 16:34:11 +00:00
Brian Carroll
8e370a32b6 repl: update some static_asserts for 32-bit compiler build 2022-02-09 16:34:11 +00:00
ayazhafiz
40196185a0 Specialize zero-argument thunks with the correct variable
Previously we would pass the annotation down as the
type-to-be-monomorphized for, but that would just mean the annotation
would unify with itself. We instead want to use the variable the thunk
is being used as to be the one unified with the thunk's annotation.

Closes #2445
Closes #2446
2022-02-06 20:58:13 -05:00
ayazhafiz
680bf8e0b7 Treat rigids as flex vars when checking number range types 2022-02-06 15:04:12 -05:00
ayazhafiz
8dc92ccd97 Second pass 2022-02-06 15:04:12 -05:00
Richard Feldman
c59aa22402
Reproduce mono::ir panic 2022-02-06 07:38:32 -05:00
ayazhafiz
097c5afc73 Check lower bounds for numeric literals, and permit 128-bit literals 2022-02-02 23:35:57 -05:00
ayazhafiz
df8113ce32 Typecheck numeric suffixes in patterns 2022-02-01 23:35:14 -05:00
ayazhafiz
320827167f Parse number literal width suffixes
Supports [u,i][8,16,32,64,128] and [nat,dec]

Part of #2350
2022-02-01 22:48:48 -05:00
hafiz
2d8bdfd863
Merge pull request #2417 from rtfeldman/i/2322
Inline polymorphic calls at usage sites
2022-01-29 16:13:43 -06: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
Brian Carroll
382ae948e1 Merge branch 'trunk' of github.com:rtfeldman/roc into repl_from_memory_trait 2022-01-28 12:49:33 +00:00
Brian Carroll
4a08fead1a repl: Change lots of pointers to addresses 2022-01-27 09:40:18 +00:00
Folkert
afd11e1cb1 move target -> roc_target 2022-01-26 23:33:29 +01:00
Folkert
fbd26c598e provide target info to number alignment function 2022-01-26 17:19:53 +01:00
Folkert
0ed259a80d phase 3 2022-01-26 14:37:32 +01:00
Folkert
74932a4cab phase 2 2022-01-26 14:30:37 +01:00
Folkert
7e90812332 ptr_bytes -> target info, step 1 2022-01-26 14:28:26 +01:00
Folkert
97d14f421d Merge remote-tracking branch 'origin/trunk' into fix-lambda-set-debug-instance 2022-01-25 09:25:20 +01:00
Folkert de Vries
6dd769aa38
Merge pull request #2393 from rtfeldman/i/2343
Generate unique symbols for shadowing identifiers
2022-01-24 11:49:51 +01:00
ayazhafiz
e7b506646b Refinements from @folkertdev review 2022-01-23 19:00:32 -05:00
ayazhafiz
0eede1cd86 Generate unique symbols for shadowing identifiers
This code has a shadowing error:

```
b = False
f = \b -> b
f b
```

but prior to this commit, the compiler would hit an internal error
during monomorphization and not even get to report the error. The reason
was that when we entered the closure `\b -> b`, we would try to
introduce the identifier `b` to the scope, see that it shadows an
existing identifier, and not insert the identifier. But this meant that
when checking the body of `\b -> b`, we would think that we captured the
value `b` in the outer scope, but that's incorrect!

The present patch fixes the issue by generating new symbols for
shadowing identifiers, so deeper scopes pick up the correct reference.
This also means in the future we may be able to compile and execute code
with shadows, even though it will still be an error.

Closes #2343
2022-01-23 12:35:31 -05:00
ayazhafiz
b2f2fcd6a8 Collect tags from extension variables during monomorphization
Fixes #2365
2022-01-23 12:33:22 -05:00
Folkert
fbf3ba77e9 fix debug impl 2022-01-23 15:44:42 +01: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
53be0e2674 Reduce visibility of partial exprs 2022-01-20 17:56:43 -05:00
Folkert de Vries
82c7e8c37e
Merge pull request #2372 from rtfeldman/effect-forever
Add `Effect.forever`
2022-01-20 09:16:32 +01:00
ayazhafiz
e7e2fa063f Specialize polymorphic values in record updates 2022-01-20 01:23:31 -05:00
ayazhafiz
8fd139ba67 Handle unspecialized symbols captured in closures 2022-01-20 00:34:11 -05:00
ayazhafiz
ae104c3791 Elide unnecessary lifetimes 2022-01-19 23:16:48 -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
Folkert
72e883a20b make symbol debug formatting consistent 2022-01-19 23:21:18 +01:00
Folkert
fbab19a937 custom debug instance for LambdaSet
so that symbols are printed as their numbers; makes mono tests reliable
2022-01-19 20:57:36 +01:00
Folkert
859bdf5212 Merge remote-tracking branch 'origin/trunk' into effect-forever 2022-01-19 20:14:24 +01:00
Folkert
4c445f9f24 recognize functions that become tail-recursive after closure conversion 2022-01-19 20:09:20 +01:00
Jan Van Bruggen
1e9d2d1239 Remove accidental trailing spaces 2022-01-18 22:25:46 -07:00