Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -4982,7 +4982,6 @@ defm second_underscore : BooleanFFlag<"second-underscore">, Group; defm sign_zero : BooleanFFlag<"sign-zero">, Group; defm stack_arrays : BooleanFFlag<"stack-arrays">, Group; -defm underscoring : BooleanFFlag<"underscoring">, Group; defm whole_file : BooleanFFlag<"whole-file">, Group; // C++ SYCL options @@ -5057,6 +5056,7 @@ defm xor_operator : OptInFC1FFlag<"xor-operator", "Enable .XOR. as a synonym of .NEQV.">; defm logical_abbreviations : OptInFC1FFlag<"logical-abbreviations", "Enable logical abbreviations">; defm implicit_none : OptInFC1FFlag<"implicit-none", "No implicit typing allowed unless overridden by IMPLICIT statements">; +defm underscoring : OptInFC1FFlag<"underscoring", "Appends one trailing underscore to external names">; def fno_automatic : Flag<["-"], "fno-automatic">, Group, HelpText<"Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE">; Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -56,7 +56,8 @@ {options::OPT_module_dir, options::OPT_fdebug_module_writer, options::OPT_fintrinsic_modules_path, options::OPT_pedantic, options::OPT_std_EQ, options::OPT_W_Joined, - options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ}); + options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ, + options::OPT_funderscoring, options::OPT_fno_underscoring}); } void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const { Index: flang/include/flang/Frontend/CodeGenOptions.def =================================================================== --- flang/include/flang/Frontend/CodeGenOptions.def +++ flang/include/flang/Frontend/CodeGenOptions.def @@ -27,6 +27,7 @@ CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module. CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level. +CODEGENOPT(Underscoring, 1, 1) ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use. #undef CODEGENOPT Index: flang/include/flang/Optimizer/Transforms/Passes.h =================================================================== --- flang/include/flang/Optimizer/Transforms/Passes.h +++ flang/include/flang/Optimizer/Transforms/Passes.h @@ -51,7 +51,8 @@ createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {}); std::unique_ptr createFirToCfgPass(); std::unique_ptr createCharacterConversionPass(); -std::unique_ptr createExternalNameConversionPass(); +std::unique_ptr +createExternalNameConversionPass(bool appendUnderscore = true); std::unique_ptr createMemDataFlowOptPass(); std::unique_ptr createPromoteToAffinePass(); std::unique_ptr createMemoryAllocationPass(); Index: flang/include/flang/Optimizer/Transforms/Passes.td =================================================================== --- flang/include/flang/Optimizer/Transforms/Passes.td +++ flang/include/flang/Optimizer/Transforms/Passes.td @@ -169,6 +169,11 @@ Demangle FIR internal name and mangle them for external interoperability. }]; let constructor = "::fir::createExternalNameConversionPass()"; + let options = [ + Option<"appendUnderscore", "append-underscore", + "bool", /*default=*/"true", + "Append trailing underscore to external names."> + ]; } def MemRefDataFlowOpt : Pass<"fir-memref-dataflow-opt", "::mlir::func::FuncOp"> { Index: flang/include/flang/Tools/CLOptions.inc =================================================================== --- flang/include/flang/Tools/CLOptions.inc +++ flang/include/flang/Tools/CLOptions.inc @@ -157,9 +157,11 @@ } #endif -inline void addExternalNameConversionPass(mlir::PassManager &pm) { - addPassConditionally(pm, disableExternalNameConversion, - [&]() { return fir::createExternalNameConversionPass(); }); +inline void addExternalNameConversionPass( + mlir::PassManager &pm, bool appendUnderscore = true) { + addPassConditionally(pm, disableExternalNameConversion, [&]() { + return fir::createExternalNameConversionPass(appendUnderscore); + }); } /// Create a pass pipeline for running default optimization passes for @@ -202,8 +204,9 @@ } #if !defined(FLANG_EXCLUDE_CODEGEN) -inline void createDefaultFIRCodeGenPassPipeline( - mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) { +inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm, + llvm::OptimizationLevel optLevel = defaultOptLevel, + bool Underscoring = true) { fir::addBoxedProcedurePass(pm); pm.addNestedPass( fir::createAbstractResultOnFuncOptPass()); @@ -211,8 +214,8 @@ fir::createAbstractResultOnGlobalOptPass()); fir::addCodeGenRewritePass(pm); fir::addTargetRewritePass(pm); - fir::addExternalNameConversionPass(pm); - fir::addFIRToLLVMPass(pm, optLevel); + fir::addExternalNameConversionPass(pm, Underscoring); + fir::addFIRToLLVMPass(pm); } /// Create a pass pipeline for lowering from MLIR to LLVM IR @@ -220,13 +223,14 @@ /// \param pm - MLIR pass manager that will hold the pipeline definition /// \param optLevel - optimization level used for creating FIR optimization /// passes pipeline -inline void createMLIRToLLVMPassPipeline( - mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) { +inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm, + llvm::OptimizationLevel optLevel = defaultOptLevel, + bool Underscoring = true) { // Add default optimizer pass pipeline. fir::createDefaultFIROptimizerPassPipeline(pm, optLevel); // Add codegen pass pipeline. - fir::createDefaultFIRCodeGenPassPipeline(pm, optLevel); + fir::createDefaultFIRCodeGenPassPipeline(pm, optLevel, Underscoring); } #undef FLANG_EXCLUDE_CODEGEN #endif Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -161,6 +161,12 @@ if (args.hasArg(clang::driver::options::OPT_pic_is_pie)) opts.IsPIE = 1; } + + // This option is compatible with -f[no-]underscoring in gfortran. + if (args.hasFlag(clang::driver::options::OPT_fno_underscoring, + clang::driver::options::OPT_funderscoring, false)) { + opts.Underscoring = 0; + } } /// Parses all target input arguments and populates the target Index: flang/lib/Frontend/FrontendActions.cpp =================================================================== --- flang/lib/Frontend/FrontendActions.cpp +++ flang/lib/Frontend/FrontendActions.cpp @@ -532,6 +532,7 @@ auto opts = ci.getInvocation().getCodeGenOpts(); llvm::OptimizationLevel level = mapToLevel(opts); + auto Underscoring = opts.Underscoring; fir::support::loadDialects(*mlirCtx); fir::support::registerLLVMTranslation(*mlirCtx); @@ -543,7 +544,7 @@ pm.enableVerifier(/*verifyPasses=*/true); // Create the pass pipeline - fir::createMLIRToLLVMPassPipeline(pm, level); + fir::createMLIRToLLVMPassPipeline(pm, level, Underscoring); mlir::applyPassManagerCLOptions(pm); // run the pass manager Index: flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp =================================================================== --- flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp +++ flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp @@ -32,11 +32,16 @@ std::string mangleExternalName(const std::pair - result) { + result, + bool appendUnderscore) { if (result.first == fir::NameUniquer::NameKind::COMMON && result.second.name.empty()) return "__BLNK__"; - return result.second.name + "_"; + + if (appendUnderscore) + return result.second.name + "_"; + + return result.second.name; } //===----------------------------------------------------------------------===// @@ -49,6 +54,10 @@ public: using OpRewritePattern::OpRewritePattern; + MangleNameOnCallOp(mlir::MLIRContext *ctx, bool appendUnderscore) + : mlir::OpRewritePattern(ctx), + appendUnderscore(appendUnderscore) {} + mlir::LogicalResult matchAndRewrite(fir::CallOp op, mlir::PatternRewriter &rewriter) const override { @@ -58,34 +67,49 @@ auto result = fir::NameUniquer::deconstruct(callee->getRootReference().getValue()); if (fir::NameUniquer::isExternalFacingUniquedName(result)) - op.setCalleeAttr( - SymbolRefAttr::get(op.getContext(), mangleExternalName(result))); + op.setCalleeAttr(SymbolRefAttr::get( + op.getContext(), mangleExternalName(result, appendUnderscore))); } rewriter.finalizeRootUpdate(op); return success(); } + +private: + bool appendUnderscore; }; struct MangleNameOnFuncOp : public mlir::OpRewritePattern { public: using OpRewritePattern::OpRewritePattern; + MangleNameOnFuncOp(mlir::MLIRContext *ctx, bool appendUnderscore) + : mlir::OpRewritePattern(ctx), + appendUnderscore(appendUnderscore) {} + mlir::LogicalResult matchAndRewrite(mlir::func::FuncOp op, mlir::PatternRewriter &rewriter) const override { rewriter.startRootUpdate(op); auto result = fir::NameUniquer::deconstruct(op.getSymName()); if (fir::NameUniquer::isExternalFacingUniquedName(result)) - op.setSymNameAttr(rewriter.getStringAttr(mangleExternalName(result))); + op.setSymNameAttr( + rewriter.getStringAttr(mangleExternalName(result, appendUnderscore))); rewriter.finalizeRootUpdate(op); return success(); } + +private: + bool appendUnderscore; }; struct MangleNameForCommonBlock : public mlir::OpRewritePattern { public: using OpRewritePattern::OpRewritePattern; + MangleNameForCommonBlock(mlir::MLIRContext *ctx, bool appendUnderscore) + : mlir::OpRewritePattern(ctx), + appendUnderscore(appendUnderscore) {} + mlir::LogicalResult matchAndRewrite(fir::GlobalOp op, mlir::PatternRewriter &rewriter) const override { @@ -93,39 +117,55 @@ auto result = fir::NameUniquer::deconstruct( op.getSymref().getRootReference().getValue()); if (fir::NameUniquer::isExternalFacingUniquedName(result)) { - auto newName = mangleExternalName(result); + auto newName = mangleExternalName(result, appendUnderscore); op.setSymrefAttr(mlir::SymbolRefAttr::get(op.getContext(), newName)); SymbolTable::setSymbolName(op, newName); } rewriter.finalizeRootUpdate(op); return success(); } + +private: + bool appendUnderscore; }; struct MangleNameOnAddrOfOp : public mlir::OpRewritePattern { public: using OpRewritePattern::OpRewritePattern; + MangleNameOnAddrOfOp(mlir::MLIRContext *ctx, bool appendUnderscore) + : mlir::OpRewritePattern(ctx), + appendUnderscore(appendUnderscore) {} + mlir::LogicalResult matchAndRewrite(fir::AddrOfOp op, mlir::PatternRewriter &rewriter) const override { auto result = fir::NameUniquer::deconstruct( op.getSymbol().getRootReference().getValue()); if (fir::NameUniquer::isExternalFacingUniquedName(result)) { - auto newName = - SymbolRefAttr::get(op.getContext(), mangleExternalName(result)); + auto newName = SymbolRefAttr::get( + op.getContext(), mangleExternalName(result, appendUnderscore)); rewriter.replaceOpWithNewOp(op, op.getResTy().getType(), newName); } return success(); } + +private: + bool appendUnderscore; }; class ExternalNameConversionPass : public fir::impl::ExternalNameConversionBase { public: + ExternalNameConversionPass(bool appendUnderscore) + : appendUnderscore(appendUnderscore) {} + mlir::ModuleOp getModule() { return getOperation(); } void runOnOperation() override; + +private: + bool appendUnderscore; }; } // namespace @@ -135,7 +175,8 @@ mlir::RewritePatternSet patterns(context); patterns.insert(context); + MangleNameForCommonBlock, MangleNameOnAddrOfOp>( + context, appendUnderscore); ConversionTarget target(*context); target.addLegalDialect fir::createExternalNameConversionPass() { - return std::make_unique(); +std::unique_ptr +fir::createExternalNameConversionPass(bool appendUnderscore) { + return std::make_unique(appendUnderscore); } Index: flang/test/Driver/driver-help-hidden.f90 =================================================================== --- flang/test/Driver/driver-help-hidden.f90 +++ flang/test/Driver/driver-help-hidden.f90 @@ -52,6 +52,7 @@ ! CHECK-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! CHECK-NEXT: -freciprocal-math Allow division operations to be reassociated ! CHECK-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages +! CHECK-NEXT: -funderscoring Appends one trailing underscore to external names ! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! CHECK-NEXT: -help Display available options ! CHECK-NEXT: -I Add directory to the end of the list of include search paths Index: flang/test/Driver/driver-help.f90 =================================================================== --- flang/test/Driver/driver-help.f90 +++ flang/test/Driver/driver-help.f90 @@ -50,6 +50,7 @@ ! HELP-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! HELP-NEXT: -freciprocal-math Allow division operations to be reassociated ! HELP-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages +! HELP-NEXT: -funderscoring Appends one trailing underscore to external names ! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! HELP-NEXT: -help Display available options ! HELP-NEXT: -I Add directory to the end of the list of include search paths @@ -135,6 +136,7 @@ ! HELP-FC1-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! HELP-FC1-NEXT: -freciprocal-math Allow division operations to be reassociated ! HELP-FC1-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages +! HELP-FC1-NEXT: -funderscoring Appends one trailing underscore to external names ! HELP-FC1-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! HELP-FC1-NEXT: -help Display available options ! HELP-FC1-NEXT: -init-only Only execute frontend initialization Index: flang/test/Driver/underscoring.f90 =================================================================== --- /dev/null +++ flang/test/Driver/underscoring.f90 @@ -0,0 +1,24 @@ +! Test the -funderscoring flag + +! RUN: %flang_fc1 -S %s -o - 2>&1 | FileCheck %s --check-prefix=UNDERSCORING +! RUN: %flang_fc1 -S -fno-underscoring %s -o - 2>&1 | FileCheck %s --check-prefix=NO-UNDERSCORING + +subroutine test() + common /comblk/ a, b + external :: ext_sub + call ext_sub() +end + +! UNDERSCORING: test_ +! UNDERSCORING: ext_sub_ +! UNDERSCORING: comblk_ + +! NO-UNDERSCORING-NOT: test_ +! NO-UNDERSCORING-NOT: _QPtest +! NO-UNDERSCORING: test +! NO-UNDERSCORING-NOT: ext_sub_ +! NO-UNDERSCORING-NOT: _QPext_sub +! NO-UNDERSCORING: ext_sub +! NO-UNDERSCORING-NOT: comblk_ +! NO-UNDERSCORING-NOT: _QBcomblk +! NO-UNDERSCORING: comblk