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

This commit is contained in:
David Rivera authored and GitHub committed 2026-06-05 20:52:21 -04:00
1 parent 7ef07bca9b
commit ee5e682f4d
12 files changed
+126 -31

No files matched your search

+37
View File
@@ -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
+10
View File
@@ -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
)
+5 -12
View File
@@ -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();
+1
View File
@@ -67,6 +67,7 @@ add_clang_library(clangCIR
clangBasic
clangLex
${dialect_libs}
CIRRegisterAllDialects
CIROpenACCSupport
CIROpenMPSupport
MLIRCIR
+37
View File
@@ -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
+24
View File
@@ -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>)
@@ -4,6 +4,7 @@ include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
set(LIBS
${test_libs}
clangCIR
CIRRegisterAllDialects
clangCIRLoweringDirectToLLVM
MLIRAffineAnalysis
MLIRAnalysis
@@ -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));
}
+1 -1
View File
@@ -23,7 +23,7 @@ clang_target_link_libraries(cir-opt
PRIVATE
clangCIR
clangCIRLoweringDirectToLLVM
CIROpenMPSupport
CIRRegisterAllDialects
MLIRCIR
MLIRCIRTransforms
)
+3 -10
View File
@@ -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();
+1 -1
View File
@@ -13,7 +13,7 @@ clang_target_link_libraries(cir-translate
PRIVATE
clangCIR
clangCIRLoweringDirectToLLVM
CIROpenMPSupport
CIRRegisterAllDialects
MLIRCIR
MLIRCIRTransforms
)
+3 -3
View File
@@ -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);
});
}