Skip to content

Commit 0e90a84

Browse files
authored
[CIR] Add support for global linkage and visibility (#141973)
This change adds support for the CIRGlobalValueInterface and attributes for visibility and comdat to GlobalOp. The comdat attribute isn't correctly calculated yet, but it was required for the CIRGlobalValueInterface interface. There are also some cases where dso_local isn't set correctly, but it is better than it was before this change. Those issues will be addressed in a future patch.
1 parent 397fdb1 commit 0e90a84

26 files changed

+475
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,46 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
276276
}];
277277
}
278278

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