clang 23.0.0git
LowerToLLVMIR.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements lowering of CIR attributes and operations directly to
10// LLVMIR.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
15#include "mlir/IR/DialectRegistry.h"
16#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
17#include "mlir/Target/LLVMIR/ModuleTranslation.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/IR/Constant.h"
21#include "llvm/IR/GlobalVariable.h"
22
23using namespace llvm;
24
25namespace cir {
26namespace direct {
27
28/// Implementation of the dialect interface that converts CIR attributes to LLVM
29/// IR metadata.
31 : public mlir::LLVMTranslationDialectInterface {
32public:
33 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
34
35 /// Translates the given operation to LLVM IR using the provided IR builder
36 /// and saving the state in `moduleTranslation`.
37 mlir::LogicalResult convertOperation(
38 mlir::Operation *op, llvm::IRBuilderBase &builder,
39 mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
40
41 if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
42 moduleTranslation.mapValue(cirOp.getResult()) =
43 llvm::Constant::getNullValue(
44 moduleTranslation.convertType(cirOp.getType()));
45
46 return mlir::success();
47 }
48
49 /// Any named attribute in the CIR dialect, i.e, with name started with
50 /// "cir.", will be handled here.
51 virtual mlir::LogicalResult amendOperation(
52 mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
53 mlir::NamedAttribute attribute,
54 mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
55 if (auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
56 if (mlir::failed(
57 amendFunction(func, instructions, attribute, moduleTranslation)))
58 return mlir::failure();
59 } else if (auto mod = dyn_cast<mlir::ModuleOp>(op)) {
60 if (mlir::failed(amendModule(mod, attribute, moduleTranslation)))
61 return mlir::failure();
62 }
63 return mlir::success();
64 }
65
66private:
67 // Translate CIR function attributes to LLVM function attributes.
68 mlir::LogicalResult
69 amendFunction(mlir::LLVM::LLVMFuncOp func,
71 mlir::NamedAttribute attribute,
72 mlir::LLVM::ModuleTranslation &moduleTranslation) const {
73 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
74 llvm::StringRef attrName = attribute.getName().strref();
75
76 // Strip the "cir." prefix to get the LLVM attribute name.
77 llvm::StringRef llvmAttrName = attrName.substr(strlen("cir."));
78 if (auto strAttr = mlir::dyn_cast<mlir::StringAttr>(attribute.getValue()))
79 llvmFunc->addFnAttr(llvmAttrName, strAttr.getValue());
80 return mlir::success();
81 }
82
83 // Translate CIR's module attributes to LLVM's module metadata
84 mlir::LogicalResult
85 amendModule(mlir::ModuleOp mod, mlir::NamedAttribute attribute,
86 mlir::LLVM::ModuleTranslation &moduleTranslation) const {
87 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
88 llvm::LLVMContext &llvmContext = llvmModule->getContext();
89
90 if (attribute.getName() == "cir.amdhsa_code_object_version") {
91 if (auto intAttr =
92 mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
93 llvmModule->addModuleFlag(llvm::Module::Error,
94 "amdhsa_code_object_version",
95 static_cast<uint32_t>(intAttr.getInt()));
96 }
97 }
98
99 if (attribute.getName() == "cir.amdgpu_printf_kind") {
100 if (auto strAttr =
101 mlir::dyn_cast<mlir::StringAttr>(attribute.getValue())) {
102 llvm::MDString *mdStr =
103 llvm::MDString::get(llvmContext, strAttr.getValue());
104 llvmModule->addModuleFlag(llvm::Module::Error, "amdgpu_printf_kind",
105 mdStr);
106 }
107 }
108
109 return mlir::success();
110 }
111};
112
113void registerCIRDialectTranslation(mlir::DialectRegistry &registry) {
114 registry.insert<cir::CIRDialect>();
115 registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
116 dialect->addInterfaces<CIRDialectLLVMIRTranslationInterface>();
117 });
118}
119
120} // namespace direct
121} // namespace cir
122
123namespace mlir {
124void registerCIRDialectTranslation(mlir::MLIRContext &context) {
125 mlir::DialectRegistry registry;
127 context.appendDialectRegistry(registry);
128}
129} // namespace mlir
Implementation of the dialect interface that converts CIR attributes to LLVM IR metadata.
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.
void registerCIRDialectTranslation(mlir::DialectRegistry &registry)
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
void registerCIRDialectTranslation(mlir::MLIRContext &context)