mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
The assertion is hit through `TypeValueInst.simplify` when constructing an integer literal instruction with a negative 64-bit `Swift.Int` and a bit width of 32 (the target pointer bit width for arm64_32 watchOS). This happens because we tell the `llvm::APInt` constructor to treat the input integer as unsigned by default in `getAPInt`, and a negative 64-bit signed integer does not fit into 32 bits when interpreted as unsigned. Fix this by flipping the default signedness assumption for the Swift API and introducing a convenience method for constructing a 1-bit integer literal instruction, where the correct signedness assumption depends on whether you want to use 1 or -1 for 'true'. In the context of using an integer to construct an `llvm::APInt`, there are 2 other cases where signedness matters that come to mind: 1. A non-decimal integer literal narrower than 64 bits, such as `0xABCD`, is used. 2. The desired bit width is >64, since `llvm::APInt` can either zero-extend or sign-extend the 64-bit integer it accepts. Neither of these appear to be exercised in SwiftCompilerSources, and if we ever do, the caller should be responsible for either (1) appropriately extending the literal manually, e.g. `Int(Int16(bitPattern: 0xABCD))`, or (2) passing along the appropriate signedness.
51 lines
1.7 KiB
Swift
51 lines
1.7 KiB
Swift
//===--- SimplifyClassifyBridgeObject.swift -------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import AST
|
|
import SIL
|
|
|
|
extension ClassifyBridgeObjectInst : OnoneSimplifiable, SILCombineSimplifiable {
|
|
func simplify(_ context: SimplifyContext) {
|
|
// Constant fold `classify_bridge_object` to `(false, false)` if the operand is known
|
|
// to be a swift class.
|
|
var walker = CheckForSwiftClasses();
|
|
if walker.walkUp(value: operand.value, path: UnusedWalkingPath()) == .abortWalk {
|
|
return
|
|
}
|
|
|
|
let builder = Builder(before: self, context)
|
|
let falseLiteral = builder.createBoolLiteral(false)
|
|
let tp = builder.createTuple(type: self.type, elements: [falseLiteral, falseLiteral])
|
|
uses.replaceAll(with: tp, context)
|
|
context.erase(instruction: self)
|
|
}
|
|
}
|
|
|
|
private struct CheckForSwiftClasses: ValueUseDefWalker {
|
|
mutating func walkUp(value: Value, path: UnusedWalkingPath) -> WalkResult {
|
|
if let nominal = value.type.nominal,
|
|
let classDecl = nominal as? ClassDecl,
|
|
!classDecl.isObjC
|
|
{
|
|
// Stop this use-def walk if the value is known to be a swift class.
|
|
return .continueWalk
|
|
}
|
|
return walkUpDefault(value: value, path: path)
|
|
}
|
|
|
|
mutating func rootDef(value: Value, path: UnusedWalkingPath) -> WalkResult {
|
|
return .abortWalk
|
|
}
|
|
|
|
var walkUpCache = WalkerCache<UnusedWalkingPath>()
|
|
}
|