Skip to content

Commit 0b29450

Browse files
[SE-0368] StaticBigInt (#40722)
1 parent e02a345 commit 0b29450

File tree

13 files changed

+573
-10
lines changed

13 files changed

+573
-10
lines changed

include/swift/AST/Builtins.def

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -701,6 +701,18 @@ BUILTIN_MISC_OPERATION(UToUCheckedTrunc, "u_to_u_checked_trunc", "n", Special)
701701
/// IntToFPWithOverflow has type (Integer) -> Float
702702
BUILTIN_MISC_OPERATION(IntToFPWithOverflow, "itofp_with_overflow", "n", Special)
703703

704+
/// Builtin.bitWidth_IntLiteral has type
705+
/// (_ value: Builtin.IntLiteral) -> Builtin.Word
706+
BUILTIN_MISC_OPERATION(BitWidth, "bitWidth", "n", Special)
707+
708+
/// Builtin.isNegative_IntLiteral has type
709+
/// (_ value: Builtin.IntLiteral) -> Builtin.Int1
710+
BUILTIN_MISC_OPERATION(IsNegative, "isNegative", "n", Special)
711+
712+
/// Builtin.wordAtIndex_IntLiteral has type
713+
/// (_ value: Builtin.IntLiteral, _ index: Builtin.Word) -> Builtin.Word
714+
BUILTIN_MISC_OPERATION(WordAtIndex, "wordAtIndex", "n", Special)
715+
704716
/// zeroInitializer has type <T> () -> T
705717
BUILTIN_MISC_OPERATION(ZeroInitializer, "zeroInitializer", "n", Special)
706718

lib/AST/Builtins.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1796,6 +1796,33 @@ static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
17961796
return getBuiltinFunction(Id, { InTy }, OutTy);
17971797
}
17981798

1799+
static ValueDecl *getBitWidthOperation(
1800+
ASTContext &ctx,
1801+
Identifier id,
1802+
Type valueTy
1803+
) {
1804+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1805+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy), _word);
1806+
}
1807+
1808+
static ValueDecl *getIsNegativeOperation(
1809+
ASTContext &ctx,
1810+
Identifier id,
1811+
Type valueTy
1812+
) {
1813+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1814+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy), _int(1));
1815+
}
1816+
1817+
static ValueDecl *getWordAtIndexOperation(
1818+
ASTContext &ctx,
1819+
Identifier id,
1820+
Type valueTy
1821+
) {
1822+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1823+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy, _word), _word);
1824+
}
1825+
17991826
static ValueDecl *getUnreachableOperation(ASTContext &Context,
18001827
Identifier Id) {
18011828
auto NeverTy = Context.getNeverType();
@@ -2781,6 +2808,18 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
27812808
if (Types.size() != 2) return nullptr;
27822809
return getIntToFPWithOverflowOperation(Context, Id, Types[0], Types[1]);
27832810

2811+
case BuiltinValueKind::BitWidth:
2812+
if (Types.size() != 1) return nullptr;
2813+
return getBitWidthOperation(Context, Id, Types[0]);
2814+
2815+
case BuiltinValueKind::IsNegative:
2816+
if (Types.size() != 1) return nullptr;
2817+
return getIsNegativeOperation(Context, Id, Types[0]);
2818+
2819+
case BuiltinValueKind::WordAtIndex:
2820+
if (Types.size() != 1) return nullptr;
2821+
return getWordAtIndexOperation(Context, Id, Types[0]);
2822+
27842823
case BuiltinValueKind::GetObjCTypeEncoding:
27852824
return getGetObjCTypeEncodingOperation(Context, Id);
27862825

lib/IRGen/GenBuiltin.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -961,6 +961,24 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
961961
return;
962962
}
963963

964+
if (Builtin.ID == BuiltinValueKind::BitWidth) {
965+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
966+
out.add(emitIntLiteralBitWidth(IGF, args));
967+
return;
968+
}
969+
970+
if (Builtin.ID == BuiltinValueKind::IsNegative) {
971+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
972+
out.add(emitIntLiteralIsNegative(IGF, args));
973+
return;
974+
}
975+
976+
if (Builtin.ID == BuiltinValueKind::WordAtIndex) {
977+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
978+
out.add(emitIntLiteralWordAtIndex(IGF, args));
979+
return;
980+
}
981+
964982
if (Builtin.ID == BuiltinValueKind::Once
965983
|| Builtin.ID == BuiltinValueKind::OnceWithContext) {
966984
// The input type is statically (Builtin.RawPointer, @convention(thin) () -> ()).

lib/IRGen/GenIntegerLiteral.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -401,3 +401,45 @@ llvm::Value *irgen::emitIntegerLiteralToFP(IRGenFunction &IGF,
401401
llvm_unreachable("not a floating-point type");
402402
}
403403
}
404+
405+
llvm::Value *irgen::emitIntLiteralBitWidth(
406+
IRGenFunction &IGF,
407+
Explosion &in
408+
) {
409+
auto data = in.claimNext();
410+
auto flags = in.claimNext();
411+
(void)data; // [[maybe_unused]]
412+
return IGF.Builder.CreateLShr(
413+
flags,
414+
IGF.IGM.getSize(Size(IntegerLiteralFlags::BitWidthShift))
415+
);
416+
}
417+
418+
llvm::Value *irgen::emitIntLiteralIsNegative(
419+
IRGenFunction &IGF,
420+
Explosion &in
421+
) {
422+
auto data = in.claimNext();
423+
auto flags = in.claimNext();
424+
(void)data; // [[maybe_unused]]
425+
static_assert(
426+
IntegerLiteralFlags::IsNegativeFlag == 1,
427+
"hardcoded in this truncation"
428+
);
429+
return IGF.Builder.CreateTrunc(flags, IGF.IGM.Int1Ty);
430+
}
431+
432+
llvm::Value *irgen::emitIntLiteralWordAtIndex(
433+
IRGenFunction &IGF,
434+
Explosion &in
435+
) {
436+
auto data = in.claimNext();
437+
auto flags = in.claimNext();
438+
auto index = in.claimNext();
439+
(void)flags; // [[maybe_unused]]
440+
return IGF.Builder.CreateLoad(
441+
IGF.Builder.CreateInBoundsGEP(IGF.IGM.SizeTy, data, index),
442+
IGF.IGM.SizeTy,
443+
IGF.IGM.getPointerAlignment()
444+
);
445+
}

lib/IRGen/GenIntegerLiteral.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -66,6 +66,10 @@ llvm::Value *emitIntegerLiteralToFP(IRGenFunction &IGF,
6666
Explosion &in,
6767
llvm::Type *toType);
6868

69+
llvm::Value *emitIntLiteralBitWidth(IRGenFunction &IGF, Explosion &in);
70+
llvm::Value *emitIntLiteralIsNegative(IRGenFunction &IGF, Explosion &in);
71+
llvm::Value *emitIntLiteralWordAtIndex(IRGenFunction &IGF, Explosion &in);
72+
6973
}
7074
}
7175

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -742,6 +742,9 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ICMP_ULE)
742742
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ICMP_ULT)
743743
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, InsertElement)
744744
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntToFPWithOverflow)
745+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, BitWidth)
746+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IsNegative)
747+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, WordAtIndex)
745748
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntToPtr)
746749
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IsOptionalType)
747750
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IsPOD)

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -495,6 +495,9 @@ CONSTANT_OWNERSHIP_BUILTIN(None, SToSCheckedTrunc)
495495
CONSTANT_OWNERSHIP_BUILTIN(None, SToUCheckedTrunc)
496496
CONSTANT_OWNERSHIP_BUILTIN(None, UToUCheckedTrunc)
497497
CONSTANT_OWNERSHIP_BUILTIN(None, IntToFPWithOverflow)
498+
CONSTANT_OWNERSHIP_BUILTIN(None, BitWidth)
499+
CONSTANT_OWNERSHIP_BUILTIN(None, IsNegative)
500+
CONSTANT_OWNERSHIP_BUILTIN(None, WordAtIndex)
498501

499502
// This is surprising, Builtin.unreachable returns a "Never" value which is
500503
// trivially typed.

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -129,6 +129,9 @@ static bool isBarrier(SILInstruction *inst) {
129129
case BuiltinValueKind::SToSCheckedTrunc:
130130
case BuiltinValueKind::UToUCheckedTrunc:
131131
case BuiltinValueKind::IntToFPWithOverflow:
132+
case BuiltinValueKind::BitWidth:
133+
case BuiltinValueKind::IsNegative:
134+
case BuiltinValueKind::WordAtIndex:
132135
case BuiltinValueKind::ZeroInitializer:
133136
case BuiltinValueKind::Once:
134137
case BuiltinValueKind::OnceWithContext:

stdlib/public/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# This source file is part of the Swift.org open source project
44
#
5-
# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
# Licensed under Apache License v2.0 with Runtime Library Exception
77
#
88
# See https://swift.org/LICENSE.txt for license information
@@ -226,6 +226,7 @@ set(SWIFTLIB_SOURCES
226226
PlaygroundDisplay.swift
227227
CommandLine.swift
228228
SliceBuffer.swift
229+
StaticBigInt.swift
229230
UnfoldSequence.swift
230231
UnsafeBufferPointerSlice.swift
231232
VarArgs.swift

stdlib/public/core/GroupInfo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
"Integers": [
168168
"Integers.swift",
169169
"IntegerTypes.swift",
170-
"IntegerParsing.swift"],
170+
"IntegerParsing.swift",
171+
"StaticBigInt.swift"],
171172
"Floating": [
172173
"FloatingPoint.swift",
173174
"FloatingPointParsing.swift",

0 commit comments

Comments
 (0)