Skip to content

fix: record extern LLVM fnptr in constants - #569

Open
jimbxb wants to merge 11 commits into
pschachte:masterfrom
jimbxb:feat/llvm-extern-refs
Open

jimbxb wants to merge 11 commits into
pschachte:masterfrom
jimbxb:feat/llvm-extern-refs

Conversation

@jimbxb

@jimbxb jimbxb commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

The test case exits with this on the a previous master build

Error detected during translating: 
llc: error: llc: /tmp/wybe-40074fe12aabd129/tmpMain.ll:8:78: error: use of undefined value '@unary_higher_order#.#anon#1<1>'
@"unary_higher_order#constant#0" = private unnamed_addr constant {ptr} { ptr @"unary_higher_order#.#anon#1<1>" }, align 8

I have also made the "env" param of closure procs explicit. This enabled further lowering of HO->FO calls, in some situations.

@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch 3 times, most recently from 77887be to 3bbeecb Compare June 7, 2026 14:20

@pschachte pschachte left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change, and a question.

Comment thread src/AST.hs Outdated
-- ^An argument to pass a resource
| Free -- ^An argument to be passed in the closure
-- environment
| ClosureEnv

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

proc #anon#1 > {inline} (1 calls)
1: box_unbox_ho.#anon#1<1>
#anon#1(anon#1#1##0 <{}; {}; {0}>, ?anon#1#2##1)<{<<wybe.io.io>>}; {<<wybe.io.io>>}; {}>:
#anon#1(@#env##0:opaque, anon#1#1##0 <{}; {}; {1}>, ?anon#1#2##1)<{<<wybe.io.io>>}; {<<wybe.io.io>>}; {}>:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there an extra environment argument in this case?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand: you're making this explicit so you can optimise it. Is that the idea?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It makes it mildly easier to align the arguments when optimising LPVM.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I want to do a bit more work on this.

I want to add explicit instructions to un-pack the closed values from the env as well. At the moment, there is still the assymetry of the env being present (and passed), but the free variables are not.

In order to do that, I need a special lpvm instruction. I'm calling it unmarshall for now. All it will do is take is the env param and the "index" of the free param of the closed variable in the non-closure variant of the proc, and return the closed value itself.

I can't use an existing instruction is because the unmarshall instructions are special. They won't have a fixed offset to read from, as some free params may be unused in the non-closure proc.

unmarshall could only be allowed in the pre-amble of a closure-proc when it comes to LLVM generation. We could also only inline a closure proc if the env is known, too, which I believe would always be the case (we only get to the point we directly call the closure proc if we inline a HO call to a FO call!)

The procs would look something like this:

def closure(#env, in, ?out) {
    foreign lpvm unmarshall(#env, 0, ?closed)
    non_closure(closed, in, ?out)
}

def non_closure(closed, in, ?out) {
    # body
}

@jimbxb jimbxb Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I dont know if this is possible. We still need some of the information contained inside those "Free" params. We do still get that if the proc is a closure of some anonymous proc, but not when it is a closure like `+`(10) :(

I think I'll need to have both the explicit env and free params. I do still need to figure out how to factor in the unmarshall bit though. Let's see

@jimbxb jimbxb Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be right now :) No need for unmarshall

Comment thread test-cases/final-dump/unary_higher_order.exp Outdated
()<{<<wybe.io.io>>}; {<<wybe.io.io>>}; {}>:
AliasPairs: []
InterestingCallProperties: []
unary_higher_order.#anon#1<0>(?tmp#0##0:wybe.int) #0 @unary_higher_order:nn:nn

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, but I don't understand why unary_higher_order.#anon#1<0> didn't get inlined here, when it's marked for inlining?

@jimbxb jimbxb Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because we only do one expansion/inlining pass. The call to fetch_number is expanded, and that lowers the HO call to a FO call, but we dont get the chance to inline the FO call

@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch 5 times, most recently from 6f3beb8 to a15bed8 Compare June 18, 2026 11:48
@jimbxb
jimbxb requested a review from pschachte June 25, 2026 07:48
@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch from a15bed8 to 0d55215 Compare July 15, 2026 11:21
@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch 2 times, most recently from 9cc9093 to 35ceac9 Compare July 24, 2026 12:31
@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch from 35ceac9 to 159ee4e Compare September 15, 2026 22:28
@jimbxb

jimbxb commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator Author

More merge conflicts 😭

@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch from 159ee4e to 1940309 Compare September 16, 2026 10:43
@jimbxb
jimbxb force-pushed the feat/llvm-extern-refs branch from 1940309 to 658cb71 Compare October 4, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants