Skip to content

Commit 830b9b0

Browse files
committed
Update some uses of getAttr() to be explicit about Inherent vs Discardable (NFC)
1 parent 0a8d17e commit 830b9b0

20 files changed

+44
-35
lines changed

mlir/docs/DeclarativeRewrites.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,10 @@ For example, we can write
736736
def GetOwner: NativeCodeCall<"$0.getOwner()">;
737737
738738
def CopyAttrFoo: NativeCodeCallVoid<
739-
"$1->setAttr($_builder.getStringAttr(\"foo\"), $0->getAttr(\"foo\"))">;
739+
"$1->setAttr($_builder.getStringAttr(\"foo\"), $0->getInherentAttr(\"foo\"))">;
740740
741741
def CopyAttrBar: NativeCodeCallVoid<
742-
"$1->setAttr($_builder.getStringAttr(\"bar\"), $0->getAttr(\"bar\"))">;
742+
"$1->setAttr($_builder.getStringAttr(\"bar\"), $0->getInherentAttr(\"bar\"))">;
743743
744744
745745
def : Pattern<

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
4242
llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
4343
llvm::DEBUG_METADATA_VERSION);
4444

45-
if (auto targetTripleAttr =
46-
module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
45+
if (auto targetTripleAttr = module->getDiscardableAttr(
46+
LLVM::LLVMDialect::getTargetTripleAttrName())) {
4747
auto targetTriple =
4848
llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
4949
if (targetTriple.isKnownWindowsMSVCEnvironment()) {

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
13271327
m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
13281328
auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
13291329
if (auto dataLayoutAttr =
1330-
m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
1330+
m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
13311331
llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
13321332
} else {
13331333
FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout(""));
@@ -1347,7 +1347,7 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
13471347
llvmModule->setDataLayout(*llvmDataLayout);
13481348
}
13491349
if (auto targetTripleAttr =
1350-
m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
1350+
m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
13511351
llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
13521352

13531353
// Inject declarations for `malloc` and `free` functions that can be used in

mlir/test/lib/Analysis/TestAliasAnalysis.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ void printModRefResult(ModRefResult result, Operation *op, Value location) {
6161

6262
void TestAliasAnalysisBase::runAliasAnalysisOnOperation(
6363
Operation *op, AliasAnalysis &aliasAnalysis) {
64-
llvm::errs() << "Testing : " << op->getAttr("sym_name") << "\n";
64+
llvm::errs() << "Testing : " << *op->getInherentAttr("sym_name") << "\n";
6565

6666
// Collect all of the values to check for aliasing behavior.
6767
SmallVector<Value, 32> valsToCheck;
6868
op->walk([&](Operation *op) {
69-
if (!op->getAttr("test.ptr"))
69+
if (!op->getDiscardableAttr("test.ptr"))
7070
return;
7171
valsToCheck.append(op->result_begin(), op->result_end());
7272
for (Region &region : op->getRegions())
@@ -82,12 +82,12 @@ void TestAliasAnalysisBase::runAliasAnalysisOnOperation(
8282

8383
void TestAliasAnalysisModRefBase::runAliasAnalysisOnOperation(
8484
Operation *op, AliasAnalysis &aliasAnalysis) {
85-
llvm::errs() << "Testing : " << op->getAttr("sym_name") << "\n";
85+
llvm::errs() << "Testing : " << *op->getInherentAttr("sym_name") << "\n";
8686

8787
// Collect all of the values to check for aliasing behavior.
8888
SmallVector<Value, 32> valsToCheck;
8989
op->walk([&](Operation *op) {
90-
if (!op->getAttr("test.ptr"))
90+
if (!op->getDiscardableAttr("test.ptr"))
9191
return;
9292
valsToCheck.append(op->result_begin(), op->result_end());
9393
for (Region &region : op->getRegions())
@@ -98,7 +98,7 @@ void TestAliasAnalysisModRefBase::runAliasAnalysisOnOperation(
9898
// Check for aliasing behavior between each of the values.
9999
for (auto &it : valsToCheck) {
100100
op->walk([&](Operation *op) {
101-
if (!op->getAttr("test.ptr"))
101+
if (!op->getDiscardableAttr("test.ptr"))
102102
return;
103103
printModRefResult(aliasAnalysis.getModRef(op, it), op, it);
104104
});

mlir/test/lib/Analysis/TestCallGraph.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ struct TestCallGraphPass
2727
return "Print the contents of a constructed callgraph.";
2828
}
2929
void runOnOperation() override {
30-
llvm::errs() << "Testing : " << getOperation()->getAttr("test.name")
31-
<< "\n";
30+
llvm::errs() << "Testing : "
31+
<< getOperation()->getDiscardableAttr("test.name") << "\n";
3232
getAnalysis<CallGraph>().print(llvm::errs());
3333
}
3434
};

mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ void VectorizerTestPass::testComposeMaps(llvm::raw_ostream &outs) {
211211
maps.reserve(matches.size());
212212
for (auto m : llvm::reverse(matches)) {
213213
auto *opInst = m.getMatchedOperation();
214-
auto map = cast<AffineMapAttr>(
215-
opInst->getAttr(VectorizerTestPass::kTestAffineMapAttrName))
216-
.getValue();
214+
auto map =
215+
cast<AffineMapAttr>(opInst->getDiscardableAttr(
216+
VectorizerTestPass::kTestAffineMapAttrName))
217+
.getValue();
217218
maps.push_back(map);
218219
}
219220
if (maps.empty())

mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct TestDataLayoutQuery
3232

3333
func.walk([&](test::DataLayoutQueryOp op) {
3434
// Skip the ops with already processed in a deeper call.
35-
if (op->getAttr("size"))
35+
if (op->getDiscardableAttr("size"))
3636
return;
3737

3838
const DataLayout &layout = layouts.getAbove(op);

mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void ConvertToTargetEnv::runOnOperation() {
212212
func::FuncOp fn = getOperation();
213213

214214
auto targetEnv = dyn_cast_or_null<spirv::TargetEnvAttr>(
215-
fn.getOperation()->getAttr(spirv::getTargetEnvAttrName()));
215+
fn.getOperation()->getDiscardableAttr(spirv::getTargetEnvAttrName()));
216216
if (!targetEnv) {
217217
fn.emitError("missing 'spirv.target_env' attribute");
218218
return signalPassFailure();

mlir/test/lib/Dialect/SPIRV/TestEntryPointAbi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void TestSpirvEntryPointABIPass::runOnOperation() {
5353
MLIRContext *context = &getContext();
5454
StringRef attrName = spirv::getEntryPointABIAttrName();
5555
for (gpu::GPUFuncOp gpuFunc : gpuModule.getOps<gpu::GPUFuncOp>()) {
56-
if (!gpu::GPUDialect::isKernel(gpuFunc) || gpuFunc->getAttr(attrName))
56+
if (!gpu::GPUDialect::isKernel(gpuFunc) ||
57+
gpuFunc->getDiscardableAttr(attrName))
5758
continue;
5859
SmallVector<int32_t, 3> workgroupSizeVec(workgroupSize.begin(),
5960
workgroupSize.end());

mlir/test/lib/Dialect/Shape/TestShapeFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void ReportShapeFnPass::runOnOperation() {
5757

5858
// Lookup shape function library.
5959
SmallVector<shape::FunctionLibraryOp, 4> libraries;
60-
auto attr = module->getAttr("shape.lib");
60+
auto attr = module->getDiscardableAttr("shape.lib");
6161
if (attr) {
6262
auto lookup = [&](Attribute attr) {
6363
return cast<shape::FunctionLibraryOp>(

mlir/test/lib/Dialect/Test/TestDialect.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/ADT/StringExtras.h"
3838
#include "llvm/ADT/StringSwitch.h"
3939
#include "llvm/Support/Base64.h"
40+
#include "llvm/Support/Casting.h"
4041

4142
#include <cstdint>
4243
#include <numeric>
@@ -528,9 +529,9 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold(
528529
}
529530

530531
OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) {
531-
if (adaptor.getOp() && !(*this)->getAttr("attr")) {
532+
if (adaptor.getOp() && !getProperties().attr) {
532533
// The folder adds "attr" if not present.
533-
(*this)->setAttr("attr", adaptor.getOp());
534+
getProperties().attr = dyn_cast_or_null<IntegerAttr>(adaptor.getOp());
534535
return getResult();
535536
}
536537
return {};

mlir/test/lib/Dialect/Test/TestPatterns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,12 @@ struct TestRegionRewriteBlockMovement : public ConversionPattern {
551551
// Inline this region into the parent region.
552552
auto &parentRegion = *op->getParentRegion();
553553
auto &opRegion = op->getRegion(0);
554-
if (op->getAttr("legalizer.should_clone"))
554+
if (op->getDiscardableAttr("legalizer.should_clone"))
555555
rewriter.cloneRegionBefore(opRegion, parentRegion, parentRegion.end());
556556
else
557557
rewriter.inlineRegionBefore(opRegion, parentRegion, parentRegion.end());
558558

559-
if (op->getAttr("legalizer.erase_old_blocks")) {
559+
if (op->getDiscardableAttr("legalizer.erase_old_blocks")) {
560560
while (!opRegion.empty())
561561
rewriter.eraseBlock(&opRegion.front());
562562
}

mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class TestTransformOp
5555
return DiagnosedSilenceableFailure::success();
5656
}
5757

58-
Attribute getMessage() { return getOperation()->getAttr("message"); }
58+
Attribute getMessage() {
59+
return getOperation()->getDiscardableAttr("message");
60+
}
5961

6062
static ParseResult parse(OpAsmParser &parser, OperationState &state) {
6163
StringAttr message;

mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,8 @@ struct TestCreateVectorBroadcast
681681
auto targetShape =
682682
cast<VectorType>(op->getResult(0).getType()).getShape();
683683
auto arrayAttr =
684-
cast<DenseI64ArrayAttr>(op->getAttr("broadcast_dims")).asArrayRef();
684+
cast<DenseI64ArrayAttr>(op->getDiscardableAttr("broadcast_dims"))
685+
.asArrayRef();
685686
llvm::SetVector<int64_t> broadcastedDims;
686687
broadcastedDims.insert(arrayAttr.begin(), arrayAttr.end());
687688
OpBuilder b(op);

mlir/test/lib/IR/TestSymbolUses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct SymbolUsesPass
5959
symbolUse.getUser()->getParentOp(), symbolUse.getSymbolRef())) {
6060
symbolUse.getUser()->emitRemark()
6161
<< "found use of symbol : " << symbolUse.getSymbolRef() << " : "
62-
<< symbol->getAttr(SymbolTable::getSymbolAttrName());
62+
<< *symbol->getInherentAttr(SymbolTable::getSymbolAttrName());
6363
}
6464
}
6565
symbol->emitRemark() << "symbol has " << llvm::size(*symbolUses) << " uses";

mlir/test/lib/Transforms/TestTopologicalSort.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct TestTopologicalSortAnalysisPass
4242
// If the root has an "ordered" attribute, we fill the selectedOps
4343
// vector in a certain order.
4444
int64_t pos =
45-
cast<IntegerAttr>(selected->getAttr("selected")).getInt();
45+
cast<IntegerAttr>(selected->getDiscardableAttr("selected"))
46+
.getInt();
4647
if (pos >= static_cast<int64_t>(selectedOps.size()))
4748
selectedOps.append(pos + 1 - selectedOps.size(), nullptr);
4849
selectedOps[pos] = selected;

mlir/tools/mlir-tblgen/RewriterGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,8 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
878878
} else if (attr.isOptional()) {
879879
// For a missing attribute that is optional according to definition, we
880880
// should just capture a mlir::Attribute() to signal the missing state.
881-
// That is precisely what getAttr() returns on missing attributes.
881+
// That is precisely what getDiscardableAttr() returns on missing
882+
// attributes.
882883
} else {
883884
emitMatchCheck(opName, tgfmt("tblgen_attr", &fmtCtx),
884885
formatv("\"expected op '{0}' to have attribute '{1}' "

mlir/unittests/Bytecode/BytecodeTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST(Bytecode, MultiModuleWithResource) {
6363

6464
// Try to see if we have a valid resource in the parsed module.
6565
auto checkResourceAttribute = [&](Operation *op) {
66-
Attribute attr = roundTripModule->getAttr("bytecode.test");
66+
Attribute attr = roundTripModule->getDiscardableAttr("bytecode.test");
6767
ASSERT_TRUE(attr);
6868
auto denseResourceAttr = dyn_cast<DenseI32ResourceElementsAttr>(attr);
6969
ASSERT_TRUE(denseResourceAttr);

mlir/unittests/IR/OperationSupportTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ TEST(OperandStorageTest, PopulateDefaultAttrs) {
285285
// Verify default attributes populated post op creation.
286286
Operation *op = b.create<test::OpAttrMatch1>(b.getUnknownLoc(), req1, nullptr,
287287
nullptr, req2);
288-
auto opt = op->getAttr("default_valued_attr");
288+
auto opt = op->getInherentAttr("default_valued_attr");
289289
EXPECT_NE(opt, nullptr) << *op;
290290

291291
op->destroy();

mlir/unittests/Pass/PassManagerTest.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ TEST(PassManagerTest, OpSpecificAnalysis) {
7676

7777
// Verify that each function got annotated with expected attributes.
7878
for (func::FuncOp func : module->getOps<func::FuncOp>()) {
79-
ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isFunc")));
80-
EXPECT_TRUE(cast<BoolAttr>(func->getAttr("isFunc")).getValue());
79+
ASSERT_TRUE(isa<BoolAttr>(func->getDiscardableAttr("isFunc")));
80+
EXPECT_TRUE(cast<BoolAttr>(func->getDiscardableAttr("isFunc")).getValue());
8181

8282
bool isSecret = func.getName() == "secret";
83-
ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isSecret")));
84-
EXPECT_EQ(cast<BoolAttr>(func->getAttr("isSecret")).getValue(), isSecret);
83+
ASSERT_TRUE(isa<BoolAttr>(func->getDiscardableAttr("isSecret")));
84+
EXPECT_EQ(cast<BoolAttr>(func->getDiscardableAttr("isSecret")).getValue(),
85+
isSecret);
8586
}
8687
}
8788

0 commit comments

Comments
 (0)