Skip to content

[CIR][LowerToLLVM][NFC] Refactor amendOperation to dispatch different ops #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,36 @@ 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)) {
auto result = amendOperationOnFunction(func, instructions, attribute,
moduleTranslation);
if (failed(result))
return result;
}
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.
mlir::LogicalResult amendOperationOnFunction(
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 +97,10 @@ class CIRDialectLLVMIRTranslationInterface
}

// Drop ammended CIR attribute from LLVM op.
op->removeAttr(attribute.getName());
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()));

func->removeAttr(attribute.getName());
return mlir::success();
}

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