Skip to content

Commit 7288f26

Browse files
committed
Add sextOrBitCast to conversions of Builtin.Word
An error was hit when attempting to build Swift on a 32-bit Linux host. It was asserting when attempting to run ‘RedunantLoadElim” during the optimizer. The reason why is a bit messy. First, Builtin.Word is always considered to be 64-bit when it is a SILType. This means the optimizer, when working with a Builtin.Word, will create 64-bit APInts when converting from other types. Second, the SIL being output for a particular literal looked like this: Int2048 (integer_literal) : Int32 (Signed Truncation) : Word (zextOrBitCast) The constant fold behavior would convert this into an integer_literal of the Builtin.Word type. But because of the zext cast, if the original literal was -1, it would become +4 billion during folding. This normally isn’t a problem **except** when SIL is still attempting to optimize. (See First) Third, The Redundant Load Elimination pass was attempting to make sense of an index_addr instruction which was indexing at -1. This happens when you perform “-= 1” on an UnsafePointer. Because SIL was interpreting Word’s size incorrectly, it caused the RLE pass to ask “what is the ProjectionKind at ptr[4billion]?” Since there’s no feasible answer for such a question, the answer was “nothing” when the Projection’s kind() was checked, which tripped the assert. This fix uses sign extension when converting “upward” and the Integer type is signed, otherwise it will still use zero extension.
1 parent 64ef8ec commit 7288f26

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,15 +1617,15 @@ ${assignmentOperatorComment(x.operator, True)}
16171617
% if BuiltinName == 'Int32':
16181618
self._value = Builtin.truncOrBitCast_Word_Int32(_v)
16191619
% elif BuiltinName == 'Int64':
1620-
self._value = Builtin.zextOrBitCast_Word_Int64(_v)
1620+
self._value = Builtin.${z}extOrBitCast_Word_Int64(_v)
16211621
% end
16221622
}
16231623

16241624
@_transparent
16251625
public // @testable
16261626
var _builtinWordValue: Builtin.Word {
16271627
% if BuiltinName == 'Int32':
1628-
return Builtin.zextOrBitCast_Int32_Word(_value)
1628+
return Builtin.${z}extOrBitCast_Int32_Word(_value)
16291629
% elif BuiltinName == 'Int64':
16301630
return Builtin.truncOrBitCast_Int64_Word(_value)
16311631
% end

0 commit comments

Comments
 (0)