[clang] [CIR] Enable PCH Build- (PR #222201)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 8 17:55:42 PDT 2026


https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/222201

>From 91a6f188abe06810f2722c7e116e584ccce88bd5 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 8 Sep 2026 16:15:46 -0700
Subject: [PATCH 1/4] [CIR] Enable PCH Build-

A very simple PCH file that seems to gain us about 3% on build time.  On
my personal build (with a bunch of our other optimizations) it goes
from:

real: 7m58  -->7m42
user: 612m53-->585m18
sys:  36m27 -->34m57

I suspect there are more gains to be had by adding other files here, but
this improvement seems worth doing.
---
 clang/lib/CIR/CodeGen/CMakeLists.txt          |  4 ++++
 clang/lib/CIR/CodeGen/pch.h                   | 23 +++++++++++++++++++
 clang/lib/CIR/Dialect/CMakeLists.txt          |  7 +++++-
 clang/lib/CIR/Dialect/IR/CMakeLists.txt       |  7 ++++++
 .../lib/CIR/Dialect/Transforms/CMakeLists.txt |  4 ++++
 clang/lib/CIR/Dialect/Transforms/pch.h        | 20 ++++++++++++++++
 6 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 clang/lib/CIR/CodeGen/pch.h
 create mode 100644 clang/lib/CIR/Dialect/Transforms/pch.h

diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt
index c4bd520fc61b7..3992f7c476b03 100644
--- a/clang/lib/CIR/CodeGen/CMakeLists.txt
+++ b/clang/lib/CIR/CodeGen/CMakeLists.txt
@@ -64,6 +64,10 @@ add_clang_library(clangCIR
   MLIRCIR
   MLIRCIROpInterfacesIncGen
 
+  DISABLE_PCH_REUSE # PCH contains private headers
+  PRECOMPILE_HEADERS
+  [["pch.h"]]
+
   LINK_LIBS
   clangAST
   clangBasic
diff --git a/clang/lib/CIR/CodeGen/pch.h b/clang/lib/CIR/CodeGen/pch.h
new file mode 100644
index 0000000000000..dca5bc15ce0e6
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/pch.h
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Precompiled header for clangCIR. Uses private headers.
+///
+//===----------------------------------------------------------------------===//
+
+#include "Address.h"
+#include "CIRGenBuilder.h"
+#include "CIRGenCXXABI.h"
+#include "CIRGenFunction.h"
+#include "CIRGenModule.h"
+#include "CIRGenValue.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
+#include "clang/AST/pch.h"
+#include "llvm/Support/pch.h"
diff --git a/clang/lib/CIR/Dialect/CMakeLists.txt b/clang/lib/CIR/Dialect/CMakeLists.txt
index 6aacd029d845d..8ddfd544cbe44 100644
--- a/clang/lib/CIR/Dialect/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/CMakeLists.txt
@@ -1,5 +1,10 @@
-add_subdirectory(Analysis)
+# IR must be processed first: it defines MLIRCIR, whose PCH other CIR
+# dialect libraries below (Analysis, OpenACC, OpenMP, Transforms) can
+# automatically reuse via llvm_update_pch's LLVM_PCH_PRIORITY dependency
+# scan -- that scan only sees targets that already exist, so MLIRCIR must
+# be defined before anything that wants to reuse its PCH.
 add_subdirectory(IR)
+add_subdirectory(Analysis)
 add_subdirectory(OpenACC)
 add_subdirectory(OpenMP)
 add_subdirectory(Transforms)
diff --git a/clang/lib/CIR/Dialect/IR/CMakeLists.txt b/clang/lib/CIR/Dialect/IR/CMakeLists.txt
index c8205ebeabf6c..c5fa438253460 100644
--- a/clang/lib/CIR/Dialect/IR/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/IR/CMakeLists.txt
@@ -13,6 +13,13 @@ add_clang_library(MLIRCIR
   MLIRCIROpInterfacesIncGen
   MLIRCIRLoopOpInterfaceIncGen
 
+  # CIRDialect.h is a public header and the most commonly-included header
+  # across the rest of clang/lib/CIR; building it here (rather than as a
+  # private per-library PCH) lets every other CIR library that links
+  # MLIRCIR automatically reuse this PCH too.
+  PRECOMPILE_HEADERS
+  [["clang/CIR/Dialect/IR/CIRDialect.h"]]
+
   LINK_LIBS PUBLIC
   MLIRIR
   MLIRCIRInterfaces
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 82078bf2e8f73..7208537642507 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -18,6 +18,10 @@ add_clang_library(MLIRCIRTransforms
   DEPENDS
   MLIRCIRPassIncGen
 
+  DISABLE_PCH_REUSE # PCH contains private headers
+  PRECOMPILE_HEADERS
+  [["pch.h"]]
+
   LINK_LIBS PUBLIC
   clangAST
   clangBasic
diff --git a/clang/lib/CIR/Dialect/Transforms/pch.h b/clang/lib/CIR/Dialect/Transforms/pch.h
new file mode 100644
index 0000000000000..df51a30d01e92
--- /dev/null
+++ b/clang/lib/CIR/Dialect/Transforms/pch.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Precompiled header for MLIRCIRTransforms. Uses private headers.
+///
+//===----------------------------------------------------------------------===//
+
+#include "PassDetail.h"
+#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/Dialect/Passes.h"
+#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
+#include "clang/CIR/MissingFeatures.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Transforms/DialectConversion.h"

>From 31b7015b11a5d06335bef0aa10d639d82b4ed033 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 8 Sep 2026 17:08:27 -0700
Subject: [PATCH 2/4] Clang-format

---
 clang/lib/CIR/Dialect/Transforms/pch.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CIR/Dialect/Transforms/pch.h b/clang/lib/CIR/Dialect/Transforms/pch.h
index df51a30d01e92..c5b8938ac86a1 100644
--- a/clang/lib/CIR/Dialect/Transforms/pch.h
+++ b/clang/lib/CIR/Dialect/Transforms/pch.h
@@ -11,10 +11,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "PassDetail.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Transforms/DialectConversion.h"
 #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "clang/CIR/Dialect/Passes.h"
 #include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
 #include "clang/CIR/MissingFeatures.h"
-#include "mlir/IR/PatternMatch.h"
-#include "mlir/Transforms/DialectConversion.h"

>From 82cd50c74a2e4740ece360db5afd61a2aeac599e Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 8 Sep 2026 17:48:47 -0700
Subject: [PATCH 3/4] Remove MemorySpaceInterfaces.h since it is transitively
 included by Address.h

---
 clang/lib/CIR/CodeGen/pch.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/lib/CIR/CodeGen/pch.h b/clang/lib/CIR/CodeGen/pch.h
index dca5bc15ce0e6..732397ec5ad1d 100644
--- a/clang/lib/CIR/CodeGen/pch.h
+++ b/clang/lib/CIR/CodeGen/pch.h
@@ -18,6 +18,5 @@
 #include "CIRGenValue.h"
 #include "mlir/Dialect/OpenACC/OpenACC.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
-#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
 #include "clang/AST/pch.h"
 #include "llvm/Support/pch.h"

>From 632159027ba8f1dd60792c344fbd81a0086cdcae Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 8 Sep 2026 17:54:44 -0700
Subject: [PATCH 4/4] Add a few more pch uses, this time in TargetLowering,
 which gives a partial percent improvement

---
 .../Transforms/TargetLowering/CMakeLists.txt      |  4 ++++
 .../CIR/Dialect/Transforms/TargetLowering/pch.h   | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 clang/lib/CIR/Dialect/Transforms/TargetLowering/pch.h

diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt
index 81cc336699391..87f2842d7df67 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt
@@ -11,6 +11,10 @@ add_clang_library(MLIRCIRTargetLowering
   DEPENDS
   clangBasic
 
+  DISABLE_PCH_REUSE # PCH contains private headers
+  PRECOMPILE_HEADERS
+  [["pch.h"]]
+
   LINK_COMPONENTS
   TargetParser
 
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/pch.h b/clang/lib/CIR/Dialect/Transforms/TargetLowering/pch.h
new file mode 100644
index 0000000000000..edef2a22aec49
--- /dev/null
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/pch.h
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Precompiled header for MLIRCIRTargetLowering. Uses private headers.
+///
+//===----------------------------------------------------------------------===//
+
+#include "CIRCXXABI.h"
+#include "LowerModule.h"
+#include "TargetLoweringInfo.h"



More information about the cfe-commits mailing list