Skip to content

Commit 8685568

Browse files
seven-milesmeenai
authored andcommitted
[CIR][LowerToLLVM][NFC] Refactor amendOperation to dispatch different ops (llvm#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 4c6bd5a commit 8685568

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
@@ -39,10 +39,32 @@ class CIRDialectLLVMIRTranslationInterface
3939
mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
4040
mlir::NamedAttribute attribute,
4141
mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
42-
// Translate CIR's extra function attributes to LLVM's function attributes.
43-
auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op);
44-
if (!func)
45-
return mlir::success();
42+
if (auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
43+
amendFunction(func, instructions, attribute, moduleTranslation);
44+
}
45+
return mlir::success();
46+
}
47+
48+
/// Translates the given operation to LLVM IR using the provided IR builder
49+
/// and saving the state in `moduleTranslation`.
50+
mlir::LogicalResult convertOperation(
51+
mlir::Operation *op, llvm::IRBuilderBase &builder,
52+
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
53+
54+
if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
55+
moduleTranslation.mapValue(cirOp.getResult()) =
56+
llvm::Constant::getNullValue(
57+
moduleTranslation.convertType(cirOp.getType()));
58+
59+
return mlir::success();
60+
}
61+
62+
private:
63+
// Translate CIR's extra function attributes to LLVM's function attributes.
64+
void amendFunction(mlir::LLVM::LLVMFuncOp func,
65+
llvm::ArrayRef<llvm::Instruction *> instructions,
66+
mlir::NamedAttribute attribute,
67+
mlir::LLVM::ModuleTranslation &moduleTranslation) const {
4668
llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
4769
if (auto extraAttr = mlir::dyn_cast<mlir::cir::ExtraFuncAttributesAttr>(
4870
attribute.getValue())) {
@@ -71,25 +93,9 @@ class CIRDialectLLVMIRTranslationInterface
7193
}
7294

7395
// Drop ammended CIR attribute from LLVM op.
74-
op->removeAttr(attribute.getName());
75-
return mlir::success();
96+
func->removeAttr(attribute.getName());
7697
}
7798

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

0 commit comments

Comments
 (0)