Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -5020,7 +5020,6 @@ defm repack_arrays : BooleanFFlag<"repack-arrays">, Group; defm second_underscore : BooleanFFlag<"second-underscore">, Group; defm sign_zero : BooleanFFlag<"sign-zero">, Group; -defm underscoring : BooleanFFlag<"underscoring">, Group; defm whole_file : BooleanFFlag<"whole-file">, Group; // C++ SYCL options @@ -5095,6 +5094,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}); Arg *stackArrays = Args.getLastArg(options::OPT_Ofast, options::OPT_fstack_arrays, Index: flang/include/flang/Frontend/CodeGenOptions.def =================================================================== --- flang/include/flang/Frontend/CodeGenOptions.def +++ flang/include/flang/Frontend/CodeGenOptions.def @@ -28,6 +28,7 @@ CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level. CODEGENOPT(StackArrays, 1, 0) ///< -fstack-arrays (enable the stack-arrays pass) +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 @@ -52,6 +52,8 @@ std::unique_ptr createFirToCfgPass(); std::unique_ptr createCharacterConversionPass(); std::unique_ptr createExternalNameConversionPass(); +std::unique_ptr +createExternalNameConversionPass(bool appendUnderscore); 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 @@ -207,8 +209,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()); @@ -216,8 +219,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 @@ -227,12 +230,12 @@ /// passes pipeline inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel, - bool stackArrays = false) { + bool stackArrays = false, bool Underscoring = true) { // Add default optimizer pass pipeline. fir::createDefaultFIROptimizerPassPipeline(pm, optLevel, stackArrays); // 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 @@ -171,6 +171,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 @@ -546,6 +546,7 @@ // Create the pass pipeline fir::createMLIRToLLVMPassPipeline(pm, level, opts.StackArrays); + fir::createMLIRToLLVMPassPipeline(pm, level, opts.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; + MangleNameOnFuncOp(mlir::MLIRContext *ctx, bool appendUnderscore) + : mlir::OpRewritePattern(ctx), + appendUnderscore(appendUnderscore) {} + mlir::LogicalResult matchAndRewrite(mlir::func::FuncOp op, mlir::PatternRewriter &rewriter) const override { @@ -56,7 +65,7 @@ rewriter.startRootUpdate(op); auto result = fir::NameUniquer::deconstruct(op.getSymName()); if (fir::NameUniquer::isExternalFacingUniquedName(result)) { - auto newSymbol = rewriter.getStringAttr(mangleExternalName(result)); + auto newSymbol = rewriter.getStringAttr(mangleExternalName(result, appendUnderscore)); // Try to update all SymbolRef's in the module that match the current op if (mlir::ModuleOp mod = op->getParentOfType()) @@ -69,12 +78,19 @@ rewriter.finalizeRootUpdate(op); return ret; } + +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 { @@ -82,39 +98,57 @@ 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 appendUnderscoring) + : appendUnderscores(appendUnderscoring) {} + + ExternalNameConversionPass() { appendUnderscores = appendUnderscore; } + mlir::ModuleOp getModule() { return getOperation(); } void runOnOperation() override; + +private: + bool appendUnderscores; }; } // namespace @@ -124,7 +158,7 @@ mlir::RewritePatternSet patterns(context); patterns.insert(context); + MangleNameOnAddrOfOp>(context, appendUnderscore); ConversionTarget target(*context); target.addLegalDialect fir::createExternalNameConversionPass() { return std::make_unique(); } + +std::unique_ptr +fir::createExternalNameConversionPass(bool appendUnderscoring) { + return std::make_unique(appendUnderscoring); +} Index: flang/test/Driver/driver-help-hidden.f90 =================================================================== --- flang/test/Driver/driver-help-hidden.f90 +++ flang/test/Driver/driver-help-hidden.f90 @@ -54,6 +54,7 @@ ! CHECK-NEXT: -freciprocal-math Allow division operations to be reassociated ! CHECK-NEXT: -fstack-arrays Attempt to allocate array temporaries on the stack, no matter their size ! 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 @@ -52,6 +52,7 @@ ! HELP-NEXT: -freciprocal-math Allow division operations to be reassociated ! HELP-NEXT: -fstack-arrays Attempt to allocate array temporaries on the stack, no matter their size ! 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 @@ -142,6 +143,7 @@ ! HELP-FC1-NEXT: -freciprocal-math Allow division operations to be reassociated ! HELP-FC1-NEXT: -fstack-arrays Attempt to allocate array temporaries on the stack, no matter their size ! 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 Index: flang/test/Fir/external-mangling.fir =================================================================== --- flang/test/Fir/external-mangling.fir +++ flang/test/Fir/external-mangling.fir @@ -1,6 +1,9 @@ -// RUN: fir-opt --external-name-interop %s | FileCheck %s -// RUN: tco --external-name-interop %s | FileCheck %s -// RUN: tco --external-name-interop %s | tco --fir-to-llvm-ir | FileCheck %s --check-prefix=LLVMIR +// RUN: fir-opt --external-name-interop="append-underscore=true" %s | FileCheck %s --check-prefix=CHECK-UNDER +// RUN: fir-opt --external-name-interop="append-underscore=false" %s | FileCheck %s --check-prefix=CHECK-NOUNDER +// RUN: tco --external-name-interop="append-underscore=true" %s | FileCheck %s --check-prefix=CHECK-UNDER +// RUN: tco --external-name-interop="append-underscore=false" %s | FileCheck %s --check-prefix=CHECK-NOUNDER +// RUN: tco --external-name-interop="append-underscore=true" %s | tco --fir-to-llvm-ir | FileCheck %s --check-prefix=LLVMIR-UNDER +// RUN: tco --external-name-interop="append-underscore=false" %s | tco --fir-to-llvm-ir | FileCheck %s --check-prefix=LLVMIR-NOUNDER func.func @_QPfoo() { %c0 = arith.constant 0 : index @@ -21,24 +24,43 @@ func.func private @_QPbar(!fir.ref) func.func private @_QPbar2(!fir.ref) -// CHECK: func @foo_ -// CHECK: %{{.*}} = fir.address_of(@a_) : !fir.ref> -// CHECK: %{{.*}} = fir.address_of(@__BLNK__) : !fir.ref> -// CHECK: fir.call @bar_ -// CHECK: fir.call @bar2_ -// CHECK: fir.global common @a_(dense<0> : vector<4xi8>) : !fir.array<4xi8> -// CHECK: fir.global common @__BLNK__(dense<0> : vector<4xi8>) : !fir.array<4xi8> -// CHECK: func private @bar_(!fir.ref) - -// LLVMIR: %{{.*}} = llvm.mlir.addressof @a_ : !llvm.ptr> -// LLVMIR: %{{.*}} = llvm.mlir.addressof @__BLNK__ : !llvm.ptr> -// LLVMIR: llvm.call @bar_(%{{.*}}) : (!llvm.ptr) -> () -// LLVMIR: llvm.call @bar2_(%{{.*}}) : (!llvm.ptr) -> () - -// LLVMIR: llvm.mlir.global common @a_(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> -// LLVMIR: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> -// LLVMIR: llvm.func @bar_(!llvm.ptr) attributes {sym_visibility = "private"} -// LLVMIR: llvm.func @bar2_(!llvm.ptr) attributes {sym_visibility = "private"} +// CHECK-UNDER: func @foo_ +// CHECK-UNDER: %{{.*}} = fir.address_of(@a_) : !fir.ref> +// CHECK-UNDER: %{{.*}} = fir.address_of(@__BLNK__) : !fir.ref> +// CHECK-UNDER: fir.call @bar_ +// CHECK-UNDER: fir.call @bar2_ +// CHECK-UNDER: fir.global common @a_(dense<0> : vector<4xi8>) : !fir.array<4xi8> +// CHECK-UNDER: fir.global common @__BLNK__(dense<0> : vector<4xi8>) : !fir.array<4xi8> +// CHECK-UNDER: func private @bar_(!fir.ref) + +// CHECK-NOUNDER: func @foo( +// CHECK-NOUNDER: %{{.*}} = fir.address_of(@a) : !fir.ref> +// CHECK-NOUNDER: %{{.*}} = fir.address_of(@__BLNK__) : !fir.ref> +// CHECK-NOUNDER: fir.call @bar( +// CHECK-NOUNDER: fir.call @bar2( +// CHECK-NOUNDER: fir.global common @a(dense<0> : vector<4xi8>) : !fir.array<4xi8> +// CHECK-NOUNDER: fir.global common @__BLNK__(dense<0> : vector<4xi8>) : !fir.array<4xi8> +// CHECK-NOUNDER: func private @bar(!fir.ref) + +// LLVMIR-UNDER: %{{.*}} = llvm.mlir.addressof @a_ : !llvm.ptr> +// LLVMIR-UNDER: %{{.*}} = llvm.mlir.addressof @__BLNK__ : !llvm.ptr> +// LLVMIR-UNDER: llvm.call @bar_(%{{.*}}) : (!llvm.ptr) -> () +// LLVMIR-UNDER: llvm.call @bar2_(%{{.*}}) : (!llvm.ptr) -> () + +// LLVMIR-UNDER: llvm.mlir.global common @a_(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> +// LLVMIR-UNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> +// LLVMIR-UNDER: llvm.func @bar_(!llvm.ptr) attributes {sym_visibility = "private"} +// LLVMIR-UNDER: llvm.func @bar2_(!llvm.ptr) attributes {sym_visibility = "private"} + +// LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @a : !llvm.ptr> +// LLVMIR-NOUNDER: %{{.*}} = llvm.mlir.addressof @__BLNK__ : !llvm.ptr> +// LLVMIR-NOUNDER: llvm.call @bar(%{{.*}}) : (!llvm.ptr) -> () +// LLVMIR-NOUNDER: llvm.call @bar2(%{{.*}}) : (!llvm.ptr) -> () + +// LLVMIR-NOUNDER: llvm.mlir.global common @a(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> +// LLVMIR-NOUNDER: llvm.mlir.global common @__BLNK__(dense<0> : vector<4xi8>) {{.*}} : !llvm.array<4 x i8> +// LLVMIR-NOUNDER: llvm.func @bar(!llvm.ptr) attributes {sym_visibility = "private"} +// LLVMIR-NOUNDER: llvm.func @bar2(!llvm.ptr) attributes {sym_visibility = "private"} // ----- @@ -52,10 +74,18 @@ return } -// CHECK: func @callee_ -// CHECK: fir.call @callee_ -// CHECK: func @caller_ -// CHECK: fir.call @callee_ +// CHECK-UNDER: func @callee_ +// CHECK-UNDER: fir.call @callee_ +// CHECK-UNDER: func @caller_ +// CHECK-UNDER: fir.call @callee_ + +// LLVMIR-UNDER: llvm.call @callee_() : () -> () +// LLVMIR-UNDER: llvm.call @callee_() : () -> () + +// CHECK-NOUNDER: func @callee( +// CHECK-NOUNDER: fir.call @callee( +// CHECK-NOUNDER: func @caller( +// CHECK-NOUNDER: fir.call @callee( -// LLVMIR: llvm.call @callee_() : () -> () -// LLVMIR: llvm.call @callee_() : () -> () +// LLVMIR-NOUNDER: llvm.call @callee() : () -> () +// LLVMIR-NOUNDER: llvm.call @callee() : () -> ()