Skip to content

Commit 5fffeb8

Browse files
authored
Merge pull request swiftlang#33128 from eeckstein/string-optimization
SIL optimizer: Add a new string optimization
2 parents 1465ee6 + 7f684b6 commit 5fffeb8

File tree

19 files changed

+823
-6
lines changed

19 files changed

+823
-6
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ class ASTContext final {
553553
/// Array.reserveCapacityForAppend(newElementsCount: Int)
554554
FuncDecl *getArrayReserveCapacityDecl() const;
555555

556+
/// Retrieve the declaration of String.init(_builtinStringLiteral ...)
557+
ConstructorDecl *getMakeUTF8StringDecl() const;
558+
556559
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
557560
FuncDecl *getIsOSVersionAtLeastDecl() const;
558561

include/swift/AST/ASTMangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ class ASTMangler : public Mangler {
213213

214214
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC);
215215

216+
/// Create a mangled name to be used for _typeName constant propagation.
217+
std::string mangleTypeForTypeName(Type type);
218+
216219
std::string mangleOpaqueTypeDescriptor(const OpaqueTypeDecl *decl);
217220

218221
std::string mangleDeclType(const ValueDecl *decl);

include/swift/AST/SemanticAttrs.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SEMANTICS_ATTR(STRING_ESCAPE_PERCENT_GET, "string.escapePercent.get")
3030
SEMANTICS_ATTR(STRING_CONCAT, "string.concat")
3131
SEMANTICS_ATTR(STRING_APPEND, "string.append")
3232
SEMANTICS_ATTR(STRING_INIT_EMPTY, "string.init_empty")
33+
SEMANTICS_ATTR(STRING_INIT_EMPTY_WITH_CAPACITY, "string.init_empty_with_capacity")
3334
SEMANTICS_ATTR(STRING_PLUS_EQUALS, "string.plusequals")
3435
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE, "findStringSwitchCase")
3536
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE_WITH_CACHE, "findStringSwitchCaseWithCache")
@@ -63,6 +64,7 @@ SEMANTICS_ATTR(ARRAY_UNINITIALIZED_INTRINSIC, "array.uninitialized_intrinsic")
6364
SEMANTICS_ATTR(ARRAY_FINALIZE_INTRINSIC, "array.finalize_intrinsic")
6465

6566
SEMANTICS_ATTR(SEQUENCE_FOR_EACH, "sequence.forEach")
67+
SEMANTICS_ATTR(TYPENAME, "typeName")
6668

6769
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_NEVER, "optimize.sil.specialize.generic.never")
6870
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER,

include/swift/Demangling/Demangle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct DemangleOptions {
6262
bool DisplayDebuggerGeneratedModule = true;
6363
bool DisplayStdlibModule = true;
6464
bool DisplayObjCModule = true;
65+
bool PrintForTypeName = false;
66+
6567
/// If this is nonempty, entities in this module name will not be qualified.
6668
llvm::StringRef HidingCurrentModule;
6769
/// A function to render generic parameter names.

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ PASS(StackPromotion, "stack-promotion",
317317
"Stack Promotion of Class Objects")
318318
PASS(StripDebugInfo, "strip-debug-info",
319319
"Strip Debug Information")
320+
PASS(StringOptimization, "string-optimization",
321+
"Optimization for String operations")
320322
PASS(SwiftArrayPropertyOpt, "array-property-opt",
321323
"Loop Specialization for Array Properties")
322324
PASS(UnsafeGuaranteedPeephole, "unsafe-guaranteed-peephole",

lib/AST/ASTContext.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/AST/PropertyWrappers.h"
4141
#include "swift/AST/ProtocolConformance.h"
4242
#include "swift/AST/RawComment.h"
43+
#include "swift/AST/SemanticAttrs.h"
4344
#include "swift/AST/SourceFile.h"
4445
#include "swift/AST/SubstitutionMap.h"
4546
#include "swift/AST/SILLayout.h"
@@ -240,6 +241,9 @@ struct ASTContext::Implementation {
240241
/// func append(Element) -> void
241242
FuncDecl *ArrayAppendElementDecl = nullptr;
242243

244+
/// init(Builtin.RawPointer, Builtin.Word, Builtin.Int1)
245+
ConstructorDecl *MakeUTF8StringDecl = nullptr;
246+
243247
/// func reserveCapacityForAppend(newElementsCount: Int)
244248
FuncDecl *ArrayReserveCapacityDecl = nullptr;
245249

@@ -1247,6 +1251,33 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
12471251
return nullptr;
12481252
}
12491253

1254+
ConstructorDecl *ASTContext::getMakeUTF8StringDecl() const {
1255+
if (getImpl().MakeUTF8StringDecl)
1256+
return getImpl().MakeUTF8StringDecl;
1257+
1258+
auto initializers =
1259+
getStringDecl()->lookupDirect(DeclBaseName::createConstructor());
1260+
1261+
for (Decl *initializer : initializers) {
1262+
auto *constructor = cast<ConstructorDecl>(initializer);
1263+
auto Attrs = constructor->getAttrs();
1264+
for (auto *A : Attrs.getAttributes<SemanticsAttr, false>()) {
1265+
if (A->Value != semantics::STRING_MAKE_UTF8)
1266+
continue;
1267+
auto ParamList = constructor->getParameters();
1268+
if (ParamList->size() != 3)
1269+
continue;
1270+
ParamDecl *param = constructor->getParameters()->get(0);
1271+
if (param->getArgumentName().str() != "_builtinStringLiteral")
1272+
continue;
1273+
1274+
getImpl().MakeUTF8StringDecl = constructor;
1275+
return constructor;
1276+
}
1277+
}
1278+
return nullptr;
1279+
}
1280+
12501281
FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
12511282
if (getImpl().IsOSVersionAtLeastDecl)
12521283
return getImpl().IsOSVersionAtLeastDecl;

lib/AST/ASTMangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
533533
return finalize();
534534
}
535535

536+
std::string ASTMangler::mangleTypeForTypeName(Type type) {
537+
beginMangling();
538+
appendType(type);
539+
return finalize();
540+
}
541+
536542
std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {
537543
DWARFMangling = true;
538544
beginMangling();

lib/Demangling/NodePrinter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,12 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
11311131
Printer << "):";
11321132
}
11331133
print(Node->getChild(1));
1134-
if (Node->getNumChildren() == 3)
1135-
print(Node->getChild(2));
1134+
if (Node->getNumChildren() == 3) {
1135+
// Currently the runtime does not mangle the generic signature.
1136+
// This is an open to-do in swift::_buildDemanglingForContext().
1137+
if (!Options.PrintForTypeName)
1138+
print(Node->getChild(2));
1139+
}
11361140
return nullptr;
11371141
case Node::Kind::Variable:
11381142
return printEntity(Node, asPrefixContext, TypePrinting::WithColon,

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ static void addSerializePipeline(SILPassPipelinePlan &P) {
524524

525525
static void addMidLevelFunctionPipeline(SILPassPipelinePlan &P) {
526526
P.startPipeline("MidLevel,Function", true /*isFunctionPassPipeline*/);
527+
528+
P.addStringOptimization();
529+
527530
addFunctionPasses(P, OptimizationLevelKind::MidLevel);
528531

529532
// Specialize partially applied functions with dead arguments as a preparation

lib/SILOptimizer/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ target_sources(swiftSILOptimizer PRIVATE
3939
Sink.cpp
4040
SpeculativeDevirtualizer.cpp
4141
StackPromotion.cpp
42+
StringOptimization.cpp
4243
TempLValueOpt.cpp
4344
TempRValueElimination.cpp
4445
UnsafeGuaranteedPeephole.cpp)

0 commit comments

Comments
 (0)