-
-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add experimental -hash-threshold option to hash very long symbol names. #1445
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//===-- mangling.cpp ------------------------------------------------------===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the BSD-style LDC license. See the LICENSE | ||
// file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Tries to centralize functionality for mangling of symbols. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gen/mangling.h" | ||
|
||
#include "ddmd/declaration.h" | ||
#include "ddmd/dsymbol.h" | ||
#include "ddmd/identifier.h" | ||
#include "ddmd/module.h" | ||
#include "gen/abi.h" | ||
#include "gen/irstate.h" | ||
#include "llvm/Support/MD5.h" | ||
|
||
namespace { | ||
|
||
// TODO: Disable hashing of symbols that are defined in libdruntime and | ||
// libphobos. This would enable hashing thresholds below the largest symbol in | ||
// libdruntime/phobos. | ||
|
||
bool shouldHashAggrName(llvm::StringRef name) { | ||
/// Add extra chars to the length of aggregate names to account for | ||
/// the additional D mangling suffix and prefix | ||
return (global.params.hashThreshold != 0) && | ||
((name.size() + 11) > global.params.hashThreshold); | ||
} | ||
|
||
llvm::SmallString<32> hashName(llvm::StringRef name) { | ||
llvm::MD5 hasher; | ||
hasher.update(name); | ||
llvm::MD5::MD5Result result; | ||
hasher.final(result); | ||
llvm::SmallString<32> hashStr; | ||
llvm::MD5::stringifyResult(result, hashStr); | ||
|
||
return hashStr; | ||
} | ||
|
||
/// Hashes the symbol name and prefixes the hash with some recognizable parts of | ||
/// the full symbol name. The prefixing means that the hashed name may be larger | ||
/// than the input when identifiers are very long and the hash threshold is low. | ||
/// Demangled hashed name is: | ||
/// module.L<line_no>.<hash>.<top aggregate>.<identifier> | ||
std::string hashSymbolName(llvm::StringRef name, Dsymbol *symb) { | ||
std::string ret; | ||
|
||
// module | ||
{ | ||
auto moddecl = symb->getModule()->md; | ||
assert(moddecl); | ||
if (auto packages = moddecl->packages) { | ||
for (size_t i = 0; i < packages->dim; ++i) { | ||
llvm::StringRef str = (*packages)[i]->toChars(); | ||
ret += std::to_string(str.size()); | ||
ret += str; | ||
} | ||
} | ||
llvm::StringRef str = moddecl->id->toChars(); | ||
ret += std::to_string(str.size()); | ||
ret += str; | ||
} | ||
|
||
// source line number | ||
auto lineNo = std::to_string(symb->loc.linnum); | ||
ret += std::to_string(lineNo.size()+1); | ||
ret += 'L'; | ||
ret += lineNo; | ||
|
||
// MD5 hash | ||
auto hashedName = hashName(name); | ||
ret += "33_"; // add underscore to delimit the 33 character count | ||
ret += hashedName; | ||
|
||
// top aggregate | ||
if (auto agg = symb->isAggregateMember()) { | ||
llvm::StringRef topaggr = agg->ident->toChars(); | ||
ret += std::to_string(topaggr.size()); | ||
ret += topaggr; | ||
} | ||
|
||
// identifier | ||
llvm::StringRef identifier = symb->toChars(); | ||
ret += std::to_string(identifier.size()); | ||
ret += identifier; | ||
|
||
return ret; | ||
} | ||
} | ||
|
||
std::string getMangledName(FuncDeclaration *fdecl, LINK link) { | ||
std::string mangledName(mangleExact(fdecl)); | ||
|
||
// Hash the name if necessary | ||
if (((link == LINKd) || (link == LINKdefault)) && | ||
(global.params.hashThreshold != 0) && | ||
(mangledName.length() > global.params.hashThreshold)) { | ||
|
||
auto hashedName = hashSymbolName(mangledName, fdecl); | ||
mangledName = "_D" + hashedName + "Z"; | ||
} | ||
|
||
return gABI->mangleForLLVM(mangledName, link); | ||
} | ||
|
||
std::string getMangledInitSymbolName(AggregateDeclaration *aggrdecl) { | ||
std::string ret = "_D"; | ||
|
||
std::string mangledName = mangle(aggrdecl); | ||
if (shouldHashAggrName(mangledName)) { | ||
ret += hashSymbolName(mangledName, aggrdecl); | ||
} else { | ||
ret += mangledName; | ||
} | ||
|
||
ret += "6__initZ"; | ||
|
||
return ret; | ||
} | ||
|
||
std::string getMangledVTableSymbolName(AggregateDeclaration *aggrdecl) { | ||
std::string ret = "_D"; | ||
|
||
std::string mangledName = mangle(aggrdecl); | ||
if (shouldHashAggrName(mangledName)) { | ||
ret += hashSymbolName(mangledName, aggrdecl); | ||
} else { | ||
ret += mangledName; | ||
} | ||
|
||
ret += "6__vtblZ"; | ||
|
||
return ret; | ||
} | ||
|
||
std::string getMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl) { | ||
std::string ret = "_D"; | ||
|
||
std::string mangledName = mangle(aggrdecl); | ||
if (shouldHashAggrName(mangledName)) { | ||
ret += hashSymbolName(mangledName, aggrdecl); | ||
} else { | ||
ret += mangledName; | ||
} | ||
|
||
if (aggrdecl->isInterfaceDeclaration()) { | ||
ret += "11__InterfaceZ"; | ||
} else { | ||
ret += "7__ClassZ"; | ||
} | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===-- mangling.h --------------------------------------------------------===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the BSD-style LDC license. See the LICENSE | ||
// file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Tries to centralize functionality for mangling of symbols. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LDC_GEN_MANGLING_H | ||
#define LDC_GEN_MANGLING_H | ||
|
||
#include <string> | ||
#include "ddmd/globals.h" | ||
|
||
class FuncDeclaration; | ||
class AggregateDeclaration; | ||
|
||
std::string getMangledName(FuncDeclaration *fdecl, LINK link); | ||
|
||
std::string getMangledInitSymbolName(AggregateDeclaration *aggrdecl); | ||
std::string getMangledVTableSymbolName(AggregateDeclaration *aggrdecl); | ||
std::string getMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl); | ||
|
||
#endif // LDC_GEN_MANGLING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Test hashing of symbols above hash threshold | ||
|
||
// RUN: %ldc -hash-threshold=90 -g -c -output-ll -of=%t90.ll %s && FileCheck %s --check-prefix HASH90 < %t90.ll | ||
// RUN: %ldc -hash-threshold=90 -run %s | ||
|
||
// Don't use Phobos functions in this test, because the test hashthreshold is too low for an unhashed libphobos. | ||
|
||
module one.two.three; | ||
|
||
// HASH90-DAG: define{{.*}} @externCfunctions_are_not_hashed_externCfunctions_are_not_hashed_externCfunctions_are_not_hashed | ||
extern (C) int externCfunctions_are_not_hashed_externCfunctions_are_not_hashed_externCfunctions_are_not_hashed() | ||
{ | ||
return 95; | ||
} | ||
|
||
auto s(T)(T t) | ||
{ | ||
// HASH90-DAG: define{{.*}} @_D3one3two5three8__T1sTiZ1sFNaNbNiNfiZS3one3two5three8__T1sTiZ1sFiZ13__T6ResultTiZ6Result | ||
// HASH90-DAG: define{{.*}} @_D3one3two5three3L1633_699ccf279a146992d539ca3ca16e22e11sZ | ||
// HASH90-DAG: define{{.*}} @_D3one3two5three3L2333_5ee632e10b6f09e8f541a143266bdf226Result3fooZ | ||
struct Result(T) | ||
{ | ||
void foo(){} | ||
} | ||
return Result!int(); | ||
} | ||
|
||
auto klass(T)(T t) | ||
{ | ||
class Result(T) | ||
{ | ||
// HASH90-DAG: define{{.*}} @_D3one3two5three12__T5klassTiZ5klassFiZ13__T6ResultTiZ6Result3fooMFZv | ||
// HASH90-DAG: define{{.*}} @_D3one3two5three3L3433_46a82aac733d8a4b3588d7fa8937aad66Result3fooZ | ||
void foo(){} | ||
} | ||
return new Result!int(); | ||
} | ||
|
||
void main() | ||
{ | ||
assert( | ||
externCfunctions_are_not_hashed_externCfunctions_are_not_hashed_externCfunctions_are_not_hashed() == 95); | ||
|
||
auto x = 1.s.s.s.s; | ||
x.foo; | ||
|
||
auto y = 1.klass.klass.klass.klass; | ||
y.foo; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems rather unfortunate. Perhaps we should ditch IN_LLVM for such cases, or replace it by an enum set from the version (so you can do
if (IN_LLVM && …
). It doesn't seem like we would ever want to try using LDC's front end sources to build DMD…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. We already have the enum IN_LLVM, so I'll use that. I also think the copying is ugly/stupid.
I will indent the DDMD source, so that we are notified of (perhaps relevant) changes by merge errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I'd probably keep the indentation as it is, so that the diff is kept tidy and the LDC-specific part is made painfully obvious when browsing the source. But I guess one could always use
diff -w
for the former…