Skip to content

[SwiftCompilerSources] Bridged in-IR testing. #68813

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

Conversation

nate-chandler
Copy link
Contributor

Added the bridging types involved and the basic functionality.

@nate-chandler nate-chandler force-pushed the swiftcompilersources/test-bridging branch from 82c8a63 to 487dc11 Compare September 28, 2023 04:11
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

nice!
I have a few comments

self.name._withStringRef { nameStr in
registerFunctionTest(
nameStr,
unsafeBitCast(impl, to: UnsafeMutableRawPointer.self))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you define the closure as @convention(c) to avoid the unsafe bitcast? Like it's done for the run-function in registerPass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, with the FunctionTest as a global struct whose member can be called which does the unwrapping, that works. Fixed.

@_semantics("arc.immortal")
public class FunctionTest {
public var name: String
init(name: String, impl: @convention(thin) (Function, TestArguments, FunctionTest) -> ()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I tried to follow the convention to not use abbreviations in function API names (which includes argument labels). Therefore I suggest impl: -> implementation:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

import SILBridging

@_semantics("arc.immortal")
public class FunctionTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this really need to be a class? And does it need to bridge to C++ (via the swift object header)?
On the C++ side, a new FunctionTest is created anyway.
I think it can be a struct, similar to FunctionPass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Switched over to that.

{ bridgedFunction, bridgedTestArguments, bridgedFunctionTest, ctx in
typealias Impl = @convention(thin) (Function, TestArguments, FunctionTest) -> ()
let impl = unsafeBitCast(ctx, to: Impl.self)
impl(bridgedFunction.function, bridgedTestArguments.native, bridgedFunctionTest.native)
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, the reason to pass the test to the closure is to get things like dominance info. In the spirit of swift passes, it's better to pass a TestContext which can provide everything which is potentially needed by a test.
Though, it will be a problem that this context provides an analysis, like the dominator tree, because this is defined in the optimizer module. Anyway, that can be solved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, it's to make analyses available. Added a Bridged/TestContext that wraps a SwiftPassInvocation. Later on I'll add extensions to make the analyses available.

}

@_semantics("arc.immortal")
public class TestArguments {
Copy link
Contributor

Choose a reason for hiding this comment

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

again, does this need to be a bridged class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, fixed.

@nate-chandler nate-chandler force-pushed the swiftcompilersources/test-bridging branch from 487dc11 to 076c520 Compare September 28, 2023 16:10
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler force-pushed the swiftcompilersources/test-bridging branch from 076c520 to 48035ab Compare September 28, 2023 18:09
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler marked this pull request as ready for review September 28, 2023 18:12
Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

lgtm!
Just one thing: can you copy the top-level comment(s) from the C++ Test.h file over to the swift implementation (in Test.swift)? Feel free to do that in a follow-up PR.

There's no advantage to using an function_ref and it might be confusing.
Added the bridging types involved and the basic functionality.
@nate-chandler nate-chandler force-pushed the swiftcompilersources/test-bridging branch from 48035ab to c9389e1 Compare September 28, 2023 18:35
@nate-chandler
Copy link
Contributor Author

can you copy the top-level comment(s) from the C++ Test.h file over to the swift implementation (in Test.swift)?

Yep! Done (with appropriate translation).

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

To prove out that the bridging works.
@nate-chandler nate-chandler force-pushed the swiftcompilersources/test-bridging branch from c9389e1 to 36805c8 Compare September 28, 2023 22:15
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please clean test linux platform

@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test linux platform

@nate-chandler
Copy link
Contributor Author

@swift-ci please test linux platform

@nate-chandler nate-chandler merged commit 954c0f5 into swiftlang:main Sep 29, 2023
@nate-chandler nate-chandler deleted the swiftcompilersources/test-bridging branch September 29, 2023 19:28
//
// CASE STUDY:
// Here's an example of how it works:
// 0) A source file, NeatUtils.cpp contains
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is supposed to be NeatUtils.swift

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops! #68924 .

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