Skip to content

Conversation

aganea
Copy link
Member

@aganea aganea commented Aug 11, 2024

Before this PR, clang --print-runtime-dir on Windows used to report a non-existent directory if LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF.

We now check if any of the known runtime directories exist before printing any of them on stdout. If none exists, we print (runtime dir is not present).

Before this PR, `clang --print-runtime-dir` used to report a non-existant
directory if LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF.

We now check if any of the known runtime directories exist before
printing on stdout.
@aganea aganea requested review from tru, rnk, mstorsjo, MaskRay and pogo59 August 11, 2024 23:09
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Aug 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 11, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Alexandre Ganea (aganea)

Changes

Before this PR, clang --print-runtime-dir used to report a non-existent directory if LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF.

We now check if any of the known runtime directories exist before printing on stdout. If it doesn't, we print (runtime dir is not present).


Full diff: https://github.com/llvm/llvm-project/pull/102834.diff

1 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+8-4)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index f4e909b79389bc..4c8cd36dd118ee 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2230,10 +2230,14 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
   }
 
   if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
-    if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
-      llvm::outs() << *RuntimePath << '\n';
-    else
-      llvm::outs() << TC.getCompilerRTPath() << '\n';
+    for (auto RuntimePath :
+         {TC.getRuntimePath(), std::make_optional(TC.getCompilerRTPath())}) {
+      if (RuntimePath && getVFS().exists(*RuntimePath)) {
+        llvm::outs() << *RuntimePath << '\n';
+        return false;
+      }
+    }
+    llvm::outs() << "(runtime dir is not present)" << '\n';
     return false;
   }
 

@MaskRay
Copy link
Member

MaskRay commented Aug 12, 2024

--print-runtime-dir can be defined in multiple ways.
The current definition is to print a directory suitable for LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on configurations.
This interppretation makes sense to me, as the "runtime" in "print-runtime-dir" matches LLVM_ENABLE_PER_TARGET_RUNTIME_DIR.

With this interpretation, --print-runtime-dir should print the directory regardless of whether the LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on directory is present or not.
If the directory is absent, --print-runtime-dir prints a directory that the user should ensure presence.

Perhaps the TC.getCompilerRTPath() fallback leads to confusion. Ideally it probably should not be present.

@aganea
Copy link
Member Author

aganea commented Aug 12, 2024

To give the whole picture, the story is that while building the LLVM package on Windows, I was seeing this:

[51/52] Running sanitizer_common testsllvm-lit.py: C:\src\git\llvm-project\llvm\utils\lit\lit\llvm\config.py:57: note: using lit tools: C:\Program Files\Git\usr\bin\
llvm-lit.py: C:\src\git\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\src\git\llvm-project\llvm_package_20.0.0\build64\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/src/git/llvm-project/llvm_package_20.0.0/build64/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-Wthread-safety', '-Wthread-safety-reference', '-Wthread-safety-beta', '-print-runtime-dir'].
llvm-lit.py: C:\src\git\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\src\git\llvm-project\llvm_package_20.0.0\build64\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/src/git/llvm-project/llvm_package_20.0.0/build64/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-Wthread-safety', '-Wthread-safety-reference', '-Wthread-safety-beta', '-print-runtime-dir'].

This is, we don't set LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON when building compiler-rt, so we end up taking this cmake codepath: https://github.com/llvm/llvm-project/blob/main/compiler-rt/cmake/base-config-ix.cmake#L106 which ends up with the compiler runtime libs in {build_folder}\lib\clang\{version}\lib\windows. However ToolChain::getRuntimePath() checks for the triple folder, as in {build_folder}\lib\clang\{version}\lib\x86_64-pc-windows-msvc which don't exist. clang -print-runtime-dir will just display that, without checking the folder's existence. However if we do clang-cl a.cpp -fsanitize=address then we check the library paths (which runtime is part of at this point) for existence: https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/MSVC.cpp#L153

I think we want to keep the dynamic aspect on supporting both LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON/OFF at runtime in Clang like @mstorsjo was suggesting in https://discourse.llvm.org/t/runtime-directory-fallback/76860/2, I feel we should check the existence of the path before printing it, thus this PR.

Zentrik added a commit to Zentrik/julia that referenced this pull request Oct 13, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Oct 13, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Oct 13, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Oct 13, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Nov 3, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Nov 24, 2024
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
vtjnash pushed a commit to Zentrik/julia that referenced this pull request Jan 8, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Feb 8, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Feb 11, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
vchuravy pushed a commit to Zentrik/julia that referenced this pull request Feb 21, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Feb 23, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Feb 23, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
giordano pushed a commit to Zentrik/julia that referenced this pull request Feb 25, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
Zentrik added a commit to Zentrik/julia that referenced this pull request Feb 25, 2025
`clang -print-runtime-dir` reports a non-existent directory as we build with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`. See llvm/llvm-project#102834. I suspect llvm/llvm-project@b6a1473 caused the change by chaning the code in Driver.cpp to not check whether the printed directory existed.
@aganea
Copy link
Member Author

aganea commented Sep 1, 2025

Ping.

This still occurs as of today on main, clang-cl -print-runtime-dir doesn't print the right folder path when targetting Windows.
On disk we have: C:\Program Files\LLVM\lib\clang\22\lib\windows,
whereas -print-runtime-path prints the full triple as on the other platforms: C:\Program Files\LLVM\lib\clang\22\lib\x86_64-pc-windows-msvc.

What is it that we want to do here? Add a dynamic behavior as in this PR, that checks if the path is there, and adjust accordingly? Rename the "windows" component to instead use the target triple, as the other plateforms (this seems to be some work). Or conversly, accept that on Windows the path is "windows" and not the target triple? Anything else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants