Skip to content

Commit 24158b2

Browse files
committed
SILGen: Drop value argument when lowering Builtin.sizeof/alignof/strideof.
The metatype value isn't needed to do the memory layout computation, and IRGen only looks at the substitutions passed to the generic signature, so we can reduce SIL code size, and avoid leaving behind useless metadata accesses, if we don't emit it.
1 parent 69c06f9 commit 24158b2

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

foo

49.6 KB
Binary file not shown.

include/swift/AST/Builtins.def

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,6 @@ BUILTIN_RUNTIME_CALL(IsOptionalType, "isOptional", "")
557557
/// Triggers a runtime failure if the condition is true.
558558
BUILTIN_MISC_OPERATION(CondFailMessage, "condfail_message", "", Special)
559559

560-
/// Sizeof has type T.Type -> Int
561-
BUILTIN_MISC_OPERATION(Sizeof, "sizeof", "n", Special)
562-
563-
/// Strideof has type T.Type -> Int
564-
BUILTIN_MISC_OPERATION(Strideof, "strideof", "n", Special)
565-
566560
/// IsPOD has type T.Type -> Bool
567561
BUILTIN_MISC_OPERATION(IsPOD, "ispod", "n", Special)
568562

@@ -580,9 +574,6 @@ BUILTIN_MISC_OPERATION(IsBitwiseTakable, "isbitwisetakable", "n", Special)
580574
/// IsSameMetatype has type (Any.Type, Any.Type) -> Bool
581575
BUILTIN_MISC_OPERATION(IsSameMetatype, "is_same_metatype", "n", Special)
582576

583-
/// Alignof has type T.Type -> Int
584-
BUILTIN_MISC_OPERATION(Alignof, "alignof", "n", Special)
585-
586577
/// AllocRaw has type (Int, Int) -> Builtin.RawPointer
587578
///
588579
/// Parameters: object size, object alignment.
@@ -851,6 +842,15 @@ BUILTIN_MISC_OPERATION(AssumeAlignment, "assumeAlignment", "n", Special)
851842
BUILTIN_MISC_OPERATION(Id, Name, Attrs, Overload)
852843
#endif
853844

845+
/// Sizeof has type T.Type -> Int
846+
BUILTIN_MISC_OPERATION_WITH_SILGEN(Sizeof, "sizeof", "n", Special)
847+
848+
/// Strideof has type T.Type -> Int
849+
BUILTIN_MISC_OPERATION_WITH_SILGEN(Strideof, "strideof", "n", Special)
850+
851+
/// Alignof has type T.Type -> Int
852+
BUILTIN_MISC_OPERATION_WITH_SILGEN(Alignof, "alignof", "n", Special)
853+
854854
// getCurrentExecutor: () async -> Builtin.Executor?
855855
//
856856
// Retrieve the ExecutorRef on which the current asynchronous

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,43 @@ static ManagedValue emitBuiltinGetCurrentExecutor(
14191419
return ManagedValue::forUnmanaged(SGF.emitGetCurrentExecutor(loc));
14201420
}
14211421

1422+
// Emit SIL for sizeof/strideof/alignof.
1423+
// These formally take a metatype argument that's never actually used, so
1424+
// we ignore it.
1425+
static ManagedValue emitBuiltinSizeof(
1426+
SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
1427+
PreparedArguments &&preparedArgs, SGFContext C) {
1428+
auto &ctx = SGF.getASTContext();
1429+
return ManagedValue::forUnmanaged(
1430+
SGF.B.createBuiltin(loc,
1431+
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::Sizeof)),
1432+
SILType::getBuiltinWordType(ctx),
1433+
subs,
1434+
{}));
1435+
}
1436+
static ManagedValue emitBuiltinStrideof(
1437+
SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
1438+
PreparedArguments &&preparedArgs, SGFContext C) {
1439+
auto &ctx = SGF.getASTContext();
1440+
return ManagedValue::forUnmanaged(
1441+
SGF.B.createBuiltin(loc,
1442+
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::Strideof)),
1443+
SILType::getBuiltinWordType(ctx),
1444+
subs,
1445+
{}));
1446+
}
1447+
static ManagedValue emitBuiltinAlignof(
1448+
SILGenFunction &SGF, SILLocation loc, SubstitutionMap subs,
1449+
PreparedArguments &&preparedArgs, SGFContext C) {
1450+
auto &ctx = SGF.getASTContext();
1451+
return ManagedValue::forUnmanaged(
1452+
SGF.B.createBuiltin(loc,
1453+
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::Alignof)),
1454+
SILType::getBuiltinWordType(ctx),
1455+
subs,
1456+
{}));
1457+
}
1458+
14221459
// Helper to lower a function argument to be usable as the entry point of a
14231460
// new async task
14241461
static ManagedValue

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,13 @@ SILCombiner::optimizeAlignment(PointerToAddressInst *ptrAdrInst) {
288288
m_SILValue(extendedAlignment))))) {
289289
alignOper = extendedAlignment;
290290
}
291-
MetatypeInst *metatype;
292291
if (match(alignOper,
293-
m_ApplyInst(BuiltinValueKind::Alignof, m_MetatypeInst(metatype)))) {
292+
m_ApplyInst(BuiltinValueKind::Alignof))) {
293+
CanType formalType = cast<BuiltinInst>(alignOper)->getSubstitutions()
294+
.getReplacementTypes()[0]->getCanonicalType(ptrAdrInst->getFunction()->getGenericSignature());
295+
294296
SILType instanceType = ptrAdrInst->getFunction()->getLoweredType(
295-
metatype->getType().castTo<MetatypeType>().getInstanceType());
297+
Lowering::AbstractionPattern::getOpaque(), formalType);
296298

297299
if (instanceType.getAddressType() != ptrAdrInst->getType())
298300
return nullptr;

0 commit comments

Comments
 (0)