Skip to content

Commit 77080e4

Browse files
committed
[Mangler] Add methods for encoding already-mangled symbols.
This commit is related to the work of encoding mangled names more efficiently by compressing them. This commit adds two new methods to the mangler that allows it to identify requests to mangle strings that are already mangled. Right now the mangler does not do anything with this information. This API is needed in all of the places in the compiler where we compose mangled names. For example, when the optimizer is cloning functions it adds a prefix to an existing name. I verified that this change is correct by adding a compress/decompress methods that add a prefix to the mangled names with assertions to catch compression of already compressed symbols or decompression of non-compressed named. I plan to commit the verification code together with the compression implementation later on.
1 parent b76a58a commit 77080e4

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

include/swift/AST/Mangle.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ class Mangler {
169169
/// Adds the char \p C into the mangled name.
170170
void append(char C);
171171

172+
/// Add the already mangled symbol \p Name as an indentifier. (using the
173+
/// length prefix).
174+
void mangleIdentifierSymbol(StringRef Name);
175+
176+
/// Add the already mangled symbol \p Name. This gives the mangler the
177+
/// opportunity to decode \p Name before adding it to the mangled name.
178+
void appendSymbol(StringRef Name);
179+
172180
/// Mangle the integer \p Nat into the name.
173181
void mangleNatural(const APInt &Nat);
174182

include/swift/SIL/Mangle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SpecializationManglerBase {
8282

8383
void mangleFunctionName() {
8484
M.append("_");
85-
M.append(Function->getName());
85+
M.appendSymbol(Function->getName());
8686
}
8787
};
8888

lib/AST/Mangle.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,18 @@ void Mangler::mangleNatural(const APInt &Nat) {
18501850
Buffer << Nat;
18511851
}
18521852

1853+
void Mangler::mangleIdentifierSymbol(StringRef Name) {
1854+
// Mangle normal identifiers as:
1855+
// count identifier-char+
1856+
// where the count is the number of characters in the identifier,
1857+
// and where individual identifier characters represent themselves.
1858+
Buffer << Name.size() << Name;
1859+
}
1860+
1861+
void Mangler::appendSymbol(StringRef Name) {
1862+
Buffer << Name;
1863+
}
1864+
18531865
void Mangler::mangleGlobalVariableFull(const VarDecl *decl) {
18541866
// As a special case, Clang functions and globals don't get mangled at all.
18551867
// FIXME: When we can import C++, use Clang's mangler.

lib/IRGen/Linking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ void LinkEntity::mangle(raw_ostream &buffer) const {
299299
}
300300

301301
case Kind::SILFunction:
302-
mangler.append(getSILFunction()->getName());
302+
mangler.appendSymbol(getSILFunction()->getName());
303303
return mangler.finalize(buffer);
304304
case Kind::SILGlobalVariable:
305-
mangler.append(getSILGlobalVariable()->getName());
305+
mangler.appendSymbol(getSILGlobalVariable()->getName());
306306
return mangler.finalize(buffer);
307307
}
308308
llvm_unreachable("bad entity kind!");

lib/SIL/Mangle.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ FunctionSignatureSpecializationMangler::mangleConstantProp(LiteralInst *LI) {
146146
case ValueKind::FunctionRefInst: {
147147
SILFunction *F = cast<FunctionRefInst>(LI)->getReferencedFunction();
148148
M.append("fr");
149-
M.mangleIdentifier(F->getName());
149+
M.mangleIdentifierSymbol(F->getName());
150150
break;
151151
}
152152
case ValueKind::GlobalAddrInst: {
153153
SILGlobalVariable *G = cast<GlobalAddrInst>(LI)->getReferencedGlobal();
154154
M.append("g");
155-
M.mangleIdentifier(G->getName());
155+
M.mangleIdentifierSymbol(G->getName());
156156
break;
157157
}
158158
case ValueKind::IntegerLiteralInst: {
@@ -196,7 +196,7 @@ mangleClosureProp(PartialApplyInst *PAI) {
196196
// closure specialization if we know the function_ref in question. When this
197197
// restriction is removed, the assert here will fire.
198198
auto *FRI = cast<FunctionRefInst>(PAI->getCallee());
199-
M.mangleIdentifier(FRI->getReferencedFunction()->getName());
199+
M.mangleIdentifierSymbol(FRI->getReferencedFunction()->getName());
200200

201201
// Then we mangle the types of the arguments that the partial apply is
202202
// specializing.
@@ -216,7 +216,7 @@ void FunctionSignatureSpecializationMangler::mangleClosureProp(
216216
// closure specialization if we know the function_ref in question. When this
217217
// restriction is removed, the assert here will fire.
218218
auto *FRI = cast<FunctionRefInst>(TTTFI->getCallee());
219-
M.mangleIdentifier(FRI->getReferencedFunction()->getName());
219+
M.mangleIdentifierSymbol(FRI->getReferencedFunction()->getName());
220220
}
221221

222222
void FunctionSignatureSpecializationMangler::mangleArgument(

0 commit comments

Comments
 (0)