Skip to content

Commit 1e7c0b7

Browse files
seven-milelanza
authored andcommitted
[CIR][LowerToLLVM][NFC] Refactor amendOperation to dispatch different ops (#768)
Soon I would like to submit a patch emitting OpenCL module metadata in LowerToLLVM path, which requires to attach the metadata to LLVM module when `amendOperaion` is called for MLIR module op. This PR refactors the method to a dispatcher to make the future changes cleaner.
1 parent 5035d73 commit 1e7c0b7

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,32 @@ class CIRDialectLLVMIRTranslationInterface
4040
mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
4141
mlir::NamedAttribute attribute,
4242
mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
43-
// Translate CIR's extra function attributes to LLVM's function attributes.
44-
auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op);
45-
if (!func)
46-
return mlir::success();
43+
if (auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
44+
amendFunction(func, instructions, attribute, moduleTranslation);
45+
}
46+
return mlir::success();
47+
}
48+
49+
/// Translates the given operation to LLVM IR using the provided IR builder
50+
/// and saving the state in `moduleTranslation`.
51+
mlir::LogicalResult convertOperation(
52+
mlir::Operation *op, llvm::IRBuilderBase &builder,
53+
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
54+
55+
if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
56+
moduleTranslation.mapValue(cirOp.getResult()) =
57+
llvm::Constant::getNullValue(
58+
moduleTranslation.convertType(cirOp.getType()));
59+
60+
return mlir::success();
61+
}
62+
63+
private:
64+
// Translate CIR's extra function attributes to LLVM's function attributes.
65+
void amendFunction(mlir::LLVM::LLVMFuncOp func,
66+
llvm::ArrayRef<llvm::Instruction *> instructions,
67+
mlir::NamedAttribute attribute,
68+
mlir::LLVM::ModuleTranslation &moduleTranslation) const {
4769
llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
4870
if (auto extraAttr = mlir::dyn_cast<mlir::cir::ExtraFuncAttributesAttr>(
4971
attribute.getValue())) {
@@ -72,25 +94,9 @@ class CIRDialectLLVMIRTranslationInterface
7294
}
7395

7496
// Drop ammended CIR attribute from LLVM op.
75-
op->removeAttr(attribute.getName());
76-
return mlir::success();
97+
func->removeAttr(attribute.getName());
7798
}
7899

79-
/// Translates the given operation to LLVM IR using the provided IR builder
80-
/// and saving the state in `moduleTranslation`.
81-
mlir::LogicalResult convertOperation(
82-
mlir::Operation *op, llvm::IRBuilderBase &builder,
83-
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
84-
85-
if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
86-
moduleTranslation.mapValue(cirOp.getResult()) =
87-
llvm::Constant::getNullValue(
88-
moduleTranslation.convertType(cirOp.getType()));
89-
90-
return mlir::success();
91-
}
92-
93-
private:
94100
void emitOpenCLKernelMetadata(
95101
mlir::cir::OpenCLKernelMetadataAttr clKernelMetadata,
96102
llvm::Function *llvmFunc,

0 commit comments

Comments
 (0)