Index: clang/include/clang/Basic/CodeGenOptions.def =================================================================== --- clang/include/clang/Basic/CodeGenOptions.def +++ clang/include/clang/Basic/CodeGenOptions.def @@ -155,6 +155,7 @@ CODEGENOPT(NoNaNsFPMath , 1, 0) ///< Assume FP arguments, results not NaN. CODEGENOPT(FlushDenorm , 1, 0) ///< Allow FP denorm numbers to be flushed to zero CODEGENOPT(CorrectlyRoundedDivSqrt, 1, 0) ///< -cl-fp32-correctly-rounded-divide-sqrt +CODEGENOPT(UniqueInternalFuncNames, 1, 0) ///< Internal Linkage functions get unique names. /// When false, this attempts to generate code as if the result of an /// overflowing conversion matches the overflowing behavior of a target's native Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1952,6 +1952,12 @@ def fno_unique_section_names : Flag <["-"], "fno-unique-section-names">, Group, Flags<[CC1Option]>; +def funique_internal_funcnames : Flag <["-"], "funique-internal-funcnames">, + Group, Flags<[CC1Option]>, + HelpText<"Uniqueify Internal Linkage Function Names">; +def fno_unique_internal_funcnames : Flag <["-"], "fno-unique-internal-funcnames">, + Group, Flags<[CC1Option]>; + def fstrict_return : Flag<["-"], "fstrict-return">, Group, Flags<[CC1Option]>, HelpText<"Always treat control flow paths that fall off the end of a " Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -1101,6 +1101,23 @@ const auto *ND = cast(GD.getDecl()); std::string MangledName = getMangledNameImpl(*this, GD, ND); + // With option -funique-internal-funcnames, functions with internal linkage + // should get unique names. Use "getUniqueModuleId" to get a unique + // identifier and this is a best effort. + if (getCodeGenOpts().UniqueInternalFuncNames && + dyn_cast(GD.getDecl()) && + this->getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage && + !getModule().getSourceFileName().empty()) { + llvm::MD5 Md5; + Md5.update(getModule().getSourceFileName()); + llvm::MD5::MD5Result R; + Md5.final(R); + SmallString<32> Str; + llvm::MD5::stringifyResult(R, Str); + std::string UniqueSuffix = (".$" + Str).str(); + MangledName = MangledName + UniqueSuffix; + } + // Adjust kernel stub mangling as we may need to be able to differentiate // them from the kernel itself (e.g., for HIP). if (auto *FD = dyn_cast(GD.getDecl())) Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -4153,6 +4153,8 @@ options::OPT_fno_function_sections, options::OPT_fdata_sections, options::OPT_fno_data_sections, + options::OPT_funique_internal_funcnames, + options::OPT_fno_unique_internal_funcnames, options::OPT_funique_section_names, options::OPT_fno_unique_section_names, options::OPT_mrestrict_it, @@ -4695,6 +4697,10 @@ options::OPT_fno_unique_section_names, true)) CmdArgs.push_back("-fno-unique-section-names"); + if (Args.hasFlag(options::OPT_funique_internal_funcnames, + options::OPT_fno_unique_internal_funcnames, false)) + CmdArgs.push_back("-funique-internal-funcnames"); + Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, options::OPT_finstrument_functions_after_inlining, options::OPT_finstrument_function_entry_bare); Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -964,6 +964,9 @@ Opts.UniqueSectionNames = Args.hasFlag(OPT_funique_section_names, OPT_fno_unique_section_names, true); + Opts.UniqueInternalFuncNames = Args.hasFlag( + OPT_funique_internal_funcnames, OPT_fno_unique_internal_funcnames, false); + Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions); Opts.NoUseJumpTables = Args.hasArg(OPT_fno_jump_tables); Index: clang/test/CodeGen/unique_internal_funcnames.c =================================================================== --- /dev/null +++ clang/test/CodeGen/unique_internal_funcnames.c @@ -0,0 +1,17 @@ +// REQUIRES: x86-registered-target + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S -o - < %s | FileCheck %s --check-prefix=PLAIN +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S -funique-internal-funcnames -fno-unique-internal-funcnames -o - < %s | FileCheck %s --check-prefix=PLAIN +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S -funique-internal-funcnames -o - < %s | FileCheck %s --check-prefix=UNIQUE + +static int foo() { + return 0; +} + +int (*bar())() { + return foo; +} + +// PLAIN: foo: +// UNIQUE-NOT: foo: +// UNIQUE: foo.${{[0-9a-f]+}}: