33 :
public mlir::LLVMTranslationDialectInterface {
35 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
40 mlir::Operation *op, llvm::IRBuilderBase &builder,
41 mlir::LLVM::ModuleTranslation &moduleTranslation)
const final {
43 if (
auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
44 moduleTranslation.mapValue(cirOp.getResult()) =
45 llvm::Constant::getNullValue(
46 moduleTranslation.convertType(cirOp.getType()));
48 return mlir::success();
55 mlir::NamedAttribute attribute,
56 mlir::LLVM::ModuleTranslation &moduleTranslation)
const override {
57 if (
auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
59 amendFunction(func, instructions, attribute, moduleTranslation)))
60 return mlir::failure();
61 }
else if (
auto mod = dyn_cast<mlir::ModuleOp>(op)) {
62 if (mlir::failed(amendModule(mod, attribute, moduleTranslation)))
63 return mlir::failure();
64 }
else if (attribute.getName() ==
"cir.riscv_nontemporal_domain") {
65 if (mlir::failed(amendRISCVNontemporalDomain(op, instructions, attribute,
67 return mlir::failure();
69 return mlir::success();
73 mlir::LogicalResult amendRISCVNontemporalDomain(
75 mlir::NamedAttribute attribute,
76 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
77 auto domain = mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue());
79 return op->emitError()
80 <<
"expected cir.riscv_nontemporal_domain to be an integer";
82 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
83 llvm::MDNode *node = llvm::MDNode::get(
84 llvmContext, llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
85 llvm::Type::getInt32Ty(llvmContext),
86 domain.getValue().getZExtValue())));
87 for (llvm::Instruction *inst : instructions)
88 inst->setMetadata(
"riscv-nontemporal-domain", node);
89 return mlir::success();
94 amendFunction(mlir::LLVM::LLVMFuncOp func,
96 mlir::NamedAttribute attribute,
97 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
98 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
99 llvm::StringRef attrName = attribute.getName().strref();
102 llvm::StringRef llvmAttrName = attrName.substr(strlen(
"cir."));
103 if (
auto strAttr = mlir::dyn_cast<mlir::StringAttr>(attribute.getValue()))
104 llvmFunc->addFnAttr(llvmAttrName, strAttr.getValue());
105 return mlir::success();
110 amendModule(mlir::ModuleOp mod, mlir::NamedAttribute attribute,
111 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
112 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
113 llvm::LLVMContext &llvmContext = llvmModule->getContext();
115 if (attribute.getName() ==
"cir.amdhsa_code_object_version") {
117 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
118 llvmModule->addModuleFlag(llvm::Module::Error,
119 "amdhsa_code_object_version",
120 static_cast<uint32_t>(intAttr.getInt()));
124 if (attribute.getName() ==
"cir.amdgpu_printf_kind") {
126 mlir::dyn_cast<mlir::StringAttr>(attribute.getValue())) {
127 llvm::MDString *mdStr =
128 llvm::MDString::get(llvmContext, strAttr.getValue());
129 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu_printf_kind",
134 if (attribute.getName() ==
"cir.amdgpu_xnack") {
136 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
137 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu.xnack",
138 static_cast<uint32_t>(intAttr.getInt()));
142 if (attribute.getName() ==
"cir.amdgpu_sramecc") {
144 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
145 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu.sramecc",
146 static_cast<uint32_t>(intAttr.getInt()));
150 return mlir::success();
mlir::LogicalResult convertOperation(mlir::Operation *op, llvm::IRBuilderBase &builder, mlir::LLVM::ModuleTranslation &moduleTranslation) const final
Translates the given operation to LLVM IR using the provided IR builder and saving the state in modul...
virtual mlir::LogicalResult amendOperation(mlir::Operation *op, llvm::ArrayRef< llvm::Instruction * > instructions, mlir::NamedAttribute attribute, mlir::LLVM::ModuleTranslation &moduleTranslation) const override
Any named attribute in the CIR dialect, i.e, with name started with "cir.", will be handled here.