Skip to content

Commit 8fb1ea2

Browse files
committed
[SE-0368] StaticBigInt: add the Builtin functions
1 parent 198b974 commit 8fb1ea2

File tree

9 files changed

+165
-8
lines changed

9 files changed

+165
-8
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
@@ -696,6 +696,18 @@ BUILTIN_MISC_OPERATION(UToUCheckedTrunc, "u_to_u_checked_trunc", "n", Special)
696696
/// IntToFPWithOverflow has type (Integer) -> Float
697697
BUILTIN_MISC_OPERATION(IntToFPWithOverflow, "itofp_with_overflow", "n", Special)
698698

699+
/// Builtin.bitWidth_IntLiteral has type
700+
/// (_ value: Builtin.IntLiteral) -> Builtin.Word
701+
BUILTIN_MISC_OPERATION(BitWidth, "bitWidth", "n", Special)
702+
703+
/// Builtin.isNegative_IntLiteral has type
704+
/// (_ value: Builtin.IntLiteral) -> Builtin.Int1
705+
BUILTIN_MISC_OPERATION(IsNegative, "isNegative", "n", Special)
706+
707+
/// Builtin.wordAtIndex_IntLiteral has type
708+
/// (_ value: Builtin.IntLiteral, _ index: Builtin.Word) -> Builtin.Word
709+
BUILTIN_MISC_OPERATION(WordAtIndex, "wordAtIndex", "n", Special)
710+
699711
/// zeroInitializer has type <T> () -> T
700712
BUILTIN_MISC_OPERATION(ZeroInitializer, "zeroInitializer", "n", Special)
701713

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
@@ -1821,6 +1821,33 @@ static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
18211821
return getBuiltinFunction(Id, { InTy }, OutTy);
18221822
}
18231823

1824+
static ValueDecl *getBitWidthOperation(
1825+
ASTContext &ctx,
1826+
Identifier id,
1827+
Type valueTy
1828+
) {
1829+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1830+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy), _word);
1831+
}
1832+
1833+
static ValueDecl *getIsNegativeOperation(
1834+
ASTContext &ctx,
1835+
Identifier id,
1836+
Type valueTy
1837+
) {
1838+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1839+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy), _int(1));
1840+
}
1841+
1842+
static ValueDecl *getWordAtIndexOperation(
1843+
ASTContext &ctx,
1844+
Identifier id,
1845+
Type valueTy
1846+
) {
1847+
if (!valueTy->getAs<BuiltinIntegerLiteralType>()) return nullptr;
1848+
return getBuiltinFunction(ctx, id, _thin, _parameters(valueTy, _word), _word);
1849+
}
1850+
18241851
static ValueDecl *getUnreachableOperation(ASTContext &Context,
18251852
Identifier Id) {
18261853
auto NeverTy = Context.getNeverType();
@@ -2811,6 +2838,18 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
28112838
if (Types.size() != 2) return nullptr;
28122839
return getIntToFPWithOverflowOperation(Context, Id, Types[0], Types[1]);
28132840

2841+
case BuiltinValueKind::BitWidth:
2842+
if (Types.size() != 1) return nullptr;
2843+
return getBitWidthOperation(Context, Id, Types[0]);
2844+
2845+
case BuiltinValueKind::IsNegative:
2846+
if (Types.size() != 1) return nullptr;
2847+
return getIsNegativeOperation(Context, Id, Types[0]);
2848+
2849+
case BuiltinValueKind::WordAtIndex:
2850+
if (Types.size() != 1) return nullptr;
2851+
return getWordAtIndexOperation(Context, Id, Types[0]);
2852+
28142853
case BuiltinValueKind::GetObjCTypeEncoding:
28152854
return getGetObjCTypeEncodingOperation(Context, Id);
28162855

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
@@ -1016,6 +1016,24 @@ if (Builtin.ID == BuiltinValueKind::id) { \
10161016
return;
10171017
}
10181018

1019+
if (Builtin.ID == BuiltinValueKind::BitWidth) {
1020+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
1021+
out.add(emitIntLiteralBitWidth(IGF, args));
1022+
return;
1023+
}
1024+
1025+
if (Builtin.ID == BuiltinValueKind::IsNegative) {
1026+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
1027+
out.add(emitIntLiteralIsNegative(IGF, args));
1028+
return;
1029+
}
1030+
1031+
if (Builtin.ID == BuiltinValueKind::WordAtIndex) {
1032+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
1033+
out.add(emitIntLiteralWordAtIndex(IGF, args));
1034+
return;
1035+
}
1036+
10191037
if (Builtin.ID == BuiltinValueKind::Once
10201038
|| Builtin.ID == BuiltinValueKind::OnceWithContext) {
10211039
// The input type is statically (Builtin.RawPointer, @convention(thin) () -> ()).

lib/IRGen/GenIntegerLiteral.cpp

Lines changed: 42 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
@@ -397,3 +397,44 @@ llvm::Value *irgen::emitIntegerLiteralToFP(IRGenFunction &IGF,
397397
llvm_unreachable("not a floating-point type");
398398
}
399399
}
400+
401+
llvm::Value *irgen::emitIntLiteralBitWidth(
402+
IRGenFunction &IGF,
403+
Explosion &in
404+
) {
405+
auto data = in.claimNext();
406+
auto flags = in.claimNext();
407+
(void)data; // [[maybe_unused]]
408+
return IGF.Builder.CreateLShr(
409+
flags,
410+
IGF.IGM.getSize(Size(IntegerLiteralFlags::BitWidthShift))
411+
);
412+
}
413+
414+
llvm::Value *irgen::emitIntLiteralIsNegative(
415+
IRGenFunction &IGF,
416+
Explosion &in
417+
) {
418+
auto data = in.claimNext();
419+
auto flags = in.claimNext();
420+
(void)data; // [[maybe_unused]]
421+
static_assert(
422+
IntegerLiteralFlags::IsNegativeFlag == 1,
423+
"hardcoded in this truncation"
424+
);
425+
return IGF.Builder.CreateTrunc(flags, IGF.IGM.Int1Ty);
426+
}
427+
428+
llvm::Value *irgen::emitIntLiteralWordAtIndex(
429+
IRGenFunction &IGF,
430+
Explosion &in
431+
) {
432+
auto data = in.claimNext();
433+
auto flags = in.claimNext();
434+
auto index = in.claimNext();
435+
(void)flags; // [[maybe_unused]]
436+
return IGF.Builder.CreateLoad(
437+
IGF.Builder.CreateInBoundsGEP(IGF.IGM.SizeTy, data, index),
438+
IGF.IGM.getPointerAlignment()
439+
);
440+
}

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
@@ -67,6 +67,10 @@ llvm::Value *emitIntegerLiteralToFP(IRGenFunction &IGF,
6767
Explosion &in,
6868
llvm::Type *toType);
6969

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

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
@@ -726,6 +726,9 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ICMP_ULE)
726726
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ICMP_ULT)
727727
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, InsertElement)
728728
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntToFPWithOverflow)
729+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, BitWidth)
730+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IsNegative)
731+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, WordAtIndex)
729732
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntToPtr)
730733
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IsOptionalType)
731734
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
@@ -497,6 +497,9 @@ CONSTANT_OWNERSHIP_BUILTIN(None, SToSCheckedTrunc)
497497
CONSTANT_OWNERSHIP_BUILTIN(None, SToUCheckedTrunc)
498498
CONSTANT_OWNERSHIP_BUILTIN(None, UToUCheckedTrunc)
499499
CONSTANT_OWNERSHIP_BUILTIN(None, IntToFPWithOverflow)
500+
CONSTANT_OWNERSHIP_BUILTIN(None, BitWidth)
501+
CONSTANT_OWNERSHIP_BUILTIN(None, IsNegative)
502+
CONSTANT_OWNERSHIP_BUILTIN(None, WordAtIndex)
500503

501504
// This is surprising, Builtin.unreachable returns a "Never" value which is
502505
// 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:

test/IRGen/integer_literal.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,37 @@ entry(%0 : $Builtin.IntLiteral):
354354
%result = builtin "itofp_with_overflow_IntLiteral_FPIEEE128"(%0 : $Builtin.IntLiteral) : $Builtin.FPIEEE128
355355
return %result : $Builtin.FPIEEE128
356356
}
357+
358+
359+
360+
/***************************************************************************/
361+
/* bitWidth, isNegative, wordAtIndex */
362+
/***************************************************************************/
363+
364+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i32 @test_bitWidth(i32* %0, i32 %1)
365+
sil @test_bitWidth : $(Builtin.IntLiteral) -> Builtin.Word {
366+
entry(%value : $Builtin.IntLiteral):
367+
// CHECK: %2 = lshr i32 %1, 8
368+
// CHECK-NEXT: ret i32 %2
369+
%result = builtin "bitWidth_IntLiteral"(%value : $Builtin.IntLiteral) : $Builtin.Word
370+
return %result : $Builtin.Word
371+
}
372+
373+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i1 @test_isNegative(i32* %0, i32 %1)
374+
sil @test_isNegative : $(Builtin.IntLiteral) -> Builtin.Int1 {
375+
entry(%value : $Builtin.IntLiteral):
376+
// CHECK: %2 = trunc i32 %1 to i1
377+
// CHECK-NEXT: ret i1 %2
378+
%result = builtin "isNegative_IntLiteral"(%value : $Builtin.IntLiteral) : $Builtin.Int1
379+
return %result : $Builtin.Int1
380+
}
381+
382+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i32 @test_wordAtIndex(i32* %0, i32 %1, i32 %2)
383+
sil @test_wordAtIndex : $(Builtin.IntLiteral, Builtin.Word) -> Builtin.Word {
384+
entry(%value : $Builtin.IntLiteral, %index : $Builtin.Word):
385+
// CHECK: %3 = getelementptr inbounds i32, i32* %0, i32 %2
386+
// CHECK-NEXT: %4 = load i32, i32* %3, align 4
387+
// CHECK-NEXT: ret i32 %4
388+
%result = builtin "wordAtIndex_IntLiteral"(%value : $Builtin.IntLiteral, %index : $Builtin.Word) : $Builtin.Word
389+
return %result : $Builtin.Word
390+
}

0 commit comments

Comments
 (0)