Skip to content

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Sep 2, 2025

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.

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test

@AnthonyLatsis AnthonyLatsis changed the title Fix some rebranch test failures Fix some rebranch test failures on main Sep 2, 2025
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test macOS

@AnthonyLatsis AnthonyLatsis changed the title Fix some rebranch test failures on main SwiftCompilerSources/SIL: Fix APInt assertion failure on rebranch Sep 24, 2025
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test

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.
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@AnthonyLatsis AnthonyLatsis merged commit 275deaf into main Sep 24, 2025
4 of 5 checks passed
@AnthonyLatsis AnthonyLatsis deleted the jepa-main2 branch September 24, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants