Skip to content
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

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

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Changes from all commits
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
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
Loading