Skip to content

Conversation

david-arm
Copy link
Contributor

@david-arm david-arm commented Sep 5, 2025

Sometimes code depends upon the constant variables declared in OptimizationLevel (e.g. see #156802), which then requires them to add a dependency on the Passes directory. Given that code is much more likely to depend upon Support than Passes I believe it makes sense to move OptimizationLevel to Support. The Passes component already depends upon Support.

This PR was inspired by a suggestion from @sdesmalen-arm!

@david-arm david-arm requested review from arsenm and tarinduj September 5, 2025 09:13
@llvmbot llvmbot added clang Clang issues not falling into any other category llvm:support labels Sep 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-mlir-execution-engine
@llvm/pr-subscribers-offload
@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-clang

Author: David Sherwood (david-arm)

Changes

Sometimes code depends upon the constant variables declared in OptimizationLevel (e.g. see #156802), which then requires them to add a dependency on the Passes directory. Given that code is much more likely to depend upon Support than Passes I believe it makes sense to move OptimizationLevel to Support. The Passes component already depends upon Support.


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

8 Files Affected:

  • (modified) clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp (+1-1)
  • (modified) llvm/include/llvm/Passes/PassBuilder.h (+1-1)
  • (renamed) llvm/include/llvm/Support/OptimizationLevel.h ()
  • (modified) llvm/lib/Passes/CMakeLists.txt (-1)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1-1)
  • (modified) llvm/lib/Passes/PassBuilderPipelines.cpp (+2-2)
  • (modified) llvm/lib/Support/CMakeLists.txt (+1)
  • (renamed) llvm/lib/Support/OptimizationLevel.cpp (+1-1)
diff --git a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
index 798b34b3ef0af..edaf0fa887720 100644
--- a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -34,9 +34,9 @@
 #include "llvm/IRPrinter/IRPrintingPasses.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/MC/TargetRegistry.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h
index 9cdb7ca7dbc9b..64bdb84a68d78 100644
--- a/llvm/include/llvm/Passes/PassBuilder.h
+++ b/llvm/include/llvm/Passes/PassBuilder.h
@@ -19,9 +19,9 @@
 #include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/CodeGen/RegAllocCommon.h"
 #include "llvm/IR/PassManager.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/PGOOptions.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/IPO/Inliner.h"
diff --git a/llvm/include/llvm/Passes/OptimizationLevel.h b/llvm/include/llvm/Support/OptimizationLevel.h
similarity index 100%
rename from llvm/include/llvm/Passes/OptimizationLevel.h
rename to llvm/include/llvm/Support/OptimizationLevel.h
diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 91c8c4f67074d..6f1ade87bc431 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -1,6 +1,5 @@
 add_llvm_component_library(LLVMPasses
   CodeGenPassBuilder.cpp
-  OptimizationLevel.cpp
   PassBuilder.cpp
   PassBuilderBindings.cpp
   PassBuilderPipelines.cpp
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 587f0ece0859b..ef18bd132499f 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -184,12 +184,12 @@
 #include "llvm/IR/SafepointIRVerifier.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRPrinter/IRPrintingPasses.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 98821bb1408a7..cd6fc51ceafec 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -26,10 +26,10 @@
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/PGOOptions.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Target/TargetMachine.h"
@@ -2355,4 +2355,4 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
 bool PassBuilder::isInstrumentedPGOUse() const {
   return (PGOOpt && PGOOpt->Action == PGOOptions::IRUse) ||
          !UseCtxProfile.empty();
-}
\ No newline at end of file
+}
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index b29dda46b757f..cb882f80bc467 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -224,6 +224,7 @@ add_llvm_component_library(LLVMSupport
   MSP430AttributeParser.cpp
   Mustache.cpp
   NativeFormatting.cpp
+  OptimizationLevel.cpp
   OptimizedStructLayout.cpp
   Optional.cpp
   OptionStrCmp.cpp
diff --git a/llvm/lib/Passes/OptimizationLevel.cpp b/llvm/lib/Support/OptimizationLevel.cpp
similarity index 95%
rename from llvm/lib/Passes/OptimizationLevel.cpp
rename to llvm/lib/Support/OptimizationLevel.cpp
index a1f8c1e14b1f0..28b742f609065 100644
--- a/llvm/lib/Passes/OptimizationLevel.cpp
+++ b/llvm/lib/Support/OptimizationLevel.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Passes/OptimizationLevel.h"
+#include "llvm/Support/OptimizationLevel.h"
 
 using namespace llvm;
 

@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-llvm-support

Author: David Sherwood (david-arm)

Changes

Sometimes code depends upon the constant variables declared in OptimizationLevel (e.g. see #156802), which then requires them to add a dependency on the Passes directory. Given that code is much more likely to depend upon Support than Passes I believe it makes sense to move OptimizationLevel to Support. The Passes component already depends upon Support.


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

8 Files Affected:

  • (modified) clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp (+1-1)
  • (modified) llvm/include/llvm/Passes/PassBuilder.h (+1-1)
  • (renamed) llvm/include/llvm/Support/OptimizationLevel.h ()
  • (modified) llvm/lib/Passes/CMakeLists.txt (-1)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1-1)
  • (modified) llvm/lib/Passes/PassBuilderPipelines.cpp (+2-2)
  • (modified) llvm/lib/Support/CMakeLists.txt (+1)
  • (renamed) llvm/lib/Support/OptimizationLevel.cpp (+1-1)
diff --git a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
index 798b34b3ef0af..edaf0fa887720 100644
--- a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -34,9 +34,9 @@
 #include "llvm/IRPrinter/IRPrintingPasses.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/MC/TargetRegistry.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h
index 9cdb7ca7dbc9b..64bdb84a68d78 100644
--- a/llvm/include/llvm/Passes/PassBuilder.h
+++ b/llvm/include/llvm/Passes/PassBuilder.h
@@ -19,9 +19,9 @@
 #include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/CodeGen/RegAllocCommon.h"
 #include "llvm/IR/PassManager.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/PGOOptions.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/IPO/Inliner.h"
diff --git a/llvm/include/llvm/Passes/OptimizationLevel.h b/llvm/include/llvm/Support/OptimizationLevel.h
similarity index 100%
rename from llvm/include/llvm/Passes/OptimizationLevel.h
rename to llvm/include/llvm/Support/OptimizationLevel.h
diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 91c8c4f67074d..6f1ade87bc431 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -1,6 +1,5 @@
 add_llvm_component_library(LLVMPasses
   CodeGenPassBuilder.cpp
-  OptimizationLevel.cpp
   PassBuilder.cpp
   PassBuilderBindings.cpp
   PassBuilderPipelines.cpp
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 587f0ece0859b..ef18bd132499f 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -184,12 +184,12 @@
 #include "llvm/IR/SafepointIRVerifier.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRPrinter/IRPrintingPasses.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 98821bb1408a7..cd6fc51ceafec 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -26,10 +26,10 @@
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
-#include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/OptimizationLevel.h"
 #include "llvm/Support/PGOOptions.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Target/TargetMachine.h"
@@ -2355,4 +2355,4 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
 bool PassBuilder::isInstrumentedPGOUse() const {
   return (PGOOpt && PGOOpt->Action == PGOOptions::IRUse) ||
          !UseCtxProfile.empty();
-}
\ No newline at end of file
+}
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index b29dda46b757f..cb882f80bc467 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -224,6 +224,7 @@ add_llvm_component_library(LLVMSupport
   MSP430AttributeParser.cpp
   Mustache.cpp
   NativeFormatting.cpp
+  OptimizationLevel.cpp
   OptimizedStructLayout.cpp
   Optional.cpp
   OptionStrCmp.cpp
diff --git a/llvm/lib/Passes/OptimizationLevel.cpp b/llvm/lib/Support/OptimizationLevel.cpp
similarity index 95%
rename from llvm/lib/Passes/OptimizationLevel.cpp
rename to llvm/lib/Support/OptimizationLevel.cpp
index a1f8c1e14b1f0..28b742f609065 100644
--- a/llvm/lib/Passes/OptimizationLevel.cpp
+++ b/llvm/lib/Support/OptimizationLevel.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Passes/OptimizationLevel.h"
+#include "llvm/Support/OptimizationLevel.h"
 
 using namespace llvm;
 

@david-arm david-arm changed the title [Support] Move OptimizationLevel to the Support directory [Support][NFC] Move OptimizationLevel to the Support directory Sep 5, 2025
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

At some point we should split Support into "ReallySupport" and "RandomStuffThatIRAndCodeGenUse"

Sometimes code depends upon the constant variables declared
in OptimizationLevel (e.g. see llvm#156802), which then requires
them to add a dependency on the Passes directory. Given that
code is much more likely to depend upon Support than Passes
I believe it makes sense to move OptimizationLevel to
Support. The Passes component already depends upon Support.
@llvmbot llvmbot added mlir flang Flang issues not falling into any other category mlir:execution-engine flang:fir-hlfir offload labels Sep 5, 2025
Copy link
Collaborator

@sdesmalen-arm sdesmalen-arm left a comment

Choose a reason for hiding this comment

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

This OptimizationClass class is pretty trivial. I think it makes sense to move OptimizationLevel to Support to avoid an unnecessary dependence on Passes when needing to reference OptimizationLevel::O0, for example.

@s-barannikov
Copy link
Contributor

s-barannikov commented Sep 5, 2025

I always thought of ADT and Support libraries as being useful for other projects, OptimizationLevel seems too llvm-specific to me. Could CodeGen directory be a better choice? Assuming this enum is not used by the middle end, that would create a circular dependency otherwise.

@s-barannikov
Copy link
Contributor

And what's wrong with Passes anyway?

Given that code is much more likely to depend upon Support than Passes I believe it makes sense to move OptimizationLevel to Support.

This doesn't look a very strong point to me. We could say this about nearly everything and move it all to Support :)

@joker-eph
Copy link
Collaborator

I always thought of ADT and Support libraries as being useful for other projects

That would be nice, and I'd love to move libSupport at the top-level (only the pieces that matches the "generic aspect" of it), but the reality today is that beyond filesystem abstractions, thread pool, and other generic things, there are also things like X86DisassemblerDecoderCommon.h, MSP430Attributes.h, AMDHSAKernelDescriptor.h, AArch64AttributeParser.h, RISCVISAUtils.h, etc.

@s-barannikov
Copy link
Contributor

I always thought of ADT and Support libraries as being useful for other projects

That would be nice, and I'd love to move libSupport at the top-level (only the pieces that matches the "generic aspect" of it), but the reality today is that beyond filesystem abstractions, thread pool, and other generic things, there are also things like X86DisassemblerDecoderCommon.h, MSP430Attributes.h, AMDHSAKernelDescriptor.h, AArch64AttributeParser.h, RISCVISAUtils.h, etc.

Perhaps the TargetParser directory is more suitable for them. Not sure what they are used for.

@david-arm
Copy link
Contributor Author

And what's wrong with Passes anyway?

See #156802 for a motivation for this change. If anybody wants to reference a constant variable from OptimizationLevel (OptimizationLevel::O0, for example) they have to add the extra Passes dependency, which seems a bit much for one variable. It felt like there wasn't really an obvious reason why OptimizationLevel had to live in the Passes directory either, and since most passes seem to require the Support directory anyway I thought it made sense to move it there? Having said that, I'm happy moving it somewhere else that isn't Passes if there are good suggestions!

@arsenm
Copy link
Contributor

arsenm commented Sep 5, 2025

Perhaps the TargetParser directory is more suitable for them. Not sure what they are used for.

It's closer but not quite right. I think there probably should be some kind of ABI-information for this sort of stuff. TargetParser is more specific to frontend-backend interactions

@s-barannikov
Copy link
Contributor

And what's wrong with Passes anyway?

See #156802 for a motivation for this change. If anybody wants to reference a constant variable from OptimizationLevel (OptimizationLevel::O0, for example) they have to add the extra Passes dependency, which seems a bit much for one variable.

Passes library closely related (and is tiny btw). I don't see a problem in keeping OptimizationLevel there. If you move the definiton of registerLateLoopOptimizationsEPCallback() (used in #15602) from h to cpp file you would need to add the dependency on Passes anyway.

@joker-eph
Copy link
Collaborator

As an alternative: can this be just made a header-only implementation?
Since C++17 we can have static inline variables in a class (and static constexpr are by default inline: https://en.cppreference.com/w/cpp/language/inline.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category flang:fir-hlfir flang Flang issues not falling into any other category llvm:support mlir:execution-engine mlir offload
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants