Skip to content

Commit 0b96f16

Browse files
xlaukolanza
authored andcommitted
[CIR] Simplify LangAttr to use enum directly (llvm#1631)
1 parent a2f5b15 commit 0b96f16

File tree

4 files changed

+17
-46
lines changed

4 files changed

+17
-46
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,16 @@ class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
5656
// LangAttr
5757
//===----------------------------------------------------------------------===//
5858

59-
def C : I32EnumAttrCase<"C", 1, "c">;
60-
def CXX : I32EnumAttrCase<"CXX", 2, "cxx">;
61-
def OpenCLC : I32EnumAttrCase<"OpenCLC", 3, "opencl_c">;
62-
63-
def SourceLanguage : I32EnumAttr<"SourceLanguage", "Source language", [
64-
C, CXX, OpenCLC
59+
def CIR_SourceLanguage : I32EnumAttr<"SourceLanguage", "Source language", [
60+
I32EnumAttrCase<"C", 1, "c">,
61+
I32EnumAttrCase<"CXX", 2, "cxx">,
62+
I32EnumAttrCase<"OpenCLC", 3, "opencl_c">
6563
]> {
6664
let cppNamespace = "::cir";
6765
}
6866

69-
def LangAttr : CIR_Attr<"Lang", "lang"> {
67+
def CIR_LangAttr : CIR_Attr<"Lang", "lang"> {
7068
let summary = "Module source language";
71-
let parameters = (ins "SourceLanguageAttr":$lang);
7269
let description = [{
7370
Represents the source language used to generate the module.
7471

@@ -80,10 +77,16 @@ def LangAttr : CIR_Attr<"Lang", "lang"> {
8077
module attributes {cir.lang = cir.lang<cxx>} {}
8178
```
8279
}];
83-
let hasCustomAssemblyFormat = 1;
80+
81+
let parameters = (ins "SourceLanguage":$lang);
82+
83+
let assemblyFormat = [{
84+
`<` $lang `>`
85+
}];
86+
8487
let extraClassDeclaration = [{
85-
bool isC() const { return getLang().getValue() == SourceLanguage::C; };
86-
bool isCXX() const { return getLang().getValue() == SourceLanguage::CXX; };
88+
bool isC() const { return getLang() == SourceLanguage::C; };
89+
bool isCXX() const { return getLang() == SourceLanguage::CXX; };
8790
}];
8891
}
8992

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
192192
// MLIR features.
193193
theModule->setAttr(cir::CIRDialect::getSOBAttrName(),
194194
cir::SignedOverflowBehaviorAttr::get(&mlirContext, sob));
195-
auto lang = SourceLanguageAttr::get(&mlirContext, getCIRSourceLanguage());
196195
theModule->setAttr(cir::CIRDialect::getLangAttrName(),
197-
cir::LangAttr::get(&mlirContext, lang));
196+
cir::LangAttr::get(&mlirContext, getCIRSourceLanguage()));
198197
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
199198
builder.getStringAttr(getTriple().str()));
200199

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -170,38 +170,6 @@ LogicalResult ConstRecordAttr::verify(
170170
return success();
171171
}
172172

173-
//===----------------------------------------------------------------------===//
174-
// LangAttr definitions
175-
//===----------------------------------------------------------------------===//
176-
177-
Attribute LangAttr::parse(AsmParser &parser, Type odsType) {
178-
auto loc = parser.getCurrentLocation();
179-
if (parser.parseLess())
180-
return {};
181-
182-
// Parse variable 'lang'.
183-
llvm::StringRef lang;
184-
if (parser.parseKeyword(&lang))
185-
return {};
186-
187-
// Check if parsed value is a valid language.
188-
auto langEnum = symbolizeSourceLanguage(lang);
189-
if (!langEnum.has_value()) {
190-
parser.emitError(loc) << "invalid language keyword '" << lang << "'";
191-
return {};
192-
}
193-
194-
if (parser.parseGreater())
195-
return {};
196-
197-
return get(parser.getContext(),
198-
SourceLanguageAttr::get(parser.getContext(), langEnum.value()));
199-
}
200-
201-
void LangAttr::print(AsmPrinter &printer) const {
202-
printer << "<" << getLang().getValue() << '>';
203-
}
204-
205173
//===----------------------------------------------------------------------===//
206174
// OptInfoAttr definitions
207175
//===----------------------------------------------------------------------===//

clang/test/CIR/IR/invalid.cir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ module {
690690

691691
// -----
692692

693-
// expected-error@+1 {{invalid language keyword 'dummy'}}
693+
// expected-error@below {{expected one of [c, cxx, opencl_c] for Source language, got: dummy}}
694+
// expected-error@below {{failed to parse CIR_LangAttr parameter 'lang'}}
694695
module attributes {cir.lang = #cir.lang<dummy>} { }
695696

696697
// -----

0 commit comments

Comments
 (0)