Author: Kaviya Rajendiran Date: 2026-09-23T18:17:35+05:30 New Revision: dd33f57479c1d1d86649a1867b79be88ce04da73
URL: https://github.com/llvm/llvm-project/commit/dd33f57479c1d1d86649a1867b79be88ce04da73 DIFF: https://github.com/llvm/llvm-project/commit/dd33f57479c1d1d86649a1867b79be88ce04da73.diff LOG: [Flang][Driver] Added support for -funique-internal-linkage-names option (#216680) The option `-funique-internal-linkage-names` appends an MD5 hash suffix `(.__uniq.<hash>)` to internal procedure names and sets `"sample-profile-suffix-elision-policy"="selected"` on those functions, enabling the sample profiler to correctly match profiles to internal procedures across compilation units. Implementation: - Added a unique hash suffix to internal procedure names when `-funique-internal-linkage-names` is enabled. The suffix is derived from the source file path and disambiguates identically named internal procedures across different compilation units, which helps accurate sample-based profiling. - Added the function attribute `"sample-profile-suffix-elision-policy"="selected"` on internal procedures in LLVM IR. This attribute is used by the LLVM sample profile loader to control suffix stripping during profile matching Added: flang/test/Driver/funique-internal-linkage-names.f90 flang/test/Lower/unique-internal-linkage-names.f90 flang/test/Transforms/function-attrs-unique-internal-linkage-names.fir mlir/test/Target/LLVMIR/Import/sample-profile-suffix-elision-policy.ll mlir/test/Target/LLVMIR/sample-profile-suffix-elision-policy.mlir Modified: clang/include/clang/Options/Options.td clang/lib/Driver/ToolChains/Flang.cpp flang/include/flang/Frontend/CodeGenOptions.def flang/include/flang/Lower/Bridge.h flang/include/flang/Optimizer/Transforms/Passes.td flang/include/flang/Tools/CrossToolHelpers.h flang/lib/Frontend/CompilerInvocation.cpp flang/lib/Lower/Bridge.cpp flang/lib/Optimizer/Passes/Pipelines.cpp flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp flang/lib/Optimizer/Transforms/FunctionAttr.cpp mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td mlir/lib/Target/LLVMIR/ModuleImport.cpp mlir/lib/Target/LLVMIR/ModuleTranslation.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index d4bf48040029f..15b2196b68e76 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -5089,10 +5089,10 @@ defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section- NegFlag<SetFalse>>; defm unique_internal_linkage_names : BoolFOption<"unique-internal-linkage-names", CodeGenOpts<"UniqueInternalLinkageNames">, DefaultFalse, - PosFlag<SetTrue, [], [ClangOption, CC1Option], + PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Uniqueify Internal Linkage Symbol Names by appending" " the MD5 hash of the module path">, - NegFlag<SetFalse>>; + NegFlag<SetFalse, [], [ClangOption, CC1Option, FlangOption]>>; defm unique_section_names : BoolFOption<"unique-section-names", CodeGenOpts<"UniqueSectionNames">, DefaultTrue, NegFlag<SetFalse, [], [ClangOption, CC1Option], diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 95a11eb36be08..b333f0d56a395 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -358,6 +358,9 @@ void Flang::addCodegenOptions(const ArgList &Args, Args.AddLastArg(CmdArgs, options::OPT_ffp_sum_reassociation, options::OPT_fno_fp_sum_reassociation); + Args.addOptInFlag(CmdArgs, options::OPT_funique_internal_linkage_names, + options::OPT_fno_unique_internal_linkage_names); + handleInterchangeLoopsArgs(Args, CmdArgs); handleVectorizeLoopsArgs(Args, CmdArgs); handleVectorizeSLPArgs(Args, CmdArgs); diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def index a036311829e28..86994f7710576 100644 --- a/flang/include/flang/Frontend/CodeGenOptions.def +++ b/flang/include/flang/Frontend/CodeGenOptions.def @@ -61,6 +61,7 @@ CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version CODEGENOPT(DebugInfoForProfiling, 1, 0) ///< Emit extra debug info to make sample profile more accurate. CODEGENOPT(PseudoProbeForProfiling, 1, 0) ///< Emit pseudo probes for sample profiling. +CODEGENOPT(UniqueInternalLinkageNames, 1, 0) ///< Append MD5 hash to internal linkage symbols. CODEGENOPT(Underscoring, 1, 1) ENUM_CODEGENOPT(FPMaxminBehavior, Fortran::common::FPMaxminBehavior, 2, Fortran::common::FPMaxminBehavior::Legacy) diff --git a/flang/include/flang/Lower/Bridge.h b/flang/include/flang/Lower/Bridge.h index dbddef7b1169d..f8421f27e6ec2 100644 --- a/flang/include/flang/Lower/Bridge.h +++ b/flang/include/flang/Lower/Bridge.h @@ -24,6 +24,7 @@ #include "flang/Support/Fortran.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/OwningOpRef.h" +#include "llvm/ProfileData/SampleProf.h" #include <set> namespace llvm { @@ -118,6 +119,8 @@ class LoweringBridge { return languageFeatures; } + const std::string &getModuleNameHash() const { return moduleNameHash; } + /// Create a folding context. Careful: this is very expensive. Fortran::evaluate::FoldingContext createFoldingContext(); @@ -180,6 +183,7 @@ class LoweringBridge { const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults; const Fortran::common::LanguageFeatureControl &languageFeatures; std::set<std::string> tempNames; + std::string moduleNameHash; std::optional<mlir::DiagnosticEngine::HandlerID> diagHandlerID; }; diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td index 619f44e53bac5..f857e19f4ae46 100644 --- a/flang/include/flang/Optimizer/Transforms/Passes.td +++ b/flang/include/flang/Optimizer/Transforms/Passes.td @@ -527,6 +527,10 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> { /*default=*/"false", "Set the disable-tail-calls attribute on functions to prevent " "tail call optimization.">, + Option<"UniqueInternalLinkageNames", "unique-internal-linkage-names", + "bool", /*default=*/"false", + "Set the sample-profile-suffix-elision-policy attribute on " + "internal linkage functions in the module.">, Option<"tuneCPU", "tune-cpu", "std::string", /*default=*/"", "Set the tune-cpu attribute on functions in the module.">, Option<"setNoCapture", "set-nocapture", "bool", /*default=*/"false", diff --git a/flang/include/flang/Tools/CrossToolHelpers.h b/flang/include/flang/Tools/CrossToolHelpers.h index 692b7fd7129f2..b9add141663b9 100644 --- a/flang/include/flang/Tools/CrossToolHelpers.h +++ b/flang/include/flang/Tools/CrossToolHelpers.h @@ -138,6 +138,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks { Reciprocals = opts.Reciprocals; PreferVectorWidth = opts.PreferVectorWidth; UseSampleProfile = !opts.SampleProfileFile.empty(); + UniqueInternalLinkageNames = opts.UniqueInternalLinkageNames; DebugInfoForProfiling = opts.DebugInfoForProfiling; if (opts.InstrumentFunctions) { InstrumentFunctionEntry = "__cyg_profile_func_enter"; @@ -178,6 +179,8 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks { bool EnableOpenMPIsTargetDevice = false; ///< Compiling for an OpenMP target device. bool UseSampleProfile = false; ///< Enable sample based profiling + bool UniqueInternalLinkageNames = false; ///< Append MD5 hash suffix to + ///< internal linkage symbol names. bool DebugInfoForProfiling = false; ///< Enable extra debugging info bool DisableTailCalls = false; ///< Disable tail call optimization bool EnableOpenMPSimd = false; ///< Enable OpenMP simd-only mode. diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 85e5f477dcb8c..1dd28e4f0a99b 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -327,6 +327,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts, args.hasFlag(clang::options::OPT_floop_interchange, clang::options::OPT_fno_loop_interchange, true); + if (args.hasArg(clang::options::OPT_funique_internal_linkage_names)) + opts.UniqueInternalLinkageNames = 1; + if (args.getLastArg(clang::options::OPT_fexperimental_loop_fusion)) opts.FuseLoops = 1; diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp index 784a87cc61997..968cdbc951ecc 100644 --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -1239,9 +1239,15 @@ class FirConverter : public Fortran::lower::AbstractConverter { } std::string mangleName(const Fortran::semantics::Symbol &symbol) override final { - return Fortran::lower::mangle::mangleName( + std::string mangledName = Fortran::lower::mangle::mangleName( symbol, scopeBlockIdMap, /*keepExternalInScope=*/false, getLoweringOptions().getUnderscoring()); + const std::string &hash = bridge.getModuleNameHash(); + if (!hash.empty() && + Fortran::semantics::ClassifyProcedure(symbol) == + Fortran::semantics::ProcedureDefinitionClass::Internal) + mangledName += hash; + return mangledName; } std::string mangleName( const Fortran::semantics::DerivedTypeSpec &derivedType) override final { @@ -7136,6 +7142,13 @@ Fortran::lower::LoweringBridge::LoweringBridge( else if (languageFeatures.IsEnabled( Fortran::common::LanguageFeature::CudaManaged)) fir::setCudaHeapAllocMode(*module, fir::CudaHeapAllocMode::Managed); + + if (cgOpts.UniqueInternalLinkageNames) { + if (auto fileLoc = mlir::dyn_cast<mlir::FileLineColLoc>(module->getLoc())) { + moduleNameHash = + llvm::getUniqueInternalLinkagePostfix(fileLoc.getFilename()); + } + } } Fortran::lower::LoweringBridge::~LoweringBridge() { diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp index b73a1ce4a47c6..fc5b5ef6460a9 100644 --- a/flang/lib/Optimizer/Passes/Pipelines.cpp +++ b/flang/lib/Optimizer/Passes/Pipelines.cpp @@ -464,8 +464,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm, config.InstrumentFunctionExit, config.NoInfsFPMath, config.NoNaNsFPMath, config.ApproxFuncFPMath, config.NoSignedZerosFPMath, config.UnsafeFPMath, config.Reciprocals, config.PreferVectorWidth, config.UseSampleProfile, - config.DisableTailCalls, /*tuneCPU=*/"", setNoCapture, setNoAlias, - setReadOnly})); + config.DisableTailCalls, config.UniqueInternalLinkageNames, + /*tuneCPU=*/"", setNoCapture, setNoAlias, setReadOnly})); if (config.EnableOpenMP) { pm.addNestedPass<mlir::func::FuncOp>( diff --git a/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp b/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp index 7a173da514b16..ca33639bbb888 100644 --- a/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp +++ b/flang/lib/Optimizer/Transforms/CompilerGeneratedNames.cpp @@ -45,6 +45,8 @@ void CompilerGeneratedNamesConversionPass::runOnOperation() { auto processOp = [&](mlir::Operation &op) { auto symName = mlir::cast<mlir::SymbolOpInterface>(&op).getNameAttr(); + if (symName.getValue().contains(".__uniq.")) + return; auto deconstructedName = fir::NameUniquer::deconstruct(symName); if (deconstructedName.first != fir::NameUniquer::NameKind::NOT_UNIQUED && !fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) { diff --git a/flang/lib/Optimizer/Transforms/FunctionAttr.cpp b/flang/lib/Optimizer/Transforms/FunctionAttr.cpp index 1aadd16fe1cf4..958a9ea87cd6d 100644 --- a/flang/lib/Optimizer/Transforms/FunctionAttr.cpp +++ b/flang/lib/Optimizer/Transforms/FunctionAttr.cpp @@ -144,6 +144,13 @@ void FunctionAttrPass::runOnOperation() { context, mlir::LLVM::LLVMFuncOp::getUseSampleProfileAttrName( llvmFuncOpName)), mlir::BoolAttr::get(context, true)); + if (UniqueInternalLinkageNames && fir::isInternalProcedure(func)) + func->setAttr( + getLlvmFuncPropertyAttrName( + context, + mlir::LLVM::LLVMFuncOp::getSampleProfileSuffixElisionPolicyAttrName( + llvmFuncOpName)), + mlir::StringAttr::get(context, "selected")); if (disableTailCalls) func->setAttr( diff --git a/flang/test/Driver/funique-internal-linkage-names.f90 b/flang/test/Driver/funique-internal-linkage-names.f90 new file mode 100644 index 0000000000000..d81e405cff541 --- /dev/null +++ b/flang/test/Driver/funique-internal-linkage-names.f90 @@ -0,0 +1,14 @@ +! Test that -funique-internal-linkage-names / -fno-unique-internal-linkage-names are forwarded to flang -fc1. + +! RUN: %flang -### %s 2>&1 | FileCheck %s --check-prefix=DISABLED +! RUN: %flang -### -funique-internal-linkage-names %s 2>&1 | FileCheck %s --check-prefix=ENABLED +! RUN: %flang -### -fno-unique-internal-linkage-names %s 2>&1 | FileCheck %s --check-prefix=DISABLED +! RUN: %flang -### -funique-internal-linkage-names -fno-unique-internal-linkage-names %s 2>&1 | FileCheck %s --check-prefix=DISABLED +! RUN: %flang -### -fno-unique-internal-linkage-names -funique-internal-linkage-names %s 2>&1 | FileCheck %s --check-prefix=ENABLED + +! DISABLED: "-fc1" +! DISABLED-NOT: "-funique-internal-linkage-names" +! DISABLED-NOT: "-fno-unique-internal-linkage-names" + +! ENABLED: "-fc1" +! ENABLED-SAME: "-funique-internal-linkage-names" diff --git a/flang/test/Lower/unique-internal-linkage-names.f90 b/flang/test/Lower/unique-internal-linkage-names.f90 new file mode 100644 index 0000000000000..7699212ddff3a --- /dev/null +++ b/flang/test/Lower/unique-internal-linkage-names.f90 @@ -0,0 +1,17 @@ +! Test that -funique-internal-linkage-names appends a .__uniq. hash suffix +! to internal procedures at the FIR level. + +! RUN: %flang_fc1 -emit-fir -funique-internal-linkage-names -o - %s | FileCheck %s + +! CHECK-LABEL: func.func @_QPtest +! CHECK: fir.call @_QFtestPfoo.__uniq.{{[0-9]+}} + +! CHECK: func.func private @_QFtestPfoo.__uniq.{{[0-9]+}} +! CHECK-SAME: attributes {fir.host_symbol = @_QPtest, llvm.linkage = #llvm.linkage<internal>} + +subroutine test() + call foo() +contains + subroutine foo() + end subroutine +end subroutine diff --git a/flang/test/Transforms/function-attrs-unique-internal-linkage-names.fir b/flang/test/Transforms/function-attrs-unique-internal-linkage-names.fir new file mode 100644 index 0000000000000..9d5cf40ea6e35 --- /dev/null +++ b/flang/test/Transforms/function-attrs-unique-internal-linkage-names.fir @@ -0,0 +1,26 @@ +// RUN: fir-opt --function-attr="unique-internal-linkage-names=true" %s | FileCheck %s --check-prefix=ENABLED +// RUN: fir-opt --function-attr="unique-internal-linkage-names=false" %s | FileCheck %s --check-prefix=DISABLED + +// Internal procedure: has fir.host_symbol, should get the attribute. +// ENABLED-LABEL: func.func @_QFhost_subPinner( +// ENABLED-SAME: llvm.sample_profile_suffix_elision_policy = "selected" + +// DISABLED-LABEL: func.func @_QFhost_subPinner( +// DISABLED-NOT: sample_profile_suffix_elision_policy +func.func @_QFhost_subPinner(%arg0: !fir.ref<i32>) attributes {fir.host_symbol = @_QFhost_sub} { + return +} + +// Host procedure: no fir.host_symbol, should NOT get the attribute. +// ENABLED-LABEL: func.func @_QFhost_sub( +// ENABLED-NOT: sample_profile_suffix_elision_policy +func.func @_QFhost_sub(%arg0: !fir.ref<i32>) { + return +} + +// External procedure: should NOT get the attribute. +// ENABLED-LABEL: func.func @_QPexternal_sub( +// ENABLED-NOT: sample_profile_suffix_elision_policy +func.func @_QPexternal_sub(%arg0: !fir.ref<i32>) { + return +} diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td index 5998bd01f9bdf..c8efc741d0acb 100644 --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -2160,7 +2160,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [ OptionalAttr<LLVM_FunctionMetadataArrayAttr>:$function_metadata, OptionalAttr<UWTableKindAttr>:$uwtable_kind, OptionalAttr<BoolAttr>:$use_sample_profile, - OptionalAttr<BoolAttr>:$disable_tail_calls + OptionalAttr<BoolAttr>:$disable_tail_calls, + OptionalAttr<StrAttr>:$sample_profile_suffix_elision_policy ); let regions = (region AnyRegion:$body); diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp index bfecde8b664a9..04f69433b97a0 100644 --- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp @@ -2927,6 +2927,7 @@ static constexpr std::array kExplicitLLVMFuncOpAttributes{ StringLiteral("save-reg-params"), StringLiteral("target-features"), StringLiteral("trap-func-name"), + StringLiteral("sample-profile-suffix-elision-policy"), StringLiteral("tune-cpu"), StringLiteral("uniform-work-group-size"), StringLiteral("uwtable"), @@ -3104,6 +3105,12 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func, << "unknown value '" << val << "' for 'disable-tail-calls' attribute"; } + if (llvm::Attribute attr = + func->getFnAttribute("sample-profile-suffix-elision-policy"); + attr.isStringAttribute()) + funcOp.setSampleProfileSuffixElisionPolicy( + StringAttr::get(context, attr.getValueAsString())); + if (llvm::Attribute attr = func->getFnAttribute("target-cpu"); attr.isStringAttribute()) funcOp.setTargetCpuAttr(StringAttr::get(context, attr.getValueAsString())); diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index 50365b5a5b3a1..bd11a493f4bd3 100644 --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -1746,6 +1746,11 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { llvmFunc->addFnAttr("disable-tail-calls", llvm::toStringRef(*disableTailCalls)); + if (auto sampleProfileSuffixElisionPolicy = + func.getSampleProfileSuffixElisionPolicy()) + llvmFunc->addFnAttr("sample-profile-suffix-elision-policy", + *sampleProfileSuffixElisionPolicy); + if (auto attr = func.getVscaleRange()) llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs( getLLVMContext(), attr->getMinRange().getInt(), diff --git a/mlir/test/Target/LLVMIR/Import/sample-profile-suffix-elision-policy.ll b/mlir/test/Target/LLVMIR/Import/sample-profile-suffix-elision-policy.ll new file mode 100644 index 0000000000000..8f9a59fea827c --- /dev/null +++ b/mlir/test/Target/LLVMIR/Import/sample-profile-suffix-elision-policy.ll @@ -0,0 +1,15 @@ +; RUN: mlir-translate -import-llvm %s | FileCheck %s + +; CHECK-LABEL: llvm.func @with_elision_policy() +; CHECK-SAME: sample_profile_suffix_elision_policy = "selected" +define void @with_elision_policy() #0 { + ret void +} + +; CHECK-LABEL: llvm.func @without_elision_policy() +; CHECK-NOT: sample_profile_suffix_elision_policy +define void @without_elision_policy() { + ret void +} + +attributes #0 = { "sample-profile-suffix-elision-policy"="selected" } diff --git a/mlir/test/Target/LLVMIR/sample-profile-suffix-elision-policy.mlir b/mlir/test/Target/LLVMIR/sample-profile-suffix-elision-policy.mlir new file mode 100644 index 0000000000000..4953620571318 --- /dev/null +++ b/mlir/test/Target/LLVMIR/sample-profile-suffix-elision-policy.mlir @@ -0,0 +1,14 @@ +// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s + +// CHECK: define void @with_elision_policy() #[[ATTRS_WITH:.*]] { +llvm.func @with_elision_policy() attributes {sample_profile_suffix_elision_policy = "selected"} { + llvm.return +} + +// CHECK: define void @without_elision_policy() { +// CHECK-NOT: "sample-profile-suffix-elision-policy" +llvm.func @without_elision_policy() { + llvm.return +} + +// CHECK: attributes #[[ATTRS_WITH]] = { "sample-profile-suffix-elision-policy"="selected" } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
