Skip to content

Commit

Permalink
Move static init function renaming to BackendPasses.cpp
Browse files Browse the repository at this point in the history
This moves the logic for making static initialization function names
unique to `BackendPasses.cpp`

Functionality remains the same as introduced in commit
d84ee9cc74fde98f878148d16f253dec99263022. 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
  • Loading branch information
devajithvs authored and jenkins committed Jan 20, 2025
1 parent fed206a commit 1d49255
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/Interpreter/BackendPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ using namespace cling;
using namespace clang;
using namespace llvm;

namespace {
class UniqueInitFunctionNamePass
: public PassInfoMixin<UniqueInitFunctionNamePass> {
// append a suffix to a symbol to make it unique
// the suffix is "_cling_module_<module number>"
llvm::SmallString<128> add_module_suffix(const StringRef OriginalName,
const StringRef ModuleName) {
llvm::SmallString<128> NewFunctionName;
NewFunctionName.append(ModuleName);
NewFunctionName.append("_");

for (size_t i = 0; i < NewFunctionName.size(); ++i) {
// Replace everything that is not [a-zA-Z0-9._] with a _. This set
// happens to be the set of C preprocessing numbers.
if (!isPreprocessingNumberBody(NewFunctionName[i]))
NewFunctionName[i] = '_';
}

return NewFunctionName;
}

// make static initialization function names (__cxx_global_var_init) unique
bool runOnFunction(Function& F, const StringRef ModuleName) {
if (F.hasName() && F.getName().starts_with("__cxx_global_var_init")) {
F.setName(add_module_suffix(F.getName(), ModuleName));
return true;
}

return false;
}

public:
PreservedAnalyses run(llvm::Module& M, ModuleAnalysisManager& AM) {
bool changed = false;
const StringRef ModuleName = M.getName();
for (auto&& F : M)
changed |= runOnFunction(F, ModuleName);
return changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}
};
} // namespace

namespace {
class WorkAroundConstructorPriorityBugPass
: public PassInfoMixin<WorkAroundConstructorPriorityBugPass> {
Expand Down Expand Up @@ -421,6 +463,7 @@ void BackendPasses::CreatePasses(int OptLevel, llvm::ModulePassManager& MPM,

// TODO: Remove this pass once we upgrade past LLVM 19 that includes the fix.
MPM.addPass(WorkAroundConstructorPriorityBugPass());
MPM.addPass(UniqueInitFunctionNamePass());
MPM.addPass(KeepLocalGVPass());
MPM.addPass(WeakTypeinfoVTablePass());
MPM.addPass(ReuseExistingWeakSymbols(m_JIT));
Expand Down

0 comments on commit 1d49255

Please sign in to comment.