From ce80af6f3decf22f809393d22ca73764d483ae69 Mon Sep 17 00:00:00 2001 From: seven-mile Date: Wed, 13 Nov 2024 15:55:09 +0000 Subject: [PATCH] [CIR][Dialect] Refactor hardcoded attribute name strings --- .../include/clang/CIR/Dialect/IR/CIRDialect.td | 9 +++++++++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 8 +++++--- clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp | 3 ++- .../CIR/Dialect/Transforms/LoweringPrepare.cpp | 6 +++--- .../Transforms/TargetLowering/LowerCall.cpp | 8 ++++---- .../Transforms/TargetLowering/LowerModule.cpp | 5 +++-- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 17 +++++++++-------- 7 files changed, 35 insertions(+), 21 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td index 5ec1865bc3e2..c27bc1f28443 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td @@ -34,6 +34,15 @@ def CIR_Dialect : Dialect { // Names of CIR parameter attributes. static llvm::StringRef getSExtAttrName() { return "cir.signext"; } static llvm::StringRef getZExtAttrName() { return "cir.zeroext"; } + static llvm::StringRef getSOBAttrName() { return "cir.sob"; } + static llvm::StringRef getLangAttrName() { return "cir.lang"; } + static llvm::StringRef getTripleAttrName() { return "cir.triple"; } + + static llvm::StringRef getGlobalCtorsAttrName() { return "cir.global_ctors"; } + static llvm::StringRef getGlobalDtorsAttrName() { return "cir.global_dtors"; } + static llvm::StringRef getGlobalAnnotationsAttrName() { return "cir.global_annotations"; } + + static llvm::StringRef getOpenCLVersionAttrName() { return "cir.cl.version"; } void registerAttributes(); void registerTypes(); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 939d4dc40433..97678fa2ad8a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -185,11 +185,13 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context, // FIXME(cir): Implement a custom CIR Module Op and attributes to leverage // MLIR features. - theModule->setAttr("cir.sob", + theModule->setAttr(cir::CIRDialect::getSOBAttrName(), cir::SignedOverflowBehaviorAttr::get(&context, sob)); auto lang = SourceLanguageAttr::get(&context, getCIRSourceLanguage()); - theModule->setAttr("cir.lang", cir::LangAttr::get(&context, lang)); - theModule->setAttr("cir.triple", builder.getStringAttr(getTriple().str())); + theModule->setAttr(cir::CIRDialect::getLangAttrName(), + cir::LangAttr::get(&context, lang)); + theModule->setAttr(cir::CIRDialect::getTripleAttrName(), + builder.getStringAttr(getTriple().str())); // Set the module name to be the name of the main file. TranslationUnitDecl // often contains invalid source locations and isn't a reliable source for the // module location. diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp index d11126940935..3db01cab6659 100644 --- a/clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp @@ -260,5 +260,6 @@ void CIRGenModule::emitOpenCLMetadata() { auto clVersionAttr = cir::OpenCLVersionAttr::get(&getMLIRContext(), major, minor); - theModule->setAttr("cir.cl.version", clVersionAttr); + theModule->setAttr(cir::CIRDialect::getOpenCLVersionAttrName(), + clVersionAttr); } diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 031c3b3b4b40..05660a132431 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -881,11 +881,11 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) { void LoweringPreparePass::buildGlobalCtorDtorList() { if (!globalCtorList.empty()) { - theModule->setAttr("cir.global_ctors", + theModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(), mlir::ArrayAttr::get(&getContext(), globalCtorList)); } if (!globalDtorList.empty()) { - theModule->setAttr("cir.global_dtors", + theModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(), mlir::ArrayAttr::get(&getContext(), globalDtorList)); } } @@ -1129,7 +1129,7 @@ void LoweringPreparePass::buildGlobalAnnotationValues() { return; mlir::ArrayAttr annotationValueArray = mlir::ArrayAttr::get(theModule.getContext(), globalAnnotations); - theModule->setAttr("cir.global_annotations", + theModule->setAttr(cir::CIRDialect::getGlobalAnnotationsAttrName(), cir::GlobalAnnotationValuesAttr::get( theModule.getContext(), annotationValueArray)); } diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp index 1978f0497c85..8b335fa9c5e8 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp @@ -211,12 +211,12 @@ void LowerModule::constructAttributeList(llvm::StringRef Name, switch (AI.getKind()) { case ABIArgInfo::Extend: if (AI.isSignExt()) - Attrs.push_back( - rewriter.getNamedAttr("cir.signext", rewriter.getUnitAttr())); + Attrs.push_back(rewriter.getNamedAttr( + cir::CIRDialect::getSExtAttrName(), rewriter.getUnitAttr())); else // FIXME(cir): Add a proper abstraction to create attributes. - Attrs.push_back( - rewriter.getNamedAttr("cir.zeroext", rewriter.getUnitAttr())); + Attrs.push_back(rewriter.getNamedAttr( + cir::CIRDialect::getZExtAttrName(), rewriter.getUnitAttr())); [[fallthrough]]; case ABIArgInfo::Direct: if (ArgNo == 0 && cir::MissingFeatures::chainCall()) diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp index 8e4ac59a9363..f6c75b39e516 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp @@ -236,8 +236,9 @@ createLowerModule(mlir::ModuleOp module, mlir::PatternRewriter &rewriter) { module->getAttr(mlir::LLVM::LLVMDialect::getDataLayoutAttrName())); // Fetch target information. - llvm::Triple triple( - mlir::cast(module->getAttr("cir.triple")).getValue()); + llvm::Triple triple(mlir::cast( + module->getAttr(cir::CIRDialect::getTripleAttrName())) + .getValue()); clang::TargetOptions targetOptions; targetOptions.Triple = triple.str(); auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index a5d6a69693ae..c5e4a02fcb9a 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -4412,7 +4412,7 @@ std::unique_ptr prepareLowerModule(mlir::ModuleOp module) { // If the triple is not present, e.g. CIR modules parsed from text, we // cannot init LowerModule properly. assert(!cir::MissingFeatures::makeTripleAlwaysPresent()); - if (!module->hasAttr("cir.triple")) + if (!module->hasAttr(cir::CIRDialect::getTripleAttrName())) return {}; return cir::createLowerModule(module, rewriter); } @@ -4670,7 +4670,8 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar( llvm::StringMap &argStringGlobalsMap, llvm::MapVector &argsVarMap) { mlir::ModuleOp module = getOperation(); - mlir::Attribute attr = module->getAttr("cir.global_annotations"); + mlir::Attribute attr = + module->getAttr(cir::CIRDialect::getGlobalAnnotationsAttrName()); if (!attr) return; if (auto globalAnnotValues = @@ -4798,8 +4799,8 @@ void ConvertCIRToLLVMPass::runOnOperation() { // Allow operations that will be lowered directly to LLVM IR. target.addLegalOp(); - getOperation()->removeAttr("cir.sob"); - getOperation()->removeAttr("cir.lang"); + getOperation()->removeAttr(cir::CIRDialect::getSOBAttrName()); + getOperation()->removeAttr(cir::CIRDialect::getLangAttrName()); llvm::SmallVector ops; ops.push_back(module); @@ -4809,8 +4810,8 @@ void ConvertCIRToLLVMPass::runOnOperation() { signalPassFailure(); // Emit the llvm.global_ctors array. - buildCtorDtorList(module, "cir.global_ctors", "llvm.global_ctors", - [](mlir::Attribute attr) { + buildCtorDtorList(module, cir::CIRDialect::getGlobalCtorsAttrName(), + "llvm.global_ctors", [](mlir::Attribute attr) { assert(mlir::isa(attr) && "must be a GlobalCtorAttr"); auto ctorAttr = mlir::cast(attr); @@ -4818,8 +4819,8 @@ void ConvertCIRToLLVMPass::runOnOperation() { ctorAttr.getPriority()); }); // Emit the llvm.global_dtors array. - buildCtorDtorList(module, "cir.global_dtors", "llvm.global_dtors", - [](mlir::Attribute attr) { + buildCtorDtorList(module, cir::CIRDialect::getGlobalDtorsAttrName(), + "llvm.global_dtors", [](mlir::Attribute attr) { assert(mlir::isa(attr) && "must be a GlobalDtorAttr"); auto dtorAttr = mlir::cast(attr);