Skip to content

Commit 70aa8d6

Browse files
committed
[CIR] Add support for global linkage and visibility
This change adds support for the CIRGlobalValueInterface and attributes for visibility and comdat to GlobalOp.
1 parent 2b96c14 commit 70aa8d6

26 files changed

+477
-194
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_H
1414
#define LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_H
1515

16+
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
1617
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1718

1819
#include "mlir/IR/Attributes.h"

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,47 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
276276
}];
277277
}
278278

279+
//===----------------------------------------------------------------------===//
280+
// VisibilityAttr
281+
//===----------------------------------------------------------------------===//
282+
283+
def VK_Default : I32EnumAttrCase<"Default", 1, "default">;
284+
def VK_Hidden : I32EnumAttrCase<"Hidden", 2, "hidden">;
285+
def VK_Protected : I32EnumAttrCase<"Protected", 3, "protected">;
286+
287+
def VisibilityKind : I32EnumAttr<"VisibilityKind", "C/C++ visibility", [
288+
VK_Default, VK_Hidden, VK_Protected
289+
]> {
290+
let cppNamespace = "::cir";
291+
}
292+
293+
def VisibilityAttr : CIR_Attr<"Visibility", "visibility"> {
294+
let summary = "Visibility attribute";
295+
let description = [{
296+
Visibility attributes.
297+
}];
298+
let parameters = (ins "VisibilityKind":$value);
299+
300+
let assemblyFormat = [{
301+
$value
302+
}];
303+
304+
let builders = [
305+
AttrBuilder<(ins CArg<"VisibilityKind", "cir::VisibilityKind::Default">:$value), [{
306+
return $_get($_ctxt, value);
307+
}]>
308+
];
309+
310+
let skipDefaultBuilders = 1;
311+
312+
// Make DefaultValuedAttr accept VisibilityKind as default value ($0).
313+
let constBuilderCall = "cir::VisibilityAttr::get($_builder.getContext(), $0)";
314+
315+
let extraClassDeclaration = [{
316+
bool isDefault() const { return getValue() == VisibilityKind::Default; };
317+
bool isHidden() const { return getValue() == VisibilityKind::Hidden; };
318+
bool isProtected() const { return getValue() == VisibilityKind::Protected; };
319+
}];
320+
}
321+
279322
#endif // LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_TD

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,8 @@ def GlobalLinkageKind : I32EnumAttr<
16341634
// properties of a global variable will be added over time as more of ClangIR
16351635
// is upstreamed.
16361636

1637-
def GlobalOp : CIR_Op<"global"> {
1637+
def GlobalOp : CIR_Op<"global",
1638+
[DeclareOpInterfaceMethods<CIRGlobalValueInterface>]> {
16381639
let summary = "Declare or define a global variable";
16391640
let description = [{
16401641
The `cir.global` operation declares or defines a named global variable.
@@ -1643,17 +1644,31 @@ def GlobalOp : CIR_Op<"global"> {
16431644
described by the type of the variable.
16441645

16451646
The `linkage` tracks C/C++ linkage types, currently very similar to LLVM's.
1647+
Symbol visibility in `sym_visibility` is defined in terms of MLIR's visibility
1648+
and verified to be in accordance to `linkage`.
16461649
}];
16471650

1651+
// Note that both sym_name and sym_visibility are tied to Symbol trait.
1652+
// TODO: sym_visibility can possibly be represented by implementing the
1653+
// necessary Symbol's interface in terms of linkage instead.
16481654
let arguments = (ins SymbolNameAttr:$sym_name,
1655+
DefaultValuedAttr<
1656+
VisibilityAttr,
1657+
"VisibilityKind::Default"
1658+
>:$global_visibility,
1659+
OptionalAttr<StrAttr>:$sym_visibility,
16491660
TypeAttr:$sym_type,
16501661
Arg<GlobalLinkageKind, "linkage type">:$linkage,
16511662
OptionalAttr<AnyAttr>:$initial_value,
1663+
UnitAttr:$comdat,
16521664
UnitAttr:$dsolocal,
16531665
OptionalAttr<I64Attr>:$alignment);
16541666

16551667
let assemblyFormat = [{
1668+
($sym_visibility^)?
1669+
(`` $global_visibility^)?
16561670
$linkage
1671+
(`comdat` $comdat^)?
16571672
(`dsolocal` $dsolocal^)?
16581673
$sym_name
16591674
custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value)

clang/include/clang/CIR/Dialect/IR/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ mlir_tablegen(CIROpsDialect.cpp.inc -gen-dialect-defs)
1414
add_public_tablegen_target(MLIRCIROpsIncGen)
1515
add_dependencies(mlir-headers MLIRCIROpsIncGen)
1616

17-
mlir_tablegen(CIROpsAttributes.h.inc -gen-attrdef-decls)
18-
mlir_tablegen(CIROpsAttributes.cpp.inc -gen-attrdef-defs)
1917
mlir_tablegen(CIROpsEnums.h.inc -gen-enum-decls)
2018
mlir_tablegen(CIROpsEnums.cpp.inc -gen-enum-defs)
19+
mlir_tablegen(CIROpsAttributes.h.inc -gen-attrdef-decls)
20+
mlir_tablegen(CIROpsAttributes.cpp.inc -gen-attrdef-defs)
2121
add_public_tablegen_target(MLIRCIREnumsGen)
2222

2323
set(LLVM_TARGET_DEFINITIONS CIRTypeConstraints.td)

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ struct MissingFeatures {
3131
static bool cgfSymbolTable() { return false; }
3232

3333
// Unhandled global/linkage information.
34-
static bool opGlobalDSOLocal() { return false; }
3534
static bool opGlobalThreadLocal() { return false; }
3635
static bool opGlobalConstant() { return false; }
3736
static bool opGlobalWeakRef() { return false; }
@@ -41,11 +40,11 @@ struct MissingFeatures {
4140
static bool opGlobalVisibility() { return false; }
4241
static bool opGlobalDLLImportExport() { return false; }
4342
static bool opGlobalPartition() { return false; }
44-
static bool opGlobalCIRGlobalValueInterface() { return false; }
4543

4644
static bool supportIFuncAttr() { return false; }
4745
static bool supportVisibility() { return false; }
48-
static bool supportComdat() { return false; }
46+
static bool hiddenVisibility() { return false; }
47+
static bool protectedVisibility() { return false; }
4948

5049
// Load/store attributes
5150
static bool opLoadStoreThreadLocal() { return false; }
@@ -188,7 +187,6 @@ struct MissingFeatures {
188187
static bool updateCompletedType() { return false; }
189188
static bool targetSpecificCXXABI() { return false; }
190189
static bool moduleNameHash() { return false; }
191-
static bool setDSOLocal() { return false; }
192190
static bool constantFoldSwitchStatement() { return false; }
193191
static bool cudaSupport() { return false; }
194192
static bool maybeHandleStaticInExternC() { return false; }

0 commit comments

Comments
 (0)