Skip to content

Commit 406aa86

Browse files
authored
Merge pull request #41557 from atrick/addrlower-update
Update and reimplement AddressLowering pass (for SIL opaque values).
2 parents 2c5f019 + 6f3a0c3 commit 406aa86

37 files changed

+4865
-1847
lines changed

docs/SIL.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,26 @@ parts::
21932193
return %1 : $Klass
21942194
}
21952195

2196+
Forwarding Address-Only Values
2197+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2198+
2199+
Address-only values are potentially unmovable when borrowed. This
2200+
means that they cannot be forwarded with guaranteed ownership unless
2201+
the forwarded value has the same representation as in the original
2202+
value and can reuse the same storage. Non-destructive projection is
2203+
allowed, such as `struct_extract`. Aggregation, such as `struct`, and
2204+
destructive disaggregation, such as `switch_enum` is not allowed. This
2205+
is an invariant for OSSA with opaque SIL values for these reasons:
2206+
2207+
1. To avoid implicit semantic copies. For move-only values, this allows
2208+
complete diagnostics. And in general, it makes it impossible for SIL
2209+
passes to "accidentally" create copies.
2210+
2211+
2. To reuse borrowed storage. This allows the optimizer to share the same
2212+
storage for multiple exclusive reads of the same variable, avoiding
2213+
copies. It may also be necessary to support native Swift atomics, which
2214+
will be unmovable-when-borrowed.
2215+
21962216
Borrowed Object based Safe Interior Pointers
21972217
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21982218

include/swift/AST/SILOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ class SILOptions {
140140
/// If this is disabled we do not serialize in OSSA form when optimizing.
141141
bool EnableOSSAModules = false;
142142

143+
/// If set to true, compile with the SIL Opaque Values enabled.
144+
bool EnableSILOpaqueValues = false;
145+
143146
// The kind of function bodies to skip emitting.
144147
FunctionBodySkipping SkipFunctionBodies = FunctionBodySkipping::None;
145148

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,6 @@ namespace swift {
379379
/// [TODO: Clang-type-plumbing] Turn on for feature rollout.
380380
bool UseClangFunctionTypes = false;
381381

382-
/// If set to true, compile with the SIL Opaque Values enabled.
383-
/// This is for bootstrapping. It can't be in SILOptions because the
384-
/// TypeChecker uses it to set resolve the ParameterConvention.
385-
bool EnableSILOpaqueValues = false;
386-
387382
/// If set to true, the diagnosis engine can assume the emitted diagnostics
388383
/// will be used in editor. This usually leads to more aggressive fixit.
389384
bool DiagnosticsEditorMode = false;

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,6 @@ def disable_sil_ownership_verifier : Flag<["-"], "disable-sil-ownership-verifier
507507
def suppress_static_exclusivity_swap : Flag<["-"], "suppress-static-exclusivity-swap">,
508508
HelpText<"Suppress static violations of exclusive access with swap()">;
509509

510-
def enable_sil_opaque_values : Flag<["-"], "enable-sil-opaque-values">,
511-
HelpText<"Enable SIL Opaque Values">;
512-
513510
def enable_experimental_static_assert :
514511
Flag<["-"], "enable-experimental-static-assert">,
515512
HelpText<"Enable experimental #assert">;
@@ -1022,6 +1019,9 @@ def enable_ossa_modules : Flag<["-"], "enable-ossa-modules">,
10221019
HelpText<"Always serialize SIL in ossa form. If this flag is not passed in, "
10231020
"when optimizing ownership will be lowered before serializing SIL">;
10241021

1022+
def enable_sil_opaque_values : Flag<["-"], "enable-sil-opaque-values">,
1023+
HelpText<"Enable SIL Opaque Values">;
1024+
10251025
def new_driver_path
10261026
: Separate<["-"], "new-driver-path">, MetaVarName<"<path>">,
10271027
HelpText<"Path of the new driver to be used">;

include/swift/SIL/ApplySite.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ class FullApplySite : public ApplySite {
585585
}
586586

587587
/// Get the SIL value that represents all of the given call's results. For a
588-
/// single direct result, returns the result. For multiple results, returns a
589-
/// fake tuple value. The tuple has no storage of its own. The real results
590-
/// must be extracted from it.
588+
/// single direct result, returns the actual result. For multiple results,
589+
/// returns a pseudo-result tuple. The tuple has no storage of its own. The
590+
/// real results must be extracted from it.
591591
///
592592
/// For ApplyInst, returns the single-value instruction itself.
593593
///
@@ -596,7 +596,7 @@ class FullApplySite : public ApplySite {
596596
/// For BeginApplyInst, returns an invalid value. For coroutines, there is no
597597
/// single value representing all results. Yielded values are generally
598598
/// handled differently since they have the convention of incoming arguments.
599-
SILValue getPseudoResult() const {
599+
SILValue getResult() const {
600600
switch (getKind()) {
601601
case FullApplySiteKind::ApplyInst:
602602
return SILValue(cast<ApplyInst>(getInstruction()));

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,10 @@ class SILBuilder {
266266
void clearInsertionPoint() { BB = nullptr; }
267267

268268
/// setInsertionPoint - Set the insertion point.
269-
void setInsertionPoint(SILBasicBlock *BB, SILBasicBlock::iterator InsertPt) {
269+
void setInsertionPoint(SILBasicBlock *BB, SILBasicBlock::iterator insertPt) {
270270
this->BB = BB;
271-
this->InsertPt = InsertPt;
272-
if (InsertPt == BB->end())
273-
return;
271+
this->InsertPt = insertPt;
272+
assert(insertPt == BB->end() || insertPt->getParent() == BB);
274273
}
275274

276275
/// setInsertionPoint - Set the insertion point to insert before the specified

include/swift/SIL/SILModule.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ class SILModule {
307307
/// The stage of processing this module is at.
308308
SILStage Stage;
309309

310+
/// True if SIL conventions force address-only to be passed by address.
311+
///
312+
/// Used for bootstrapping the AddressLowering pass. This should eventually
313+
/// be inferred from the SIL stage to be true only when Stage == Lowered.
314+
bool loweredAddresses;
315+
310316
/// The set of deserialization notification handlers.
311317
DeserializationNotificationHandlerSet deserializationNotificationHandlers;
312318

@@ -806,6 +812,11 @@ class SILModule {
806812
Stage = s;
807813
}
808814

815+
/// True if SIL conventions force address-only to be passed by address.
816+
bool useLoweredAddresses() const { return loweredAddresses; }
817+
818+
void setLoweredAddresses(bool val) { loweredAddresses = val; }
819+
809820
llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
810821

811822
void setPGOReader(std::unique_ptr<llvm::IndexedInstrProfReader> IPR) {
@@ -972,15 +983,13 @@ inline bool SILOptions::supportsLexicalLifetimes(const SILModule &mod) const {
972983
// entirely.
973984
return LexicalLifetimes != LexicalLifetimesOption::Off;
974985
case SILStage::Canonical:
986+
case SILStage::Lowered:
975987
// In Canonical SIL, lexical markers are used to ensure that object
976988
// lifetimes do not get observably shortened from the end of a lexical
977989
// scope. That behavior only occurs when lexical lifetimes is (fully)
978990
// enabled. (When only diagnostic markers are enabled, the markers are
979991
// stripped as part of lowering from raw to canonical SIL.)
980992
return LexicalLifetimes == LexicalLifetimesOption::On;
981-
case SILStage::Lowered:
982-
// We do not support OSSA in Lowered SIL, so this is always false.
983-
return false;
984993
}
985994
}
986995

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
770770
Opts.EnableObjCInterop =
771771
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,
772772
Target.isOSDarwin());
773-
Opts.EnableSILOpaqueValues |= Args.hasArg(OPT_enable_sil_opaque_values);
774773

775774
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
776775

@@ -1678,6 +1677,7 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
16781677
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
16791678
Opts.EnableOSSAModules |= Args.hasArg(OPT_enable_ossa_modules);
16801679
Opts.EnableOSSAOptimizations &= !Args.hasArg(OPT_disable_ossa_opts);
1680+
Opts.EnableSILOpaqueValues |= Args.hasArg(OPT_enable_sil_opaque_values);
16811681
Opts.EnableSpeculativeDevirtualization |= Args.hasArg(OPT_enable_spec_devirt);
16821682
Opts.EnableActorDataRaceChecks |= Args.hasFlag(
16831683
OPT_enable_actor_data_race_checks,

lib/SIL/IR/SILModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class SILModule::SerializationCallback final
9191

9292
SILModule::SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
9393
Lowering::TypeConverter &TC, const SILOptions &Options)
94-
: Stage(SILStage::Raw), indexTrieRoot(new IndexTrieNode()),
95-
Options(Options), serialized(false),
94+
: Stage(SILStage::Raw), loweredAddresses(!Options.EnableSILOpaqueValues),
95+
indexTrieRoot(new IndexTrieNode()), Options(Options), serialized(false),
9696
regDeserializationNotificationHandlerForNonTransparentFuncOME(false),
9797
regDeserializationNotificationHandlerForAllFuncOME(false),
9898
prespecializedFunctionDeclsImported(false), SerializeSILAction(),

lib/SIL/IR/SILType.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,7 @@ SILResultInfo::getOwnershipKind(SILFunction &F,
506506
}
507507

508508
SILModuleConventions::SILModuleConventions(SILModule &M)
509-
: M(&M),
510-
loweredAddresses(!M.getASTContext().LangOpts.EnableSILOpaqueValues
511-
|| M.getStage() == SILStage::Lowered)
512-
{}
509+
: M(&M), loweredAddresses(M.useLoweredAddresses()) {}
513510

514511
bool SILModuleConventions::isReturnedIndirectlyInSIL(SILType type,
515512
SILModule &M) {

0 commit comments

Comments
 (0)