Skip to content

Commit ac56f5d

Browse files
seven-milelanza
authored andcommitted
[CIR][Dialect][NFC] Refactor hardcoded attribute name strings (#1122)
As title, this patch refactors raw string literals for (module) attribute names into static methods of `CIRDialect`, following the convention of MLIR.
1 parent acecfa5 commit ac56f5d

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def CIR_Dialect : Dialect {
3434
// Names of CIR parameter attributes.
3535
static llvm::StringRef getSExtAttrName() { return "cir.signext"; }
3636
static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; }
37+
static llvm::StringRef getSOBAttrName() { return "cir.sob"; }
38+
static llvm::StringRef getLangAttrName() { return "cir.lang"; }
39+
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
40+
41+
static llvm::StringRef getGlobalCtorsAttrName() { return "cir.global_ctors"; }
42+
static llvm::StringRef getGlobalDtorsAttrName() { return "cir.global_dtors"; }
43+
static llvm::StringRef getGlobalAnnotationsAttrName() { return "cir.global_annotations"; }
44+
45+
static llvm::StringRef getOpenCLVersionAttrName() { return "cir.cl.version"; }
3746

3847
void registerAttributes();
3948
void registerTypes();

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,13 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
185185

186186
// FIXME(cir): Implement a custom CIR Module Op and attributes to leverage
187187
// MLIR features.
188-
theModule->setAttr("cir.sob",
188+
theModule->setAttr(cir::CIRDialect::getSOBAttrName(),
189189
cir::SignedOverflowBehaviorAttr::get(&context, sob));
190190
auto lang = SourceLanguageAttr::get(&context, getCIRSourceLanguage());
191-
theModule->setAttr("cir.lang", cir::LangAttr::get(&context, lang));
192-
theModule->setAttr("cir.triple", builder.getStringAttr(getTriple().str()));
191+
theModule->setAttr(cir::CIRDialect::getLangAttrName(),
192+
cir::LangAttr::get(&context, lang));
193+
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
194+
builder.getStringAttr(getTriple().str()));
193195
// Set the module name to be the name of the main file. TranslationUnitDecl
194196
// often contains invalid source locations and isn't a reliable source for the
195197
// module location.

clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,6 @@ void CIRGenModule::emitOpenCLMetadata() {
260260
auto clVersionAttr =
261261
cir::OpenCLVersionAttr::get(&getMLIRContext(), major, minor);
262262

263-
theModule->setAttr("cir.cl.version", clVersionAttr);
263+
theModule->setAttr(cir::CIRDialect::getOpenCLVersionAttrName(),
264+
clVersionAttr);
264265
}

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,11 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
888888

889889
void LoweringPreparePass::buildGlobalCtorDtorList() {
890890
if (!globalCtorList.empty()) {
891-
theModule->setAttr("cir.global_ctors",
891+
theModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
892892
mlir::ArrayAttr::get(&getContext(), globalCtorList));
893893
}
894894
if (!globalDtorList.empty()) {
895-
theModule->setAttr("cir.global_dtors",
895+
theModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(),
896896
mlir::ArrayAttr::get(&getContext(), globalDtorList));
897897
}
898898
}
@@ -1136,7 +1136,7 @@ void LoweringPreparePass::buildGlobalAnnotationValues() {
11361136
return;
11371137
mlir::ArrayAttr annotationValueArray =
11381138
mlir::ArrayAttr::get(theModule.getContext(), globalAnnotations);
1139-
theModule->setAttr("cir.global_annotations",
1139+
theModule->setAttr(cir::CIRDialect::getGlobalAnnotationsAttrName(),
11401140
cir::GlobalAnnotationValuesAttr::get(
11411141
theModule.getContext(), annotationValueArray));
11421142
}

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ void LowerModule::constructAttributeList(llvm::StringRef Name,
211211
switch (AI.getKind()) {
212212
case ABIArgInfo::Extend:
213213
if (AI.isSignExt())
214-
Attrs.push_back(
215-
rewriter.getNamedAttr("cir.signext", rewriter.getUnitAttr()));
214+
Attrs.push_back(rewriter.getNamedAttr(
215+
cir::CIRDialect::getSExtAttrName(), rewriter.getUnitAttr()));
216216
else
217217
// FIXME(cir): Add a proper abstraction to create attributes.
218-
Attrs.push_back(
219-
rewriter.getNamedAttr("cir.zeroext", rewriter.getUnitAttr()));
218+
Attrs.push_back(rewriter.getNamedAttr(
219+
cir::CIRDialect::getZExtAttrName(), rewriter.getUnitAttr()));
220220
[[fallthrough]];
221221
case ABIArgInfo::Direct:
222222
if (ArgNo == 0 && cir::MissingFeatures::chainCall())

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ createLowerModule(mlir::ModuleOp module, mlir::PatternRewriter &rewriter) {
236236
module->getAttr(mlir::LLVM::LLVMDialect::getDataLayoutAttrName()));
237237

238238
// Fetch target information.
239-
llvm::Triple triple(
240-
mlir::cast<mlir::StringAttr>(module->getAttr("cir.triple")).getValue());
239+
llvm::Triple triple(mlir::cast<mlir::StringAttr>(
240+
module->getAttr(cir::CIRDialect::getTripleAttrName()))
241+
.getValue());
241242
clang::TargetOptions targetOptions;
242243
targetOptions.Triple = triple.str();
243244
auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -4448,7 +4448,7 @@ std::unique_ptr<cir::LowerModule> prepareLowerModule(mlir::ModuleOp module) {
44484448
// If the triple is not present, e.g. CIR modules parsed from text, we
44494449
// cannot init LowerModule properly.
44504450
assert(!cir::MissingFeatures::makeTripleAlwaysPresent());
4451-
if (!module->hasAttr("cir.triple"))
4451+
if (!module->hasAttr(cir::CIRDialect::getTripleAttrName()))
44524452
return {};
44534453
return cir::createLowerModule(module, rewriter);
44544454
}
@@ -4706,7 +4706,8 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar(
47064706
llvm::StringMap<mlir::LLVM::GlobalOp> &argStringGlobalsMap,
47074707
llvm::MapVector<mlir::ArrayAttr, mlir::LLVM::GlobalOp> &argsVarMap) {
47084708
mlir::ModuleOp module = getOperation();
4709-
mlir::Attribute attr = module->getAttr("cir.global_annotations");
4709+
mlir::Attribute attr =
4710+
module->getAttr(cir::CIRDialect::getGlobalAnnotationsAttrName());
47104711
if (!attr)
47114712
return;
47124713
if (auto globalAnnotValues =
@@ -4834,8 +4835,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
48344835
// Allow operations that will be lowered directly to LLVM IR.
48354836
target.addLegalOp<mlir::LLVM::ZeroOp>();
48364837

4837-
getOperation()->removeAttr("cir.sob");
4838-
getOperation()->removeAttr("cir.lang");
4838+
getOperation()->removeAttr(cir::CIRDialect::getSOBAttrName());
4839+
getOperation()->removeAttr(cir::CIRDialect::getLangAttrName());
48394840

48404841
llvm::SmallVector<mlir::Operation *> ops;
48414842
ops.push_back(module);
@@ -4845,17 +4846,17 @@ void ConvertCIRToLLVMPass::runOnOperation() {
48454846
signalPassFailure();
48464847

48474848
// Emit the llvm.global_ctors array.
4848-
buildCtorDtorList(module, "cir.global_ctors", "llvm.global_ctors",
4849-
[](mlir::Attribute attr) {
4849+
buildCtorDtorList(module, cir::CIRDialect::getGlobalCtorsAttrName(),
4850+
"llvm.global_ctors", [](mlir::Attribute attr) {
48504851
assert(mlir::isa<cir::GlobalCtorAttr>(attr) &&
48514852
"must be a GlobalCtorAttr");
48524853
auto ctorAttr = mlir::cast<cir::GlobalCtorAttr>(attr);
48534854
return std::make_pair(ctorAttr.getName(),
48544855
ctorAttr.getPriority());
48554856
});
48564857
// Emit the llvm.global_dtors array.
4857-
buildCtorDtorList(module, "cir.global_dtors", "llvm.global_dtors",
4858-
[](mlir::Attribute attr) {
4858+
buildCtorDtorList(module, cir::CIRDialect::getGlobalDtorsAttrName(),
4859+
"llvm.global_dtors", [](mlir::Attribute attr) {
48594860
assert(mlir::isa<cir::GlobalDtorAttr>(attr) &&
48604861
"must be a GlobalDtorAttr");
48614862
auto dtorAttr = mlir::cast<cir::GlobalDtorAttr>(attr);

0 commit comments

Comments
 (0)