Skip to content

Commit df06292

Browse files
committed
[CIR] Clean up enum attributes
Create CIR specific EnumAttr bases and prefix enum attributes with `CIR_`
1 parent 74f16f7 commit df06292

File tree

14 files changed

+395
-526
lines changed

14 files changed

+395
-526
lines changed

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

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ class CIR_TypedAttr<string name, string attrMnemonic, list<Trait> traits = []>
4444
let assemblyFormat = [{}];
4545
}
4646

47+
class CIR_I32EnumAttr<string name, string summary, list<I32EnumAttrCase> cases>
48+
: I32EnumAttr<name, summary, cases> {
49+
let cppNamespace = "::cir";
50+
}
51+
52+
class CIR_I64EnumAttr<string name, string summary, list<I64EnumAttrCase> cases>
53+
: I64EnumAttr<name, summary, cases> {
54+
let cppNamespace = "::cir";
55+
}
56+
57+
class CIR_EnumAttr<EnumAttrInfo info, string name = "", list<Trait> traits = []>
58+
: EnumAttr<CIR_Dialect, info, name, traits> {
59+
let assemblyFormat = "`<` $value `>`";
60+
}
61+
4762
class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
4863
: CIR_Attr<name, attrMnemonic, traits> {
4964
let returnType = "bool";
@@ -53,18 +68,21 @@ class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
5368
}
5469

5570
//===----------------------------------------------------------------------===//
56-
// LangAttr
71+
// SourceLanguageAttr
5772
//===----------------------------------------------------------------------===//
5873

59-
def CIR_SourceLanguage : I32EnumAttr<"SourceLanguage", "Source language", [
74+
def CIR_SourceLanguage : CIR_I32EnumAttr<"SourceLanguage", "source language", [
6075
I32EnumAttrCase<"C", 1, "c">,
6176
I32EnumAttrCase<"CXX", 2, "cxx">,
6277
I32EnumAttrCase<"OpenCLC", 3, "opencl_c">
6378
]> {
64-
let cppNamespace = "::cir";
79+
// The enum attr class is defined in `CIR_SourceLanguageAttr` below,
80+
// so that it can define extra class methods.
81+
let genSpecializedAttr = 0;
6582
}
6683

67-
def CIR_LangAttr : CIR_Attr<"Lang", "lang"> {
84+
def CIR_SourceLanguageAttr : CIR_EnumAttr<CIR_SourceLanguage, "lang"> {
85+
6886
let summary = "Module source language";
6987
let description = [{
7088
Represents the source language used to generate the module.
@@ -76,17 +94,15 @@ def CIR_LangAttr : CIR_Attr<"Lang", "lang"> {
7694
// Module compiled from C++.
7795
module attributes {cir.lang = cir.lang<cxx>} {}
7896
```
79-
}];
80-
81-
let parameters = (ins "SourceLanguage":$lang);
8297

83-
let assemblyFormat = [{
84-
`<` $lang `>`
98+
Module source language attribute name is `cir.lang` is defined by
99+
`getSourceLanguageAttrName` method in CIRDialect class.
85100
}];
86101

87102
let extraClassDeclaration = [{
88-
bool isC() const { return getLang() == SourceLanguage::C; };
89-
bool isCXX() const { return getLang() == SourceLanguage::CXX; };
103+
bool isC() const { return getValue() == SourceLanguage::C; }
104+
bool isCXX() const { return getValue() == SourceLanguage::CXX; }
105+
bool isOpenCLC() const { return getValue() == SourceLanguage::OpenCLC; }
90106
}];
91107
}
92108

@@ -516,14 +532,12 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
516532
// CmpThreeWayInfoAttr
517533
//===----------------------------------------------------------------------===//
518534

519-
def CmpOrdering_Strong : I32EnumAttrCase<"Strong", 1, "strong">;
520-
def CmpOrdering_Partial : I32EnumAttrCase<"Partial", 2, "partial">;
521-
522-
def CmpOrdering : I32EnumAttr<
523-
"CmpOrdering", "three-way comparison ordering kind",
524-
[CmpOrdering_Strong, CmpOrdering_Partial]
525-
> {
526-
let cppNamespace = "::cir";
535+
def CIR_CmpOrdering : CIR_I32EnumAttr<
536+
"CmpOrdering", "three-way comparison ordering kind", [
537+
I32EnumAttrCase<"Strong", 1, "strong">,
538+
I32EnumAttrCase<"Partial", 2, "partial">
539+
]> {
540+
let genSpecializedAttr = 0;
527541
}
528542

529543
def CmpThreeWayInfoAttr : CIR_Attr<"CmpThreeWayInfo", "cmp3way_info"> {
@@ -682,18 +696,6 @@ def MethodAttr : CIR_Attr<"Method", "method", [TypedAttrInterface]> {
682696
}];
683697
}
684698

685-
//===----------------------------------------------------------------------===//
686-
// SignedOverflowBehaviorAttr
687-
//===----------------------------------------------------------------------===//
688-
689-
def SignedOverflowBehaviorAttr : AttrDef<CIR_Dialect, "SignedOverflowBehavior"> {
690-
let mnemonic = "signed_overflow_behavior";
691-
let parameters = (ins
692-
"sob::SignedOverflowBehavior":$behavior
693-
);
694-
let hasCustomAssemblyFormat = 1;
695-
}
696-
697699
//===----------------------------------------------------------------------===//
698700
// GlobalViewAttr
699701
//===----------------------------------------------------------------------===//
@@ -1133,37 +1135,32 @@ def ASTCallExprAttr : AST<"CallExpr", "call.expr",
11331135
// VisibilityAttr
11341136
//===----------------------------------------------------------------------===//
11351137

1136-
def VK_Default : I32EnumAttrCase<"Default", 1, "default">;
1137-
def VK_Hidden : I32EnumAttrCase<"Hidden", 2, "hidden">;
1138-
def VK_Protected : I32EnumAttrCase<"Protected", 3, "protected">;
1139-
1140-
def VisibilityKind : I32EnumAttr<"VisibilityKind", "C/C++ visibility", [
1141-
VK_Default, VK_Hidden, VK_Protected
1138+
def CIR_VisibilityKind : CIR_I32EnumAttr<"VisibilityKind", "C/C++ visibility", [
1139+
I32EnumAttrCase<"Default", 1, "default">,
1140+
I32EnumAttrCase<"Hidden", 2, "hidden">,
1141+
I32EnumAttrCase<"Protected", 3, "protected">
11421142
]> {
1143-
let cppNamespace = "::cir";
1143+
let genSpecializedAttr = 0;
11441144
}
11451145

1146-
def VisibilityAttr : CIR_Attr<"Visibility", "visibility"> {
1146+
def CIR_VisibilityAttr : CIR_EnumAttr<CIR_VisibilityKind, "visibility"> {
11471147
let summary = "Visibility attribute";
11481148
let description = [{
11491149
Visibility attributes.
11501150
}];
1151-
let parameters = (ins "VisibilityKind":$value);
11521151

1153-
let assemblyFormat = [{
1154-
$value
1155-
}];
1152+
let cppClassName = "VisibilityAttr";
11561153

1154+
let skipDefaultBuilders = 1;
11571155
let builders = [
11581156
AttrBuilder<(ins CArg<"VisibilityKind", "cir::VisibilityKind::Default">:$value), [{
11591157
return $_get($_ctxt, value);
11601158
}]>
11611159
];
11621160

1163-
let skipDefaultBuilders = 1;
1164-
1165-
// Make DefaultValuedAttr accept VisibilityKind as default value ($0).
1166-
let constBuilderCall = "cir::VisibilityAttr::get($_builder.getContext(), $0)";
1161+
let assemblyFormat = [{
1162+
$value
1163+
}];
11671164

11681165
let extraClassDeclaration = [{
11691166
bool isDefault() const { return getValue() == VisibilityKind::Default; };
@@ -1172,7 +1169,6 @@ def VisibilityAttr : CIR_Attr<"Visibility", "visibility"> {
11721169
}];
11731170
}
11741171

1175-
11761172
//===----------------------------------------------------------------------===//
11771173
// ExtraFuncAttr
11781174
//===----------------------------------------------------------------------===//
@@ -1197,27 +1193,25 @@ def ExtraFuncAttr : CIR_Attr<"ExtraFuncAttributes", "extra"> {
11971193
// Printing and parsing also available in CIRDialect.cpp
11981194
}
11991195

1200-
def NoInline : I32EnumAttrCase<"NoInline", 1, "no">;
1201-
def AlwaysInline : I32EnumAttrCase<"AlwaysInline", 2, "always">;
1202-
def InlineHint : I32EnumAttrCase<"InlineHint", 3, "hint">;
1196+
//===----------------------------------------------------------------------===//
1197+
// InlineAttr
1198+
//===----------------------------------------------------------------------===//
12031199

1204-
def InlineKind : I32EnumAttr<"InlineKind", "inlineKind", [
1205-
NoInline, AlwaysInline, InlineHint
1200+
def CIR_InlineKind : CIR_I32EnumAttr<"InlineKind", "inlineKind", [
1201+
I32EnumAttrCase<"NoInline", 1, "no">,
1202+
I32EnumAttrCase<"AlwaysInline", 2, "always">,
1203+
I32EnumAttrCase<"InlineHint", 3, "hint">
12061204
]> {
1207-
let cppNamespace = "::cir";
1205+
let genSpecializedAttr = 0;
12081206
}
12091207

1210-
def InlineAttr : CIR_Attr<"Inline", "inline"> {
1208+
def CIR_InlineAttr : CIR_EnumAttr<CIR_InlineKind, "inline"> {
12111209
let summary = "Inline attribute";
12121210
let description = [{
12131211
Inline attributes represents user directives.
12141212
}];
12151213

1216-
let parameters = (ins "InlineKind":$value);
1217-
1218-
let assemblyFormat = [{
1219-
`<` $value `>`
1220-
}];
1214+
let cppClassName = "InlineAttr";
12211215

12221216
let extraClassDeclaration = [{
12231217
bool isNoInline() const { return getValue() == InlineKind::NoInline; };
@@ -1226,6 +1220,10 @@ def InlineAttr : CIR_Attr<"Inline", "inline"> {
12261220
}];
12271221
}
12281222

1223+
//===----------------------------------------------------------------------===//
1224+
// Unit Function Attributes
1225+
//===----------------------------------------------------------------------===//
1226+
12291227
def OptNoneAttr : CIRUnitAttr<"OptNone", "optnone"> {
12301228
let storageType = [{ OptNoneAttr }];
12311229
}
@@ -1238,21 +1236,19 @@ def ConvergentAttr : CIRUnitAttr<"Convergent", "convergent"> {
12381236
let storageType = [{ ConvergentAttr }];
12391237
}
12401238

1241-
def UWTableKindNone
1242-
: I32EnumAttrCase<"None", 0, "none">;
1243-
def UWTableKindSync
1244-
: I32EnumAttrCase<"Sync", 1, "sync">;
1245-
def UWTableKindAsync
1246-
: I32EnumAttrCase<"Async", 2, "async">;
1239+
//===----------------------------------------------------------------------===//
1240+
// UWTableAttr
1241+
//===----------------------------------------------------------------------===//
12471242

1248-
def UWTableKind : I32EnumAttr<"UWTableKind", "Unwind table kind", [
1249-
UWTableKindNone, UWTableKindSync, UWTableKindAsync
1243+
def CIR_UWTableKind : CIR_I32EnumAttr<"UWTableKind", "Unwind table kind", [
1244+
I32EnumAttrCase<"None", 0, "none">,
1245+
I32EnumAttrCase<"Sync", 1, "sync">,
1246+
I32EnumAttrCase<"Async", 2, "async">
12501247
]> {
1251-
let cppNamespace = "::cir";
12521248
let genSpecializedAttr = 0;
12531249
}
12541250

1255-
def UWTableAttr : EnumAttr<CIR_Dialect, UWTableKind, "uwtable"> {
1251+
def CIR_UWTableAttr : CIR_EnumAttr<CIR_UWTableKind, "uwtable"> {
12561252
let summary = "Unwind table kind attribute";
12571253
let description = [{
12581254
The kind of unwind tables to generate for a function. `none` means no unwind
@@ -1263,9 +1259,6 @@ def UWTableAttr : EnumAttr<CIR_Dialect, UWTableKind, "uwtable"> {
12631259
}];
12641260

12651261
let cppClassName = "UWTableAttr";
1266-
let assemblyFormat = [{
1267-
`<` $value `>`
1268-
}];
12691262
}
12701263

12711264
class CIR_GlobalCtorDtor<string name, string attrMnemonic,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def CIR_Dialect : Dialect {
3636
static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; }
3737
static llvm::StringRef getTypeSizeInfoAttrName() { return "cir.type_size_info"; }
3838
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
39-
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
39+
static llvm::StringRef getSourceLanguageAttrName() { return "cir.lang"; }
4040
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
4141
static llvm::StringRef getOptInfoAttrName() { return "cir.opt_info"; }
4242
static llvm::StringRef getUWTableAttrName() { return "cir.uwtable"; }

0 commit comments

Comments
 (0)