Skip to content

Prevent analysis of functions only referenced at comptime - #15891

Merged
andrewrk merged 2 commits into
ziglang:masterfrom
mlugg:fix/dont-emit-fn-called-at-comptime
May 30, 2023
Merged

andrewrk merged 2 commits into
ziglang:masterfrom
mlugg:fix/dont-emit-fn-called-at-comptime

Conversation

@mlugg

@mlugg mlugg commented May 29, 2023

Copy link
Copy Markdown
Member

The idea here is that there are two ways we can reference a function at runtime:

  • Through a direct call, i.e. where the function is comptime-known
  • Through a function pointer

This means we can easily perform a form of rudimentary escape analysis on functions. If we ever see a decl_ref or ref of a function, we have a function pointer, which could "leak" into runtime code, so we emit the function; but for a plain decl_val, there's no need to.

This change means that comptime { _ = f; } no longer forces a function to be emitted, which was used for some things (mainly tests). These use sites have been replaced with _ = &f;, which still triggers analysis of the function body, since you're taking a pointer to the function.

Resolves: #6256
Resolves: #15353

@mlugg
mlugg force-pushed the fix/dont-emit-fn-called-at-comptime branch 3 times, most recently from 93a8cb5 to 1df228c Compare May 29, 2023 11:01
@mlugg

mlugg commented May 29, 2023 •

Copy link
Copy Markdown
Member Author

Due to some issues I hit with incremental compilation, currently, outdated function decls will always be codegenned, regardless of whether they're actually referenced at runtime. This is a bug, and I plan to address it, but I will do this in a follow-up PR; I don't believe this should block the merge since incremental compilation is not currently practically used.

Note the second commit in this PR, which disables incremental test cases for now. After some discussion, Andrew and I agreed this is a better course of action than trying to patch a questionable fix into the incremental compilation logic; better to allow for immediate progress on the compiler and come back to these tests once incremental compilation is more mature.

@mlugg
mlugg force-pushed the fix/dont-emit-fn-called-at-comptime branch from 1df228c to d0e0bac Compare May 29, 2023 19:32
@andrewrk
andrewrk enabled auto-merge May 29, 2023 20:40
mlugg added 2 commits May 29, 2023 23:06
The idea here is that there are two ways we can reference a function at runtime:

* Through a direct call, i.e. where the function is comptime-known
* Through a function pointer

This means we can easily perform a form of rudimentary escape analysis
on functions. If we ever see a `decl_ref` or `ref` of a function, we
have a function pointer, which could "leak" into runtime code, so we
emit the function; but for a plain `decl_val`, there's no need to.

This change means that `comptime { _ = f; }` no longer forces a function
to be emitted, which was used for some things (mainly tests). These use
sites have been replaced with `_ = &f;`, which still triggers analysis
of the function body, since you're taking a pointer to the function.

Resolves: ziglang#6256
Resolves: ziglang#15353
These were failing on the prior commit. Unfortunately, the fix would
have been relatively complicated, and ties into underlying issues with
the current incremental compilation logic. After discussing this with
Andrew, we agreed that the best course of action is to completely
disable incremental compilation tests for now until it's more mature, at
which point we can re-enable them.
auto-merge was automatically disabled May 29, 2023 22:06

Head branch was pushed to by a user without write access

@mlugg
mlugg force-pushed the fix/dont-emit-fn-called-at-comptime branch from d0e0bac to 0f58d34 Compare May 29, 2023 22:06
@andrewrk
andrewrk enabled auto-merge May 29, 2023 22:16
@mlugg

mlugg commented May 30, 2023

Copy link
Copy Markdown
Member Author

(aarch64-windows was a random failure and just needs a re-run)

@mlugg

mlugg commented May 30, 2023 •

Copy link
Copy Markdown
Member Author
run test std-native-Debug-libc: error: 'test.manage resources correctly' failed: Unable to dump stack trace: InvalidBlockIndex

oh what the heck it happened again

@andrewrk
andrewrk disabled auto-merge May 30, 2023 06:41
@andrewrk
andrewrk merged commit 22b0457 into ziglang:master May 30, 2023
@mlugg
mlugg deleted the fix/dont-emit-fn-called-at-comptime branch May 18, 2025 20:08
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.

too much junk is outputted into object file Function only called at comptime is emitted to binary

2 participants