Skip to content

Conversation

@jmmartinez
Copy link
Contributor

When using -no-integrated-cpp, before, the driver won't collapse actions when the input was not llvm-ir
or it would collapse them too aggressively with -save-temps

The original code was checking the action type (which is IR too for preprocessed->bc actions) instead of the action inputs.

@jmmartinez jmmartinez self-assigned this Apr 23, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Apr 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

When using -no-integrated-cpp, before, the driver won't collapse actions when the input was not llvm-ir
or it would collapse them too aggressively with -save-temps

The original code was checking the action type (which is IR too for preprocessed->bc actions) instead of the action inputs.


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

2 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+9-8)
  • (added) clang/test/Driver/no-integrated-cpp.c (+83)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 2b8c6e35263b1..2751bd919ae16 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -5609,19 +5609,20 @@ class ToolSelector final {
     if (!BJ || !CJ)
       return nullptr;
 
+    auto HasBitcodeInput = [](const JobActionInfo &AI) {
+      for (auto &Input : AI.JA->getInputs())
+        if (!types::isLLVMIR(Input->getType()))
+          return false;
+      return true;
+    };
+
     // Check if the initial input (to the compile job or its predessor if one
     // exists) is LLVM bitcode. In that case, no preprocessor step is required
     // and we can still collapse the compile and backend jobs when we have
     // -save-temps. I.e. there is no need for a separate compile job just to
     // emit unoptimized bitcode.
-    bool InputIsBitcode = true;
-    for (size_t i = 1; i < ActionInfo.size(); i++)
-      if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC &&
-          ActionInfo[i].JA->getType() != types::TY_LTO_BC) {
-        InputIsBitcode = false;
-        break;
-      }
-    if (!InputIsBitcode && !canCollapsePreprocessorAction())
+    bool InputIsBitcode = all_of(ActionInfo, HasBitcodeInput);
+    if (SaveTemps && !InputIsBitcode)
       return nullptr;
 
     // Get compiler tool.
diff --git a/clang/test/Driver/no-integrated-cpp.c b/clang/test/Driver/no-integrated-cpp.c
new file mode 100644
index 0000000000000..a7dc847f08a67
--- /dev/null
+++ b/clang/test/Driver/no-integrated-cpp.c
@@ -0,0 +1,83 @@
+// RUN: %clang -O2 %s -E -o %t.i
+//
+// RUN: %clang -O2 %s -c -o a.o -no-integrated-cpp -### 2>&1 | FileCheck %s --check-prefixes=SRC
+// SRC: "-E"
+// SRC-SAME: "-o" "[[PREPROC:.*.i]]"
+// SRC-SAME: "-x" "c" "{{.*}}no-integrated-cpp.c"
+//
+// SRC-NEXT: "-emit-obj"
+// SRC-SAME: "-o" "a.o"
+// SRC-SAME: "-x" "cpp-output" "[[PREPROC]]"
+//
+// RUN: %clang -O2 %s -c -o a.o -no-integrated-cpp -save-temps -### 2>&1 | FileCheck %s --check-prefixes=SRC-SAVE
+// SRC-SAVE: "-E"
+// SRC-SAVE-SAME: "-o" "[[PREPROC:.*.i]]"
+// SRC-SAVE-SAME: "-x" "c" "{{.*}}no-integrated-cpp.c"
+//
+// SRC-SAVE-NEXT: "-emit-llvm-bc"
+// SRC-SAVE-SAME: "-o" "[[BITCODE:.*.bc]]"
+// SRC-SAVE-SAME: "-x" "cpp-output" "[[PREPROC]]"
+//
+// SRC-SAVE-NEXT: "-S"
+// SRC-SAVE-SAME: "-o" "[[ASM:.*.s]]"
+// SRC-SAVE-SAME: "-x" "ir" "[[BITCODE]]"
+//
+// SRC-SAVE-NEXT: "-cc1as"
+// SRC-SAVE-SAME: "-o" "a.o" "[[ASM]]"
+//
+// RUN: %clang -O2 %t.i  -c -o a.o -no-integrated-cpp -### 2>&1 | FileCheck %s --check-prefixes=PRE
+// PRE-NOT: "-E"
+// PRE: "-emit-obj"
+// PRE-SAME: "-o" "a.o"
+// PRE-SAME: "-x" "cpp-output" "{{.*}}no-integrated-cpp.c.tmp.i"
+//
+// RUN: %clang -O2 %t.i  -c -o a.o -no-integrated-cpp -save-temps -### 2>&1 | FileCheck %s --check-prefixes=PRE-SAVE
+// PRE-SAVE-NOT: "-E"
+// PRE-SAVE: "-emit-llvm-bc"
+// PRE-SAVE-SAME: "-o" "[[BITCODE:.*.bc]]"
+// PRE-SAVE-SAME: "-x" "cpp-output" "{{.*}}no-integrated-cpp.c.tmp.i"
+//
+// PRE-SAVE-NEXT: "-S"
+// PRE-SAVE-SAME: "-o" "[[ASM:.*.s]]"
+// PRE-SAVE-SAME: "-x" "ir" "[[BITCODE]]"
+//
+// PRE-SAVE-NEXT: "-cc1as"
+// PRE-SAVE-SAME: "-o" "a.o" "[[ASM]]"
+//
+// RUN: %clang -O2 %s -c -emit-llvm -o a.bc -no-integrated-cpp -### 2>&1 | FileCheck %s --check-prefixes=LLVM
+// LLVM: "-E"
+// LLVM-SAME: "-o" "[[PREPROC:.*.i]]"
+// LLVM-SAME: "-x" "c" "{{.*}}no-integrated-cpp.c"
+//
+// LLVM-NEXT: "-emit-llvm-bc"
+// LLVM-SAME: "-o" "a.bc"
+// LLVM-SAME: "-x" "cpp-output" "[[PREPROC]]"
+//
+// RUN: %clang -O2 %s -c -emit-llvm -o a.bc -no-integrated-cpp -save-temps -### 2>&1 | FileCheck %s --check-prefixes=LLVM-SAVE
+// LLVM-SAVE: "-E"
+// LLVM-SAVE-SAME: "-o" "[[PREPROC:.*.i]]"
+// LLVM-SAVE-SAME: "-x" "c" "{{.*}}no-integrated-cpp.c"
+//
+// LLVM-SAVE-NEXT: "-emit-llvm-bc"
+// LLVM-SAVE-SAME: "-o" "[[BITCODE:.*.bc]]"
+// LLVM-SAVE-SAME: "-x" "cpp-output" "[[PREPROC]]"
+//
+// LLVM-SAVE-NEXT: "-emit-llvm-bc"
+// LLVM-SAVE-SAME: "-o" "a.bc"
+// LLVM-SAVE-SAME: "-x" "ir" "[[BITCODE]]"
+//
+// RUN: %clang -O2 %t.i -c -emit-llvm -o a.bc -no-integrated-cpp -### 2>&1 | FileCheck %s --check-prefixes=PRE-LLVM
+// PRE-LLVM-NOT: "-E"
+// PRE-LLVM: "-emit-llvm-bc"
+// PRE-LLVM-SAME: "-o" "a.bc"
+// PRE-LLVM-SAME: "-x" "cpp-output" "{{.*}}no-integrated-cpp.c.tmp.i"
+//
+// RUN: %clang -O2 %t.i -c -emit-llvm -o a.bc -no-integrated-cpp -save-temps -### 2>&1 | FileCheck %s --check-prefixes=PRE-LLVM-SAVE
+// PRE-LLVM-SAVE-NOT: "-E"
+// PRE-LLVM-SAVE: "-emit-llvm-bc"
+// PRE-LLVM-SAVE-SAME: "-o" "[[BITCODE:.*.bc]]"
+// PRE-LLVM-SAVE-SAME: "-x" "cpp-output" "{{.*}}no-integrated-cpp.c.tmp.i"
+
+// PRE-LLVM-SAVE-NEXT: "-emit-llvm-bc"
+// PRE-LLVM-SAVE-SAME: "-o" "a.bc"
+// PRE-LLVM-SAVE-SAME: "-x" "ir" "[[BITCODE]]"

…-integrated-cpp

When using -no-integrated-cpp, before, the driver won't collapse actions when the input was not llvm-ir
or it would collapse them aggresively with -save-temps
@jmmartinez jmmartinez force-pushed the fix_combine_backend_compile branch from 335eb33 to ddbd409 Compare April 24, 2025 17:20
@jmmartinez jmmartinez requested a review from lamb-j April 25, 2025 10:12
Copy link
Contributor

@lamb-j lamb-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Seems more aligned with the intended behavior, and updates to use types::isLLVMIR() API instead of individually checking getType()

@jmmartinez jmmartinez merged commit ca3121b into llvm:main May 12, 2025
11 checks passed
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