[Mlir-commits] [mlir] [mlir] Add config for PDL (PR #69927)

Jacques Pienaar llvmlistbot at llvm.org
Mon Oct 23 06:53:17 PDT 2023


https://github.com/jpienaar created https://github.com/llvm/llvm-project/pull/69927

Make it so that PDL can be optionally disabled. PDL is different than other dialects as its included in the core rewrite framework. This results in these being included even where it isn't used and not removed during compilation. Add option to disable for workloads where it isn't needed or can't be used. This ends up being rather invasive due to how PDL is included. Ideally we'd have less `#if`'s, but didn't want to change structure purely to reduce those (felt better to keep the organization in case anything needs to be changed, forgetting to guard results in compile time failures which should be easy to trace).

PDL is enabled by default and not optional bazel. So this should be a NOP for most folks, while enabling other to disable.

This is piped through mlir-tblgen invocation and that could be changed/avoided by splitting up the passes file instead.

This only works with tests disabled. With tests enabled this still compiles but tests fail as there is no lit config to disable tests that depend on PDL yet.

>From eea36708d838411d70eb99265c3a2f3aabb91460 Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Sun, 22 Oct 2023 09:33:40 -0700
Subject: [PATCH] [mlir] Add config for PDL

Make it so that PDL can be optionally disabled. PDL is different than
other dialects as its included in rewrite framework. This results in
these being included even where it isn't used and not removed during
compilation. Add option to disable for workloads where it isn't needed
or used. This ends up being rather invasive due to how PDL is included.
Ideally we'd have less ifdefs, not particularly happy with those, but
given how integrated it is, couldn't see simple alternative.

PDL is enabled by default (and not optional bazel).

This only works with tests disabled. With tests enabled this still
compiles but tests fail as there is no lit config to disable tests that
depend on PDL yet.
---
 mlir/CMakeLists.txt                           | 12 ++--
 mlir/cmake/modules/AddMLIR.cmake              |  3 +
 mlir/examples/CMakeLists.txt                  |  4 +-
 mlir/examples/minimal-opt/README.md           |  7 ++-
 mlir/include/mlir/Config/mlir-config.h.cmake  |  3 +
 mlir/include/mlir/Conversion/Passes.td        |  2 +
 mlir/include/mlir/Dialect/CMakeLists.txt      |  6 +-
 .../mlir/Dialect/Transform/CMakeLists.txt     |  4 +-
 mlir/include/mlir/IR/PatternMatch.h           | 17 +++++
 mlir/include/mlir/InitAllDialects.h           |  9 ++-
 mlir/include/mlir/InitAllExtensions.h         | 11 +++-
 .../mlir/Rewrite/FrozenRewritePatternSet.h    |  6 ++
 mlir/include/mlir/Rewrite/PatternApplicator.h |  5 ++
 .../mlir/Transforms/DialectConversion.h       |  2 +
 mlir/lib/CAPI/Dialect/CMakeLists.txt          | 18 +++---
 mlir/lib/Conversion/CMakeLists.txt            |  4 +-
 mlir/lib/Dialect/Bufferization/CMakeLists.txt |  4 +-
 .../Bufferization/TransformOps/CMakeLists.txt |  1 -
 mlir/lib/Dialect/CMakeLists.txt               |  6 +-
 mlir/lib/Dialect/Transform/CMakeLists.txt     |  4 +-
 mlir/lib/IR/PatternMatch.cpp                  |  2 +
 mlir/lib/Rewrite/CMakeLists.txt               | 24 +++++--
 mlir/lib/Rewrite/FrozenRewritePatternSet.cpp  | 13 +++-
 mlir/lib/Rewrite/PatternApplicator.cpp        | 26 +++++++-
 mlir/lib/Tools/CMakeLists.txt                 |  6 +-
 .../Transforms/Utils/DialectConversion.cpp    |  2 +
 mlir/python/CMakeLists.txt                    | 62 ++++++++++---------
 mlir/test/CAPI/CMakeLists.txt                 | 16 ++---
 mlir/test/CMakeLists.txt                      | 20 ++++--
 mlir/test/lib/Dialect/CMakeLists.txt          |  5 +-
 mlir/test/lib/Rewrite/CMakeLists.txt          |  3 +-
 mlir/test/lib/Tools/CMakeLists.txt            |  4 +-
 mlir/test/lib/Transforms/CMakeLists.txt       | 28 ++++++---
 mlir/tools/CMakeLists.txt                     |  4 +-
 mlir/tools/mlir-lsp-server/CMakeLists.txt     | 10 ++-
 .../tools/mlir-lsp-server/mlir-lsp-server.cpp |  4 ++
 mlir/tools/mlir-opt/CMakeLists.txt            | 10 ++-
 mlir/tools/mlir-opt/mlir-opt.cpp              | 16 +++--
 mlir/unittests/Conversion/CMakeLists.txt      |  4 +-
 mlir/unittests/Dialect/CMakeLists.txt         |  4 +-
 .../llvm-project-overlay/mlir/BUILD.bazel     |  1 +
 41 files changed, 283 insertions(+), 109 deletions(-)

diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt
index ac120aad0d1eda7..d0db9e341765b3d 100644
--- a/mlir/CMakeLists.txt
+++ b/mlir/CMakeLists.txt
@@ -132,6 +132,8 @@ set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
     "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
     for compiling PTX to cubin")
 
+set(MLIR_ENABLE_PDL 1 CACHE BOOL "Enable PDL")
+
 option(MLIR_INCLUDE_TESTS
        "Generate build targets for the MLIR unit tests."
        ${LLVM_INCLUDE_TESTS})
@@ -179,12 +181,14 @@ include_directories( ${MLIR_INCLUDE_DIR})
 # from another directory like tools
 add_subdirectory(tools/mlir-tblgen)
 add_subdirectory(tools/mlir-linalg-ods-gen)
-add_subdirectory(tools/mlir-pdll)
-
 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
-set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
-set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
+
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(tools/mlir-pdll)
+  set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
+  set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
+endif()
 
 add_subdirectory(include/mlir)
 add_subdirectory(lib)
diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index 544abe43688820e..f20a2bc75d5433b 100644
--- a/mlir/cmake/modules/AddMLIR.cmake
+++ b/mlir/cmake/modules/AddMLIR.cmake
@@ -6,6 +6,9 @@ include(LLVMDistributionSupport)
 file(REMOVE ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml)
 
 function(mlir_tablegen ofn)
+  if (MLIR_ENABLE_PDL)
+    list(APPEND LLVM_TABLEGEN_FLAGS "-DMLIR_ENABLE_PDL")
+  endif()
   tablegen(MLIR ${ARGV})
   set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
       PARENT_SCOPE)
diff --git a/mlir/examples/CMakeLists.txt b/mlir/examples/CMakeLists.txt
index d256bf1a5cbb13d..88a22201c73d9f7 100644
--- a/mlir/examples/CMakeLists.txt
+++ b/mlir/examples/CMakeLists.txt
@@ -1,3 +1,5 @@
 add_subdirectory(toy)
-add_subdirectory(transform)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(transform)
+endif()
 add_subdirectory(minimal-opt)
diff --git a/mlir/examples/minimal-opt/README.md b/mlir/examples/minimal-opt/README.md
index 99179c4c11c597f..09d0f20c34e4265 100644
--- a/mlir/examples/minimal-opt/README.md
+++ b/mlir/examples/minimal-opt/README.md
@@ -15,9 +15,9 @@ using clang-14 on a X86 Ubuntu and [bloaty](https://github.com/google/bloaty).
 
 |                                  | Base   | Os     | Oz     | Os LTO | Oz LTO |
 | :------------------------------: | ------ | ------ | ------ | ------ | ------ |
-| `mlir-cat`                       | 1018kB | 836KB  | 879KB  | 697KB  | 649KB  |
-| `mlir-minimal-opt`               | 1.54MB | 1.25MB | 1.29MB | 1.10MB | 1.00MB |
-| `mlir-minimal-opt-canonicalizer` | 2.24MB | 1.81MB | 1.86MB | 1.62MB | 1.48MB |
+| `mlir-cat`                       | 1024KB |  840KB |  885KB |  706KB |  657KB |
+| `mlir-minimal-opt`               | 1.62MB | 1.32MB | 1.36MB | 1.17MB | 1.07MB |
+| `mlir-minimal-opt-canonicalize`  | 1.83MB | 1.40MB | 1.45MB | 1.25MB | 1.14MB |
 
 Base configuration:
 
@@ -32,6 +32,7 @@ cmake ../llvm/ -G Ninja \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DLLVM_ENABLE_LLD=ON \
    -DLLVM_ENABLE_BACKTRACES=OFF \
+   -DMLIR_ENABLE_PDL=OFF \
    -DCMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO=-Wl,-icf=all
 ```
 
diff --git a/mlir/include/mlir/Config/mlir-config.h.cmake b/mlir/include/mlir/Config/mlir-config.h.cmake
index efa77b2e5ce5db6..a2789b8e2d5867b 100644
--- a/mlir/include/mlir/Config/mlir-config.h.cmake
+++ b/mlir/include/mlir/Config/mlir-config.h.cmake
@@ -26,4 +26,7 @@
    numeric seed that is passed to the random number generator. */
 #cmakedefine MLIR_GREEDY_REWRITE_RANDOMIZER_SEED ${MLIR_GREEDY_REWRITE_RANDOMIZER_SEED}
 
+/* If set, enables PDL usage. */
+#cmakedefine MLIR_ENABLE_PDL ${MLIR_ENABLE_PDL}
+
 #endif
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index 274784fe4a7b29c..39cc229b1492ba1 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -864,6 +864,7 @@ def ConvertOpenMPToLLVMPass : Pass<"convert-openmp-to-llvm", "ModuleOp"> {
   let dependentDialects = ["LLVM::LLVMDialect"];
 }
 
+#ifdef MLIR_ENABLE_PDL
 //===----------------------------------------------------------------------===//
 // PDLToPDLInterp
 //===----------------------------------------------------------------------===//
@@ -873,6 +874,7 @@ def ConvertPDLToPDLInterp : Pass<"convert-pdl-to-pdl-interp", "ModuleOp"> {
   let constructor = "mlir::createPDLToPDLInterpPass()";
   let dependentDialects = ["pdl_interp::PDLInterpDialect"];
 }
+#endif
 
 //===----------------------------------------------------------------------===//
 // ReconcileUnrealizedCasts
diff --git a/mlir/include/mlir/Dialect/CMakeLists.txt b/mlir/include/mlir/Dialect/CMakeLists.txt
index 1c4569ecfa58485..c2dd7a9dcfd9c74 100644
--- a/mlir/include/mlir/Dialect/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/CMakeLists.txt
@@ -25,8 +25,10 @@ add_subdirectory(NVGPU)
 add_subdirectory(OpenACC)
 add_subdirectory(OpenACCMPCommon)
 add_subdirectory(OpenMP)
-add_subdirectory(PDL)
-add_subdirectory(PDLInterp)
+if (MLIR_ENABLE_PDL)
+  add_subdirectory(PDL)
+  add_subdirectory(PDLInterp)
+endif()
 add_subdirectory(Quant)
 add_subdirectory(SCF)
 add_subdirectory(Shape)
diff --git a/mlir/include/mlir/Dialect/Transform/CMakeLists.txt b/mlir/include/mlir/Dialect/Transform/CMakeLists.txt
index d9fbaee802398fb..cbdd3bbbe2315f6 100644
--- a/mlir/include/mlir/Dialect/Transform/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/Transform/CMakeLists.txt
@@ -1,3 +1,5 @@
 add_subdirectory(IR)
-add_subdirectory(PDLExtension)
+if (MLIR_ENABLE_PDL)
+  add_subdirectory(PDLExtension)
+endif()
 add_subdirectory(Transforms)
diff --git a/mlir/include/mlir/IR/PatternMatch.h b/mlir/include/mlir/IR/PatternMatch.h
index 6625ef553eba21f..060f8f5f602b99c 100644
--- a/mlir/include/mlir/IR/PatternMatch.h
+++ b/mlir/include/mlir/IR/PatternMatch.h
@@ -9,6 +9,7 @@
 #ifndef MLIR_IR_PATTERNMATCH_H
 #define MLIR_IR_PATTERNMATCH_H
 
+#include "mlir/Config/mlir-config.h"
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "llvm/ADT/FunctionExtras.h"
@@ -735,6 +736,7 @@ class PatternRewriter : public RewriterBase {
   virtual bool canRecoverFromRewriteFailure() const { return false; }
 };
 
+#if MLIR_ENABLE_PDL
 //===----------------------------------------------------------------------===//
 // PDL Patterns
 //===----------------------------------------------------------------------===//
@@ -1661,6 +1663,7 @@ class PDLPatternModule {
   llvm::StringMap<PDLConstraintFunction> constraintFunctions;
   llvm::StringMap<PDLRewriteFunction> rewriteFunctions;
 };
+#endif // MLIR_ENABLE_PDL
 
 //===----------------------------------------------------------------------===//
 // RewritePatternSet
@@ -1678,22 +1681,28 @@ class RewritePatternSet {
       : context(context) {
     nativePatterns.emplace_back(std::move(pattern));
   }
+#if MLIR_ENABLE_PDL
   RewritePatternSet(PDLPatternModule &&pattern)
       : context(pattern.getModule()->getContext()),
         pdlPatterns(std::move(pattern)) {}
+#endif // MLIR_ENABLE_PDL
 
   MLIRContext *getContext() const { return context; }
 
   /// Return the native patterns held in this list.
   NativePatternListT &getNativePatterns() { return nativePatterns; }
 
+#if MLIR_ENABLE_PDL
   /// Return the PDL patterns held in this list.
   PDLPatternModule &getPDLPatterns() { return pdlPatterns; }
+#endif
 
   /// Clear out all of the held patterns in this list.
   void clear() {
     nativePatterns.clear();
+#if MLIR_ENABLE_PDL
     pdlPatterns.clear();
+#endif
   }
 
   //===--------------------------------------------------------------------===//
@@ -1746,12 +1755,14 @@ class RewritePatternSet {
     return *this;
   }
 
+#if MLIR_ENABLE_PDL
   /// Add the given PDL pattern to the pattern list. Return a reference to
   /// `this` for chaining insertions.
   RewritePatternSet &add(PDLPatternModule &&pattern) {
     pdlPatterns.mergeIn(std::move(pattern));
     return *this;
   }
+#endif // MLIR_ENABLE_PDL
 
   // Add a matchAndRewrite style pattern represented as a C function pointer.
   template <typename OpType>
@@ -1812,12 +1823,14 @@ class RewritePatternSet {
     return *this;
   }
 
+#if MLIR_ENABLE_PDL
   /// Add the given PDL pattern to the pattern list. Return a reference to
   /// `this` for chaining insertions.
   RewritePatternSet &insert(PDLPatternModule &&pattern) {
     pdlPatterns.mergeIn(std::move(pattern));
     return *this;
   }
+#endif // MLIR_ENABLE_PDL
 
   // Add a matchAndRewrite style pattern represented as a C function pointer.
   template <typename OpType>
@@ -1853,6 +1866,7 @@ class RewritePatternSet {
     pattern->addDebugLabels(debugLabels);
     nativePatterns.emplace_back(std::move(pattern));
   }
+#if MLIR_ENABLE_PDL
   template <typename T, typename... Args>
   std::enable_if_t<std::is_base_of<PDLPatternModule, T>::value>
   addImpl(ArrayRef<StringRef> debugLabels, Args &&...args) {
@@ -1860,10 +1874,13 @@ class RewritePatternSet {
     // labels.
     pdlPatterns.mergeIn(T(std::forward<Args>(args)...));
   }
+#endif // MLIR_ENABLE_PDL
 
   MLIRContext *const context;
   NativePatternListT nativePatterns;
+#if MLIR_ENABLE_PDL
   PDLPatternModule pdlPatterns;
+#endif // MLIR_ENABLE_PDL
 };
 
 } // namespace mlir
diff --git a/mlir/include/mlir/InitAllDialects.h b/mlir/include/mlir/InitAllDialects.h
index 00f400aab5d50a0..42e91c21428d3bc 100644
--- a/mlir/include/mlir/InitAllDialects.h
+++ b/mlir/include/mlir/InitAllDialects.h
@@ -14,6 +14,7 @@
 #ifndef MLIR_INITALLDIALECTS_H_
 #define MLIR_INITALLDIALECTS_H_
 
+#include "mlir/Config/mlir-config.h"
 #include "mlir/Dialect/AMDGPU/IR/AMDGPUDialect.h"
 #include "mlir/Dialect/AMX/AMXDialect.h"
 #include "mlir/Dialect/Affine/IR/AffineOps.h"
@@ -59,8 +60,6 @@
 #include "mlir/Dialect/NVGPU/IR/NVGPUDialect.h"
 #include "mlir/Dialect/OpenACC/OpenACC.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
-#include "mlir/Dialect/PDL/IR/PDL.h"
-#include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
 #include "mlir/Dialect/Quant/QuantOps.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
 #include "mlir/Dialect/SCF/IR/ValueBoundsOpInterfaceImpl.h"
@@ -90,6 +89,10 @@
 #include "mlir/Interfaces/CastInterfaces.h"
 #include "mlir/Target/LLVM/NVVM/Target.h"
 #include "mlir/Target/LLVM/ROCDL/Target.h"
+#if MLIR_ENABLE_PDL
+#include "mlir/Dialect/PDL/IR/PDL.h"
+#include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
+#endif // MLIR_ENABLE_PDL
 
 namespace mlir {
 
@@ -123,8 +126,10 @@ inline void registerAllDialects(DialectRegistry &registry) {
                   nvgpu::NVGPUDialect,
                   NVVM::NVVMDialect,
                   omp::OpenMPDialect,
+#if MLIR_ENABLE_PDL
                   pdl::PDLDialect,
                   pdl_interp::PDLInterpDialect,
+#endif // MLIR_ENABLE_PDL
                   quant::QuantizationDialect,
                   ROCDL::ROCDLDialect,
                   scf::SCFDialect,
diff --git a/mlir/include/mlir/InitAllExtensions.h b/mlir/include/mlir/InitAllExtensions.h
index 8e2ad3a2e34f60e..28db193c6aa3286 100644
--- a/mlir/include/mlir/InitAllExtensions.h
+++ b/mlir/include/mlir/InitAllExtensions.h
@@ -14,6 +14,7 @@
 #ifndef MLIR_INITALLEXTENSIONS_H_
 #define MLIR_INITALLEXTENSIONS_H_
 
+#include "mlir/Config/mlir-config.h"
 #include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
 #include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h"
 #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
@@ -24,7 +25,6 @@
 #include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
 #include "mlir/Conversion/UBToLLVM/UBToLLVM.h"
 #include "mlir/Dialect/Affine/TransformOps/AffineTransformOps.h"
-#include "mlir/Dialect/Bufferization/TransformOps/BufferizationTransformOps.h"
 #include "mlir/Dialect/Func/Extensions/AllExtensions.h"
 #include "mlir/Dialect/Func/TransformOps/FuncTransformOps.h"
 #include "mlir/Dialect/GPU/TransformOps/GPUTransformOps.h"
@@ -34,12 +34,15 @@
 #include "mlir/Dialect/SCF/TransformOps/SCFTransformOps.h"
 #include "mlir/Dialect/SparseTensor/TransformOps/SparseTensorTransformOps.h"
 #include "mlir/Dialect/Tensor/TransformOps/TensorTransformOps.h"
-#include "mlir/Dialect/Transform/PDLExtension/PDLExtension.h"
 #include "mlir/Dialect/Vector/TransformOps/VectorTransformOps.h"
 #include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h"
+#if MLIR_ENABLE_PDL
+#include "mlir/Dialect/Bufferization/TransformOps/BufferizationTransformOps.h"
+#include "mlir/Dialect/Transform/PDLExtension/PDLExtension.h"
+#endif // MLIR_ENABLE_PDL
 
 #include <cstdlib>
 
@@ -65,7 +68,6 @@ inline void registerAllExtensions(DialectRegistry &registry) {
 
   // Register all transform dialect extensions.
   affine::registerTransformDialectExtension(registry);
-  bufferization::registerTransformDialectExtension(registry);
   func::registerTransformDialectExtension(registry);
   gpu::registerTransformDialectExtension(registry);
   linalg::registerTransformDialectExtension(registry);
@@ -74,7 +76,10 @@ inline void registerAllExtensions(DialectRegistry &registry) {
   scf::registerTransformDialectExtension(registry);
   sparse_tensor::registerTransformDialectExtension(registry);
   tensor::registerTransformDialectExtension(registry);
+#if MLIR_ENABLE_PDL
+  bufferization::registerTransformDialectExtension(registry);
   transform::registerPDLExtension(registry);
+#endif
   vector::registerTransformDialectExtension(registry);
 
   // Translation extensions need to be registered by calling
diff --git a/mlir/include/mlir/Rewrite/FrozenRewritePatternSet.h b/mlir/include/mlir/Rewrite/FrozenRewritePatternSet.h
index 4c6e3cd9ce6f4cd..5c2edade10f0ced 100644
--- a/mlir/include/mlir/Rewrite/FrozenRewritePatternSet.h
+++ b/mlir/include/mlir/Rewrite/FrozenRewritePatternSet.h
@@ -12,9 +12,11 @@
 #include "mlir/IR/PatternMatch.h"
 
 namespace mlir {
+#if MLIR_ENABLE_PDL
 namespace detail {
 class PDLByteCode;
 } // namespace detail
+#endif // MLIR_ENABLE_PDL
 
 /// This class represents a frozen set of patterns that can be processed by a
 /// pattern applicator. This class is designed to enable caching pattern lists
@@ -64,11 +66,13 @@ class FrozenRewritePatternSet {
     return llvm::make_pointee_range(nativeList);
   }
 
+#if MLIR_ENABLE_PDL
   /// Return the compiled PDL bytecode held by this list. Returns null if
   /// there are no PDL patterns within the list.
   const detail::PDLByteCode *getPDLByteCode() const {
     return impl->pdlByteCode.get();
   }
+#endif // MLIR_ENABLE_PDL
 
 private:
   /// The internal implementation of the frozen pattern list.
@@ -85,8 +89,10 @@ class FrozenRewritePatternSet {
     /// operation.
     NativePatternListT nativeAnyOpPatterns;
 
+#if MLIR_ENABLE_PDL
     /// The bytecode containing the compiled PDL patterns.
     std::unique_ptr<detail::PDLByteCode> pdlByteCode;
+#endif // MLIR_ENABLE_PDL
   };
 
   /// A pointer to the internal pattern list. This uses a shared_ptr to avoid
diff --git a/mlir/include/mlir/Rewrite/PatternApplicator.h b/mlir/include/mlir/Rewrite/PatternApplicator.h
index f7871f819a273b2..1c0f94c1bcc54dc 100644
--- a/mlir/include/mlir/Rewrite/PatternApplicator.h
+++ b/mlir/include/mlir/Rewrite/PatternApplicator.h
@@ -21,9 +21,11 @@
 namespace mlir {
 class PatternRewriter;
 
+#if MLIR_ENABLE_PDL
 namespace detail {
 class PDLByteCodeMutableState;
 } // namespace detail
+#endif // MLIR_ENABLE_PDL
 
 /// This is the type of Action that is dispatched when a pattern is applied.
 /// It captures the pattern to apply on top of the usual context.
@@ -92,8 +94,11 @@ class PatternApplicator {
   /// The set of patterns that may match against any operation type, stable
   /// sorted by benefit.
   SmallVector<const RewritePattern *, 1> anyOpPatterns;
+
+#if MLIR_ENABLE_PDL
   /// The mutable state used during execution of the PDL bytecode.
   std::unique_ptr<detail::PDLByteCodeMutableState> mutableByteCodeState;
+#endif // MLIR_ENABLE_PDL
 };
 
 } // namespace mlir
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h b/mlir/include/mlir/Transforms/DialectConversion.h
index 6de981d35c8c3ab..9c5e3aa4e7e42a9 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -1015,6 +1015,7 @@ class ConversionTarget {
   MLIRContext &ctx;
 };
 
+#if MLIR_ENABLE_PDL
 //===----------------------------------------------------------------------===//
 // PDL Configuration
 //===----------------------------------------------------------------------===//
@@ -1043,6 +1044,7 @@ class PDLConversionConfig final
 
 /// Register the dialect conversion PDL functions with the given pattern set.
 void registerConversionPDLFunctions(RewritePatternSet &patterns);
+#endif // MLIR_ENABLE_PDL
 
 //===----------------------------------------------------------------------===//
 // Op Conversion Entry Points
diff --git a/mlir/lib/CAPI/Dialect/CMakeLists.txt b/mlir/lib/CAPI/Dialect/CMakeLists.txt
index d815eba48d9b9df..73488d4b41bc84a 100644
--- a/mlir/lib/CAPI/Dialect/CMakeLists.txt
+++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt
@@ -207,14 +207,16 @@ add_mlir_upstream_c_api_library(MLIRCAPIOpenMP
   MLIROpenMPDialect
 )
 
-add_mlir_upstream_c_api_library(MLIRCAPIPDL
-  PDL.cpp
-
-  PARTIAL_SOURCES_INTENDED
-  LINK_LIBS PUBLIC
-  MLIRCAPIIR
-  MLIRPDLDialect
-)
+if(MLIR_ENABLE_PDL)
+  add_mlir_upstream_c_api_library(MLIRCAPIPDL
+    PDL.cpp
+
+    PARTIAL_SOURCES_INTENDED
+    LINK_LIBS PUBLIC
+    MLIRCAPIIR
+    MLIRPDLDialect
+  )
+endif()
 
 add_mlir_upstream_c_api_library(MLIRCAPIVector
   Vector.cpp
diff --git a/mlir/lib/Conversion/CMakeLists.txt b/mlir/lib/Conversion/CMakeLists.txt
index 7e1c7bcf9a8678a..fa594a784cafb69 100644
--- a/mlir/lib/Conversion/CMakeLists.txt
+++ b/mlir/lib/Conversion/CMakeLists.txt
@@ -37,7 +37,9 @@ add_subdirectory(NVGPUToNVVM)
 add_subdirectory(NVVMToLLVM)
 add_subdirectory(OpenACCToSCF)
 add_subdirectory(OpenMPToLLVM)
-add_subdirectory(PDLToPDLInterp)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(PDLToPDLInterp)
+endif()
 add_subdirectory(ReconcileUnrealizedCasts)
 add_subdirectory(SCFToControlFlow)
 add_subdirectory(SCFToEmitC)
diff --git a/mlir/lib/Dialect/Bufferization/CMakeLists.txt b/mlir/lib/Dialect/Bufferization/CMakeLists.txt
index 215ec562c9818c8..a6ebca41ff3487a 100644
--- a/mlir/lib/Dialect/Bufferization/CMakeLists.txt
+++ b/mlir/lib/Dialect/Bufferization/CMakeLists.txt
@@ -1,4 +1,6 @@
 add_subdirectory(IR)
 add_subdirectory(Pipelines)
-add_subdirectory(TransformOps)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(TransformOps)
+endif()
 add_subdirectory(Transforms)
diff --git a/mlir/lib/Dialect/Bufferization/TransformOps/CMakeLists.txt b/mlir/lib/Dialect/Bufferization/TransformOps/CMakeLists.txt
index 7f7b348b17ae680..be5eb73b91229f8 100644
--- a/mlir/lib/Dialect/Bufferization/TransformOps/CMakeLists.txt
+++ b/mlir/lib/Dialect/Bufferization/TransformOps/CMakeLists.txt
@@ -14,7 +14,6 @@ add_mlir_dialect_library(MLIRBufferizationTransformOps
   MLIRFunctionInterfaces
   MLIRLinalgDialect
   MLIRParser
-  MLIRPDLDialect
   MLIRSideEffectInterfaces
   MLIRTransformDialect
   )
diff --git a/mlir/lib/Dialect/CMakeLists.txt b/mlir/lib/Dialect/CMakeLists.txt
index 68776a695cac4d4..ae7c1d50e47939f 100644
--- a/mlir/lib/Dialect/CMakeLists.txt
+++ b/mlir/lib/Dialect/CMakeLists.txt
@@ -25,8 +25,10 @@ add_subdirectory(NVGPU)
 add_subdirectory(OpenACC)
 add_subdirectory(OpenACCMPCommon)
 add_subdirectory(OpenMP)
-add_subdirectory(PDL)
-add_subdirectory(PDLInterp)
+if (MLIR_ENABLE_PDL)
+  add_subdirectory(PDL)
+  add_subdirectory(PDLInterp)
+endif()
 add_subdirectory(Quant)
 add_subdirectory(SCF)
 add_subdirectory(Shape)
diff --git a/mlir/lib/Dialect/Transform/CMakeLists.txt b/mlir/lib/Dialect/Transform/CMakeLists.txt
index 9e144eba25710dd..d2e71391fd262e2 100644
--- a/mlir/lib/Dialect/Transform/CMakeLists.txt
+++ b/mlir/lib/Dialect/Transform/CMakeLists.txt
@@ -1,4 +1,6 @@
 add_subdirectory(IR)
-add_subdirectory(PDLExtension)
+if (MLIR_ENABLE_PDL)
+  add_subdirectory(PDLExtension)
+endif()
 add_subdirectory(Transforms)
 add_subdirectory(Utils)
diff --git a/mlir/lib/IR/PatternMatch.cpp b/mlir/lib/IR/PatternMatch.cpp
index 5e9b9b2a810a4c5..a95b669557aab1f 100644
--- a/mlir/lib/IR/PatternMatch.cpp
+++ b/mlir/lib/IR/PatternMatch.cpp
@@ -97,6 +97,7 @@ LogicalResult RewritePattern::match(Operation *op) const {
 /// Out-of-line vtable anchor.
 void RewritePattern::anchor() {}
 
+#if MLIR_ENABLE_PDL
 //===----------------------------------------------------------------------===//
 // PDLValue
 //===----------------------------------------------------------------------===//
@@ -214,6 +215,7 @@ void PDLPatternModule::registerRewriteFunction(StringRef name,
   // rewrite.
   rewriteFunctions.try_emplace(name, std::move(rewriteFn));
 }
+#endif // MLIR_ENABLE_PDL
 
 //===----------------------------------------------------------------------===//
 // RewriterBase
diff --git a/mlir/lib/Rewrite/CMakeLists.txt b/mlir/lib/Rewrite/CMakeLists.txt
index e0395be6cd6f598..3f0ad81e96967cf 100644
--- a/mlir/lib/Rewrite/CMakeLists.txt
+++ b/mlir/lib/Rewrite/CMakeLists.txt
@@ -1,5 +1,23 @@
+set(LLVM_OPTIONAL_SOURCES ByteCode.cpp)
+
+if(MLIR_ENABLE_PDL)
+  set(MLIRRewritePDLDeps
+    MLIRPDLDialect
+    MLIRPDLInterpDialect
+    MLIRPDLToPDLInterp)
+else()
+  set(MLIRRewritePDLDeps)
+endif()
+
+if(MLIR_ENABLE_PDL)
+  set(MLIRRewritePDLSource
+    ByteCode.cpp)
+else()
+  set(MLIRRewritePDLSource)
+endif()
+
 add_mlir_library(MLIRRewrite
-  ByteCode.cpp
+  ${MLIRRewritePDLSource}
   FrozenRewritePatternSet.cpp
   PatternApplicator.cpp
 
@@ -11,8 +29,6 @@ add_mlir_library(MLIRRewrite
 
   LINK_LIBS PUBLIC
   MLIRIR
-  MLIRPDLDialect
-  MLIRPDLInterpDialect
-  MLIRPDLToPDLInterp
+  ${MLIRRewritePDLDeps}
   MLIRSideEffectInterfaces
   )
diff --git a/mlir/lib/Rewrite/FrozenRewritePatternSet.cpp b/mlir/lib/Rewrite/FrozenRewritePatternSet.cpp
index 43840d1e8cec2f7..8793a6791ad94e7 100644
--- a/mlir/lib/Rewrite/FrozenRewritePatternSet.cpp
+++ b/mlir/lib/Rewrite/FrozenRewritePatternSet.cpp
@@ -7,16 +7,20 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Rewrite/FrozenRewritePatternSet.h"
-#include "ByteCode.h"
-#include "mlir/Conversion/PDLToPDLInterp/PDLToPDLInterp.h"
-#include "mlir/Dialect/PDL/IR/PDLOps.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 #include "mlir/Pass/Pass.h"
 #include "mlir/Pass/PassManager.h"
 #include <optional>
 
+#if MLIR_ENABLE_PDL
+#include "ByteCode.h"
+#include "mlir/Conversion/PDLToPDLInterp/PDLToPDLInterp.h"
+#include "mlir/Dialect/PDL/IR/PDLOps.h"
+#endif // MLIR_ENABLE_PDL
+
 using namespace mlir;
 
+#if MLIR_ENABLE_PDL
 static LogicalResult
 convertPDLToPDLInterp(ModuleOp pdlModule,
                       DenseMap<Operation *, PDLPatternConfigSet *> &configMap) {
@@ -48,6 +52,7 @@ convertPDLToPDLInterp(ModuleOp pdlModule,
   pdlModule.getBody()->walk(simplifyFn);
   return success();
 }
+#endif // MLIR_ENABLE_PDL
 
 //===----------------------------------------------------------------------===//
 // FrozenRewritePatternSet
@@ -121,6 +126,7 @@ FrozenRewritePatternSet::FrozenRewritePatternSet(
     impl->nativeAnyOpPatterns.push_back(std::move(pat));
   }
 
+#if MLIR_ENABLE_PDL
   // Generate the bytecode for the PDL patterns if any were provided.
   PDLPatternModule &pdlPatterns = patterns.getPDLPatterns();
   ModuleOp pdlModule = pdlPatterns.getModule();
@@ -137,6 +143,7 @@ FrozenRewritePatternSet::FrozenRewritePatternSet(
       pdlModule, pdlPatterns.takeConfigs(), configMap,
       pdlPatterns.takeConstraintFunctions(),
       pdlPatterns.takeRewriteFunctions());
+#endif // MLIR_ENABLE_PDL
 }
 
 FrozenRewritePatternSet::~FrozenRewritePatternSet() = default;
diff --git a/mlir/lib/Rewrite/PatternApplicator.cpp b/mlir/lib/Rewrite/PatternApplicator.cpp
index 08d6ee618ac690f..157de4bff06400e 100644
--- a/mlir/lib/Rewrite/PatternApplicator.cpp
+++ b/mlir/lib/Rewrite/PatternApplicator.cpp
@@ -12,8 +12,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Rewrite/PatternApplicator.h"
-#include "ByteCode.h"
 #include "llvm/Support/Debug.h"
+#if MLIR_ENABLE_PDL
+#include "ByteCode.h"
+#endif // MLIR_ENABLE_PDL
 
 #define DEBUG_TYPE "pattern-application"
 
@@ -23,10 +25,12 @@ using namespace mlir::detail;
 PatternApplicator::PatternApplicator(
     const FrozenRewritePatternSet &frozenPatternList)
     : frozenPatternList(frozenPatternList) {
+#if MLIR_ENABLE_PDL
   if (const PDLByteCode *bytecode = frozenPatternList.getPDLByteCode()) {
     mutableByteCodeState = std::make_unique<PDLByteCodeMutableState>();
     bytecode->initializeMutableState(*mutableByteCodeState);
   }
+#endif // MLIR_ENABLE_PDL
 }
 PatternApplicator::~PatternApplicator() = default;
 
@@ -50,12 +54,14 @@ static void logSucessfulPatternApplication(Operation *op) {
 #endif
 
 void PatternApplicator::applyCostModel(CostModel model) {
+#if MLIR_ENABLE_PDL
   // Apply the cost model to the bytecode patterns first, and then the native
   // patterns.
   if (const PDLByteCode *bytecode = frozenPatternList.getPDLByteCode()) {
     for (const auto &it : llvm::enumerate(bytecode->getPatterns()))
       mutableByteCodeState->updatePatternBenefit(it.index(), model(it.value()));
   }
+#endif // MLIR_ENABLE_PDL
 
   // Copy over the patterns so that we can sort by benefit based on the cost
   // model. Patterns that are already impossible to match are ignored.
@@ -117,10 +123,12 @@ void PatternApplicator::walkAllPatterns(
       walk(*pattern);
   for (const Pattern &it : frozenPatternList.getMatchAnyOpNativePatterns())
     walk(it);
+#if MLIR_ENABLE_PDL
   if (const PDLByteCode *bytecode = frozenPatternList.getPDLByteCode()) {
     for (const Pattern &it : bytecode->getPatterns())
       walk(it);
   }
+#endif // MLIR_ENABLE_PDL
 }
 
 LogicalResult PatternApplicator::matchAndRewrite(
@@ -128,6 +136,7 @@ LogicalResult PatternApplicator::matchAndRewrite(
     function_ref<bool(const Pattern &)> canApply,
     function_ref<void(const Pattern &)> onFailure,
     function_ref<LogicalResult(const Pattern &)> onSuccess) {
+#if MLIR_ENABLE_PDL
   // Before checking native patterns, first match against the bytecode. This
   // won't automatically perform any rewrites so there is no need to worry about
   // conflicts.
@@ -135,6 +144,7 @@ LogicalResult PatternApplicator::matchAndRewrite(
   const PDLByteCode *bytecode = frozenPatternList.getPDLByteCode();
   if (bytecode)
     bytecode->match(op, rewriter, pdlMatches, *mutableByteCodeState);
+#endif // MLIR_ENABLE_PDL
 
   // Check to see if there are patterns matching this specific operation type.
   MutableArrayRef<const RewritePattern *> opPatterns;
@@ -146,13 +156,14 @@ LogicalResult PatternApplicator::matchAndRewrite(
   // operation type in an interleaved fashion.
   unsigned opIt = 0, opE = opPatterns.size();
   unsigned anyIt = 0, anyE = anyOpPatterns.size();
+#if MLIR_ENABLE_PDL
   unsigned pdlIt = 0, pdlE = pdlMatches.size();
+#endif // MLIR_ENABLE_PDL
   LogicalResult result = failure();
   do {
     // Find the next pattern with the highest benefit.
     const Pattern *bestPattern = nullptr;
     unsigned *bestPatternIt = &opIt;
-    const PDLByteCode::MatchResult *pdlMatch = nullptr;
 
     /// Operation specific patterns.
     if (opIt < opE)
@@ -164,6 +175,9 @@ LogicalResult PatternApplicator::matchAndRewrite(
       bestPatternIt = &anyIt;
       bestPattern = anyOpPatterns[anyIt];
     }
+
+#if MLIR_ENABLE_PDL
+    const PDLByteCode::MatchResult *pdlMatch = nullptr;
     /// PDL patterns.
     if (pdlIt < pdlE && (!bestPattern || bestPattern->getBenefit() <
                                              pdlMatches[pdlIt].benefit)) {
@@ -171,6 +185,8 @@ LogicalResult PatternApplicator::matchAndRewrite(
       pdlMatch = &pdlMatches[pdlIt];
       bestPattern = pdlMatch->pattern;
     }
+#endif // MLIR_ENABLE_PDL
+
     if (!bestPattern)
       break;
 
@@ -194,10 +210,14 @@ LogicalResult PatternApplicator::matchAndRewrite(
           // pattern.
           Operation *dumpRootOp = getDumpRootOp(op);
 #endif
+#if MLIR_ENABLE_PDL
           if (pdlMatch) {
             result =
                 bytecode->rewrite(rewriter, *pdlMatch, *mutableByteCodeState);
           } else {
+#else
+          {
+#endif // MLIR_ENABLE_PDL
             LLVM_DEBUG(llvm::dbgs() << "Trying to match \""
                                     << bestPattern->getDebugName() << "\"\n");
 
@@ -228,7 +248,9 @@ LogicalResult PatternApplicator::matchAndRewrite(
       break;
   } while (true);
 
+#if MLIR_ENABLE_PDL
   if (mutableByteCodeState)
     mutableByteCodeState->cleanupAfterMatchAndRewrite();
+#endif
   return result;
 }
diff --git a/mlir/lib/Tools/CMakeLists.txt b/mlir/lib/Tools/CMakeLists.txt
index 01270fa4b0fc341..54f03bd711d1683 100644
--- a/mlir/lib/Tools/CMakeLists.txt
+++ b/mlir/lib/Tools/CMakeLists.txt
@@ -1,11 +1,13 @@
 add_subdirectory(lsp-server-support)
 add_subdirectory(mlir-lsp-server)
 add_subdirectory(mlir-opt)
-add_subdirectory(mlir-pdll-lsp-server)
 add_subdirectory(mlir-query)
 add_subdirectory(mlir-reduce)
 add_subdirectory(mlir-tblgen)
 add_subdirectory(mlir-translate)
-add_subdirectory(PDLL)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(PDLL)
+  add_subdirectory(mlir-pdll-lsp-server)
+endif()	
 add_subdirectory(Plugins)
 add_subdirectory(tblgen-lsp-server)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 4d2afe462b9281d..f9e0db11a92e2ce 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -3312,6 +3312,7 @@ auto ConversionTarget::getOpInfo(OperationName op) const
   return std::nullopt;
 }
 
+#if MLIR_ENABLE_PDL
 //===----------------------------------------------------------------------===//
 // PDL Configuration
 //===----------------------------------------------------------------------===//
@@ -3382,6 +3383,7 @@ void mlir::registerConversionPDLFunctions(RewritePatternSet &patterns) {
         return std::move(remappedTypes);
       });
 }
+#endif // MLIR_ENABLE_PDL
 
 //===----------------------------------------------------------------------===//
 // Op Conversion Entry Points
diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index 88e6e13602d291a..9cf3af082c08319 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -142,14 +142,16 @@ declare_mlir_dialect_python_bindings(
   DIALECT_NAME llvm
   GEN_ENUM_BINDINGS)
 
-declare_mlir_dialect_extension_python_bindings(
-ADD_TO_PARENT MLIRPythonSources.Dialects
-ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
-  TD_FILE dialects/TransformPDLExtensionOps.td
-  SOURCES
-    dialects/transform/pdl.py
-  DIALECT_NAME transform
-  EXTENSION_NAME transform_pdl_extension)
+if(MLIR_ENABLE_PDL)
+  declare_mlir_dialect_extension_python_bindings(
+  ADD_TO_PARENT MLIRPythonSources.Dialects
+  ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
+    TD_FILE dialects/TransformPDLExtensionOps.td
+    SOURCES
+      dialects/transform/pdl.py
+    DIALECT_NAME transform
+    EXTENSION_NAME transform_pdl_extension)
+endif()
 
 declare_mlir_dialect_python_bindings(
   ADD_TO_PARENT MLIRPythonSources.Dialects
@@ -317,14 +319,16 @@ declare_mlir_python_sources(
     dialects/quant.py
     _mlir_libs/_mlir/dialects/quant.pyi)
 
-declare_mlir_dialect_python_bindings(
-  ADD_TO_PARENT MLIRPythonSources.Dialects
-  ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
-  TD_FILE dialects/PDLOps.td
-  SOURCES
-    dialects/pdl.py
-    _mlir_libs/_mlir/dialects/pdl.pyi
-  DIALECT_NAME pdl)
+if(MLIR_ENABLE_PDL)
+  declare_mlir_dialect_python_bindings(
+    ADD_TO_PARENT MLIRPythonSources.Dialects
+    ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
+    TD_FILE dialects/PDLOps.td
+    SOURCES
+      dialects/pdl.py
+      _mlir_libs/_mlir/dialects/pdl.pyi
+    DIALECT_NAME pdl)
+endif()
 
 declare_mlir_dialect_python_bindings(
   ADD_TO_PARENT MLIRPythonSources.Dialects
@@ -469,18 +473,20 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Pybind
     MLIRCAPIQuant
 )
 
-declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Pybind
-  MODULE_NAME _mlirDialectsPDL
-  ADD_TO_PARENT MLIRPythonSources.Dialects.pdl
-  ROOT_DIR "${PYTHON_SOURCE_DIR}"
-  SOURCES
-    DialectPDL.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
-  EMBED_CAPI_LINK_LIBS
-    MLIRCAPIIR
-    MLIRCAPIPDL
-)
+if(MLIR_ENABLE_PDL)
+  declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Pybind
+    MODULE_NAME _mlirDialectsPDL
+    ADD_TO_PARENT MLIRPythonSources.Dialects.pdl
+    ROOT_DIR "${PYTHON_SOURCE_DIR}"
+    SOURCES
+      DialectPDL.cpp
+    PRIVATE_LINK_LIBS
+      LLVMSupport
+    EMBED_CAPI_LINK_LIBS
+      MLIRCAPIIR
+      MLIRCAPIPDL
+  )
+endif()
 
 declare_mlir_python_extension(MLIRPythonExtension.Dialects.SparseTensor.Pybind
   MODULE_NAME _mlirDialectsSparseTensor
diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt
index 16a3d0ed9c62fbf..ad2e875143b1e27 100644
--- a/mlir/test/CAPI/CMakeLists.txt
+++ b/mlir/test/CAPI/CMakeLists.txt
@@ -54,13 +54,15 @@ _add_capi_test_executable(mlir-capi-pass-test
     MLIRCAPITransforms
 )
 
-_add_capi_test_executable(mlir-capi-pdl-test
-  pdl.c
-  LINK_LIBS PRIVATE
-    MLIRCAPIIR
-    MLIRCAPIRegisterEverything
-    MLIRCAPIPDL
-)
+if(MLIR_ENABLE_PDL)
+  _add_capi_test_executable(mlir-capi-pdl-test
+    pdl.c
+    LINK_LIBS PRIVATE
+      MLIRCAPIIR
+      MLIRCAPIRegisterEverything
+      MLIRCAPIPDL
+  )
+endif()
 
 _add_capi_test_executable(mlir-capi-sparse-tensor-test
   sparse_tensor.c
diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt
index 6fc9ae0f3fc58fa..d48f05d9874c646 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -95,15 +95,12 @@ set(MLIR_TEST_DEPENDS
   mlir-capi-ir-test
   mlir-capi-llvm-test
   mlir-capi-pass-test
-  mlir-capi-pdl-test
   mlir-capi-quant-test
   mlir-capi-sparse-tensor-test
   mlir-capi-transform-test
   mlir-linalg-ods-yaml-gen
   mlir-lsp-server
-  mlir-pdll-lsp-server
   mlir-opt
-  mlir-pdll
   mlir-query
   mlir-reduce
   mlir-tblgen
@@ -111,6 +108,14 @@ set(MLIR_TEST_DEPENDS
   tblgen-lsp-server
   )
 
+if(MLIR_ENABLE_PDL)
+  set(MLIR_TEST_DEPENDS ${MLIR_TEST_DEPENDS}
+    mlir-capi-pdl-test
+    mlir-pdll-lsp-server
+    mlir-pdll
+    )
+endif()
+
 # The native target may not be enabled, in this case we won't
 # run tests that involves executing on the host: do not build
 # useless binaries.
@@ -147,10 +152,13 @@ if(LLVM_BUILD_EXAMPLES)
     toyc-ch3
     toyc-ch4
     toyc-ch5
-    transform-opt-ch2
-    transform-opt-ch3
-    mlir-minimal-opt
     )
+  if(MLIR_ENABLE_PDL)
+    list(APPEND MLIR_TEST_DEPENDS
+      transform-opt-ch2
+      transform-opt-ch3
+      )
+  endif()
   if(MLIR_ENABLE_EXECUTION_ENGINE)
     list(APPEND MLIR_TEST_DEPENDS
       toyc-ch6
diff --git a/mlir/test/lib/Dialect/CMakeLists.txt b/mlir/test/lib/Dialect/CMakeLists.txt
index 48bde69e0170041..27e6a5c469602d9 100644
--- a/mlir/test/lib/Dialect/CMakeLists.txt
+++ b/mlir/test/lib/Dialect/CMakeLists.txt
@@ -17,5 +17,8 @@ add_subdirectory(Tensor)
 add_subdirectory(Test)
 add_subdirectory(TestDyn)
 add_subdirectory(Tosa)
-add_subdirectory(Transform)
+if(MLIR_ENABLE_PDL)
+  # Depends on PDL for testing.
+  add_subdirectory(Transform)
+endif()
 add_subdirectory(Vector)
diff --git a/mlir/test/lib/Rewrite/CMakeLists.txt b/mlir/test/lib/Rewrite/CMakeLists.txt
index fd5d5d586160188..e5c73613aaa84a4 100644
--- a/mlir/test/lib/Rewrite/CMakeLists.txt
+++ b/mlir/test/lib/Rewrite/CMakeLists.txt
@@ -1,4 +1,5 @@
 # Exclude tests from libMLIR.so
+if(MLIR_ENABLE_PDL)
 add_mlir_library(MLIRTestRewrite
   TestPDLByteCode.cpp
 
@@ -13,4 +14,4 @@ add_mlir_library(MLIRTestRewrite
   MLIRSupport
   MLIRTransformUtils
   )
-
+endif()
diff --git a/mlir/test/lib/Tools/CMakeLists.txt b/mlir/test/lib/Tools/CMakeLists.txt
index 5aa03a3833812eb..5b8e02a14b22eed 100644
--- a/mlir/test/lib/Tools/CMakeLists.txt
+++ b/mlir/test/lib/Tools/CMakeLists.txt
@@ -1 +1,3 @@
-add_subdirectory(PDLL)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(PDLL)
+endif()
diff --git a/mlir/test/lib/Transforms/CMakeLists.txt b/mlir/test/lib/Transforms/CMakeLists.txt
index e032ce7200fbf80..ee7c215a430205e 100644
--- a/mlir/test/lib/Transforms/CMakeLists.txt
+++ b/mlir/test/lib/Transforms/CMakeLists.txt
@@ -1,22 +1,32 @@
-add_mlir_pdll_library(MLIRTestDialectConversionPDLLPatternsIncGen
-  TestDialectConversion.pdll
-  TestDialectConversionPDLLPatterns.h.inc
+set(LLVM_OPTIONAL_SOURCES 
+  TestDialectConversion.cpp)
+set(MLIRTestTransformsPDLDep)
+set(MLIRTestTransformsPDLSrc)
+if(MLIR_ENABLE_PDL)
+  add_mlir_pdll_library(MLIRTestDialectConversionPDLLPatternsIncGen
+    TestDialectConversion.pdll
+    TestDialectConversionPDLLPatterns.h.inc
 
-  EXTRA_INCLUDES
-  ${CMAKE_CURRENT_SOURCE_DIR}/../Dialect/Test
-  ${CMAKE_CURRENT_BINARY_DIR}/../Dialect/Test
-  )
+    EXTRA_INCLUDES
+    ${CMAKE_CURRENT_SOURCE_DIR}/../Dialect/Test
+    ${CMAKE_CURRENT_BINARY_DIR}/../Dialect/Test
+    )
+    set(MLIRTestTransformsPDLSrc
+      TestDialectConversion.cpp)
+    set(MLIRTestTransformsPDLDep
+      MLIRTestDialectConversionPDLLPatternsIncGen)
+endif()
 
 # Exclude tests from libMLIR.so
 add_mlir_library(MLIRTestTransforms
   TestCommutativityUtils.cpp
   TestConstantFold.cpp
   TestControlFlowSink.cpp
-  TestDialectConversion.cpp
   TestInlining.cpp
   TestIntRangeInference.cpp
   TestMakeIsolatedFromAbove.cpp
   TestTopologicalSort.cpp
+  ${MLIRTestTransformsPDLSrc}
 
   EXCLUDE_FROM_LIBMLIR
 
@@ -24,7 +34,7 @@ add_mlir_library(MLIRTestTransforms
   ${MLIR_MAIN_INCLUDE_DIR}/mlir/Transforms
 
   DEPENDS
-  MLIRTestDialectConversionPDLLPatternsIncGen
+  ${MLIRTestTransformsPDLDep}
 
   LINK_LIBS PUBLIC
   MLIRAnalysis
diff --git a/mlir/tools/CMakeLists.txt b/mlir/tools/CMakeLists.txt
index a01f74f737e1bc1..f7313077e9bf35b 100644
--- a/mlir/tools/CMakeLists.txt
+++ b/mlir/tools/CMakeLists.txt
@@ -1,7 +1,9 @@
 add_subdirectory(mlir-lsp-server)
 add_subdirectory(mlir-opt)
 add_subdirectory(mlir-parser-fuzzer)
-add_subdirectory(mlir-pdll-lsp-server)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(mlir-pdll-lsp-server)
+endif()
 add_subdirectory(mlir-query)
 add_subdirectory(mlir-reduce)
 add_subdirectory(mlir-shlib)
diff --git a/mlir/tools/mlir-lsp-server/CMakeLists.txt b/mlir/tools/mlir-lsp-server/CMakeLists.txt
index e90ccf17af17f54..8b960d7977d3413 100644
--- a/mlir/tools/mlir-lsp-server/CMakeLists.txt
+++ b/mlir/tools/mlir-lsp-server/CMakeLists.txt
@@ -21,10 +21,14 @@ if(MLIR_INCLUDE_TESTS)
     MLIRTestIR
     MLIRTestPass
     MLIRTestReducer
-    MLIRTestRewrite
-    MLIRTestTransformDialect
-    MLIRTestTransforms
     )
+  if(MLIR_ENABLE_PDL)
+    set(test_libs
+      ${test_libs}
+      MLIRTestRewrite
+      MLIRTestTransformDialect
+      MLIRTestTransforms)
+  endif()
 endif()
 
 set(LIBS
diff --git a/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp b/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp
index f0ecc5adc68b36c..4d6b83da05d8b11 100644
--- a/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp
+++ b/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp
@@ -18,7 +18,9 @@ using namespace mlir;
 namespace test {
 void registerTestDialect(DialectRegistry &);
 void registerTestDynDialect(DialectRegistry &);
+#if MLIR_ENABLE_PDL
 void registerTestTransformDialectExtension(DialectRegistry &);
+#endif // MLIR_ENABLE_PDL
 } // namespace test
 #endif
 
@@ -29,7 +31,9 @@ int main(int argc, char **argv) {
 
 #ifdef MLIR_INCLUDE_TESTS
   ::test::registerTestDialect(registry);
+#if MLIR_ENABLE_PDL
   ::test::registerTestTransformDialectExtension(registry);
+#endif // MLIR_ENABLE_PDL
   ::test::registerTestDynDialect(registry);
 #endif
   return failed(MlirLspServerMain(argc, argv, registry));
diff --git a/mlir/tools/mlir-opt/CMakeLists.txt b/mlir/tools/mlir-opt/CMakeLists.txt
index 88a0562cb6e7207..fd9b61d7bd7f31c 100644
--- a/mlir/tools/mlir-opt/CMakeLists.txt
+++ b/mlir/tools/mlir-opt/CMakeLists.txt
@@ -37,16 +37,20 @@ if(MLIR_INCLUDE_TESTS)
     MLIRTestIR
     MLIRTestOneToNTypeConversionPass
     MLIRTestPass
-    MLIRTestPDLL
     MLIRTestReducer
-    MLIRTestRewrite
-    MLIRTestTransformDialect
     MLIRTestTransforms
     MLIRTilingInterfaceTestPasses
     MLIRVectorTestPasses
     MLIRTestVectorToSPIRV
     MLIRLLVMTestPasses
     )
+  if(MLIR_ENABLE_PDL)
+    set(test_libs ${test_libs}
+      MLIRTestPDLL
+      MLIRTestRewrite
+      MLIRTestTransformDialect
+      )
+  endif()
 endif()
 
 set(LIBS
diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp
index b7647d7de78a10e..45ee0a081ca48a7 100644
--- a/mlir/tools/mlir-opt/mlir-opt.cpp
+++ b/mlir/tools/mlir-opt/mlir-opt.cpp
@@ -145,8 +145,8 @@ void registerTestNvgpuLowerings();
 
 namespace test {
 void registerTestDialect(DialectRegistry &);
-void registerTestTransformDialectExtension(DialectRegistry &);
 void registerTestDynDialect(DialectRegistry &);
+void registerTestTransformDialectExtension(DialectRegistry &);
 } // namespace test
 
 #ifdef MLIR_INCLUDE_TESTS
@@ -200,7 +200,6 @@ void registerTestPasses() {
   mlir::test::registerTestConstantFold();
   mlir::test::registerTestControlFlowSink();
   mlir::test::registerTestDiagnosticsPass();
-  mlir::test::registerTestDialectConversionPasses();
 #if MLIR_CUDA_CONVERSIONS_ENABLED
   mlir::test::registerTestLowerToNVVM();
 #endif
@@ -242,8 +241,6 @@ void registerTestPasses() {
   mlir::test::registerTestOneToNTypeConversionPass();
   mlir::test::registerTestOpaqueLoc();
   mlir::test::registerTestPadFusion();
-  mlir::test::registerTestPDLByteCodePass();
-  mlir::test::registerTestPDLLPasses();
   mlir::test::registerTestRecursiveTypesPass();
   mlir::test::registerTestSCFUtilsPass();
   mlir::test::registerTestSCFWhileOpBuilderPass();
@@ -253,12 +250,17 @@ void registerTestPasses() {
   mlir::test::registerTestTensorTransforms();
   mlir::test::registerTestTilingInterface();
   mlir::test::registerTestTopologicalSortAnalysisPass();
-  mlir::test::registerTestTransformDialectEraseSchedulePass();
-  mlir::test::registerTestTransformDialectInterpreterPass();
   mlir::test::registerTestVectorLowerings();
   mlir::test::registerTestVectorReductionToSPIRVDotProd();
   mlir::test::registerTestNvgpuLowerings();
   mlir::test::registerTestWrittenToPass();
+#if MLIR_ENABLE_PDL
+  mlir::test::registerTestDialectConversionPasses();
+  mlir::test::registerTestPDLByteCodePass();
+  mlir::test::registerTestPDLLPasses();
+  mlir::test::registerTestTransformDialectEraseSchedulePass();
+  mlir::test::registerTestTransformDialectInterpreterPass();
+#endif
 }
 #endif
 
@@ -278,7 +280,9 @@ int main(int argc, char **argv) {
 
 #ifdef MLIR_INCLUDE_TESTS
   ::test::registerTestDialect(registry);
+#if MLIR_ENABLE_PDL
   ::test::registerTestTransformDialectExtension(registry);
+#endif
   ::test::registerTestDynDialect(registry);
 #endif
   return mlir::asMainReturnCode(mlir::MlirOptMain(
diff --git a/mlir/unittests/Conversion/CMakeLists.txt b/mlir/unittests/Conversion/CMakeLists.txt
index 2dee5e7dac90c4d..7ca1ee2c3f2395b 100644
--- a/mlir/unittests/Conversion/CMakeLists.txt
+++ b/mlir/unittests/Conversion/CMakeLists.txt
@@ -1 +1,3 @@
-add_subdirectory(PDLToPDLInterp)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(PDLToPDLInterp)
+endif()
diff --git a/mlir/unittests/Dialect/CMakeLists.txt b/mlir/unittests/Dialect/CMakeLists.txt
index fbb73e8f499a35e..6a1141b0694568c 100644
--- a/mlir/unittests/Dialect/CMakeLists.txt
+++ b/mlir/unittests/Dialect/CMakeLists.txt
@@ -12,5 +12,7 @@ add_subdirectory(MemRef)
 add_subdirectory(SCF)
 add_subdirectory(SparseTensor)
 add_subdirectory(SPIRV)
-add_subdirectory(Transform)
+if(MLIR_ENABLE_PDL)
+  add_subdirectory(Transform)
+endif()
 add_subdirectory(Utils)
diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 4c6ad69ae00d994..ef7b1308ff96924 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -35,6 +35,7 @@ expand_template(
     substitutions = {
         "#cmakedefine01 MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS": "#define MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS 0",
         "#cmakedefine MLIR_GREEDY_REWRITE_RANDOMIZER_SEED ${MLIR_GREEDY_REWRITE_RANDOMIZER_SEED}": "/* #undef MLIR_GREEDY_REWRITE_RANDOMIZER_SEED */",
+        "#cmakedefine MLIR_ENABLE_PDL ${MLIR_ENABLE_PDL}": "#define MLIR_ENABLE_PDLL 1",
     },
     template = "include/mlir/Config/mlir-config.h.cmake",
 )



More information about the Mlir-commits mailing list