Skip to content

[libswift] Refactor libSwift to use SIL types directly and remove bridged versions. #39958

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zoecarver
Copy link
Contributor

@zoecarver zoecarver commented Oct 28, 2021

Specifically, this replaces BridgedInstruction, BridgedFunction, BridgedBasicBlock, and all optional variants to use SILFunction and Optional (etc.) directly.

This patch is based on #39605.

Refs #40293.

@@ -13,6 +13,10 @@
import OptimizerBridging
import SIL

extension Instruction {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a super weird hack that I'm trying to debug right now. I'm not sure if this has to do with extensions that use foreign types or what...

@eeckstein
Copy link
Contributor

nice!

@zoecarver zoecarver force-pushed the pick-remove-silfunction-bridging branch from cf97d7c to 64c92d5 Compare November 25, 2021 00:23
@zoecarver
Copy link
Contributor Author

@eeckstein @hyp @ravikandhadai and anyone else who might be interested: I just totally overhauled this PR. I updated it to remove both the C-bridging types and the Swift bridging types. We now simply extend the imported foreign reference types, and as you can see, the API doesn't change much (once we model inheritance correctly, it shouldn't change at all).

I know there's a lot of code here, but I'd be interested in your overall thoughts. (Also, I know there are still some rough edges, this commit has been through a lot, and I'm going to spend some time cleaning it up next week.)

let context = PassContext(passContext: bridgedCtxt.passContext)
runFunction(function, context)
}
}

struct InstructionPass<InstType: Instruction> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woops, this is one of the rough edges: I can revert this change. Generics are working fine (as you can see with List).

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.

It's nice that you can get rid of the C bridging types and directly use the C++ types.
But the Swift API changes are not acceptable.
I'd like to emphasize once again: the swift SIL API should look like as if SIL was implemented in pure swift from scratch.
The most important goal is simplicity and not to make any compromises to make the clang importer happy.

I really think the way to go here is to keep the swift SIL classes and their var bridged computed member. Just let var bridged return the C++ classes instead of the C bridging type. As we discussed in a private chat some time ago.

@@ -11,14 +11,15 @@
//===----------------------------------------------------------------------===//

import SIL
import SILBridging
Copy link
Contributor

Choose a reason for hiding this comment

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

It should not be required to import SILBriding in an optimization pass. SILBridging is an implementation detail of SIL

if let tei = cfi.operand as? TupleExtractInst {
return tei.operand is BuiltinInst
private func hasOverflowConditionOperand(_ cfi: swift.CondFailInst) -> Bool {
if let tei = getAsTupleExtractInst(cfi.operand) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No! That's a terrible API. Casts should be done with swift's native as? operator

// Merge cond_fail instructions if there is no side-effect or read in
// between them.
for block in function.blocks {
// Per basic block list of cond_fails to merge.
var condFailToMerge = StackList<CondFailInst>(context)
var condFailToMerge = StackList<swift.CondFailInst>(context)
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be CondFailInst, not any clang imported type from the SIL module

@zoecarver
Copy link
Contributor Author

But the Swift API changes are not acceptable.
I'd like to emphasize once again: the swift SIL API should look like as if SIL was implemented in pure swift from scratch.
The most important goal is simplicity and not to make any compromises to make the clang importer happy.

Sorry, Erik, I was meaning to discuss with out before posting this patch. I also should have elaborated on "the API doesn't change much."

My reasoning here is that we don't really need the Swift classes, because we can have exactly the same API by just using extensions. Right now there are only two changes to how this API is consumed: sometimes we say swift.TupleExtractInst instead of TupleExtractInst, and more importantly, casting and inheritance isn't modeled. I hope that we will model inheritance and bridge casting operations very soon though, at which point we can go back to Swift style casting. If we model inheritance and support casting, do you agree that this style of bridging is much better? We can reduce a lot of complexity, a lot of bridging boiler plate, and improve the performance of both the Swift code and C++? (Btw, I can go into more detail on these pros either in a follow up comment or offline.)

@eeckstein
Copy link
Contributor

My reasoning here is that we don't really need the Swift classes, because we can have exactly the same API by just using extensions.

That's not true. The swift classes are needed for casting.

I hope that we will model inheritance and bridge casting operations very soon though, at which point we can go back to Swift style casting.

No, we will not change the API temporarily. We can reconsider when the clang importer can do this (though, I see other problems, too).

@zoecarver
Copy link
Contributor Author

That's not true. The swift classes are needed for casting.

Why? If we bridge isa -> is and dyn_cast -> as, then this should work fine, no?

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