1212
1313#ifndef CLANG_INTERFACES_CIR_CIRLINKINTERFACE_H_
1414#define CLANG_INTERFACES_CIR_CIRLINKINTERFACE_H_
15+ #include " clang/CIR/Dialect/IR/CIROpsEnums.h"
1516#include " mlir/Linker/LLVMLinkerMixin.h"
16- #include " mlir/Linker/LinkerInterface.h"
17- #include " clang/CIR/Dialect/IR/CIRDialect.h"
17+
1818#include < optional>
1919
2020namespace mlir {
@@ -35,183 +35,44 @@ class CIRSymbolLinkerInterface
3535 CIRSymbolLinkerInterface (Dialect *dialect)
3636 : SymbolAttrLLVMLinkerInterface(dialect) {}
3737
38- bool canBeLinked (Operation *op) const override {
39- return isa<cir::GlobalOp>(op) || isa<cir::FuncOp>(op);
40- }
38+ bool canBeLinked (Operation *op) const override ;
4139
4240 // ===--------------------------------------------------------------------===//
4341 // LLVMLinkerMixin required methods from derived linker interface
4442 // ===--------------------------------------------------------------------===//
4543
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- }
44+ static Linkage getLinkage (Operation *op);
45+
46+ static bool isComdat (Operation *op);
8747
8848 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- }
49+ getComdatSelector (Operation *op);
50+
51+ static Visibility getVisibility (Operation *op);
52+
53+ static void setVisibility (Operation *op, Visibility visibility);
54+
55+ static bool isDeclaration (Operation *op);
56+
57+ static unsigned getBitWidth (Operation *op);
58+
59+ static UnnamedAddr getUnnamedAddr (Operation *op);
60+
61+ static void setUnnamedAddr (Operation *op, UnnamedAddr addr);
62+
63+ static std::optional<uint64_t > getAlignment (Operation *op);
64+
65+ static void setAlignment (Operation *op, std::optional<uint64_t > align);
66+
67+ static bool isConstant (Operation *op);
68+
69+ static void setIsConstant (Operation *op, bool value);
70+
71+ static bool isGlobalVar (Operation *op);
72+
73+ static llvm::StringRef getSection (Operation *op);
74+
75+ static std::optional<cir::AddressSpace> getAddressSpace (Operation *op);
21576};
21677
21778void registerLinkerInterface (mlir::DialectRegistry ®istry);
0 commit comments