Skip to content
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

Support 'raw mangles' via leading "\1" in pragma(mangle) strings #3854

Merged
merged 3 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,15 @@ version (IN_LLVM)
dchar c = slice[i];
if (c < 0x80)
{
version (IN_LLVM)
{
// LDC: allow leading "\1" to prevent target-specific prefix
if (i == 0 && c == '\1')
{
++i;
continue;
}
}
if (c.isValidMangling)
{
++i;
Expand Down
8 changes: 6 additions & 2 deletions gen/mangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,15 @@ std::string getIRMangledName(VarDeclaration *vd) {
}

std::string getIRMangledFuncName(std::string baseMangle, LINK link) {
return gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? std::move(baseMangle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move is not needed here (you are returning a func param), but (I think) are needed when passing to mangleFunctionForLLVM. Yes the c++ rules are tough and I had to look it up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about the ternary expression. Seems like std::move is indeed needed to avoid a full copy: https://cpp.godbolt.org/z/1e6cPzqzW

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah interesting, good thinking.

: gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
}

std::string getIRMangledVarName(std::string baseMangle, LINK link) {
return gABI->mangleVariableForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? std::move(baseMangle)
: gABI->mangleVariableForLLVM(std::move(baseMangle), link);
}

std::string getIRMangledAggregateName(AggregateDeclaration *ad,
Expand Down
11 changes: 11 additions & 0 deletions tests/codegen/mangling_raw.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Tests that 'raw mangles' starting with "\1" are correctly propagated to IR.

// RUN: %ldc -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// CHECK: @"\01my$Global" = global i32
pragma(mangle, "\1my$Global")
__gshared int myGlobal;

// CHECK: define {{.*}} @"\01my$Function"()
pragma(mangle, "\1my$Function")
void myFunction() {}