|
12 | 12 |
|
13 | 13 | #ifndef CLANG_INTERFACES_CIR_CIRLINKINTERFACE_H_ |
14 | 14 | #define CLANG_INTERFACES_CIR_CIRLINKINTERFACE_H_ |
| 15 | +#include "mlir/Linker/LLVMLinkerMixin.h" |
| 16 | +#include "mlir/Linker/LinkerInterface.h" |
| 17 | +#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 18 | +#include <optional> |
15 | 19 |
|
16 | 20 | namespace mlir { |
17 | 21 | class DialectRegistry; |
18 | 22 | } // namespace mlir |
19 | 23 |
|
| 24 | +using namespace mlir; |
| 25 | +using namespace mlir::link; |
| 26 | + |
20 | 27 | namespace cir { |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | +// CIRSymbolLinkerInterface |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | + |
| 32 | +class CIRSymbolLinkerInterface |
| 33 | + : public SymbolAttrLLVMLinkerInterface<CIRSymbolLinkerInterface> { |
| 34 | +public: |
| 35 | + CIRSymbolLinkerInterface(Dialect *dialect) |
| 36 | + : SymbolAttrLLVMLinkerInterface(dialect) {} |
| 37 | + |
| 38 | + bool canBeLinked(Operation *op) const override { |
| 39 | + return isa<cir::GlobalOp>(op) || isa<cir::FuncOp>(op); |
| 40 | + } |
| 41 | + |
| 42 | + //===--------------------------------------------------------------------===// |
| 43 | + // LLVMLinkerMixin required methods from derived linker interface |
| 44 | + //===--------------------------------------------------------------------===// |
| 45 | + |
| 46 | + // TODO: expose convertLinkage from LowerToLLVM.cpp |
| 47 | + static Linkage toLLVMLinkage(cir::GlobalLinkageKind linkage) { |
| 48 | + using CIR = cir::GlobalLinkageKind; |
| 49 | + using LLVM = mlir::LLVM::Linkage; |
| 50 | + |
| 51 | + switch (linkage) { |
| 52 | + case CIR::AvailableExternallyLinkage: |
| 53 | + return LLVM::AvailableExternally; |
| 54 | + case CIR::CommonLinkage: |
| 55 | + return LLVM::Common; |
| 56 | + case CIR::ExternalLinkage: |
| 57 | + return LLVM::External; |
| 58 | + case CIR::ExternalWeakLinkage: |
| 59 | + return LLVM::ExternWeak; |
| 60 | + case CIR::InternalLinkage: |
| 61 | + return LLVM::Internal; |
| 62 | + case CIR::LinkOnceAnyLinkage: |
| 63 | + return LLVM::Linkonce; |
| 64 | + case CIR::LinkOnceODRLinkage: |
| 65 | + return LLVM::LinkonceODR; |
| 66 | + case CIR::PrivateLinkage: |
| 67 | + return LLVM::Private; |
| 68 | + case CIR::WeakAnyLinkage: |
| 69 | + return LLVM::Weak; |
| 70 | + case CIR::WeakODRLinkage: |
| 71 | + return LLVM::WeakODR; |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + static Linkage getLinkage(Operation *op) { |
| 76 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 77 | + return toLLVMLinkage(gv.getLinkage()); |
| 78 | + if (auto fn = dyn_cast<cir::FuncOp>(op)) |
| 79 | + return toLLVMLinkage(fn.getLinkage()); |
| 80 | + llvm_unreachable("unexpected operation"); |
| 81 | + } |
| 82 | + |
| 83 | + static bool isComdat(Operation *op) { |
| 84 | + // TODO(frabert): Extracting comdat info from CIR is not implemented yet |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + static std::optional<mlir::link::ComdatSelector> |
| 89 | + getComdatSelector(Operation *op) { |
| 90 | + // TODO(frabert): Extracting comdat info from CIR is not implemented yet |
| 91 | + return std::nullopt; |
| 92 | + } |
| 93 | + |
| 94 | + // TODO: expose lowerCIRVisibilityToLLVMVisibility from LowerToLLVM.cpp |
| 95 | + static Visibility toLLVMVisibility(cir::VisibilityAttr visibility) { |
| 96 | + return toLLVMVisibility(visibility.getValue()); |
| 97 | + } |
| 98 | + |
| 99 | + static Visibility toLLVMVisibility(cir::VisibilityKind visibility) { |
| 100 | + using CIR = cir::VisibilityKind; |
| 101 | + using LLVM = mlir::LLVM::Visibility; |
| 102 | + |
| 103 | + switch (visibility) { |
| 104 | + case CIR::Default: |
| 105 | + return LLVM::Default; |
| 106 | + case CIR::Hidden: |
| 107 | + return LLVM::Hidden; |
| 108 | + case CIR::Protected: |
| 109 | + return LLVM::Protected; |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + static cir::VisibilityKind toCIRVisibility(Visibility visibility) { |
| 114 | + using CIR = cir::VisibilityKind; |
| 115 | + using LLVM = mlir::LLVM::Visibility; |
| 116 | + |
| 117 | + switch (visibility) { |
| 118 | + case LLVM::Default: |
| 119 | + return CIR::Default; |
| 120 | + case LLVM::Hidden: |
| 121 | + return CIR::Hidden; |
| 122 | + case LLVM::Protected: |
| 123 | + return CIR::Protected; |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + static cir::VisibilityAttr toCIRVisibilityAttr(Visibility visibility, |
| 128 | + MLIRContext *mlirContext) { |
| 129 | + return cir::VisibilityAttr::get(mlirContext, toCIRVisibility(visibility)); |
| 130 | + } |
| 131 | + |
| 132 | + static Visibility getVisibility(Operation *op) { |
| 133 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 134 | + return toLLVMVisibility(gv.getGlobalVisibility()); |
| 135 | + if (auto fn = dyn_cast<cir::FuncOp>(op)) |
| 136 | + return toLLVMVisibility(fn.getGlobalVisibility()); |
| 137 | + llvm_unreachable("unexpected operation"); |
| 138 | + } |
| 139 | + |
| 140 | + static void setVisibility(Operation *op, Visibility visibility) { |
| 141 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 142 | + return gv.setGlobalVisibilityAttr( |
| 143 | + toCIRVisibilityAttr(visibility, op->getContext())); |
| 144 | + if (auto fn = dyn_cast<cir::FuncOp>(op)) |
| 145 | + return fn.setGlobalVisibilityAttr( |
| 146 | + toCIRVisibilityAttr(visibility, op->getContext())); |
| 147 | + llvm_unreachable("unexpected operation"); |
| 148 | + } |
| 149 | + |
| 150 | + static bool isDeclaration(Operation *op) { |
| 151 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 152 | + return gv.isDeclaration(); |
| 153 | + if (auto fn = dyn_cast<cir::FuncOp>(op)) |
| 154 | + return fn.isDeclaration(); |
| 155 | + llvm_unreachable("unexpected operation"); |
| 156 | + } |
| 157 | + |
| 158 | + static unsigned getBitWidth(Operation *op) { llvm_unreachable("NYI"); } |
| 159 | + |
| 160 | + // FIXME: CIR does not yet have UnnamedAddr attribute |
| 161 | + static UnnamedAddr getUnnamedAddr(Operation * /* op*/) { |
| 162 | + return UnnamedAddr::Global; |
| 163 | + } |
| 164 | + |
| 165 | + // FIXME: CIR does not yet have UnnamedAddr attribute |
| 166 | + static void setUnnamedAddr(Operation * /* op*/, UnnamedAddr addr) {} |
| 167 | + |
| 168 | + static std::optional<uint64_t> getAlignment(Operation *op) { |
| 169 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 170 | + return gv.getAlignment(); |
| 171 | + // FIXME: CIR does not (yet?) have alignment for functions |
| 172 | + llvm_unreachable("unexpected operation"); |
| 173 | + } |
| 174 | + |
| 175 | + static void setAlignment(Operation *op, std::optional<uint64_t> align) { |
| 176 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 177 | + return gv.setAlignment(align); |
| 178 | + // FIXME: CIR does not (yet?) have alignment for functions |
| 179 | + llvm_unreachable("unexpected operation"); |
| 180 | + } |
| 181 | + |
| 182 | + static bool isConstant(Operation *op) { |
| 183 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 184 | + return gv.getConstant(); |
| 185 | + llvm_unreachable("unexpected operation"); |
| 186 | + } |
| 187 | + |
| 188 | + static void setIsConstant(Operation *op, bool value) { |
| 189 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) |
| 190 | + return gv.setConstant(value); |
| 191 | + llvm_unreachable("constness setting allowed only for globals"); |
| 192 | + } |
| 193 | + |
| 194 | + static bool isGlobalVar(Operation *op) { return isa<cir::GlobalOp>(op); } |
| 195 | + |
| 196 | + static llvm::StringRef getSection(Operation *op) { |
| 197 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) { |
| 198 | + auto section = gv.getSection(); |
| 199 | + return section ? section.value() : llvm::StringRef(); |
| 200 | + } |
| 201 | + // FIXME: CIR func does not yet have section attribute |
| 202 | + llvm_unreachable("unexpected operation"); |
| 203 | + } |
| 204 | + |
| 205 | + static std::optional<cir::AddressSpaceAttr> getAddressSpace(Operation *op) { |
| 206 | + if (auto gv = dyn_cast<cir::GlobalOp>(op)) { |
| 207 | + if (auto addrSpace = gv.getAddrSpaceAttr()) { |
| 208 | + return addrSpace; |
| 209 | + } |
| 210 | + return std::nullopt; |
| 211 | + } |
| 212 | + |
| 213 | + llvm_unreachable("unexpected operation"); |
| 214 | + } |
| 215 | +}; |
| 216 | + |
21 | 217 | void registerLinkerInterface(mlir::DialectRegistry ®istry); |
22 | 218 | } // namespace cir |
23 | 219 |
|
|
0 commit comments