Skip to content

Commit

Permalink
[CIR][LowerToLLVM][NFC] Refactor amendOperation to dispatch differe…
Browse files Browse the repository at this point in the history
…nt 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.
  • Loading branch information
seven-mile authored Aug 2, 2024
1 parent 13ed46a commit c8b2aed
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,32 @@ class CIRDialectLLVMIRTranslationInterface
mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
mlir::NamedAttribute attribute,
mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
// Translate CIR's extra function attributes to LLVM's function attributes.
auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op);
if (!func)
return mlir::success();
if (auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
amendFunction(func, instructions, attribute, moduleTranslation);
}
return mlir::success();
}

/// Translates the given operation to LLVM IR using the provided IR builder
/// and saving the state in `moduleTranslation`.
mlir::LogicalResult convertOperation(
mlir::Operation *op, llvm::IRBuilderBase &builder,
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {

if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
moduleTranslation.mapValue(cirOp.getResult()) =
llvm::Constant::getNullValue(
moduleTranslation.convertType(cirOp.getType()));

return mlir::success();
}

private:
// Translate CIR's extra function attributes to LLVM's function attributes.
void amendFunction(mlir::LLVM::LLVMFuncOp func,
llvm::ArrayRef<llvm::Instruction *> instructions,
mlir::NamedAttribute attribute,
mlir::LLVM::ModuleTranslation &moduleTranslation) const {
llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
if (auto extraAttr = mlir::dyn_cast<mlir::cir::ExtraFuncAttributesAttr>(
attribute.getValue())) {
Expand Down Expand Up @@ -71,25 +93,9 @@ class CIRDialectLLVMIRTranslationInterface
}

// Drop ammended CIR attribute from LLVM op.
op->removeAttr(attribute.getName());
return mlir::success();
func->removeAttr(attribute.getName());
}

/// Translates the given operation to LLVM IR using the provided IR builder
/// and saving the state in `moduleTranslation`.
mlir::LogicalResult convertOperation(
mlir::Operation *op, llvm::IRBuilderBase &builder,
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {

if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
moduleTranslation.mapValue(cirOp.getResult()) =
llvm::Constant::getNullValue(
moduleTranslation.convertType(cirOp.getType()));

return mlir::success();
}

private:
void emitOpenCLKernelMetadata(
mlir::cir::OpenCLKernelMetadataAttr clKernelMetadata,
llvm::Function *llvmFunc,
Expand Down

0 comments on commit c8b2aed

Please sign in to comment.