[flang-commits] [flang] 2b36039 - [flang] Remove directory path from compiler remarks (#212669)
via flang-commits
flang-commits at lists.llvm.org
Thu Sep 3 06:46:32 PDT 2026
Author: John Otken
Date: 2026-09-03T14:46:26+01:00
New Revision: 2b36039c6d49b9c46e14327dbb71b30983aabcc5
URL: https://github.com/llvm/llvm-project/commit/2b36039c6d49b9c46e14327dbb71b30983aabcc5
DIFF: https://github.com/llvm/llvm-project/commit/2b36039c6d49b9c46e14327dbb71b30983aabcc5.diff
LOG: [flang] Remove directory path from compiler remarks (#212669)
flang prefixes optimization remarks with the full path to the
sourcefile. This is not handled by some test frameworks that compare
compiler output to a reference. Change remarks to only display the
source filename.
$ flang -O3 -Rpass=loop-vectorize constr26.f90
home/users/john/test/constr26.f90:6:3: remark: vectorized loop
(vectorization width: 4, interleaved count: 1) [-Rpass=loop-vectorize]
after this change:
$ flang -O3 -Rpass=loop-vectorize constr26.f90
constr26.f90:6:3: remark: vectorized loop (vectorization width: 4,
interleaved count: 1) [-Rpass=loop-vectorize]
gnu fortran output:
$ gfortran -march=native -fopt-info-vec -O3 constr26.f90
constr26.f90:6:0: note: loop vectorized
Co-authored-by: John Otken john.otken at hpe.com
Assisted-by: Copilot and Claude Opus 4.8.
---------
Co-authored-by: Tarun Prabhu <tarunprabhu at gmail.com>
Co-authored-by: John Otken <john.otken at hpe.com>
Added:
Modified:
flang/docs/ReleaseNotes.md
flang/lib/Frontend/TextDiagnosticPrinter.cpp
flang/lib/Lower/Bridge.cpp
flang/test/Driver/optimization-remark.f90
Removed:
################################################################################
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index c8d6466ac60d6..a5705fe18983c 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -37,6 +37,11 @@ page](https://llvm.org/releases/).
(OpenMP 5.2, section 3.1): `!$omx`, `c$omx` and `*$omx` in fixed source form
and `!$ompx` in free source form. These sentinels are recognized like their
`omp` counterparts when OpenMP is enabled.
+
+- Change source path in -Rpass remarks (e.g., -Rpass=loop-vectorize) from a
+ (mostly) full path to clang's behavior which is to use the source filename
+ as specified on the command line (except that ./foo.f90 removes the ./
+ prefix).
- The legacy array-value operations (`fir.array_load`, `fir.array_fetch`,
`fir.array_update`, `fir.array_modify`, `fir.array_access`,
diff --git a/flang/lib/Frontend/TextDiagnosticPrinter.cpp b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
index 33d54d9ad0b9c..9b5f939611b25 100644
--- a/flang/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/flang/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -21,7 +21,6 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
using namespace Fortran::frontend;
@@ -55,10 +54,11 @@ static void printRemarkOption(llvm::raw_ostream &os,
// For remarks only, if we are receiving a message of this format
// [file location with line and column];;[path to file];;[the remark message]
-// then print the absolute file path, line and column number.
+// then print the file path (as given on the command line), line and column
+// number.
void TextDiagnosticPrinter::printLocForRemarks(
llvm::raw_svector_ostream &diagMessageStream, llvm::StringRef &diagMsg) {
- // split incoming string to get the absolute path and filename in the
+ // split incoming string to get the location and filename in the
// case we are receiving optimization remarks from BackendRemarkConsumer
diagMsg = diagMessageStream.str();
llvm::StringRef delimiter = ";;";
@@ -73,19 +73,20 @@ void TextDiagnosticPrinter::printLocForRemarks(
// tokens will always be of size 2 in the case of optimization
// remark message received
if (tokens.size() == 2) {
- // Extract absolute path
- llvm::SmallString<128> absPath = llvm::sys::path::relative_path(tokens[1]);
- llvm::sys::path::remove_filename(absPath);
- // Add the last separator before the file name
- llvm::sys::path::append(absPath, llvm::sys::path::get_separator());
- llvm::sys::path::make_preferred(absPath);
+ // tokens[0] has the form [base file name]:[line]:[column] and tokens[1] is
+ // the full path to the file. Display the full path (as presented on the
+ // command line) with the line and column, matching Clang: relative paths
+ // (e.g. ../vec.f90, dir/vec.f90) stay relative, and absolute paths are
+ // shown in full. The ":[line]:[column]" suffix is taken from tokens[0].
+ llvm::StringRef path = tokens[1];
+ llvm::StringRef lineAndColumn = tokens[0].substr(tokens[0].find(':'));
// Used for changing only the bold attribute
if (diagOpts.showColors(os.has_colors()))
os.changeColor(llvm::raw_ostream::SAVEDCOLOR, true);
- // Print path, file name, line and column
- os << absPath << tokens[0] << ": ";
+ // Print file path, line and column
+ os << path << lineAndColumn << ": ";
}
}
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index e3d27b7bdc22c..8e33239be0f8b 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1161,7 +1161,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
static mlir::Location genLocation(Fortran::parser::SourcePosition pos,
mlir::MLIRContext &ctx) {
llvm::SmallString<256> path(*pos.path);
- llvm::sys::fs::make_absolute(path);
+ // Preserve the file path as it was presented on the command line (matching
+ // Clang): relative paths stay relative and absolute paths stay absolute.
+ // Only normalize '.' components; do not force the path to be absolute.
llvm::sys::path::remove_dots(path);
return mlir::FileLineColLoc::get(&ctx, path.str(), pos.line, pos.column);
}
diff --git a/flang/test/Driver/optimization-remark.f90 b/flang/test/Driver/optimization-remark.f90
index 76b794720e593..ad70d4bbf685e 100644
--- a/flang/test/Driver/optimization-remark.f90
+++ b/flang/test/Driver/optimization-remark.f90
@@ -82,3 +82,19 @@ subroutine swap_real(a1, a2)
end do
end subroutine swap_real
+
+! Check that the file path is displayed as it was presented on the command
+! line, matching how Clang prints optimization remarks: an absolute path is
+! shown in full and relative paths are kept relative.
+! The RUN and CHECK lines are placed at the end of the file so that adding them
+! does not shift the source line numbers referenced by the checks above.
+
+! Absolute path: %s is an absolute path, so the full path is displayed.
+! RUN: %flang %s -O2 -Rpass -S %{output} 2>&1 | FileCheck %s -DFILE=%s --check-prefix=ABSPATH
+! ABSPATH: {{^}}[[FILE]]:{{[0-9]+}}:{{[0-9]+}}: remark:
+
+! Relative path in a subdirectory: the relative path is displayed as given.
+! RUN: rm -rf %t && mkdir -p %t/sub
+! RUN: cp %s %t/sub/vec.f90
+! RUN: cd %t && %flang sub/vec.f90 -O2 -Rpass -S %{output} 2>&1 | FileCheck %s --check-prefix=SUBDIR
+! SUBDIR: {{^}}sub{{[/\\]}}vec.f90:{{[0-9]+}}:{{[0-9]+}}: remark:
More information about the flang-commits
mailing list