Skip to content

Commit 4bcc584

Browse files
committed
[cling] Move static init function renaming to BackendPasses.cpp
This moves the logic for making static initialization function names unique to `BackendPasses.cpp` Functionality remains the same as introduced in commit d84ee9c. The only difference being the the names are suffixed with numbering (_1, _2, etc.) before the module suffix: __cxx_global_var_initcling_module_27_ __cxx_global_var_init_1cling_module_27_ __cxx_global_var_init_2cling_module_27_ __cxx_global_var_init_3cling_module_27_ instead of __cxx_global_var_initcling_module_27_ __cxx_global_var_initcling_module_27__1 __cxx_global_var_initcling_module_27__2 __cxx_global_var_initcling_module_27__3
1 parent eb1ae84 commit 4bcc584

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

interpreter/cling/lib/Interpreter/BackendPasses.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,48 @@ using namespace cling;
4040
using namespace clang;
4141
using namespace llvm;
4242

43+
namespace {
44+
class UniqueInitFunctionNamePass
45+
: public PassInfoMixin<UniqueInitFunctionNamePass> {
46+
// append a suffix to a symbol to make it unique
47+
// the suffix is "_cling_module_<module number>"
48+
llvm::SmallString<128> add_module_suffix(const StringRef OriginalName,
49+
const StringRef ModuleName) {
50+
llvm::SmallString<128> NewFunctionName;
51+
NewFunctionName.append(ModuleName);
52+
NewFunctionName.append("_");
53+
54+
for (size_t i = 0; i < NewFunctionName.size(); ++i) {
55+
// Replace everything that is not [a-zA-Z0-9._] with a _. This set
56+
// happens to be the set of C preprocessing numbers.
57+
if (!isPreprocessingNumberBody(NewFunctionName[i]))
58+
NewFunctionName[i] = '_';
59+
}
60+
61+
return NewFunctionName;
62+
}
63+
64+
// make static initialization function names (__cxx_global_var_init) unique
65+
bool runOnFunction(Function& F, const StringRef ModuleName) {
66+
if (F.hasName() && F.getName().starts_with("__cxx_global_var_init")) {
67+
F.setName(add_module_suffix(F.getName(), ModuleName));
68+
return true;
69+
}
70+
71+
return false;
72+
}
73+
74+
public:
75+
PreservedAnalyses run(llvm::Module& M, ModuleAnalysisManager& AM) {
76+
bool changed = false;
77+
const StringRef ModuleName = M.getName();
78+
for (auto&& F : M)
79+
changed |= runOnFunction(F, ModuleName);
80+
return changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
81+
}
82+
};
83+
} // namespace
84+
4385
namespace {
4486
class WorkAroundConstructorPriorityBugPass
4587
: public PassInfoMixin<WorkAroundConstructorPriorityBugPass> {
@@ -421,6 +463,7 @@ void BackendPasses::CreatePasses(int OptLevel, llvm::ModulePassManager& MPM,
421463

422464
// TODO: Remove this pass once we upgrade past LLVM 19 that includes the fix.
423465
MPM.addPass(WorkAroundConstructorPriorityBugPass());
466+
MPM.addPass(UniqueInitFunctionNamePass());
424467
MPM.addPass(KeepLocalGVPass());
425468
MPM.addPass(WeakTypeinfoVTablePass());
426469
MPM.addPass(ReuseExistingWeakSymbols(m_JIT));

0 commit comments

Comments
 (0)