Skip to content

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Sep 9, 2025

Introduces a new experimental flag, AST, SILGen and limited SIL support.

Adds support for writing borrow accessors that return projections of self representable by a new ResultConvention::Guaranteed or ResultConvention::Guaranteed.

There are still several unimplemented parts. The main ones are:

  • mutate accessor support in SIL and lower (InOutType is banned)
  • borrow and mutate accessors in address-only types with multiple returns (address phis are banned in SIL)
  • borrow accessors in loadable ~Copyable types (SILGen of borrow accessors clashes with late resolution of move-only types)

And handle them in various covered switches
…edAddress

ResultConvention::Guaranteed will be used by borrow accessors when the storage type can be returned by value.

ResultConvention::GuaranteedAddress will be used by mutate accessors and borrow accessors when the storage type
cannot be returned by value.
@meg-gupta meg-gupta marked this pull request as ready for review September 9, 2025 22:46
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

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

@swift-ci Please test

1 similar comment
@meg-gupta
Copy link
Contributor Author

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

@swift-ci Please test

@meg-gupta
Copy link
Contributor Author

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

@swift-ci Please test macOS platform

@meg-gupta
Copy link
Contributor Author

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

@swift-ci Please test

@atrick
Copy link
Contributor

atrick commented Sep 12, 2025

For the record, I suggested that we make a new rule that 'self' on a borrow accessor cannot have a lexical lifetime. Note that 'self' already needs to be borrowed over the duration of the accessor and the dependency between the borrowed value and 'self' needs to be represented in SIL. There would never be a need for a lexical lifetime. That's good, because it would be impossible to represent a returns borrow value from a begin_borrow scope.

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

This all looks great to me. I glossed over most of the SILGen implementation because I'm not much use there.


We need verification to ensure that, whenever a borrow accessor returns an address, it's implementation only refers to addressable storage recursively up the the addressable 'self' parameter. I know your diagnostics mostly cover this. I just think we need SIL verification on top of that which can run after any pass.


We should test IRGen for large loadable values.


It's to be determined whether the SIL should model the borrows result with mark_dependence . If the apply can always be viewed as an ownership forwarding operation on the 'self' operand, and the we guarantee that the accessor implementation only allows forwarding (or escaping) operations. Then we don't structly need another SIL dependency.

@@ -767,6 +767,12 @@ Scope scopeForArgument(Scope nonlexicalScope, SILValue callArg, unsigned index,
// the argument. Just do an ownership conversion if needed.
return nonlexicalScope;
}

// Use non-lexical scope for functions returning @guaranteed results.
if (callee->getConventions().hasGuaranteedResult()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In Ownership.md, Variable Lifetimes, we can add a note that lexical lifetimes are not support on self for borrow accessors.

Add a read effect on the self parameter. Without this, the self parameter can get dead code eliminated by the GenericSpecializer.
…sult() with ApplyInst::hasGuaranteedAddressResult()
…essors

It uses a check on conformance to ForwardInstruction for walking down guaranteed forwarding uses.
Since apply of borrow accessors cannot be represented as ForwardingInstruction, handle them separately.

Representing apply of borrow accessors for consistent handling in the optimizer is TBD.
@meg-gupta
Copy link
Contributor Author

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

@swift-ci Please test

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