Skip to content

[5.3] SIL: fix memory behavior of global_addr for globals with non-fixed layout. #31231

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
Apr 23, 2020
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
16 changes: 16 additions & 0 deletions lib/SIL/IR/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,22 @@ SILInstruction::MemoryBehavior SILInstruction::getMemoryBehavior() const {
}
}

if (auto *ga = dyn_cast<GlobalAddrInst>(this)) {
// Global variables with resilient types might be allocated into a buffer
// and not statically in the data segment.
// In this case, the global_addr depends on alloc_global being executed
// first. We model this by letting global_addr have a side effect.
// It prevents e.g. LICM to move a global_addr out of a loop while keeping
// the alloc_global inside the loop.
SILModule &M = ga->getFunction()->getModule();
auto expansion = TypeExpansionContext::maximal(M.getAssociatedContext(),
M.isWholeModule());
const TypeLowering &tl =
M.Types.getTypeLowering(ga->getType().getObjectType(), expansion);
return tl.isFixedABI() ? MemoryBehavior::None :
MemoryBehavior::MayHaveSideEffects;
}

switch (getKind()) {
#define FULL_INST(CLASS, TEXTUALNAME, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
case SILInstructionKind::CLASS: \
Expand Down
2 changes: 2 additions & 0 deletions test/SILOptimizer/Inputs/licm_and_global_addr/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

testit(3)
17 changes: 17 additions & 0 deletions test/SILOptimizer/Inputs/licm_and_global_addr/test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

public struct Abc {
public var a = 1
public var b = 2
public var c = 3
public var d = 4

public init() { }

public mutating func printit() {
print(a)
}
}

public func unknown() {
}

29 changes: 29 additions & 0 deletions test/SILOptimizer/licm_and_global_addr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/Inputs/licm_and_global_addr/test.swift -parse-as-library -wmo -enable-library-evolution -module-name=Test -emit-module -emit-module-path=%t/Test.swiftmodule -c -o %t/test.o
// RUN: %target-build-swift -O %S/Inputs/licm_and_global_addr/main.swift %s -I%t %t/test.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test

import Test

// Check that LICM does not move a global_addr above an alloc_global in case
// the global might be dynamically allocated.

// The type Abc is resilient and > 3 words. So it is allocated dynamically in a buffer.
var a = Abc()

@inline(never)
func testit(_ n: Int) {
for _ in 0..<n {
// Prevent hoisting the initialization of 'a',
// which would defeat the purpose of the test.
unknown()

// CHECK: 1
// CHECK: 1
// CHECK: 1
a.printit()
}
}