Skip to content

Commit 04ab79b

Browse files
committed
[CIR][Lowering] Add CIRDialectLLVMIRTranslationInterface to handle CIR attributes to LLVM conversion.
Summary: The interface is currently just a placeholder and doesn't do real work. Suppport for specific atttribure is on the way.
1 parent 14c44ee commit 04ab79b

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
66
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
77

88
add_clang_library(clangCIRLoweringDirectToLLVM
9+
LowerAttrToLLVMIR.cpp
910
LowerToLLVM.cpp
1011

1112
DEPENDS
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//====- LowerAttrToLLVMIR.cpp - Lowering CIR attributes to LLVMIR ---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements lowering of CIR attributes to LLVMIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/IR/DialectRegistry.h"
14+
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
15+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
16+
#include "clang/CIR/Passes.h"
17+
18+
using namespace cir;
19+
using namespace llvm;
20+
21+
namespace cir {
22+
namespace direct {
23+
24+
/// Implementation of the dialect interface that converts CIR attributes to LLVM
25+
/// IR metadata.
26+
class CIRDialectLLVMIRTranslationInterface
27+
: public mlir::LLVMTranslationDialectInterface {
28+
public:
29+
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
30+
31+
/// Any named attribute in the CIR dialect, i.e, with name started with
32+
/// "cir.", will be handled here.
33+
mlir::LogicalResult
34+
amendOperation(mlir::Operation *op, mlir::NamedAttribute attribute,
35+
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
36+
// TODO: Implement this
37+
return mlir::success();
38+
}
39+
};
40+
41+
void registerCIRDialectTranslation(mlir::DialectRegistry &registry) {
42+
registry.insert<mlir::cir::CIRDialect>();
43+
registry.addExtension(+[](mlir::MLIRContext *ctx, mlir::cir::CIRDialect *dialect) {
44+
dialect->addInterfaces<CIRDialectLLVMIRTranslationInterface>();
45+
});
46+
}
47+
48+
void registerCIRDialectTranslation(mlir::MLIRContext &context) {
49+
mlir::DialectRegistry registry;
50+
registerCIRDialectTranslation(registry);
51+
context.appendDialectRegistry(registry);
52+
}
53+
} // namespace direct
54+
} // namespace cir

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ std::unique_ptr<mlir::Pass> createConvertCIRToLLVMPass() {
12111211
return std::make_unique<ConvertCIRToLLVMPass>();
12121212
}
12131213

1214+
extern void registerCIRDialectTranslation(mlir::MLIRContext &context);
1215+
12141216
std::unique_ptr<llvm::Module>
12151217
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule,
12161218
std::unique_ptr<mlir::MLIRContext> mlirCtx,
@@ -1236,6 +1238,7 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule,
12361238

12371239
mlir::registerBuiltinDialectTranslation(*mlirCtx);
12381240
mlir::registerLLVMDialectTranslation(*mlirCtx);
1241+
registerCIRDialectTranslation(*mlirCtx);
12391242

12401243
auto ModuleName = theModule.getName();
12411244
auto llvmModule = mlir::translateModuleToLLVMIR(

0 commit comments

Comments
 (0)