Skip to content

[SUA][IRGen] Add stub for swift_coroFrameAlloc that weakly links against the runtime function #79889

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

Merged
merged 12 commits into from
Apr 8, 2025

Conversation

t-rasmud
Copy link
Contributor

@t-rasmud t-rasmud commented Mar 10, 2025

This PR modifies IRGen to emit a stub function __swift_coroFrameAllocStub instead of the
newly introduced swift-rt function swift_coroFrameAlloc. The stub checks whether the runtime has the symbol
swift_coroFrameAlloc and dispatches to it if it exists, uses malloc otherwise. This ensures the
ability to back deploy the feature to older OS targets.

rdar://145239850

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud t-rasmud marked this pull request as ready for review March 11, 2025 22:06
@t-rasmud t-rasmud requested a review from rjmccall as a code owner March 11, 2025 22:06
@t-rasmud t-rasmud changed the title [SUA][IRGen] Add stub for swift_coroFrameAlloc [SUA][IRGen] Add stub for swift_coroFrameAlloc that weakly links against the runtime function Mar 11, 2025
@t-rasmud t-rasmud marked this pull request as draft March 12, 2025 02:11
@t-rasmud
Copy link
Contributor Author

Please test with following pull request:
swiftlang/llvm-project#10236

@swift-ci test

@t-rasmud
Copy link
Contributor Author

Please test with following pull request:
swiftlang/llvm-project#10239

@swift-ci test

@t-rasmud
Copy link
Contributor Author

@swift-ci test

3 similar comments
@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud t-rasmud marked this pull request as ready for review March 21, 2025 21:39
@t-rasmud
Copy link
Contributor Author

@swift-ci test

…nst the runtime function

This commit modifies IRGen to emit a stub function `__swift_coroFrameAllocStub` instead of the
newly introduced swift-rt function `swift_coroFrameAlloc`. The stub checks whether the runtime has the symbol
`swift_coroFrameAlloc` and dispatches to it if it exists, uses `malloc` otherwise. This ensures the
ability to back deploy the feature to older OS targets.

rdar://145239850
@t-rasmud t-rasmud requested a review from compnerd as a code owner March 25, 2025 18:48
@t-rasmud
Copy link
Contributor Author

@swift-ci test

auto coroAllocPtr = IGF.IGM.getCoroFrameAllocFn();
auto coroAllocFn = dyn_cast<llvm::Function>(coroAllocPtr);
if (!llvm::Triple(IGM.Triple).isOSWindows())
coroAllocFn->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Copy link
Member

Choose a reason for hiding this comment

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

This makes the symbol strong on windows and weak on everything else. Is this intentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

@compnerd

@t-rasmud saw errors of the form:

conflicting weak extern definition ...  New default ... conflicts with old default 

I think we have to answer first why we are getting this error message to decide that disabling weak linkage is the right thing to do here.
I am doubtful, without further information of why marking the declaration weak caused the failure.

Ideally, we can weakly link the symbol also on windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the exact failure: https://ci-external.swift.org/job/swift-PR-windows/38338/consoleText

FAILED: lib/lib_FoundationCollections.lib 
C:\Windows\system32\cmd.exe /C "cd . && T:\5\bin\swiftc.exe -target x86_64-unknown-windows-msvc -emit-library -static -o lib\lib_FoundationCollections.lib @CMakeFiles\_FoundationCollections.rsp  && cd ."

Rope+_Node.swift.obj : fatal error LNK1227: conflicting weak extern definition for 'swift_coroFrameAlloc'.  New default '.weak.swift_coroFrameAlloc.default.$s22_FoundationCollections4RopeV5_NodeV6objectyXlvs' conflicts with old default '.weak.swift_coroFrameAlloc.default.$s22_FoundationCollections4RopeV7isValidySbAC5IndexVyx_GF' in Rope+Collection.swift.obj

Copy link
Contributor

Choose a reason for hiding this comment

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

@compnerd I don't know why a weak external reference would result in an error message like that. But it seems that others ran into a similar issue:

// Windows does not allow multiple definitions of weak symbols.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm

https://maskray.me/blog/2021-04-25-weak-symbol

Is the problem that entry in the stdlib runtime is not marked weak?

void *swift_coroFrameAlloc(size_t bytes, MallocTypeId typeId);

What happens if we change that line to use SWIFT_RUNTIME_WEAK_IMPORT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using SWIFT_RUNTIME_WEAK_IMPORT results in linker error: Undefined symbols for architecture x86_64: "_swift_coroFrameAlloc"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I only now realize that typed malloc is not available on windows (or any non apple platform for that matter).

You can disable coroFrameAlloc emission on windows.

#if defined(__APPLE__) && defined(__LP64__) && __has_include(<malloc_type_private.h>) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC

#if defined(__APPLE__) && defined(__LP64__) && __has_include(<malloc_type_private.h>) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
# include <TargetConditionals.h>
# if TARGET_OS_IOS && !TARGET_OS_SIMULATOR ...
#  define SWIFT_STDLIB_HAS_MALLOC_TYPE 1
# endif
#endif
#ifndef SWIFT_STDLIB_HAS_MALLOC_TYPE
# define SWIFT_STDLIB_HAS_MALLOC_TYPE 0
#endif

Copy link
Contributor

Choose a reason for hiding this comment

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

You would achieve that with something similar in spirit to the following change:

+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -3766,6 +3766,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
     Args.hasFlag(OPT_enable_async_frame_push_pop_metadata,
                  OPT_disable_async_frame_push_pop_metadata,
                  Opts.EmitAsyncFramePushPopMetadata);
+
+  if (Opts.EmitTypeMallocForCoroFrame && (Triple.isOSLinux() || Triple.isOSWindows())) {
+    Opts.EmitTypeMallocForCoroFrame = false;
+  }
   Opts.EmitTypeMallocForCoroFrame =
   Args.hasFlag(OPT_enable_emit_type_malloc_for_coro_frame,
               OPT_disable_emit_type_malloc_for_coro_frame,

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@t-rasmud
Copy link
Contributor Author

@swift-ci test

@aschwaighofer
Copy link
Contributor

You can restrict a test to run only on say macOS and iOS by adding the following line to the test:

// REQUIRES: OS=macosx || OS=iOS
...

@t-rasmud
Copy link
Contributor Author

t-rasmud commented Apr 1, 2025

@swift-ci test

1 similar comment
@t-rasmud
Copy link
Contributor Author

t-rasmud commented Apr 7, 2025

@swift-ci test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

LGTM

@t-rasmud t-rasmud merged commit 5e2f20b into swiftlang:main Apr 8, 2025
5 checks passed
t-rasmud added a commit that referenced this pull request Apr 11, 2025
…nst the runtime function (#79889)

* [SUA][IRGen] Add stub for swift_coroFrameAlloc that weakly links against the runtime function

This commit modifies IRGen to emit a stub function `__swift_coroFrameAllocStub` instead of the
newly introduced swift-rt function `swift_coroFrameAlloc`. The stub checks whether the runtime has the symbol
`swift_coroFrameAlloc` and dispatches to it if it exists, uses `malloc` otherwise. This ensures the
ability to back deploy the feature to older OS targets.

rdar://145239850
(cherry picked from commit 5e2f20b)
t-rasmud added a commit that referenced this pull request Apr 15, 2025
…nst the runtime function (#79889) (#80769)

* [SUA][IRGen] Add stub for swift_coroFrameAlloc that weakly links against the runtime function

This commit modifies IRGen to emit a stub function `__swift_coroFrameAllocStub` instead of the
newly introduced swift-rt function `swift_coroFrameAlloc`. The stub checks whether the runtime has the symbol
`swift_coroFrameAlloc` and dispatches to it if it exists, uses `malloc` otherwise. This ensures the
ability to back deploy the feature to older OS targets.

rdar://145239850
(cherry picked from commit 5e2f20b)
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.

3 participants