diff --git a/flang/include/flang/Lower/Bridge.h b/flang/include/flang/Lower/Bridge.h --- a/flang/include/flang/Lower/Bridge.h +++ b/flang/include/flang/Lower/Bridge.h @@ -57,11 +57,10 @@ const Fortran::parser::AllCookedSources &allCooked, llvm::StringRef triple, fir::KindMapping &kindMap, const Fortran::lower::LoweringOptions &loweringOptions, - const std::vector &envDefaults, - llvm::StringRef filePath) { + const std::vector &envDefaults) { return LoweringBridge(ctx, semanticsContext, defaultKinds, intrinsics, targetCharacteristics, allCooked, triple, kindMap, - loweringOptions, envDefaults, filePath); + loweringOptions, envDefaults); } //===--------------------------------------------------------------------===// @@ -130,8 +129,7 @@ const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple, fir::KindMapping &kindMap, const Fortran::lower::LoweringOptions &loweringOptions, - const std::vector &envDefaults, - llvm::StringRef filePath); + const std::vector &envDefaults); LoweringBridge() = delete; LoweringBridge(const LoweringBridge &) = delete; diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -171,8 +171,7 @@ ci.getInvocation().getSemanticsContext().targetCharacteristics(), ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple, kindMap, ci.getInvocation().getLoweringOpts(), - ci.getInvocation().getFrontendOpts().envDefaults, - getCurrentFileOrBufferName()); + ci.getInvocation().getFrontendOpts().envDefaults); // Fetch module from lb, so we can set mlirModule = std::make_unique(lb.getModule()); @@ -698,8 +697,8 @@ llvm::PassInstrumentationCallbacks pic; llvm::PipelineTuningOptions pto; std::optional pgoOpt; - llvm::StandardInstrumentations si( - llvmModule->getContext(), opts.DebugPassManager); + llvm::StandardInstrumentations si(llvmModule->getContext(), + opts.DebugPassManager); si.registerCallbacks(pic, &fam); llvm::PassBuilder pb(tm.get(), pto, pgoOpt, &pic); diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -58,6 +58,8 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" #include #define DEBUG_TYPE "flang-lower-bridge" @@ -3785,8 +3787,7 @@ const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple, fir::KindMapping &kindMap, const Fortran::lower::LoweringOptions &loweringOptions, - const std::vector &envDefaults, - llvm::StringRef filePath) + const std::vector &envDefaults) : semanticsContext{semanticsContext}, defaultKinds{defaultKinds}, intrinsics{intrinsics}, targetCharacteristics{targetCharacteristics}, cooked{&cooked}, context{context}, kindMap{kindMap}, @@ -3814,10 +3815,30 @@ return mlir::success(); }); + auto getPathLocation = [&semanticsContext, &context]() -> mlir::Location { + std::optional path; + const auto &allSources{semanticsContext.allCookedSources().allSources()}; + if (auto initial{allSources.GetFirstFileProvenance()}; + initial && !initial->empty()) { + if (const auto *sourceFile{allSources.GetSourceFile(initial->start())}) { + path = sourceFile->path(); + } + } + + if (path.has_value()) { + llvm::SmallString<256> curPath(*path); + llvm::sys::fs::make_absolute(curPath); + llvm::sys::path::remove_dots(curPath); + return mlir::FileLineColLoc::get(&context, curPath.str(), /*line=*/0, + /*col=*/0); + } else { + return mlir::UnknownLoc::get(&context); + } + }; + // Create the module and attach the attributes. module = std::make_unique( - mlir::ModuleOp::create(mlir::FileLineColLoc::get( - &getMLIRContext(), filePath, /*line=*/0, /*col=*/0))); + mlir::ModuleOp::create(getPathLocation())); assert(module.get() && "module was not created"); fir::setTargetTriple(*module.get(), triple); fir::setKindMapping(*module.get(), kindMap); diff --git a/flang/test/Lower/module-debug-file-loc-linux.f90 b/flang/test/Lower/module-debug-file-loc-linux.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/module-debug-file-loc-linux.f90 @@ -0,0 +1,14 @@ +! Test that the module has the location information + +! RUN: %flang_fc1 -mmlir --mlir-print-debuginfo -emit-fir -o - %s | FileCheck %s + +! REQUIRES: linux + +subroutine sb1() +end subroutine + +! CHECK: module attributes +! CHECK: func.func @_QPsb1() { +! CHECK: } +! CHECK: } loc(#[[MODULE_LOC:.*]]) +! CHECK: #[[MODULE_LOC]] = loc("/{{.*}}flang/test/Lower/module-debug-file-loc-linux.f90":0:0) diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp --- a/flang/tools/bbc/bbc.cpp +++ b/flang/tools/bbc/bbc.cpp @@ -235,7 +235,7 @@ auto burnside = Fortran::lower::LoweringBridge::create( ctx, semanticsContext, defKinds, semanticsContext.intrinsics(), semanticsContext.targetCharacteristics(), parsing.allCooked(), "", - kindMap, loweringOptions, {}, inputFilename); + kindMap, loweringOptions, {}); burnside.lower(parseTree, semanticsContext); mlir::ModuleOp mlirModule = burnside.getModule(); std::error_code ec;