Skip to content

Commit b35ac50

Browse files
committed
Optimize TypeValueInst in Swift
1 parent 15db739 commit b35ac50

File tree

11 files changed

+99
-18
lines changed

11 files changed

+99
-18
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ swift_compiler_sources(Optimizer
2424
SimplifyInitEnumDataAddr.swift
2525
SimplifyKeyPath.swift
2626
SimplifyLoad.swift
27+
SimplifyMisc.swift
2728
SimplifyPartialApply.swift
2829
SimplifyPointerToAddress.swift
2930
SimplifyRefCasts.swift
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- SimplifyMisc.swift -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
extension TypeValueInst: OnoneSimplifyable, SILCombineSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
// If our parameter is not known statically, then bail.
18+
guard paramType.isInteger() else {
19+
return
20+
}
21+
22+
// Note: Right now, value generics only support 'Int'. If we ever expand the
23+
// scope of types supported, then this should change.
24+
let fieldType = type.getNominalFields(in: parentFunction)![0]
25+
26+
let builder = Builder(before: self, context)
27+
let intLiteral = builder.createIntegerLiteral(value, type: fieldType)
28+
let structInst = builder.createStruct(type: type, elements: [intLiteral])
29+
uses.replaceAll(with: structInst, context)
30+
context.erase(instruction: self)
31+
}
32+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private func registerSwiftPasses() {
108108
registerForSILCombine(DestroyValueInst.self, { run(DestroyValueInst.self, $0) })
109109
registerForSILCombine(DestructureStructInst.self, { run(DestructureStructInst.self, $0) })
110110
registerForSILCombine(DestructureTupleInst.self, { run(DestructureTupleInst.self, $0) })
111+
registerForSILCombine(TypeValueInst.self, { run(TypeValueInst.self, $0) })
111112

112113
// Test passes
113114
registerPass(aliasInfoDumper, { aliasInfoDumper.run($0) })

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,15 @@ class ExistentialMetatypeInst : SingleValueInstruction, UnaryInstruction {}
718718

719719
final public class ObjCProtocolInst : SingleValueInstruction {}
720720

721-
final public class TypeValueInst: SingleValueInstruction, UnaryInstruction {}
721+
final public class TypeValueInst: SingleValueInstruction, UnaryInstruction {
722+
public var paramType: BridgedASTType {
723+
bridged.TypeValueInst_getParamType()
724+
}
725+
726+
public var value: Int {
727+
bridged.TypeValueInst_getValue()
728+
}
729+
}
722730

723731
public class GlobalAccessInstruction : SingleValueInstruction {
724732
final public var global: GlobalVariable {

include/swift/SIL/SILBridging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ struct BridgedASTType {
323323

324324
BRIDGED_INLINE bool isNoEscape() const;
325325

326+
BRIDGED_INLINE bool isInteger() const;
327+
326328
inline bool mayEscape() const { return !isNoEscape() && isEscapable(); }
327329

328330
// =========================================================================//
@@ -1019,6 +1021,8 @@ struct BridgedInstruction {
10191021
BRIDGED_INLINE bool ApplySite_isCalleeNoReturn() const;
10201022
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;
10211023
BRIDGED_INLINE bool ConvertFunctionInst_withoutActuallyEscaping() const;
1024+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType TypeValueInst_getParamType() const;
1025+
BRIDGED_INLINE SwiftInt TypeValueInst_getValue() const;
10221026

10231027
// =========================================================================//
10241028
// VarDeclInst and DebugVariableInst

include/swift/SIL/SILBridgingImpl.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ bool BridgedASTType::isNoEscape() const {
161161
return unbridged()->isNoEscape();
162162
}
163163

164+
bool BridgedASTType::isInteger() const {
165+
return unbridged()->is<swift::IntegerType>();
166+
}
167+
164168
BridgedResultInfoArray
165169
BridgedASTType::SILFunctionType_getResultsWithError() const {
166170
return unbridged()->castTo<swift::SILFunctionType>()->getResultsWithError();
@@ -1410,6 +1414,23 @@ bool BridgedInstruction::ConvertFunctionInst_withoutActuallyEscaping() const {
14101414
return getAs<swift::ConvertFunctionInst>()->withoutActuallyEscaping();
14111415
}
14121416

1417+
BridgedASTType BridgedInstruction::TypeValueInst_getParamType() const {
1418+
return {getAs<swift::TypeValueInst>()->getParamType().getPointer()};
1419+
}
1420+
1421+
SwiftInt BridgedInstruction::TypeValueInst_getValue() const {
1422+
auto tvi = getAs<swift::TypeValueInst>();
1423+
1424+
// Assume we've already checked that the parameter type is an IntegerType.
1425+
auto integer = tvi->getParamType()->castTo<swift::IntegerType>();
1426+
1427+
if (integer->isNegative()) {
1428+
return integer->getValue().getSExtValue();
1429+
} else {
1430+
return integer->getValue().getZExtValue();
1431+
}
1432+
}
1433+
14131434
//===----------------------------------------------------------------------===//
14141435
// VarDeclInst and DebugVariableInst
14151436
//===----------------------------------------------------------------------===//

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ SWIFT_SILCOMBINE_PASS(CopyValueInst)
527527
SWIFT_SILCOMBINE_PASS(DestroyValueInst)
528528
SWIFT_SILCOMBINE_PASS(DestructureStructInst)
529529
SWIFT_SILCOMBINE_PASS(DestructureTupleInst)
530+
SWIFT_SILCOMBINE_PASS(TypeValueInst)
530531

531532
#undef IRGEN_PASS
532533
#undef SWIFT_MODULE_PASS

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ class SILCombiner :
303303
SILInstruction *visitPackElementGetInst(PackElementGetInst *PEGI);
304304
SILInstruction *visitTuplePackElementAddrInst(TuplePackElementAddrInst *TPEAI);
305305
SILInstruction *visitCopyAddrInst(CopyAddrInst *CAI);
306-
SILInstruction *visitTypeValueInst(TypeValueInst *TVI);
307306

308307
SILInstruction *legacyVisitGlobalValueInst(GlobalValueInst *globalValue);
309308

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,18 +2237,3 @@ SILCombiner::visitCopyAddrInst(CopyAddrInst *CAI) {
22372237
CAI->getLoc(), Src->getOperand(), Dst->getOperand(),
22382238
CAI->isTakeOfSrc(), CAI->isInitializationOfDest());
22392239
}
2240-
2241-
SILInstruction *SILCombiner::visitTypeValueInst(TypeValueInst *TVI) {
2242-
auto integer = TVI->getParamType()->getAs<IntegerType>();
2243-
2244-
if (!integer) {
2245-
return nullptr;
2246-
}
2247-
2248-
auto fieldType = TVI->getType().getFieldType((intptr_t)0, TVI->getFunction());
2249-
assert(fieldType.isBuiltinInteger());
2250-
2251-
auto intLiteral = Builder.createIntegerLiteral(TVI->getLoc(), fieldType,
2252-
integer->getValue().getZExtValue());
2253-
return Builder.createStruct(TVI->getLoc(), TVI->getType(), {intLiteral});
2254-
}

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5156,7 +5156,8 @@ NeverNullType
51565156
TypeResolver::resolveIntegerTypeRepr(IntegerTypeRepr *repr,
51575157
TypeResolutionOptions options) {
51585158
if (!options.is(TypeResolverContext::ValueGenericArgument) &&
5159-
!options.is(TypeResolverContext::SameTypeRequirement)) {
5159+
!options.is(TypeResolverContext::SameTypeRequirement) &&
5160+
!options.contains(TypeResolutionFlags::SILMode)) {
51605161
diagnoseInvalid(repr, repr->getLoc(),
51615162
diag::integer_type_not_accepted);
51625163
return ErrorType::get(getASTContext());

0 commit comments

Comments
 (0)