Author: David Rivera
Date: 2026-06-05T20:52:21-04:00
New Revision: ee5e682f4db85afca52ac6519a4b9bf0bfab4cf9

URL: 
https://github.com/llvm/llvm-project/commit/ee5e682f4db85afca52ac6519a4b9bf0bfab4cf9
DIFF: 
https://github.com/llvm/llvm-project/commit/ee5e682f4db85afca52ac6519a4b9bf0bfab4cf9.diff

LOG: [CIR] Centralize dialect registration across CIR tools (#200266)

Added: 
    clang/include/clang/CIR/InitAllDialects.h
    clang/lib/CIR/RegisterAllDialects.cpp
    clang/test/CIR/IR/openacc.cir

Modified: 
    clang/lib/CIR/CMakeLists.txt
    clang/lib/CIR/CodeGen/CIRGenerator.cpp
    clang/lib/CIR/CodeGen/CMakeLists.txt
    clang/tools/cir-lsp-server/CMakeLists.txt
    clang/tools/cir-lsp-server/cir-lsp-server.cpp
    clang/tools/cir-opt/CMakeLists.txt
    clang/tools/cir-opt/cir-opt.cpp
    clang/tools/cir-translate/CMakeLists.txt
    clang/tools/cir-translate/cir-translate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/CIR/InitAllDialects.h 
b/clang/include/clang/CIR/InitAllDialects.h
new file mode 100644
index 0000000000000..61aef3bb7fe05
--- /dev/null
+++ b/clang/include/clang/CIR/InitAllDialects.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Centralised dialect registration for CIR tools. Every tool that parses or
+// transforms CIR (cir-opt, cir-lsp-server, -fclangir...) should call
+// registerAllDialects() instead of registering dialects and extensions
+// individually, so the dialect surface presented to all tools is always
+// consistent.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_CIR_INITALLDIALECTS_H
+#define CLANG_CIR_INITALLDIALECTS_H
+
+namespace mlir {
+class DialectRegistry;
+class MLIRContext;
+} // namespace mlir
+
+namespace cir {
+
+// Populate \p registry with every dialect and dialect extension required to
+// parse, verify and transform CIR.
+void registerAllDialects(mlir::DialectRegistry &registry);
+
+// Convenience overload that registers the same dialects directly into \p
+// context for lazy loading.
+void registerAllDialects(mlir::MLIRContext &context);
+
+} // namespace cir
+
+#endif // CLANG_CIR_INITALLDIALECTS_H

diff  --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt
index 7bdf3fcc59035..f215c927565de 100644
--- a/clang/lib/CIR/CMakeLists.txt
+++ b/clang/lib/CIR/CMakeLists.txt
@@ -17,3 +17,13 @@ add_subdirectory(CodeGen)
 add_subdirectory(FrontendAction)
 add_subdirectory(Interfaces)
 add_subdirectory(Lowering)
+
+add_clang_library(CIRRegisterAllDialects
+  RegisterAllDialects.cpp
+
+  LINK_LIBS PUBLIC
+  MLIRCIR
+  CIROpenACCSupport
+  CIROpenMPSupport
+  MLIRDLTIDialect
+)

diff  --git a/clang/lib/CIR/CodeGen/CIRGenerator.cpp 
b/clang/lib/CIR/CodeGen/CIRGenerator.cpp
index 31d40c21ef6e1..d4fcbb6e42f3e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenerator.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenerator.cpp
@@ -12,16 +12,15 @@
 
 #include "CIRGenModule.h"
 
-#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
+#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/Target/LLVMIR/Import.h"
 
 #include "clang/AST/DeclGroup.h"
 #include "clang/CIR/CIRGenerator.h"
-#include "clang/CIR/Dialect/IR/CIRDialect.h"
-#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
-#include "clang/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.h"
+#include "clang/CIR/InitAllDialects.h"
 #include "llvm/IR/DataLayout.h"
 
 using namespace cir;
@@ -52,17 +51,11 @@ void CIRGenerator::Initialize(ASTContext &astContext) {
   this->astContext = &astContext;
 
   mlirContext = std::make_unique<mlir::MLIRContext>();
-  mlirContext->loadDialect<mlir::DLTIDialect>();
-  mlirContext->loadDialect<cir::CIRDialect>();
+  cir::registerAllDialects(*mlirContext);
+  mlirContext->loadDialect<mlir::DLTIDialect, cir::CIRDialect>();
   mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
   mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>();
 
-  // Register extensions to integrate CIR types with OpenACC and OpenMP.
-  mlir::DialectRegistry registry;
-  cir::acc::registerOpenACCExtensions(registry);
-  cir::omp::registerOpenMPExtensions(registry);
-  mlirContext->appendDialectRegistry(registry);
-
   cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
       *mlirContext.get(), astContext, codeGenOpts, diags);
   mlir::ModuleOp mod = cgm->getModule();

diff  --git a/clang/lib/CIR/CodeGen/CMakeLists.txt 
b/clang/lib/CIR/CodeGen/CMakeLists.txt
index f3f523ab3c21f..79d0a49570b3e 100644
--- a/clang/lib/CIR/CodeGen/CMakeLists.txt
+++ b/clang/lib/CIR/CodeGen/CMakeLists.txt
@@ -67,6 +67,7 @@ add_clang_library(clangCIR
   clangBasic
   clangLex
   ${dialect_libs}
+  CIRRegisterAllDialects
   CIROpenACCSupport
   CIROpenMPSupport
   MLIRCIR

diff  --git a/clang/lib/CIR/RegisterAllDialects.cpp 
b/clang/lib/CIR/RegisterAllDialects.cpp
new file mode 100644
index 0000000000000..8c2961bdebbaa
--- /dev/null
+++ b/clang/lib/CIR/RegisterAllDialects.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/InitAllDialects.h"
+
+#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "mlir/IR/BuiltinDialect.h"
+#include "mlir/IR/MLIRContext.h"
+
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
+#include "clang/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.h"
+
+namespace cir {
+
+void registerAllDialects(mlir::DialectRegistry &registry) {
+  registry.insert<mlir::BuiltinDialect, cir::CIRDialect, mlir::DLTIDialect,
+                  mlir::omp::OpenMPDialect, mlir::acc::OpenACCDialect>();
+  // Register extensions to integrate CIR types with OpenACC and OpenMP.
+  cir::omp::registerOpenMPExtensions(registry);
+  cir::acc::registerOpenACCExtensions(registry);
+}
+
+void registerAllDialects(mlir::MLIRContext &context) {
+  mlir::DialectRegistry registry;
+  registerAllDialects(registry);
+  context.appendDialectRegistry(registry);
+}
+
+} // namespace cir

diff  --git a/clang/test/CIR/IR/openacc.cir b/clang/test/CIR/IR/openacc.cir
new file mode 100644
index 0000000000000..2049e603ea026
--- /dev/null
+++ b/clang/test/CIR/IR/openacc.cir
@@ -0,0 +1,24 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+
+// Verify that cir-opt registers the OpenACC dialect along with the CIR type
+// extensions, so OpenACC data clauses operating on a !cir.ptr verify and
+// round-trip.
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @acc_copyin(%arg0: !cir.ptr<!s32i>) {
+    %0 = acc.copyin varPtr(%arg0 : !cir.ptr<!s32i>) -> !cir.ptr<!s32i> {name = 
"x"}
+    acc.parallel dataOperands(%0 : !cir.ptr<!s32i>) {
+      acc.yield
+    }
+    acc.delete accPtr(%0 : !cir.ptr<!s32i>) {dataClause = #acc<data_clause 
acc_copyin>, name = "x"}
+    cir.return
+  }
+}
+
+// CHECK: cir.func{{.*}} @acc_copyin(%[[ARG:.*]]: !cir.ptr<!s32i>)
+// CHECK:   %[[COPYIN:.*]] = acc.copyin varPtr(%[[ARG]] : !cir.ptr<!s32i>) -> 
!cir.ptr<!s32i> {name = "x"}
+// CHECK:   acc.parallel dataOperands(%[[COPYIN]] : !cir.ptr<!s32i>)
+// CHECK:     acc.yield
+// CHECK:   acc.delete accPtr(%[[COPYIN]] : !cir.ptr<!s32i>)

diff  --git a/clang/tools/cir-lsp-server/CMakeLists.txt 
b/clang/tools/cir-lsp-server/CMakeLists.txt
index f421215173e62..28232a0edb311 100644
--- a/clang/tools/cir-lsp-server/CMakeLists.txt
+++ b/clang/tools/cir-lsp-server/CMakeLists.txt
@@ -4,6 +4,7 @@ include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
 set(LIBS
   ${test_libs}
   clangCIR
+  CIRRegisterAllDialects
   clangCIRLoweringDirectToLLVM
   MLIRAffineAnalysis
   MLIRAnalysis

diff  --git a/clang/tools/cir-lsp-server/cir-lsp-server.cpp 
b/clang/tools/cir-lsp-server/cir-lsp-server.cpp
index 555967a5a21d5..3c155d9cd88d4 100644
--- a/clang/tools/cir-lsp-server/cir-lsp-server.cpp
+++ b/clang/tools/cir-lsp-server/cir-lsp-server.cpp
@@ -10,15 +10,14 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include "mlir/IR/Dialect.h"
-#include "mlir/IR/MLIRContext.h"
+#include "mlir/IR/DialectRegistry.h"
 #include "mlir/InitAllDialects.h"
 #include "mlir/Tools/mlir-lsp-server/MlirLspServerMain.h"
-#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/InitAllDialects.h"
 
 int main(int argc, char **argv) {
   mlir::DialectRegistry registry;
   mlir::registerAllDialects(registry);
-  registry.insert<cir::CIRDialect>();
+  cir::registerAllDialects(registry);
   return failed(mlir::MlirLspServerMain(argc, argv, registry));
 }

diff  --git a/clang/tools/cir-opt/CMakeLists.txt 
b/clang/tools/cir-opt/CMakeLists.txt
index 4e9553ed8a7e7..287eba226cfe3 100644
--- a/clang/tools/cir-opt/CMakeLists.txt
+++ b/clang/tools/cir-opt/CMakeLists.txt
@@ -23,7 +23,7 @@ clang_target_link_libraries(cir-opt
   PRIVATE
   clangCIR
   clangCIRLoweringDirectToLLVM
-  CIROpenMPSupport
+  CIRRegisterAllDialects
   MLIRCIR
   MLIRCIRTransforms
 )

diff  --git a/clang/tools/cir-opt/cir-opt.cpp b/clang/tools/cir-opt/cir-opt.cpp
index b6d2438812070..6742985e149e8 100644
--- a/clang/tools/cir-opt/cir-opt.cpp
+++ b/clang/tools/cir-opt/cir-opt.cpp
@@ -13,21 +13,16 @@
 
//===----------------------------------------------------------------------===//
 
 #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
-#include "mlir/Dialect/DLTI/DLTI.h"
-#include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/MemRef/IR/MemRef.h"
-#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/OpenMP/Transforms/Passes.h"
-#include "mlir/IR/BuiltinDialect.h"
 #include "mlir/Pass/PassManager.h"
 #include "mlir/Pass/PassOptions.h"
 #include "mlir/Pass/PassRegistry.h"
 #include "mlir/Tools/mlir-opt/MlirOptMain.h"
 #include "mlir/Transforms/Passes.h"
-#include "clang/CIR/Dialect/IR/CIRDialect.h"
-#include "clang/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.h"
 #include "clang/CIR/Dialect/Passes.h"
+#include "clang/CIR/InitAllDialects.h"
 #include "clang/CIR/Passes.h"
 
 struct CIRToLLVMPipelineOptions
@@ -36,10 +31,8 @@ struct CIRToLLVMPipelineOptions
 int main(int argc, char **argv) {
   // TODO: register needed MLIR passes for CIR?
   mlir::DialectRegistry registry;
-  registry.insert<mlir::BuiltinDialect, cir::CIRDialect,
-                  mlir::memref::MemRefDialect, mlir::LLVM::LLVMDialect,
-                  mlir::DLTIDialect, mlir::omp::OpenMPDialect>();
-  cir::omp::registerOpenMPExtensions(registry);
+  cir::registerAllDialects(registry);
+  registry.insert<mlir::memref::MemRefDialect, mlir::LLVM::LLVMDialect>();
 
   ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
     return mlir::createCIRCanonicalizePass();

diff  --git a/clang/tools/cir-translate/CMakeLists.txt 
b/clang/tools/cir-translate/CMakeLists.txt
index 53e60220b8736..1e85c541457d9 100644
--- a/clang/tools/cir-translate/CMakeLists.txt
+++ b/clang/tools/cir-translate/CMakeLists.txt
@@ -13,7 +13,7 @@ clang_target_link_libraries(cir-translate
   PRIVATE
   clangCIR
   clangCIRLoweringDirectToLLVM
-  CIROpenMPSupport
+  CIRRegisterAllDialects
   MLIRCIR
   MLIRCIRTransforms
 )

diff  --git a/clang/tools/cir-translate/cir-translate.cpp 
b/clang/tools/cir-translate/cir-translate.cpp
index 997d44dc5a62f..26adf2cf47d17 100644
--- a/clang/tools/cir-translate/cir-translate.cpp
+++ b/clang/tools/cir-translate/cir-translate.cpp
@@ -31,8 +31,8 @@
 #include "clang/Basic/DiagnosticOptions.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
-#include "clang/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.h"
 #include "clang/CIR/Dialect/Passes.h"
+#include "clang/CIR/InitAllDialects.h"
 #include "clang/CIR/LowerToLLVM.h"
 #include "clang/CIR/MissingFeatures.h"
 
@@ -167,10 +167,10 @@ void registerToLLVMTranslation() {
         return mlir::success();
       },
       [](mlir::DialectRegistry &registry) {
-        registry.insert<mlir::DLTIDialect, mlir::func::FuncDialect>();
+        cir::registerAllDialects(registry);
+        registry.insert<mlir::func::FuncDialect>();
         mlir::registerAllToLLVMIRTranslations(registry);
         cir::direct::registerCIRDialectTranslation(registry);
-        cir::omp::registerOpenMPExtensions(registry);
       });
 }
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to