[Mlir-commits] [mlir] 830b9b0 - Update some uses of `getAttr()` to be explicit about Inherent vs Discardable (NFC)

Mehdi Amini llvmlistbot at llvm.org
Tue Sep 12 01:34:11 PDT 2023


Author: Mehdi Amini
Date: 2023-09-12T01:33:47-07:00
New Revision: 830b9b072d8458ee89c48f00d4de59456c9f467f

URL: https://github.com/llvm/llvm-project/commit/830b9b072d8458ee89c48f00d4de59456c9f467f
DIFF: https://github.com/llvm/llvm-project/commit/830b9b072d8458ee89c48f00d4de59456c9f467f.diff

LOG: Update some uses of `getAttr()` to be explicit about Inherent vs Discardable (NFC)

Added: 
    

Modified: 
    mlir/docs/DeclarativeRewrites.md
    mlir/lib/Target/LLVMIR/DebugTranslation.cpp
    mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
    mlir/test/lib/Analysis/TestAliasAnalysis.cpp
    mlir/test/lib/Analysis/TestCallGraph.cpp
    mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
    mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp
    mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp
    mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp
    mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp
    mlir/test/lib/Dialect/Test/TestDialect.cpp
    mlir/test/lib/Dialect/Test/TestPatterns.cpp
    mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp
    mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
    mlir/test/lib/IR/TestSymbolUses.cpp
    mlir/test/lib/Transforms/TestTopologicalSort.cpp
    mlir/tools/mlir-tblgen/RewriterGen.cpp
    mlir/unittests/Bytecode/BytecodeTest.cpp
    mlir/unittests/IR/OperationSupportTest.cpp
    mlir/unittests/Pass/PassManagerTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/docs/DeclarativeRewrites.md b/mlir/docs/DeclarativeRewrites.md
index 2ae99f4ea32b3e9..888ce57fa3b5336 100644
--- a/mlir/docs/DeclarativeRewrites.md
+++ b/mlir/docs/DeclarativeRewrites.md
@@ -736,10 +736,10 @@ For example, we can write
 def GetOwner: NativeCodeCall<"$0.getOwner()">;
 
 def CopyAttrFoo: NativeCodeCallVoid<
-  "$1->setAttr($_builder.getStringAttr(\"foo\"), $0->getAttr(\"foo\"))">;
+  "$1->setAttr($_builder.getStringAttr(\"foo\"), $0->getInherentAttr(\"foo\"))">;
 
 def CopyAttrBar: NativeCodeCallVoid<
-  "$1->setAttr($_builder.getStringAttr(\"bar\"), $0->getAttr(\"bar\"))">;
+  "$1->setAttr($_builder.getStringAttr(\"bar\"), $0->getInherentAttr(\"bar\"))">;
 
 
 def : Pattern<

diff  --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index 2053f5bcef06aa6..52a01d7e8e0e1d0 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -42,8 +42,8 @@ DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
     llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
                              llvm::DEBUG_METADATA_VERSION);
 
-  if (auto targetTripleAttr =
-          module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
+  if (auto targetTripleAttr = module->getDiscardableAttr(
+          LLVM::LLVMDialect::getTargetTripleAttrName())) {
     auto targetTriple =
         llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
     if (targetTriple.isKnownWindowsMSVCEnvironment()) {

diff  --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index ce9bf2fbe35a1a1..24ddd47a794cee9 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1327,7 +1327,7 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
   m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
   auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
   if (auto dataLayoutAttr =
-          m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
+          m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
     llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
   } else {
     FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout(""));
@@ -1347,7 +1347,7 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
     llvmModule->setDataLayout(*llvmDataLayout);
   }
   if (auto targetTripleAttr =
-          m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
+          m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
     llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
 
   // Inject declarations for `malloc` and `free` functions that can be used in

diff  --git a/mlir/test/lib/Analysis/TestAliasAnalysis.cpp b/mlir/test/lib/Analysis/TestAliasAnalysis.cpp
index 8dd5f1d3672ee07..65e3490953d6ec0 100644
--- a/mlir/test/lib/Analysis/TestAliasAnalysis.cpp
+++ b/mlir/test/lib/Analysis/TestAliasAnalysis.cpp
@@ -61,12 +61,12 @@ void printModRefResult(ModRefResult result, Operation *op, Value location) {
 
 void TestAliasAnalysisBase::runAliasAnalysisOnOperation(
     Operation *op, AliasAnalysis &aliasAnalysis) {
-  llvm::errs() << "Testing : " << op->getAttr("sym_name") << "\n";
+  llvm::errs() << "Testing : " << *op->getInherentAttr("sym_name") << "\n";
 
   // Collect all of the values to check for aliasing behavior.
   SmallVector<Value, 32> valsToCheck;
   op->walk([&](Operation *op) {
-    if (!op->getAttr("test.ptr"))
+    if (!op->getDiscardableAttr("test.ptr"))
       return;
     valsToCheck.append(op->result_begin(), op->result_end());
     for (Region &region : op->getRegions())
@@ -82,12 +82,12 @@ void TestAliasAnalysisBase::runAliasAnalysisOnOperation(
 
 void TestAliasAnalysisModRefBase::runAliasAnalysisOnOperation(
     Operation *op, AliasAnalysis &aliasAnalysis) {
-  llvm::errs() << "Testing : " << op->getAttr("sym_name") << "\n";
+  llvm::errs() << "Testing : " << *op->getInherentAttr("sym_name") << "\n";
 
   // Collect all of the values to check for aliasing behavior.
   SmallVector<Value, 32> valsToCheck;
   op->walk([&](Operation *op) {
-    if (!op->getAttr("test.ptr"))
+    if (!op->getDiscardableAttr("test.ptr"))
       return;
     valsToCheck.append(op->result_begin(), op->result_end());
     for (Region &region : op->getRegions())
@@ -98,7 +98,7 @@ void TestAliasAnalysisModRefBase::runAliasAnalysisOnOperation(
   // Check for aliasing behavior between each of the values.
   for (auto &it : valsToCheck) {
     op->walk([&](Operation *op) {
-      if (!op->getAttr("test.ptr"))
+      if (!op->getDiscardableAttr("test.ptr"))
         return;
       printModRefResult(aliasAnalysis.getModRef(op, it), op, it);
     });

diff  --git a/mlir/test/lib/Analysis/TestCallGraph.cpp b/mlir/test/lib/Analysis/TestCallGraph.cpp
index 0bd048f0e8df6f4..bdb11a2108968ee 100644
--- a/mlir/test/lib/Analysis/TestCallGraph.cpp
+++ b/mlir/test/lib/Analysis/TestCallGraph.cpp
@@ -27,8 +27,8 @@ struct TestCallGraphPass
     return "Print the contents of a constructed callgraph.";
   }
   void runOnOperation() override {
-    llvm::errs() << "Testing : " << getOperation()->getAttr("test.name")
-                 << "\n";
+    llvm::errs() << "Testing : "
+                 << getOperation()->getDiscardableAttr("test.name") << "\n";
     getAnalysis<CallGraph>().print(llvm::errs());
   }
 };

diff  --git a/mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp b/mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
index f8588fab3aef7a2..b497f8d75fde75e 100644
--- a/mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
+++ b/mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
@@ -211,9 +211,10 @@ void VectorizerTestPass::testComposeMaps(llvm::raw_ostream &outs) {
   maps.reserve(matches.size());
   for (auto m : llvm::reverse(matches)) {
     auto *opInst = m.getMatchedOperation();
-    auto map = cast<AffineMapAttr>(
-                   opInst->getAttr(VectorizerTestPass::kTestAffineMapAttrName))
-                   .getValue();
+    auto map =
+        cast<AffineMapAttr>(opInst->getDiscardableAttr(
+                                VectorizerTestPass::kTestAffineMapAttrName))
+            .getValue();
     maps.push_back(map);
   }
   if (maps.empty())

diff  --git a/mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp b/mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp
index c326a746601b279..7e3d3f6dc3f0e6d 100644
--- a/mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp
+++ b/mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp
@@ -32,7 +32,7 @@ struct TestDataLayoutQuery
 
     func.walk([&](test::DataLayoutQueryOp op) {
       // Skip the ops with already processed in a deeper call.
-      if (op->getAttr("size"))
+      if (op->getDiscardableAttr("size"))
         return;
 
       const DataLayout &layout = layouts.getAbove(op);

diff  --git a/mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp b/mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp
index 091c7cadd902a01..2e5e591fe5f911b 100644
--- a/mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp
+++ b/mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp
@@ -212,7 +212,7 @@ void ConvertToTargetEnv::runOnOperation() {
   func::FuncOp fn = getOperation();
 
   auto targetEnv = dyn_cast_or_null<spirv::TargetEnvAttr>(
-      fn.getOperation()->getAttr(spirv::getTargetEnvAttrName()));
+      fn.getOperation()->getDiscardableAttr(spirv::getTargetEnvAttrName()));
   if (!targetEnv) {
     fn.emitError("missing 'spirv.target_env' attribute");
     return signalPassFailure();

diff  --git a/mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp b/mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp
index 129ba729755fc7c..47b06e4531152d1 100644
--- a/mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp
+++ b/mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp
@@ -53,7 +53,8 @@ void TestSpirvEntryPointABIPass::runOnOperation() {
   MLIRContext *context = &getContext();
   StringRef attrName = spirv::getEntryPointABIAttrName();
   for (gpu::GPUFuncOp gpuFunc : gpuModule.getOps<gpu::GPUFuncOp>()) {
-    if (!gpu::GPUDialect::isKernel(gpuFunc) || gpuFunc->getAttr(attrName))
+    if (!gpu::GPUDialect::isKernel(gpuFunc) ||
+        gpuFunc->getDiscardableAttr(attrName))
       continue;
     SmallVector<int32_t, 3> workgroupSizeVec(workgroupSize.begin(),
                                              workgroupSize.end());

diff  --git a/mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp b/mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp
index 0f0875874c49898..5239dc5388951b3 100644
--- a/mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp
+++ b/mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp
@@ -57,7 +57,7 @@ void ReportShapeFnPass::runOnOperation() {
 
   // Lookup shape function library.
   SmallVector<shape::FunctionLibraryOp, 4> libraries;
-  auto attr = module->getAttr("shape.lib");
+  auto attr = module->getDiscardableAttr("shape.lib");
   if (attr) {
     auto lookup = [&](Attribute attr) {
       return cast<shape::FunctionLibraryOp>(

diff  --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp
index 55cf4246562e6af..e5fc97dafae9bb7 100644
--- a/mlir/test/lib/Dialect/Test/TestDialect.cpp
+++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp
@@ -37,6 +37,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/Base64.h"
+#include "llvm/Support/Casting.h"
 
 #include <cstdint>
 #include <numeric>
@@ -528,9 +529,9 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold(
 }
 
 OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) {
-  if (adaptor.getOp() && !(*this)->getAttr("attr")) {
+  if (adaptor.getOp() && !getProperties().attr) {
     // The folder adds "attr" if not present.
-    (*this)->setAttr("attr", adaptor.getOp());
+    getProperties().attr = dyn_cast_or_null<IntegerAttr>(adaptor.getOp());
     return getResult();
   }
   return {};

diff  --git a/mlir/test/lib/Dialect/Test/TestPatterns.cpp b/mlir/test/lib/Dialect/Test/TestPatterns.cpp
index 47afe6d2eecb6d9..e23ed105e383390 100644
--- a/mlir/test/lib/Dialect/Test/TestPatterns.cpp
+++ b/mlir/test/lib/Dialect/Test/TestPatterns.cpp
@@ -551,12 +551,12 @@ struct TestRegionRewriteBlockMovement : public ConversionPattern {
     // Inline this region into the parent region.
     auto &parentRegion = *op->getParentRegion();
     auto &opRegion = op->getRegion(0);
-    if (op->getAttr("legalizer.should_clone"))
+    if (op->getDiscardableAttr("legalizer.should_clone"))
       rewriter.cloneRegionBefore(opRegion, parentRegion, parentRegion.end());
     else
       rewriter.inlineRegionBefore(opRegion, parentRegion, parentRegion.end());
 
-    if (op->getAttr("legalizer.erase_old_blocks")) {
+    if (op->getDiscardableAttr("legalizer.erase_old_blocks")) {
       while (!opRegion.empty())
         rewriter.eraseBlock(&opRegion.front());
     }

diff  --git a/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp b/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp
index ac7e186843e24e1..afd5011f17c6d2b 100644
--- a/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp
+++ b/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp
@@ -55,7 +55,9 @@ class TestTransformOp
     return DiagnosedSilenceableFailure::success();
   }
 
-  Attribute getMessage() { return getOperation()->getAttr("message"); }
+  Attribute getMessage() {
+    return getOperation()->getDiscardableAttr("message");
+  }
 
   static ParseResult parse(OpAsmParser &parser, OperationState &state) {
     StringAttr message;

diff  --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index 370d8bb19364f0d..2fbf1babf437f08 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -681,7 +681,8 @@ struct TestCreateVectorBroadcast
       auto targetShape =
           cast<VectorType>(op->getResult(0).getType()).getShape();
       auto arrayAttr =
-          cast<DenseI64ArrayAttr>(op->getAttr("broadcast_dims")).asArrayRef();
+          cast<DenseI64ArrayAttr>(op->getDiscardableAttr("broadcast_dims"))
+              .asArrayRef();
       llvm::SetVector<int64_t> broadcastedDims;
       broadcastedDims.insert(arrayAttr.begin(), arrayAttr.end());
       OpBuilder b(op);

diff  --git a/mlir/test/lib/IR/TestSymbolUses.cpp b/mlir/test/lib/IR/TestSymbolUses.cpp
index d311f7ac8a89982..0e1368f2e0ecaff 100644
--- a/mlir/test/lib/IR/TestSymbolUses.cpp
+++ b/mlir/test/lib/IR/TestSymbolUses.cpp
@@ -59,7 +59,7 @@ struct SymbolUsesPass
               symbolUse.getUser()->getParentOp(), symbolUse.getSymbolRef())) {
         symbolUse.getUser()->emitRemark()
             << "found use of symbol : " << symbolUse.getSymbolRef() << " : "
-            << symbol->getAttr(SymbolTable::getSymbolAttrName());
+            << *symbol->getInherentAttr(SymbolTable::getSymbolAttrName());
       }
     }
     symbol->emitRemark() << "symbol has " << llvm::size(*symbolUses) << " uses";

diff  --git a/mlir/test/lib/Transforms/TestTopologicalSort.cpp b/mlir/test/lib/Transforms/TestTopologicalSort.cpp
index a8cc7a5af60d8d4..3b110c712620040 100644
--- a/mlir/test/lib/Transforms/TestTopologicalSort.cpp
+++ b/mlir/test/lib/Transforms/TestTopologicalSort.cpp
@@ -42,7 +42,8 @@ struct TestTopologicalSortAnalysisPass
           // If the root has an "ordered" attribute, we fill the selectedOps
           // vector in a certain order.
           int64_t pos =
-              cast<IntegerAttr>(selected->getAttr("selected")).getInt();
+              cast<IntegerAttr>(selected->getDiscardableAttr("selected"))
+                  .getInt();
           if (pos >= static_cast<int64_t>(selectedOps.size()))
             selectedOps.append(pos + 1 - selectedOps.size(), nullptr);
           selectedOps[pos] = selected;

diff  --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
index 6bb79fb4b4cbe67..131662d0cda67d3 100644
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -878,7 +878,8 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
   } else if (attr.isOptional()) {
     // For a missing attribute that is optional according to definition, we
     // should just capture a mlir::Attribute() to signal the missing state.
-    // That is precisely what getAttr() returns on missing attributes.
+    // That is precisely what getDiscardableAttr() returns on missing
+    // attributes.
   } else {
     emitMatchCheck(opName, tgfmt("tblgen_attr", &fmtCtx),
                    formatv("\"expected op '{0}' to have attribute '{1}' "

diff  --git a/mlir/unittests/Bytecode/BytecodeTest.cpp b/mlir/unittests/Bytecode/BytecodeTest.cpp
index 290233d12137a07..fc86f132dd60b4d 100644
--- a/mlir/unittests/Bytecode/BytecodeTest.cpp
+++ b/mlir/unittests/Bytecode/BytecodeTest.cpp
@@ -63,7 +63,7 @@ TEST(Bytecode, MultiModuleWithResource) {
 
   // Try to see if we have a valid resource in the parsed module.
   auto checkResourceAttribute = [&](Operation *op) {
-    Attribute attr = roundTripModule->getAttr("bytecode.test");
+    Attribute attr = roundTripModule->getDiscardableAttr("bytecode.test");
     ASSERT_TRUE(attr);
     auto denseResourceAttr = dyn_cast<DenseI32ResourceElementsAttr>(attr);
     ASSERT_TRUE(denseResourceAttr);

diff  --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp
index e8ba6d07ac610c5..8a4f67b9262744c 100644
--- a/mlir/unittests/IR/OperationSupportTest.cpp
+++ b/mlir/unittests/IR/OperationSupportTest.cpp
@@ -285,7 +285,7 @@ TEST(OperandStorageTest, PopulateDefaultAttrs) {
   // Verify default attributes populated post op creation.
   Operation *op = b.create<test::OpAttrMatch1>(b.getUnknownLoc(), req1, nullptr,
                                                nullptr, req2);
-  auto opt = op->getAttr("default_valued_attr");
+  auto opt = op->getInherentAttr("default_valued_attr");
   EXPECT_NE(opt, nullptr) << *op;
 
   op->destroy();

diff  --git a/mlir/unittests/Pass/PassManagerTest.cpp b/mlir/unittests/Pass/PassManagerTest.cpp
index 70a679125c0ea1e..9a30f64eaabc293 100644
--- a/mlir/unittests/Pass/PassManagerTest.cpp
+++ b/mlir/unittests/Pass/PassManagerTest.cpp
@@ -76,12 +76,13 @@ TEST(PassManagerTest, OpSpecificAnalysis) {
 
   // Verify that each function got annotated with expected attributes.
   for (func::FuncOp func : module->getOps<func::FuncOp>()) {
-    ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isFunc")));
-    EXPECT_TRUE(cast<BoolAttr>(func->getAttr("isFunc")).getValue());
+    ASSERT_TRUE(isa<BoolAttr>(func->getDiscardableAttr("isFunc")));
+    EXPECT_TRUE(cast<BoolAttr>(func->getDiscardableAttr("isFunc")).getValue());
 
     bool isSecret = func.getName() == "secret";
-    ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isSecret")));
-    EXPECT_EQ(cast<BoolAttr>(func->getAttr("isSecret")).getValue(), isSecret);
+    ASSERT_TRUE(isa<BoolAttr>(func->getDiscardableAttr("isSecret")));
+    EXPECT_EQ(cast<BoolAttr>(func->getDiscardableAttr("isSecret")).getValue(),
+              isSecret);
   }
 }
 


        


More information about the Mlir-commits mailing list