[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-21 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur updated 
https://github.com/llvm/llvm-project/pull/196558

>From 9a2dc9c9ab9db05fba11cd32a9457e89a3804402 Mon Sep 17 00:00:00 2001
From: Michael Kruse 
Date: Fri, 8 May 2026 17:12:13 +0200
Subject: [PATCH 01/10] [Flang][Driver] Add per-target search path for modules

---
 clang/include/clang/Driver/ToolChain.h|  4 ++
 clang/lib/Driver/Driver.cpp   | 11 
 clang/lib/Driver/ToolChain.cpp|  6 ++
 clang/lib/Driver/ToolChains/Flang.cpp | 31 ++
 .../flang/Frontend/CompilerInvocation.h   |  7 +++
 flang/lib/Frontend/CompilerInvocation.cpp | 14 +
 flang/test/Driver/Inputs/ieee_arithmetic.mod  |  1 +
 flang/test/Driver/Inputs/iso_fortran_env.mod  |  1 +
 .../basictestmoduleone.mod|  0
 .../flang/x86_64-unknown-linux-gnu/.keep  |  0
 .../intrinsic-module-path_per_target.f90  | 56 +++
 flang/test/Driver/use-module.f90  | 12 ++--
 flang/tools/bbc/bbc.cpp   |  5 ++
 13 files changed, 142 insertions(+), 6 deletions(-)
 rename flang/test/Driver/Inputs/{ => module-dir-one}/basictestmoduleone.mod 
(100%)
 create mode 100644 
flang/test/Driver/Inputs/resource_dir_with_per_target_subdir/finclude/flang/x86_64-unknown-linux-gnu/.keep
 create mode 100644 flang/test/Driver/intrinsic-module-path_per_target.f90

diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index 8bda212312aee..ff044722369cc 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -560,6 +560,10 @@ class ToolChain {
   // Returns Triple without the OSs version.
   llvm::Triple getTripleWithoutOSVersion() const;
 
+  /// Returns the target-specific path for Flang's intrinsic modules in the
+  /// resource directory if it exists.
+  std::optional getDefaultIntrinsicModuleDir() const;
+
   // Returns the target specific runtime path if it exists.
   std::optional getRuntimePath() const;
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b9c072a0a0edb..279dc78309bef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6766,6 +6766,17 @@ std::string Driver::GetFilePath(StringRef Name, const 
ToolChain &TC) const {
   if (llvm::sys::fs::exists(Twine(P)))
 return std::string(P);
 
+  // With Flang, also look for intrinsic modules
+  if (IsFlangMode()) {
+if (std::optional IntrPath =
+TC.getDefaultIntrinsicModuleDir()) {
+  SmallString<128> P(*IntrPath);
+  llvm::sys::path::append(P, Name);
+  if (llvm::sys::fs::exists(P))
+return std::string(P);
+}
+  }
+
   SmallString<128> D(Dir);
   llvm::sys::path::append(D, "..", Name);
   if (llvm::sys::fs::exists(Twine(D)))
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 996c4ab217c23..522dba348e444 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1137,6 +1137,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   return {};
 }
 
+std::optional ToolChain::getDefaultIntrinsicModuleDir() const {
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "finclude", "flang");
+  return getTargetSubDirPath(P);
+}
+
 std::optional ToolChain::getRuntimePath() const {
   SmallString<128> P(D.ResourceDir);
   llvm::sys::path::append(P, "lib");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index ce503b74295e4..f1204b4142163 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, getting the builtin modules for offload targets requires building
+  // the flang-rt and openmp for those targets as well:
+  // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
+  // To reduce friction when build systems have not yet been updated, we also
+  // add the host's builtin module to the search path (with lower priority), in
+  // case a module file has not been found for the offload targets itself.
+  // FIXME: This workaround may mix module files targeting different triples 
and
+  //should eventually be removed.
+  auto &&HostTCs =
+  C.getOffloadToolChains();
+  for (auto [OKind, HostTC] : llvm::make_range(HostTCs.fi

[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-19 Thread Tarun Prabhu via cfe-commits

https://github.com/tarunprabhu edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-19 Thread Michael Kruse via cfe-commits


@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, getting the builtin modules for offload targets requires building
+  // the flang-rt and openmp for those targets as well:
+  // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
+  // To reduce friction when build systems have not yet been updated, we also
+  // add the host's builtin module to the search path (with lower priority), in
+  // case a module file has not been found for the offload targets itself.
+  // FIXME: This workaround may mix module files targeting different triples 
and
+  //should eventually be removed.

Meinersbur wrote:

What this entire PR does is adding an additional search path where only one of 
them is ever needed. If you have concerns with temporary workarounds, you 
should have been against extracting this out of #171515. Having two search 
paths is the direct result of having to handle the intermediate state between 
landing #196558 and #171515.

https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-19 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur updated 
https://github.com/llvm/llvm-project/pull/196558

>From 9a2dc9c9ab9db05fba11cd32a9457e89a3804402 Mon Sep 17 00:00:00 2001
From: Michael Kruse 
Date: Fri, 8 May 2026 17:12:13 +0200
Subject: [PATCH 01/11] [Flang][Driver] Add per-target search path for modules

---
 clang/include/clang/Driver/ToolChain.h|  4 ++
 clang/lib/Driver/Driver.cpp   | 11 
 clang/lib/Driver/ToolChain.cpp|  6 ++
 clang/lib/Driver/ToolChains/Flang.cpp | 31 ++
 .../flang/Frontend/CompilerInvocation.h   |  7 +++
 flang/lib/Frontend/CompilerInvocation.cpp | 14 +
 flang/test/Driver/Inputs/ieee_arithmetic.mod  |  1 +
 flang/test/Driver/Inputs/iso_fortran_env.mod  |  1 +
 .../basictestmoduleone.mod|  0
 .../flang/x86_64-unknown-linux-gnu/.keep  |  0
 .../intrinsic-module-path_per_target.f90  | 56 +++
 flang/test/Driver/use-module.f90  | 12 ++--
 flang/tools/bbc/bbc.cpp   |  5 ++
 13 files changed, 142 insertions(+), 6 deletions(-)
 rename flang/test/Driver/Inputs/{ => module-dir-one}/basictestmoduleone.mod 
(100%)
 create mode 100644 
flang/test/Driver/Inputs/resource_dir_with_per_target_subdir/finclude/flang/x86_64-unknown-linux-gnu/.keep
 create mode 100644 flang/test/Driver/intrinsic-module-path_per_target.f90

diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index 8bda212312aee..ff044722369cc 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -560,6 +560,10 @@ class ToolChain {
   // Returns Triple without the OSs version.
   llvm::Triple getTripleWithoutOSVersion() const;
 
+  /// Returns the target-specific path for Flang's intrinsic modules in the
+  /// resource directory if it exists.
+  std::optional getDefaultIntrinsicModuleDir() const;
+
   // Returns the target specific runtime path if it exists.
   std::optional getRuntimePath() const;
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b9c072a0a0edb..279dc78309bef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6766,6 +6766,17 @@ std::string Driver::GetFilePath(StringRef Name, const 
ToolChain &TC) const {
   if (llvm::sys::fs::exists(Twine(P)))
 return std::string(P);
 
+  // With Flang, also look for intrinsic modules
+  if (IsFlangMode()) {
+if (std::optional IntrPath =
+TC.getDefaultIntrinsicModuleDir()) {
+  SmallString<128> P(*IntrPath);
+  llvm::sys::path::append(P, Name);
+  if (llvm::sys::fs::exists(P))
+return std::string(P);
+}
+  }
+
   SmallString<128> D(Dir);
   llvm::sys::path::append(D, "..", Name);
   if (llvm::sys::fs::exists(Twine(D)))
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 996c4ab217c23..522dba348e444 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1137,6 +1137,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   return {};
 }
 
+std::optional ToolChain::getDefaultIntrinsicModuleDir() const {
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "finclude", "flang");
+  return getTargetSubDirPath(P);
+}
+
 std::optional ToolChain::getRuntimePath() const {
   SmallString<128> P(D.ResourceDir);
   llvm::sys::path::append(P, "lib");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index ce503b74295e4..f1204b4142163 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, getting the builtin modules for offload targets requires building
+  // the flang-rt and openmp for those targets as well:
+  // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
+  // To reduce friction when build systems have not yet been updated, we also
+  // add the host's builtin module to the search path (with lower priority), in
+  // case a module file has not been found for the offload targets itself.
+  // FIXME: This workaround may mix module files targeting different triples 
and
+  //should eventually be removed.
+  auto &&HostTCs =
+  C.getOffloadToolChains();
+  for (auto [OKind, HostTC] : llvm::make_range(HostTCs.fi

[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-18 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-18 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur updated 
https://github.com/llvm/llvm-project/pull/196558

>From 9a2dc9c9ab9db05fba11cd32a9457e89a3804402 Mon Sep 17 00:00:00 2001
From: Michael Kruse 
Date: Fri, 8 May 2026 17:12:13 +0200
Subject: [PATCH 1/8] [Flang][Driver] Add per-target search path for modules

---
 clang/include/clang/Driver/ToolChain.h|  4 ++
 clang/lib/Driver/Driver.cpp   | 11 
 clang/lib/Driver/ToolChain.cpp|  6 ++
 clang/lib/Driver/ToolChains/Flang.cpp | 31 ++
 .../flang/Frontend/CompilerInvocation.h   |  7 +++
 flang/lib/Frontend/CompilerInvocation.cpp | 14 +
 flang/test/Driver/Inputs/ieee_arithmetic.mod  |  1 +
 flang/test/Driver/Inputs/iso_fortran_env.mod  |  1 +
 .../basictestmoduleone.mod|  0
 .../flang/x86_64-unknown-linux-gnu/.keep  |  0
 .../intrinsic-module-path_per_target.f90  | 56 +++
 flang/test/Driver/use-module.f90  | 12 ++--
 flang/tools/bbc/bbc.cpp   |  5 ++
 13 files changed, 142 insertions(+), 6 deletions(-)
 rename flang/test/Driver/Inputs/{ => module-dir-one}/basictestmoduleone.mod 
(100%)
 create mode 100644 
flang/test/Driver/Inputs/resource_dir_with_per_target_subdir/finclude/flang/x86_64-unknown-linux-gnu/.keep
 create mode 100644 flang/test/Driver/intrinsic-module-path_per_target.f90

diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index 8bda212312aee..ff044722369cc 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -560,6 +560,10 @@ class ToolChain {
   // Returns Triple without the OSs version.
   llvm::Triple getTripleWithoutOSVersion() const;
 
+  /// Returns the target-specific path for Flang's intrinsic modules in the
+  /// resource directory if it exists.
+  std::optional getDefaultIntrinsicModuleDir() const;
+
   // Returns the target specific runtime path if it exists.
   std::optional getRuntimePath() const;
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b9c072a0a0edb..279dc78309bef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6766,6 +6766,17 @@ std::string Driver::GetFilePath(StringRef Name, const 
ToolChain &TC) const {
   if (llvm::sys::fs::exists(Twine(P)))
 return std::string(P);
 
+  // With Flang, also look for intrinsic modules
+  if (IsFlangMode()) {
+if (std::optional IntrPath =
+TC.getDefaultIntrinsicModuleDir()) {
+  SmallString<128> P(*IntrPath);
+  llvm::sys::path::append(P, Name);
+  if (llvm::sys::fs::exists(P))
+return std::string(P);
+}
+  }
+
   SmallString<128> D(Dir);
   llvm::sys::path::append(D, "..", Name);
   if (llvm::sys::fs::exists(Twine(D)))
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 996c4ab217c23..522dba348e444 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1137,6 +1137,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   return {};
 }
 
+std::optional ToolChain::getDefaultIntrinsicModuleDir() const {
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "finclude", "flang");
+  return getTargetSubDirPath(P);
+}
+
 std::optional ToolChain::getRuntimePath() const {
   SmallString<128> P(D.ResourceDir);
   llvm::sys::path::append(P, "lib");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index ce503b74295e4..f1204b4142163 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, getting the builtin modules for offload targets requires building
+  // the flang-rt and openmp for those targets as well:
+  // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
+  // To reduce friction when build systems have not yet been updated, we also
+  // add the host's builtin module to the search path (with lower priority), in
+  // case a module file has not been found for the offload targets itself.
+  // FIXME: This workaround may mix module files targeting different triples 
and
+  //should eventually be removed.
+  auto &&HostTCs =
+  C.getOffloadToolChains();
+  for (auto [OKind, HostTC] : llvm::make_range(HostTCs.firs

[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-18 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-18 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-14 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur edited 
https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread Michael Kruse via cfe-commits


@@ -0,0 +1,58 @@
+! Ensure argument -fintrinsic-modules-path works as expected.
+
+!-
+! FLANG DRIVER
+!-
+! NOTE: Depending on how Flang is built, the default intrinsics may have higher
+!   or lower priority than -fintrinsic-modules-path added here. Using
+!   basictestmoduleone.mod from Inputs/module-dir/ will trigger an error.

Meinersbur wrote:

Because it is missing the checksum. See comment in that file.

https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- 
clang/include/clang/Driver/ToolChain.h clang/lib/Driver/Driver.cpp 
clang/lib/Driver/ToolChain.cpp clang/lib/Driver/ToolChains/Flang.cpp 
flang/lib/Frontend/CompilerInvocation.cpp --diff_from_common_commit
``

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index 70520bf53..696b29366 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1178,7 +1178,7 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back(D.ResourceDir.c_str());
 
   // Default intrinsic module dirs must be added after any user-provided dirs 
in
-  // -fintrinsic-modules-path since the default dirs have lower precedence 
than 
+  // -fintrinsic-modules-path since the default dirs have lower precedence than
   // user-provided dirs
   if (std::optional IntrModPath =
   TC.getDefaultIntrinsicModuleDir()) {

``




https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur updated 
https://github.com/llvm/llvm-project/pull/196558

>From 9a2dc9c9ab9db05fba11cd32a9457e89a3804402 Mon Sep 17 00:00:00 2001
From: Michael Kruse 
Date: Fri, 8 May 2026 17:12:13 +0200
Subject: [PATCH 1/6] [Flang][Driver] Add per-target search path for modules

---
 clang/include/clang/Driver/ToolChain.h|  4 ++
 clang/lib/Driver/Driver.cpp   | 11 
 clang/lib/Driver/ToolChain.cpp|  6 ++
 clang/lib/Driver/ToolChains/Flang.cpp | 31 ++
 .../flang/Frontend/CompilerInvocation.h   |  7 +++
 flang/lib/Frontend/CompilerInvocation.cpp | 14 +
 flang/test/Driver/Inputs/ieee_arithmetic.mod  |  1 +
 flang/test/Driver/Inputs/iso_fortran_env.mod  |  1 +
 .../basictestmoduleone.mod|  0
 .../flang/x86_64-unknown-linux-gnu/.keep  |  0
 .../intrinsic-module-path_per_target.f90  | 56 +++
 flang/test/Driver/use-module.f90  | 12 ++--
 flang/tools/bbc/bbc.cpp   |  5 ++
 13 files changed, 142 insertions(+), 6 deletions(-)
 rename flang/test/Driver/Inputs/{ => module-dir-one}/basictestmoduleone.mod 
(100%)
 create mode 100644 
flang/test/Driver/Inputs/resource_dir_with_per_target_subdir/finclude/flang/x86_64-unknown-linux-gnu/.keep
 create mode 100644 flang/test/Driver/intrinsic-module-path_per_target.f90

diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index 8bda212312aee..ff044722369cc 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -560,6 +560,10 @@ class ToolChain {
   // Returns Triple without the OSs version.
   llvm::Triple getTripleWithoutOSVersion() const;
 
+  /// Returns the target-specific path for Flang's intrinsic modules in the
+  /// resource directory if it exists.
+  std::optional getDefaultIntrinsicModuleDir() const;
+
   // Returns the target specific runtime path if it exists.
   std::optional getRuntimePath() const;
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b9c072a0a0edb..279dc78309bef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6766,6 +6766,17 @@ std::string Driver::GetFilePath(StringRef Name, const 
ToolChain &TC) const {
   if (llvm::sys::fs::exists(Twine(P)))
 return std::string(P);
 
+  // With Flang, also look for intrinsic modules
+  if (IsFlangMode()) {
+if (std::optional IntrPath =
+TC.getDefaultIntrinsicModuleDir()) {
+  SmallString<128> P(*IntrPath);
+  llvm::sys::path::append(P, Name);
+  if (llvm::sys::fs::exists(P))
+return std::string(P);
+}
+  }
+
   SmallString<128> D(Dir);
   llvm::sys::path::append(D, "..", Name);
   if (llvm::sys::fs::exists(Twine(D)))
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 996c4ab217c23..522dba348e444 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1137,6 +1137,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   return {};
 }
 
+std::optional ToolChain::getDefaultIntrinsicModuleDir() const {
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "finclude", "flang");
+  return getTargetSubDirPath(P);
+}
+
 std::optional ToolChain::getRuntimePath() const {
   SmallString<128> P(D.ResourceDir);
   llvm::sys::path::append(P, "lib");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index ce503b74295e4..f1204b4142163 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, getting the builtin modules for offload targets requires building
+  // the flang-rt and openmp for those targets as well:
+  // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
+  // To reduce friction when build systems have not yet been updated, we also
+  // add the host's builtin module to the search path (with lower priority), in
+  // case a module file has not been found for the offload targets itself.
+  // FIXME: This workaround may mix module files targeting different triples 
and
+  //should eventually be removed.
+  auto &&HostTCs =
+  C.getOffloadToolChains();
+  for (auto [OKind, HostTC] : llvm::make_range(HostTCs.firs

[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread Michael Kruse via cfe-commits


@@ -0,0 +1,58 @@
+! Ensure argument -fintrinsic-modules-path works as expected.
+
+!-
+! FLANG DRIVER
+!-
+! NOTE: Depending on how Flang is built, the default intrinsics may have higher
+!   or lower priority than -fintrinsic-modules-path added here. Using
+!   basictestmoduleone.mod from Inputs/module-dir/ will trigger an error.
+
+! RUN: %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -### 2>&1 | 
FileCheck %s --check-prefix=DEFAULTPATH
+
+! RUN: %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_DEFAULT
+! RUN: not %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_INPUTONE 2>&1 | FileCheck %s --check-prefix=NOINPUTONE
+! RUN: not %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_INPUTTWO 2>&1 | FileCheck %s --check-prefix=NOINPUTTWO
+! RUN: %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_DEFAULT -DINTRINSICS_INPUTTWO 
-fintrinsic-modules-path=%S/Inputs/module-dir/
+! RUN: %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_INPUTONE -fintrinsic-modules-path=%S/Inputs/module-dir-one/
+! RUN: %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_INPUTONE -DINTRINSICS_INPUTTWO 
-fintrinsic-modules-path=%S/Inputs/module-dir-one/ 
-fintrinsic-modules-path=%S/Inputs/module-dir/
+! RUN: not %flang -fsyntax-only --target=x86_64-unknown-linux-gnu 
-resource-dir %S/Inputs/resource_dir_with_per_target_subdir %s -cpp 
-DINTRINSICS_INPUTONE -DINTRINSICS_INPUTTWO 
-fintrinsic-modules-path=%S/Inputs/module-dir/ 
-fintrinsic-modules-path=%S/Inputs/module-dir-one/ 2>&1 | FileCheck %s 
--check-prefix=WRONGINPUTONE
+
+
+!-
+! FLANG FRONTEND (flang -fc1)
+!-
+! NOTE: %flang_cc1 the default intrinsics path always has higher priority than
+!   -fintrinsic-modules-path added here. Accidentally using
+!   ieee_arithmetic/iso_fortran_env from the Inputs/ directory will trigger
+!   an error (e.g. when the default intrinsics dir is empty).
+!   Requires the intrinsic modules to be available.
+
+! RUN: %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT
+! RUN: not %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT 
-DINTRINSICS_INPUTONE 2>&1 | FileCheck %s --check-prefix=NOINPUTONE
+! RUN: not %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT 
-DINTRINSICS_INPUTTWO 2>&1 | FileCheck %s --check-prefix=NOINPUTTWO
+! RUN: %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT 
-DINTRINSICS_INPUTTWO -fintrinsic-modules-path=%S/Inputs/module-dir/
+! RUN: %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT 
-DINTRINSICS_INPUTONE -fintrinsic-modules-path=%S/Inputs/module-dir-one/
+! RUN: %flang_fc1 -fsyntax-only -cpp %s -DINTRINSICS_DEFAULT 
-DINTRINSICS_INPUTONE -DINTRINSICS_INPUTTWO 
-fintrinsic-modules-path=%S/Inputs/module-dir-one/ 
-fintrinsic-modules-path=%S/Inputs/module-dir/

Meinersbur wrote:

It checks that the command succeeds. `--allow-empty` would accept anything 
without actual check line, that would be pointless.

https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-13 Thread Michael Kruse via cfe-commits


@@ -1,5 +1,6 @@
 ! DUMMY module
 ! Added for testing purposes. The contents of this file are currently not 
relevant.
+! Using this file will cause an error because of missing checksum

Meinersbur wrote:

Removing this file will cause tests to fail. It is "used" so that when module 
file search finds this file (instead of the actual builtin intrinsic), an error 
occurs.

Missing the checksum was probably unintentional when the test was added when 
module files did not yet have checksums. Now tests depend on it. Since it took 
me time to find out I am adding this comment so others don't need to rediscover 
this again.

https://github.com/llvm/llvm-project/pull/196558
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-09 Thread Michael Kruse via cfe-commits

https://github.com/Meinersbur created 
https://github.com/llvm/llvm-project/pull/196558

Adds the version- and target-specific path

../lib/clang//finclude/flang/

to the intrinsic module search path in addition to

../finclude/flang

with the former taking precedence if a module file should exist in both. The 
version/target-specific path is added by the driver by passing 
`-fintrinsic-modules-path` to the `-fc1` invocation. This is consistent with 
gfortran and the usual pattern that the driver resolves paths into the resource 
path, not the frontend.

This PR adds nothing into that directory, which will be done in #171515.

Extracted out of #171515 as requested by 
https://github.com/llvm/llvm-project/pull/171515#pullrequestreview-4179212219


>From 9a2dc9c9ab9db05fba11cd32a9457e89a3804402 Mon Sep 17 00:00:00 2001
From: Michael Kruse 
Date: Fri, 8 May 2026 17:12:13 +0200
Subject: [PATCH] [Flang][Driver] Add per-target search path for modules

---
 clang/include/clang/Driver/ToolChain.h|  4 ++
 clang/lib/Driver/Driver.cpp   | 11 
 clang/lib/Driver/ToolChain.cpp|  6 ++
 clang/lib/Driver/ToolChains/Flang.cpp | 31 ++
 .../flang/Frontend/CompilerInvocation.h   |  7 +++
 flang/lib/Frontend/CompilerInvocation.cpp | 14 +
 flang/test/Driver/Inputs/ieee_arithmetic.mod  |  1 +
 flang/test/Driver/Inputs/iso_fortran_env.mod  |  1 +
 .../basictestmoduleone.mod|  0
 .../flang/x86_64-unknown-linux-gnu/.keep  |  0
 .../intrinsic-module-path_per_target.f90  | 56 +++
 flang/test/Driver/use-module.f90  | 12 ++--
 flang/tools/bbc/bbc.cpp   |  5 ++
 13 files changed, 142 insertions(+), 6 deletions(-)
 rename flang/test/Driver/Inputs/{ => module-dir-one}/basictestmoduleone.mod 
(100%)
 create mode 100644 
flang/test/Driver/Inputs/resource_dir_with_per_target_subdir/finclude/flang/x86_64-unknown-linux-gnu/.keep
 create mode 100644 flang/test/Driver/intrinsic-module-path_per_target.f90

diff --git a/clang/include/clang/Driver/ToolChain.h 
b/clang/include/clang/Driver/ToolChain.h
index 8bda212312aee..ff044722369cc 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -560,6 +560,10 @@ class ToolChain {
   // Returns Triple without the OSs version.
   llvm::Triple getTripleWithoutOSVersion() const;
 
+  /// Returns the target-specific path for Flang's intrinsic modules in the
+  /// resource directory if it exists.
+  std::optional getDefaultIntrinsicModuleDir() const;
+
   // Returns the target specific runtime path if it exists.
   std::optional getRuntimePath() const;
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b9c072a0a0edb..279dc78309bef 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6766,6 +6766,17 @@ std::string Driver::GetFilePath(StringRef Name, const 
ToolChain &TC) const {
   if (llvm::sys::fs::exists(Twine(P)))
 return std::string(P);
 
+  // With Flang, also look for intrinsic modules
+  if (IsFlangMode()) {
+if (std::optional IntrPath =
+TC.getDefaultIntrinsicModuleDir()) {
+  SmallString<128> P(*IntrPath);
+  llvm::sys::path::append(P, Name);
+  if (llvm::sys::fs::exists(P))
+return std::string(P);
+}
+  }
+
   SmallString<128> D(Dir);
   llvm::sys::path::append(D, "..", Name);
   if (llvm::sys::fs::exists(Twine(D)))
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 996c4ab217c23..522dba348e444 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1137,6 +1137,12 @@ ToolChain::getTargetSubDirPath(StringRef BaseDir) const {
   return {};
 }
 
+std::optional ToolChain::getDefaultIntrinsicModuleDir() const {
+  SmallString<128> P(D.ResourceDir);
+  llvm::sys::path::append(P, "finclude", "flang");
+  return getTargetSubDirPath(P);
+}
+
 std::optional ToolChain::getRuntimePath() const {
   SmallString<128> P(D.ResourceDir);
   llvm::sys::path::append(P, "lib");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp 
b/clang/lib/Driver/ToolChains/Flang.cpp
index ce503b74295e4..f1204b4142163 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1179,6 +1179,37 @@ void Flang::ConstructJob(Compilation &C, const JobAction 
&JA,
   CmdArgs.push_back("-resource-dir");
   CmdArgs.push_back(D.ResourceDir.c_str());
 
+  // Default intrinsic module dirs must be added after any user-provided
+  // -fintrinsic-modules-path to have lower precedence
+  if (std::optional IntrModPath =
+  TC.getDefaultIntrinsicModuleDir()) {
+CmdArgs.push_back("-fintrinsic-modules-path");
+CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
+  }
+
+  // Ideally, every target triple has its own set of builtin modules since they
+  // are compiled with platform-dependent conditionals such as `#if 
__x86_64__`.
+  // However, ge

[clang] [flang] [Flang][Driver] Add per-target search path for modules (PR #196558)

2026-05-09 Thread via cfe-commits

github-actions[bot] wrote:


# :window: Windows x64 Test Results

* 58088 tests passed
* 1448 tests skipped
* 1 test failed

## Failed Tests
(click on a test name to see its output)

### Flang

Flang.Driver/intrinsic-module-path_per_target.f90

```
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 10
c:\_work\llvm-project\llvm-project\build\bin\flang.exe -fsyntax-only 
-resource-dir 
C:\_work\llvm-project\llvm-project\flang\test\Driver/Inputs/resource_dir_with_per_target_subdir
 
C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90
 -### 2>&1 | c:\_work\llvm-project\llvm-project\build\bin\filecheck.exe 
C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90
 --check-prefix=DEFAULTPATH
# executed command: 'c:\_work\llvm-project\llvm-project\build\bin\flang.exe' 
-fsyntax-only -resource-dir 
'C:\_work\llvm-project\llvm-project\flang\test\Driver/Inputs/resource_dir_with_per_target_subdir'
 
'C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90'
 '-###'
# note: command had no output on stdout or stderr
# executed command: 
'c:\_work\llvm-project\llvm-project\build\bin\filecheck.exe' 
'C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90'
 --check-prefix=DEFAULTPATH
# .---command stderr
# | 
C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90:39:16:
 error: DEFAULTPATH: expected string not found in input
# | ! DEFAULTPATH: flang{{.*}}-fc1{{.*}}-fintrinsic-modules-path 
# |^
# | :1:1: note: scanning from here
# | flang version 23.0.0git (https://github.com/llvm/llvm-project 
a725ebfe5e734ec26a697ec6b3178cb150dfbb47)
# | ^
# | :6:587: note: possible intended match here
# |  "C:\\_work\\llvm-project\\llvm-project\\build\\bin\\flang" "-fc1" 
"-triple" "x86_64-pc-windows-msvc19.44.35226" "-fsyntax-only" 
"-mrelocation-model" "pic" "-pic-level" "2" "-target-cpu" "x86-64" 
"--dependent-lib=clang_rt.builtins.lib" "-D_MT" "--dependent-lib=libcmt" 
"--dependent-lib=flang_rt.runtime.static.lib" "-D_MSC_VER=1944" 
"-D_MSC_FULL_VER=194435226" "-D_WIN32" "-D_M_X64=100" "-resource-dir" 
"C:\\_work\\llvm-project\\llvm-project\\flang\\test\\Driver/Inputs/resource_dir_with_per_target_subdir"
 "-mframe-pointer=none" "-x" "f95" 
"C:\\_work\\llvm-project\\llvm-project\\flang\\test\\Driver\\intrinsic-module-path_per_target.f90"
# | 






  ^
# | 
# | Input file: 
# | Check file: 
C:\_work\llvm-project\llvm-project\flang\test\Driver\intrinsic-module-path_per_target.f90
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<
# | 1: flang version 23.0.0git 
(https://github.com/llvm/llvm-project a725ebfe5e734ec26a697ec6b3178cb150dfbb47) 
# | check:39'0 
X~~~
 error: no match found
# | 2: Target: x86_64-pc-windows-msvc 
# | check:39'0 ~~~
# | 3: Thread model: posix 
# | check:39'0 
# | 4: InstalledDir: C:\_work\llvm-project\llvm-project\build\bin 
# | check:39'0 ~~~
# | 5: Build config: +assertions 
# | check:39'0 ~~
# | 6:  "C:\\_work\\llvm-project\\llvm-project\\build\\bin\\flang" 
"-fc1" "-triple" "x86_64-pc-windows-msvc19.44.35226" "-fsyntax-only" 
"-mrelocation-model" "pic" "-pic-level" "2" "-target-cpu" "x86-64" 
"--dependent-lib=clang_rt.builtins.lib" "-D_MT" "--dependent-lib=libcmt" 
"--dependent-lib=flang_rt.runtime.static.lib" "-D_MSC_VER=1944" 
"-D_MSC_FULL_VER=194435226" "-D_WIN32" "-D_M_X64=100" "-resource-dir" 
"C:\\_work\\llvm-project\\llvm-project\\flang\\test\\Driver/Inputs/resource_dir_with_per_target_subdir"
 "-mframe-pointer=none" "-x" "f95" 
"C:\\_work\\llvm-project\\llvm-project\\flang\\test\\Driver\\intrinsic-module-path_per_target.f90"
 
# | check:39'0