Skip to content

[pull] swiftwasm from main #3817

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 2 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,12 @@ static bool isLazilyEmittedFunction(SILFunction &f, SILModule &m) {
if (hasCodeCoverageInstrumentation(f, m))
return false;

// Needed by lldb to print global variables which are propagated by the
// mandatory GlobalOpt.
if (m.getOptions().OptMode == OptimizationMode::NoOptimization &&
f.isGlobalInit())
return false;

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/SIL/IR/SILGlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ swift::getVariableOfStaticInitializer(SILFunction *InitFunc,
return nullptr;
HasStore = true;
InitVal = cast<SingleValueInstruction>(SI->getSrc());
} else if (auto *mt = dyn_cast<MetatypeInst>(&I)) {
// Unused meta_type instructions are sometimes generated by SILGen.
// Handle this case to not require to run DeadCodeElimination before
// MandatoryGlobalOpt.
if (!mt->use_empty())
return nullptr;
} else if (!SILGlobalVariable::isValidStaticInitializerInst(&I,
I.getModule())) {
return nullptr;
Expand Down
12 changes: 8 additions & 4 deletions lib/SILOptimizer/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ bool SILGlobalOpt::isAssignedOnlyOnceInInitializer(SILGlobalVariable *SILG,
if (SILG->isLet())
return true;

// Don't replace loads from `var` globals when compiled with -Onone.
if (Module->getOptions().OptMode == OptimizationMode::NoOptimization)
return false;

// If we should skip this, it is probably because there are multiple stores.
// Return false if there are multiple stores or no stores.
if (GlobalVarSkipProcessing.count(SILG) || !GlobalVarStore.count(SILG))
Expand Down Expand Up @@ -768,10 +772,6 @@ bool SILGlobalOpt::run() {
do {
changed = false;
for (auto &InitCalls : GlobalInitCallMap) {
// Don't optimize functions that are marked with the opt.never attribute.
if (!InitCalls.first->shouldOptimize())
continue;

// Try to create a static initializer for the global and replace all uses
// of the global by this constant value.
changed |= optimizeInitializer(InitCalls.first, InitCalls.second);
Expand All @@ -788,6 +788,10 @@ bool SILGlobalOpt::run() {
optimizeGlobalAccess(Init.first, Init.second);
}

/// Don't perform the remaining optimizations when compiled with -Onone.
if (Module->getOptions().OptMode == OptimizationMode::NoOptimization)
return HasChanged;

SmallVector<SILGlobalVariable *, 8> addrGlobals;
for (auto &addrPair : GlobalAddrMap) {
// Don't optimize functions that are marked with the opt.never attribute.
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) {
P.addDiagnoseLifetimeIssues();
}

P.addGlobalOpt();
P.addPerformanceDiagnostics();

// Canonical swift requires all non cond_br critical edges to be split.
Expand Down
7 changes: 3 additions & 4 deletions test/IRGen/force_public_metadata_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ public func use() -> Int {
}
}

// CHECK: define {{.*}} @"$s31force_public_metadata_accessors3useSiyF"()
// CHECK-NOT: define
// CHECK: call {{.*}} %swift.metadata_response @"$s31force_public_metadata_accessors14FixedContainer{{.*}}LLOMa"

// FIXME: From within LLDB, this would be a forward declaration.
// Unfortunately this is difficult to reproduce from source alone.
// Really this should be a check for a non-internal "declare".
// CHECK: define{{.*}} swiftcc %swift.metadata_response @"$s31force_public_metadata_accessors14FixedContainer{{.*}}LLOMa"

// CHECK: define {{.*}} @"$s31force_public_metadata_accessors3useSiyF"()
// CHECK-NOT: define
// CHECK: call {{.*}} %swift.metadata_response @"$s31force_public_metadata_accessors14FixedContainer{{.*}}LLOMa"

4 changes: 1 addition & 3 deletions test/IRGen/globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ extension A {
// CHECK: @"$s7globals2g3Sbvp" = hidden global [[BOOL]] zeroinitializer, align 1
// CHECK: @"$s7globals2g6Sdvp" = hidden global [[DOUBLE]] zeroinitializer, align 8
// CHECK: @"$s7globals2g7Sfvp" = hidden global [[FLOAT]] zeroinitializer, align 4
// CHECK: @"$s7globals1AV3fooSivpZ" = hidden global [[INT]] zeroinitializer, align 8
// CHECK: @"$s7globals1AV3fooSivpZ" = hidden global [[INT]] <{ i64 5 }>, align 8

// CHECK-NOT: g8
// CHECK-NOT: g9

// CHECK: define{{( dllexport)?}}{{( protected)?}} i32 @main(i32 %0, i8** %1) {{.*}} {
// CHECK: store i64 {{.*}}, i64* getelementptr inbounds ([[INT]], [[INT]]* @"$s7globals2g0Sivp", i32 0, i32 0), align 8

// CHECK: define internal void @"{{.*}}WZ"() {{.*}} {
// CHECK: store i64 5, i64* getelementptr inbounds (%TSi, %TSi* @"$s7globals1AV3fooSivpZ", i32 0, i32 0), align 8
4 changes: 2 additions & 2 deletions test/ModuleInterface/optimization-level.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// optimization passes don't run when compiling a .swiftinterface that was
// generated with -Onone.

// OPT: GlobalOpt
// UNOPT-NOT: GlobalOpt
// OPT: AccessMarkerElimination
// UNOPT-NOT: AccessMarkerElimination
public func f() {}

16 changes: 16 additions & 0 deletions test/SILOptimizer/performance-annotations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ open class Cl {
final func finalMethod() {}
}

func initFunc() -> Int { return 3 }

struct Str : P {
let x: Int

func protoMethod(_ a: Int) -> Int {
return a + x
}

static let s = 27
static var s2 = 10 + s
static var s3 = initFunc() // expected-error {{global/static variable initialization can cause locking}}
}

struct AllocatingStr : P {
Expand Down Expand Up @@ -107,3 +112,14 @@ func testRecursion(_ i: Int) -> Int {
}
return 0
}

@_noLocks
func testGlobal() -> Int {
return Str.s + Str.s2
}

@_noLocks
func testGlobalWithComplexInit() -> Int {
return Str.s3 // expected-note {{called from here}}
}

4 changes: 3 additions & 1 deletion test/Serialization/global_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// The only way to inspect the serialized module is sil-opt. The swift
// driver will only output the SIL that it deserializes.

func initFunc() -> Int { return 42 }

@usableFromInline
let MyConst = 42
let MyConst = initFunc()
@usableFromInline
var MyVar = 3

Expand Down