Skip to content
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
43 changes: 43 additions & 0 deletions interpreter/cling/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
12 changes: 1 addition & 11 deletions interpreter/llvm-project/clang/lib/CodeGen/CGDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,19 +540,9 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
}

// Use the module name to make the initializer unique accross modules.
SmallString<128> moduleName(TheModule.getName());
for (size_t i = 0; i < moduleName.size(); ++i) {
// Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
// to be the set of C preprocessing numbers.
if (!isPreprocessingNumberBody(moduleName[i]))
moduleName[i] = '_';
}

// Create a variable initialization function.
llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
FTy, llvm::Twine(FnName)+moduleName.str() + "_",
getTypes().arrangeNullaryFunction(), D->getLocation());
FTy, FnName.str(), getTypes().arrangeNullaryFunction(), D->getLocation());

auto *ISA = D->getAttr<InitSegAttr>();
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
Expand Down
2 changes: 1 addition & 1 deletion interpreter/llvm-project/llvm-project.tag
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ROOT-llvm18-20250113-01
ROOT-llvm18-20250117-01
Loading