Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: lazy OSX frameworks with lzld #23716

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ rustflags = [
]

[target.aarch64-apple-darwin]
linker = "tools/lzld/lzld"
rustflags = [
"-C",
"link-args=-fuse-ld=lld -weak_framework Metal -weak_framework MetalPerformanceShaders -weak_framework QuartzCore -weak_framework CoreGraphics",
"linker-flavor=ld64.lld",
]

[target.'cfg(all())']
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/ci.generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,16 @@ const ci = {
run:
"deno run --allow-write --allow-read --allow-run=git ./tests/node_compat/runner/setup.ts --check",
},
// For remote debugging only.
// {
// name: "Setup tmate session",
// if: [
// "(matrix.job == 'test' || matrix.job == 'bench') &&",
// "matrix.profile == 'debug' && (matrix.use_sysroot ||",
// "github.repository == 'denoland/deno')",
// ].join("\n"),
// uses: "mxschmitt/action-tmate@v3",
// },
{
name: "Build debug",
if: "matrix.job == 'test' && matrix.profile == 'debug'",
Expand Down
3 changes: 3 additions & 0 deletions tools/lzld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target

*.o
12 changes: 12 additions & 0 deletions tools/lzld/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARCH = $(shell uname -m)

liblzld_${ARCH}.a: lzld.m
cc -c lzld.m -o lzld.o
ar rcs liblzld_${ARCH}.a lzld.o

clean:
rm -f liblzld_${ARCH}.a lzld.o

all: liblzld_${ARCH}.a

.PHONY: clean all
36 changes: 36 additions & 0 deletions tools/lzld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
A `ld -lazy_framework` for macOS. Designed to build Deno.

## Usage

```toml
[target.aarch64-apple-darwin]
rustflags = [
"-C",
"linker=/path/to/lzld/lzld",
"-C",
"link-args=-L/path/to/lzld -llzld",
]
```

### Usage without `lzld` wrapper

1. `rustc -Z link-native-libraries=no -L/path/to/lzld -llzld`: Requires nightly
but doesn't need a wrapper linker.

2. Manaully source modification: Remove `#[link]` attributes from all
dependencies and link to `liblzld.a`.

## Design

It's pretty simple. Drop in `lzld` as the linker. It strips out `-framework`
arguments and links a static library (`liblzld.a`) that will lazy load the
framework via `dlopen` when needed.

Rest of the arguments are passed as-is to `lld`.

<!--
Supported frameworks:
- QuartzCore
- CoreFoundation
- TODO
-->
Binary file added tools/lzld/liblzld_arm64.a
Binary file not shown.
52 changes: 52 additions & 0 deletions tools/lzld/lzld
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
arch=$(uname -m)

# Find the path to the `lld` binary.
lld=$(which lld)
if [ -z "$lld" ]; then
lld=$(which ld64.lld)
fi

if [ ! -x "$lld" ]; then
echo "Error: unable to find 'lld' binary" >&2
exit 23
fi

# Filter out `-framework` arguments.
args=()

while [ $# -gt 0 ]; do
case "$1" in
-framework)
shift 2
;;
-Wl)
# Process `-Wl`
shift
IFS=',' read -r -a wl_args <<< "$1"
for arg in "${wl_args[@]}"; do
args+=("$arg")
done
shift
;;
*)
if [[ $1 == -Wl,* ]]; then
IFS=',' read -r -a wl_args <<< "${1:4}"
for arg in "${wl_args[@]}"; do
args+=("$arg")
done
else
args+=("$1")
fi
shift
;;
esac
done

args+=("-L$(dirname "$(realpath "$0")")")
args+=("-llzld_${arch}")
# Needed for fsevents. TODO: add patches for these as well.
args+=("-framework" "CoreFoundation" "-framework" "CoreServices")

exec "$lld" "${args[@]}"

24 changes: 24 additions & 0 deletions tools/lzld/lzld.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#import <dlfcn.h>

// -- QuartzCore.framework

extern void *kCAGravityTopLeft = 0;

// -- Metal.framework

void *(*MTLCopyAllDevices_)(void) = 0;

void loadMetalFramework() {
void *handle = dlopen("/System/Library/Frameworks/Metal.framework/Metal", RTLD_LAZY);
if (handle) {
MTLCopyAllDevices_ = dlsym(handle, "MTLCopyAllDevices");
}
}

extern void *MTLCopyAllDevices(void) {
if (MTLCopyAllDevices_ == 0) {
loadMetalFramework();
}

return MTLCopyAllDevices_();
}
Loading