Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -6408,7 +6408,7 @@ def main_file_name : Separate<["-"], "main-file-name">, HelpText<"Main file name to use for debug info and source if missing">, - Flags<[CC1Option, CC1AsOption, NoDriverOption]>, + Flags<[CC1Option, CC1AsOption, NoDriverOption, FC1Option]>, MarshallingInfoString>; def split_dwarf_output : Separate<["-"], "split-dwarf-output">, HelpText<"File name to use for split dwarf debug info output">, Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -12,6 +12,7 @@ #include "clang/Driver/Options.h" #include "llvm/Frontend/Debug/Options.h" +#include "llvm/Support/Path.h" #include @@ -429,6 +430,11 @@ } const InputInfo &Input = Inputs[0]; + if (Input.isFilename()) { + CmdArgs.push_back("-main-file-name"); + CmdArgs.push_back( + Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()))); + } types::ID InputType = Input.getType(); // Add preprocessing options like -I, -D, etc. if we are using the Index: flang/include/flang/Frontend/FrontendOptions.h =================================================================== --- flang/include/flang/Frontend/FrontendOptions.h +++ flang/include/flang/Frontend/FrontendOptions.h @@ -308,6 +308,8 @@ /// \return The input kind for the extension, or Language::Unknown if the /// extension is not recognized. static InputKind getInputKindForExtension(llvm::StringRef extension); + + std::string mainFileName; }; } // namespace Fortran::frontend Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -934,6 +934,9 @@ res.frontendOpts.mlirArgs = args.getAllArgValues(clang::driver::options::OPT_mmlir); + res.frontendOpts.mainFileName = std::string( + args.getLastArgValue(clang::driver::options::OPT_main_file_name)); + success &= parseFloatingPointArgs(res, args, diags); // Set the string to be used as the return value of the COMPILER_OPTIONS Index: flang/lib/Frontend/FrontendActions.cpp =================================================================== --- flang/lib/Frontend/FrontendActions.cpp +++ flang/lib/Frontend/FrontendActions.cpp @@ -275,7 +275,10 @@ // Fetch module from lb, so we can set mlirModule = std::make_unique(lb.getModule()); - + llvm::StringRef mainFileName = + ci.getInvocation().getFrontendOpts().mainFileName; + if (!mainFileName.empty()) + mlirModule->setName(ci.getInvocation().getFrontendOpts().mainFileName); if (!setUpTargetMachine()) return false; Index: flang/test/Driver/driver-help.f90 =================================================================== --- flang/test/Driver/driver-help.f90 +++ flang/test/Driver/driver-help.f90 @@ -175,6 +175,7 @@ ! HELP-FC1-NEXT: -init-only Only execute frontend initialization ! HELP-FC1-NEXT: -I Add directory to the end of the list of include search paths ! HELP-FC1-NEXT: -load Load the named plugin (dynamic shared object) +! HELP-FC1-NEXT: -main-file-name Main file name to use for debug info and source if missing ! HELP-FC1-NEXT: -menable-no-infs Allow optimization to assume there are no infinities. ! HELP-FC1-NEXT: -menable-no-nans Allow optimization to assume there are no NaNs. ! HELP-FC1-NEXT: -mllvm Additional arguments to forward to LLVM's option processing Index: flang/test/Driver/frontend-forwarding.f90 =================================================================== --- flang/test/Driver/frontend-forwarding.f90 +++ flang/test/Driver/frontend-forwarding.f90 @@ -22,6 +22,7 @@ ! RUN: -P \ ! RUN: | FileCheck %s +! CHECK: "-main-file-name" "frontend-forwarding.f90" ! CHECK: "-P" ! CHECK: "-finput-charset=utf-8" ! CHECK: "-fdefault-double-8" Index: flang/test/Driver/mllvm.f90 =================================================================== --- flang/test/Driver/mllvm.f90 +++ flang/test/Driver/mllvm.f90 @@ -2,14 +2,14 @@ ! 1. Test typical usage. ! RUN: %flang -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT -! RUN: %flang_fc1 -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT +! RUN: %flang_fc1 -S -mllvm -print-before-all -main-file-name mllvm.f90 %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT ! 2. Test invalid usage (`-print-before` requires an argument) ! RUN: not %flang -S -mllvm -print-before %s -o - 2>&1 | FileCheck %s --check-prefix=INVALID_USAGE ! OUTPUT: *** IR Dump Before Pre-ISel Intrinsic Lowering (pre-isel-intrinsic-lowering) *** -! OUTPUT-NEXT: ; ModuleID = 'FIRModule' -! OUTPUT-NEXT: source_filename = "FIRModule" +! OUTPUT-NEXT: ; ModuleID = 'mllvm.f90' +! OUTPUT-NEXT: source_filename = "mllvm.f90" ! INVALID_USAGE: flang (LLVM option parsing): for the --print-before option: requires a value! Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp =================================================================== --- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -1585,11 +1585,12 @@ static bool getTargetEntryUniqueInfo(llvm::TargetRegionEntryInfo &targetInfo, omp::TargetOp targetOp, - llvm::StringRef parentName = "") { + llvm::StringRef parentName = "", + llvm::StringRef hostFileName = "") { auto fileLoc = targetOp.getLoc()->findInstanceOf(); - assert(fileLoc && "No file found from location"); - StringRef fileName = fileLoc.getFilename().getValue(); + StringRef fileName = + (!hostFileName.empty()) ? hostFileName : fileLoc.getFilename().getValue(); llvm::sys::fs::UniqueID id; if (auto ec = llvm::sys::fs::getUniqueID(fileName, id)) { @@ -1660,9 +1661,12 @@ llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder); StringRef parentName = opInst.getParentOfType().getName(); + StringRef fileName = ""; llvm::TargetRegionEntryInfo entryInfo; + auto moduleOp = opInst.getParentOfType(); + fileName = moduleOp.getName().value_or(""); - if (!getTargetEntryUniqueInfo(entryInfo, targetOp, parentName)) + if (!getTargetEntryUniqueInfo(entryInfo, targetOp, parentName, fileName)) return failure(); int32_t defaultValTeams = -1;