swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.32k stars 10.34k forks source link

[SR-12485] Code fails when run for -Onone optimization #54926

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12485
Radar rdar://problem/62201610
Original Reporter Jaap (JIRA User)
Type Bug
Environment macOS Catalina 10.15.3 \`swift --version\` ``` java Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29) Target: x86_64-apple-darwin19.3.0 ``` Xcode 11.4 (Not necessary it can be reproduced using the swiftc compiler)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: bdc1a101ab95f24062c16848738af624

Issue Description:

The following code crashes with a bad access error when compiled with the -Onone flag. It does work using the REPL or when compiled using -O and -Osize.

struct Transform {
    var position: SIMD3<Float>
}

class Node {
    private var transform: Transform

    var position: SIMD3<Float> {
        get { return transform.position }
        set { transform.position = newValue}
    }

    init() {
        transform = Transform(position: SIMD3<Float>(0, 0, 0))
    }
}

let node = Node()
print(node.position) //SIMD3<Float>(0.0, 0.0, 0.0)
node.position = SIMD3<Float>(1, 1, 1)
print(node.position) //SIMD3<Float>(1.0, 1.0, 1.0)
node.position.x = 3
print(node.position) //SIMD3<Float>(3.0, 1.0, 1.0)
node.position.y = node.position.y + 5
print(node.position) // SIMD3<Float>(3.0, 6.0, 1.0)
node.position.z += 9 // getting a bad access error here
print(node.position)
swiftc Sources/test/test.swift -O
./test

resulting in:

SIMD3<Float>(0.0, 0.0, 0.0)
SIMD3<Float>(1.0, 1.0, 1.0)
SIMD3<Float>(3.0, 1.0, 1.0)
SIMD3<Float>(3.0, 6.0, 1.0)
SIMD3<Float>(3.0, 6.0, 10.0)

Compared to:

swiftc Sources/test/test.swift -Onone
./test

resulting in:

SIMD3<Float>(0.0, 0.0, 0.0)
SIMD3<Float>(1.0, 1.0, 1.0)
SIMD3<Float>(3.0, 1.0, 1.0)
SIMD3<Float>(3.0, 6.0, 1.0)
Segmentation fault: 11
beccadax commented 4 years ago

@swift-ci create