73 :
public mlir::LLVMTranslationDialectInterface {
75 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
80 mlir::Operation *op, llvm::IRBuilderBase &builder,
81 mlir::LLVM::ModuleTranslation &moduleTranslation)
const final {
83 if (
auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
84 moduleTranslation.mapValue(cirOp.getResult()) =
85 llvm::Constant::getNullValue(
86 moduleTranslation.convertType(cirOp.getType()));
88 return mlir::success();
95 mlir::NamedAttribute attribute,
96 mlir::LLVM::ModuleTranslation &moduleTranslation)
const override {
97 if (
auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
99 amendFunction(func, instructions, attribute, moduleTranslation)))
100 return mlir::failure();
101 }
else if (
auto mod = dyn_cast<mlir::ModuleOp>(op)) {
102 if (mlir::failed(amendModule(mod, attribute, moduleTranslation)))
103 return mlir::failure();
104 }
else if (attribute.getName() ==
"cir.riscv_nontemporal_domain") {
105 if (mlir::failed(amendRISCVNontemporalDomain(op, instructions, attribute,
107 return mlir::failure();
108 }
else if (attribute.getName() == cir::CIRDialect::getSrcLocAttrName()) {
109 if (mlir::failed(amendSrcLoc(instructions, attribute, moduleTranslation)))
110 return mlir::failure();
111 }
else if (attribute.getName() ==
112 cir::CIRDialect::getAMDGPUNoFineGrainedMemoryAttrName() ||
113 attribute.getName() ==
114 cir::CIRDialect::getAMDGPUNoRemoteMemoryAttrName() ||
115 attribute.getName() ==
116 cir::CIRDialect::getAMDGPUIgnoreDenormalModeAttrName() ||
117 attribute.getName() ==
118 cir::CIRDialect::getAMDGPUNoPrivateAddrSpaceAttrName()) {
119 amendAMDGPUAtomicMetadata(instructions, attribute, moduleTranslation);
121 return mlir::success();
129 mlir::NamedAttribute attribute,
130 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
131 auto srcLoc = mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue());
133 return mlir::failure();
135 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
136 llvm::MDNode *node = llvm::MDNode::get(
137 llvmContext, llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
138 llvm::Type::getInt64Ty(llvmContext),
139 srcLoc.getValue().getZExtValue())));
140 for (llvm::Instruction *inst : instructions)
141 inst->setMetadata(
"srcloc", node);
142 return mlir::success();
147 void amendAMDGPUAtomicMetadata(
149 mlir::NamedAttribute attribute,
150 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
151 if (attribute.getName() ==
152 cir::CIRDialect::getAMDGPUNoPrivateAddrSpaceAttrName()) {
153 llvm::MDBuilder mdHelper(moduleTranslation.getLLVMContext());
154 llvm::MDNode *asRange = mdHelper.createRange(
155 llvm::APInt(32, llvm::AMDGPUAS::PRIVATE_ADDRESS),
156 llvm::APInt(32, llvm::AMDGPUAS::PRIVATE_ADDRESS + 1));
157 for (llvm::Instruction *inst : instructions)
158 inst->setMetadata(llvm::LLVMContext::MD_noalias_addrspace, asRange);
161 llvm::MDNode *empty =
162 llvm::MDNode::get(moduleTranslation.getLLVMContext(), {});
165 if (attribute.getName() ==
166 cir::CIRDialect::getAMDGPUIgnoreDenormalModeAttrName()) {
167 for (llvm::Instruction *inst : instructions)
168 inst->setMetadata(llvm::LLVMContext::MD_atomic_ignore_denormal_mode,
172 llvm::StringRef mdName;
173 if (attribute.getName() ==
174 cir::CIRDialect::getAMDGPUNoFineGrainedMemoryAttrName())
175 mdName =
"amdgpu.no.fine.grained.memory";
176 else if (attribute.getName() ==
177 cir::CIRDialect::getAMDGPUNoRemoteMemoryAttrName())
178 mdName =
"amdgpu.no.remote.memory";
179 for (llvm::Instruction *inst : instructions)
180 inst->setMetadata(mdName, empty);
183 mlir::LogicalResult amendRISCVNontemporalDomain(
184 mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
185 mlir::NamedAttribute attribute,
186 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
187 auto domain = mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue());
189 return op->emitError()
190 <<
"expected cir.riscv_nontemporal_domain to be an integer";
192 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
193 llvm::MDNode *
node = llvm::MDNode::get(
194 llvmContext, llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
195 llvm::Type::getInt32Ty(llvmContext),
196 domain.getValue().getZExtValue())));
197 for (llvm::Instruction *inst : instructions)
198 inst->setMetadata(
"riscv-nontemporal-domain", node);
199 return mlir::success();
204 amendFunction(mlir::LLVM::LLVMFuncOp func,
205 llvm::ArrayRef<llvm::Instruction *> instructions,
206 mlir::NamedAttribute attribute,
207 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
208 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
209 llvm::StringRef attrName = attribute.getName().strref();
212 llvm::StringRef llvmAttrName = attrName.substr(strlen(
"cir."));
213 if (
auto strAttr = mlir::dyn_cast<mlir::StringAttr>(attribute.getValue()))
214 llvmFunc->addFnAttr(llvmAttrName, strAttr.getValue());
215 return mlir::success();
220 amendModule(mlir::ModuleOp mod, mlir::NamedAttribute attribute,
221 mlir::LLVM::ModuleTranslation &moduleTranslation)
const {
222 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
223 llvm::LLVMContext &llvmContext = llvmModule->getContext();
225 if (attribute.getName() ==
226 cir::CIRDialect::getAMDGPUCodeObjectVersionAttrName()) {
228 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
229 llvmModule->addModuleFlag(llvm::Module::Error,
230 "amdhsa_code_object_version",
231 static_cast<uint32_t>(intAttr.getInt()));
235 if (attribute.getName() == cir::CIRDialect::getAMDGPUPrintfKindAttrName()) {
237 mlir::dyn_cast<mlir::StringAttr>(attribute.getValue())) {
238 llvm::MDString *mdStr =
239 llvm::MDString::get(llvmContext, strAttr.getValue());
240 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu_printf_kind",
245 if (attribute.getName() == cir::CIRDialect::getAMDGPUXnackAttrName()) {
247 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
248 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu.xnack",
249 static_cast<uint32_t>(intAttr.getInt()));
253 if (attribute.getName() ==
254 cir::CIRDialect::getCUDAOffloadEntriesAttrName()) {
255 if (
auto entries = mlir::dyn_cast<mlir::ArrayAttr>(attribute.getValue()))
256 moduleTranslation.getOrCreateExtension<CUDAOffloadEntries>(mod, entries,
260 if (attribute.getName() == cir::CIRDialect::getAMDGPUSramEccAttrName()) {
262 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
263 llvmModule->addModuleFlag(llvm::Module::Error,
"amdgpu.sramecc",
264 static_cast<uint32_t>(intAttr.getInt()));
268 return mlir::success();