Skip to content

Commit 73589ec

Browse files
authored
Merge pull request #20208 from rjmccall/builtin-integer-literal
Change the integer-literal type from Int2048 to IntLiteral
2 parents e3cd69b + abdba1d commit 73589ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+251
-366
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,8 +2619,6 @@ NOTE(found_candidate,none,
26192619
NOTE(found_candidate_type,none,
26202620
"found candidate with type %0", (Type))
26212621

2622-
ERROR(no_MaxBuiltinIntegerType_found,none,
2623-
"standard library error: _MaxBuiltinIntegerType is not properly defined", ())
26242622
ERROR(no_MaxBuiltinFloatType_found,none,
26252623
"standard library error: _MaxBuiltinFloatType is not properly defined", ())
26262624
ERROR(integer_literal_overflows_maxwidth, none,

lib/AST/Builtins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ static ValueDecl *getCheckedConversionOperation(ASTContext &Context,
11141114
static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
11151115
Identifier Id, Type InputTy,
11161116
Type OutputTy) {
1117-
auto InTy = InputTy->getAs<AnyBuiltinIntegerType>();
1117+
auto InTy = InputTy->getAs<BuiltinIntegerLiteralType>();
11181118
auto OutTy = OutputTy->getAs<BuiltinFloatType>();
11191119
if (!InTy || !OutTy)
11201120
return nullptr;

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ APInt IntegerLiteralExpr::getRawValue() const {
857857
APInt IntegerLiteralExpr::getValue() const {
858858
assert(!getType().isNull() && "Semantic analysis has not completed");
859859
assert(!getType()->hasError() && "Should have a valid type");
860-
auto width = getType()->castTo<BuiltinIntegerType>()->getWidth();
860+
auto width = getType()->castTo<AnyBuiltinIntegerType>()->getWidth();
861861
return width.parse(getDigitsText(), /*radix*/ 0, isNegative());
862862
}
863863

lib/IRGen/GenBuiltin.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -777,28 +777,12 @@ if (Builtin.ID == BuiltinValueKind::id) { \
777777
// We are currently emitting code for '_convertFromBuiltinIntegerLiteral',
778778
// which will call the builtin and pass it a non-compile-time-const parameter.
779779
if (Builtin.ID == BuiltinValueKind::IntToFPWithOverflow) {
780-
if (Builtin.Types[0]->is<BuiltinIntegerLiteralType>()) {
781-
auto toType =
782-
IGF.IGM.getStorageTypeForLowered(Builtin.Types[1]->getCanonicalType());
783-
auto result = emitIntegerLiteralToFP(IGF, args, toType);
784-
out.add(result);
785-
return;
786-
}
787-
auto ToTy =
780+
assert(Builtin.Types[0]->is<BuiltinIntegerLiteralType>());
781+
auto toType =
788782
IGF.IGM.getStorageTypeForLowered(Builtin.Types[1]->getCanonicalType());
789-
llvm::Value *Arg = args.claimNext();
790-
unsigned bitSize = Arg->getType()->getScalarSizeInBits();
791-
if (bitSize > 64) {
792-
// TODO: the integer literal bit size is 2048, but we only have a 64-bit
793-
// conversion function available (on all platforms).
794-
Arg = IGF.Builder.CreateTrunc(Arg, IGF.IGM.Int64Ty);
795-
} else if (bitSize < 64) {
796-
// Just for completeness. IntToFPWithOverflow is currently only used to
797-
// convert 2048 bit integer literals.
798-
Arg = IGF.Builder.CreateSExt(Arg, IGF.IGM.Int64Ty);
799-
}
800-
llvm::Value *V = IGF.Builder.CreateSIToFP(Arg, ToTy);
801-
return out.add(V);
783+
auto result = emitIntegerLiteralToFP(IGF, args, toType);
784+
out.add(result);
785+
return;
802786
}
803787

804788
if (Builtin.ID == BuiltinValueKind::Once

lib/IRGen/GenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "Outlining.h"
4545
#include "ProtocolInfo.h"
4646
#include "ReferenceTypeInfo.h"
47-
#include "ScalarTypeInfo.h"
47+
#include "ScalarPairTypeInfo.h"
4848
#include "NativeConventionSchema.h"
4949
#include "IRGenMangler.h"
5050
#include "NonFixedTypeInfo.h"

lib/SILGen/SILGenLValue.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,8 +1070,13 @@ static bool areCertainlyEqualIndices(const Expr *e1, const Expr *e2) {
10701070
}
10711071

10721072
// Compare a variety of literals.
1073-
if (auto *il1 = dyn_cast<IntegerLiteralExpr>(e1))
1074-
return il1->getValue() == cast<IntegerLiteralExpr>(e2)->getValue();
1073+
if (auto *il1 = dyn_cast<IntegerLiteralExpr>(e1)) {
1074+
const auto &val1 = il1->getValue();
1075+
const auto &val2 = cast<IntegerLiteralExpr>(e2)->getValue();
1076+
// If the integers are arbitrary-precision, their bit-widths may differ,
1077+
// but only if they represent different values.
1078+
return val1.getBitWidth() == val2.getBitWidth() && val1 == val2;
1079+
}
10751080
if (auto *il1 = dyn_cast<FloatLiteralExpr>(e1))
10761081
return il1->getValue().bitwiseIsEqual(
10771082
cast<FloatLiteralExpr>(e2)->getValue());

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,10 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
772772
}
773773
}
774774

775-
776-
// Assume that we are converting from a literal if the Source size is
777-
// 2048. Is there a better way to identify conversions from literals?
778-
bool Literal = (SrcBitWidth == 2048 ||
779-
isa<BuiltinIntegerLiteralType>(SrcTy));
775+
// Assume that we're converting from a literal if the source type is
776+
// IntegerLiteral. Is there a better way to identify this if we start
777+
// using Builtin.IntegerLiteral in an exposed type?
778+
bool Literal = isa<BuiltinIntegerLiteralType>(SrcTy);
780779

781780
// FIXME: This will prevent hard error in cases the error is coming
782781
// from ObjC interoperability code. Currently, we treat NSUInteger as

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,9 +1868,7 @@ namespace {
18681868
tc.Context.Id_IntegerLiteralType,
18691869
initName,
18701870
builtinProtocol,
1871-
// Note that 'MaxIntegerType' is guaranteed to be available.
1872-
// Otherwise it would be caught by CSGen::visitLiteralExpr
1873-
tc.getMaxIntegerType(dc),
1871+
tc.Context.TheIntegerLiteralType,
18741872
builtinInitName,
18751873
nullptr,
18761874
diag::integer_literal_broken_proto,

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,34 +1203,6 @@ namespace {
12031203
if (!protocol)
12041204
return nullptr;
12051205

1206-
// Make sure that MaxIntegerType is defined if it would used later
1207-
// during constraint solving (CSApply).
1208-
if (isa<IntegerLiteralExpr>(expr) ||
1209-
(isa<MagicIdentifierLiteralExpr>(expr) &&
1210-
dyn_cast<MagicIdentifierLiteralExpr>(expr)->isColumn())) {
1211-
1212-
auto maxIntType = CS.TC.getMaxIntegerType(CS.DC);
1213-
if (maxIntType.isNull()) {
1214-
CS.TC.diagnose(expr->getLoc(), diag::no_MaxBuiltinIntegerType_found);
1215-
return nullptr;
1216-
}
1217-
1218-
// In the case of integer literal, make sure that the literal value
1219-
// will fit within the bit width of the maximum integer type.
1220-
if (IntegerLiteralExpr *intLit = dyn_cast<IntegerLiteralExpr>(expr)) {
1221-
unsigned maxWidth =
1222-
maxIntType->castTo<BuiltinIntegerType>()->getGreatestWidth();
1223-
unsigned signedLitWidth = intLit->getRawValue().getMinSignedBits();
1224-
1225-
if (signedLitWidth > maxWidth) { // overflow?
1226-
CS.TC.diagnose(expr->getLoc(),
1227-
diag::integer_literal_overflows_maxwidth, maxWidth,
1228-
signedLitWidth);
1229-
return nullptr;
1230-
}
1231-
}
1232-
}
1233-
12341206
auto tv = CS.createTypeVariable(CS.getConstraintLocator(expr),
12351207
TVO_PrefersSubtypeBinding);
12361208
CS.addConstraint(ConstraintKind::LiteralConformsTo, tv,

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,12 +1265,11 @@ static LiteralExpr *getAutomaticRawValueExpr(TypeChecker &TC,
12651265
/*Implicit=*/true);
12661266
}
12671267
// If the prevValue is not a well-typed integer, then break.
1268-
// This could happen if the literal value overflows _MaxBuiltinIntegerType.
12691268
if (!prevValue->getType())
12701269
return nullptr;
12711270

12721271
if (auto intLit = dyn_cast<IntegerLiteralExpr>(prevValue)) {
1273-
APInt nextVal = intLit->getValue() + 1;
1272+
APInt nextVal = intLit->getValue().sextOrSelf(128) + 1;
12741273
bool negative = nextVal.slt(0);
12751274
if (negative)
12761275
nextVal = -nextVal;

lib/Sema/TypeCheckType.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -432,31 +432,6 @@ Type TypeChecker::getUInt8Type(DeclContext *dc) {
432432
return ::getStdlibType(*this, UInt8Type, dc, "UInt8");
433433
}
434434

435-
/// Returns the maximum-sized builtin integer type.
436-
Type TypeChecker::getMaxIntegerType(DeclContext *dc) {
437-
if (!MaxIntegerType.isNull())
438-
return MaxIntegerType;
439-
440-
SmallVector<ValueDecl *, 1> lookupResults;
441-
getStdlibModule(dc)->lookupValue(/*AccessPath=*/{},
442-
Context.Id_MaxBuiltinIntegerType,
443-
NLKind::QualifiedLookup, lookupResults);
444-
if (lookupResults.size() != 1)
445-
return MaxIntegerType;
446-
447-
auto *maxIntegerTypeDecl = dyn_cast<TypeAliasDecl>(lookupResults.front());
448-
if (!maxIntegerTypeDecl)
449-
return MaxIntegerType;
450-
451-
validateDecl(maxIntegerTypeDecl);
452-
if (!maxIntegerTypeDecl->hasInterfaceType() ||
453-
!maxIntegerTypeDecl->getDeclaredInterfaceType()->is<BuiltinIntegerType>())
454-
return MaxIntegerType;
455-
456-
MaxIntegerType = maxIntegerTypeDecl->getUnderlyingTypeLoc().getType();
457-
return MaxIntegerType;
458-
}
459-
460435
/// Find the standard type of exceptions.
461436
///
462437
/// We call this the "exception type" to try to avoid confusion with

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ class TypeChecker final : public LazyResolver {
832832
Type getIntType(DeclContext *dc);
833833
Type getInt8Type(DeclContext *dc);
834834
Type getUInt8Type(DeclContext *dc);
835-
Type getMaxIntegerType(DeclContext *dc);
836835
Type getNSObjectType(DeclContext *dc);
837836
Type getObjCSelectorType(DeclContext *dc);
838837
Type getExceptionType(DeclContext *dc, SourceLoc loc);

stdlib/public/core/CompilerProtocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public protocol ExpressibleByNilLiteral {
233233
}
234234

235235
public protocol _ExpressibleByBuiltinIntegerLiteral {
236-
init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType)
236+
init(_builtinIntegerLiteral value: Builtin.IntLiteral)
237237
}
238238

239239
/// A type that can be initialized with an integer literal.

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,8 @@ extension ${Self}: BinaryFloatingPoint {
14381438
extension ${Self} : _ExpressibleByBuiltinIntegerLiteral, ExpressibleByIntegerLiteral {
14391439
@_transparent
14401440
public
1441-
init(_builtinIntegerLiteral value: Builtin.Int${builtinIntLiteralBits}){
1442-
self = ${Self}(Builtin.itofp_with_overflow_Int${builtinIntLiteralBits}_FPIEEE${bits}(value))
1441+
init(_builtinIntegerLiteral value: Builtin.IntLiteral){
1442+
self = ${Self}(Builtin.itofp_with_overflow_IntLiteral_FPIEEE${bits}(value))
14431443
}
14441444

14451445
/// Creates a new value from the given integer literal.

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ from itertools import chain
2323
# Number of bits in the Builtin.Word type
2424
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
2525

26-
# Number of bits in integer literals.
27-
builtinIntLiteralBits = 2048
28-
IntLiteral = 'Int%s' % builtinIntLiteralBits
29-
3026
class struct(object):
3127
def __init__(self, **kw):
3228
self.__dict__ = kw
@@ -1112,8 +1108,8 @@ public struct ${Self}
11121108

11131109

11141110
@_transparent
1115-
public init(_builtinIntegerLiteral x: _MaxBuiltinIntegerType) {
1116-
_value = Builtin.s_to_${u}_checked_trunc_${IntLiteral}_${BuiltinName}(x).0
1111+
public init(_builtinIntegerLiteral x: Builtin.IntLiteral) {
1112+
_value = Builtin.s_to_${u}_checked_trunc_IntLiteral_${BuiltinName}(x).0
11171113
}
11181114

11191115
/// Creates a new instance with the same memory representation as the given

stdlib/public/core/Policy.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,6 @@ public typealias StringLiteralType = String
109109
//===----------------------------------------------------------------------===//
110110
// Default types for unconstrained number literals
111111
//===----------------------------------------------------------------------===//
112-
// Integer literals are limited to 2048 bits.
113-
// The intent is to have arbitrary-precision literals, but implementing that
114-
// requires more work.
115-
//
116-
// Rationale: 1024 bits are enough to represent the absolute value of min/max
117-
// IEEE Binary64, and we need 1 bit to represent the sign. Instead of using
118-
// 1025, we use the next round number -- 2048.
119-
public typealias _MaxBuiltinIntegerType = Builtin.Int2048
120112
#if !os(Windows) && (arch(i386) || arch(x86_64))
121113
public typealias _MaxBuiltinFloatType = Builtin.FPIEEE80
122114
#else

stdlib/public/runtime/KnownMetadata.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ namespace {
3939
char data[16];
4040
};
4141

42-
struct alignas(32) int256_like {
42+
static_assert(MaximumAlignment == 16, "max alignment was hardcoded");
43+
struct alignas(16) int256_like {
4344
char data[32];
4445
};
45-
struct alignas(64) int512_like {
46+
struct alignas(16) int512_like {
4647
char data[64];
4748
};
4849

@@ -122,8 +123,9 @@ namespace {
122123
SET_FIXED_ALIGNMENT(uint32_t, 4)
123124
SET_FIXED_ALIGNMENT(uint64_t, 8)
124125
SET_FIXED_ALIGNMENT(int128_like, 16)
125-
SET_FIXED_ALIGNMENT(int256_like, 32)
126-
SET_FIXED_ALIGNMENT(int512_like, 64)
126+
static_assert(MaximumAlignment == 16, "max alignment was hardcoded");
127+
SET_FIXED_ALIGNMENT(int256_like, 16)
128+
SET_FIXED_ALIGNMENT(int512_like, 16)
127129

128130
#undef SET_FIXED_ALIGNMENT
129131

test/DebugInfo/value-update.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ bb0:
3737
// CHECK: {{^}}}
3838
}
3939

40-
// Swift.Int.init (_builtinIntegerLiteral : Builtin.Int2048) -> Swift.Int
41-
sil [transparent] [serialized] @_TFSiCfT22_builtinIntegerLiteralBi2048__Si : $@convention(thin) (Builtin.Int2048, @thin Int.Type) -> Int
40+
// Swift.Int.init (_builtinIntegerLiteral : Builtin.IntLiteral) -> Swift.Int
41+
sil [transparent] [serialized] @_TFSiCfT22_builtinIntegerLiteralBI__Si : $@convention(thin) (Builtin.IntLiteral, @thin Int.Type) -> Int
4242

4343
// StdlibUnittest._blackHole <A> (A) -> ()
4444
sil @_TF14StdlibUnittest10_blackHoleurFxT_ : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()

test/IDE/print_types.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,46 @@ func testVariableTypes(_ param: Int, param2: inout Double) {
1414

1515
var a1 = 42
1616
// CHECK: VarDecl '''a1''' Int{{$}}
17-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
17+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
1818
// FULL: VarDecl '''a1''' Swift.Int{{$}}
19-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
19+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
2020
a1 = 17; _ = a1
2121

2222

2323
var a2 : Int = 42
2424
// CHECK: VarDecl '''a2''' Int{{$}}
25-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
25+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
2626
// FULL: VarDecl '''a2''' Swift.Int{{$}}
27-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
27+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
2828
a2 = 17; _ = a2
2929

3030
var a3 = Int16(42)
3131
// CHECK: VarDecl '''a3''' Int16{{$}}
32-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
32+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
3333
// FULL: VarDecl '''a3''' Swift.Int16{{$}}
34-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
34+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
3535
a3 = 17; _ = a3
3636

3737

3838
var a4 = Int32(42)
3939
// CHECK: VarDecl '''a4''' Int32{{$}}
40-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
40+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
4141
// FULL: VarDecl '''a4''' Swift.Int32{{$}}
42-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
42+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
4343
a4 = 17; _ = a4
4444

4545
var a5 : Int64 = 42
4646
// CHECK: VarDecl '''a5''' Int64{{$}}
47-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
47+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
4848
// FULL: VarDecl '''a5''' Swift.Int64{{$}}
49-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
49+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
5050
a5 = 17; _ = a5
5151

5252
var typealias1 : MyInt = 42
5353
// CHECK: VarDecl '''typealias1''' MyInt{{$}}
54-
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Int2048{{$}}
54+
// CHECK: IntegerLiteralExpr:[[@LINE-2]] '''42''' Builtin.IntLiteral{{$}}
5555
// FULL: VarDecl '''typealias1''' MyInt{{$}}
56-
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Int2048{{$}}
56+
// FULL: IntegerLiteralExpr:[[@LINE-4]] '''42''' Builtin.IntLiteral{{$}}
5757
_ = typealias1 ; typealias1 = 1
5858

5959
var optional1 = Optional<Int>.none

test/IRGen/int_to_fp.sil

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)