-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir] Make overloads of SymbolTable::replaceAllSymbolUses consistent. #68320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ingomueller-net
merged 4 commits into
llvm:main
from
ingomueller-net:fix-replace-all-symbol-uses
Oct 10, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
abe59c4
[mlir] Make overloads of SymbolTable::replaceAllSymbolUses consistent.
ingomueller-net 24a6403
Add unittest that tests all overloads.
ingomueller-net bcd7d65
Address comments from @ftynse's review.
ingomueller-net 657fe51
Turn test lambda function into proper fixture.
ingomueller-net File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//===- SymbolTableTest.cpp - SymbolTable unit tests -----------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "mlir/IR/SymbolTable.h" | ||
#include "../../test/lib/Dialect/Test/TestDialect.h" | ||
#include "mlir/IR/Verifier.h" | ||
#include "mlir/Interfaces/CallInterfaces.h" | ||
#include "mlir/Interfaces/FunctionInterfaces.h" | ||
#include "mlir/Parser/Parser.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
TEST(SymbolTableTest, ReplaceAllSymbolUses) { | ||
MLIRContext context; | ||
context.getOrLoadDialect<test::TestDialect>(); | ||
|
||
auto testReplaceAllSymbolUses = [&](auto replaceFn) { | ||
ingomueller-net marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const static llvm::StringLiteral input = R"MLIR( | ||
module { | ||
test.conversion_func_op private @foo() { | ||
"test.conversion_call_op"() { callee=@bar } : () -> () | ||
"test.return"() : () -> () | ||
} | ||
test.conversion_func_op private @bar() | ||
} | ||
)MLIR"; | ||
|
||
// Set up IR and find func ops. | ||
OwningOpRef<Operation *> module = parseSourceString(input, &context); | ||
ingomueller-net marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SymbolTable symbolTable(module.get()); | ||
auto ops = module->getRegion(0).getBlocks().front().getOperations().begin(); | ||
ingomueller-net marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto fooOp = cast<FunctionOpInterface>(ops++); | ||
auto barOp = cast<FunctionOpInterface>(ops++); | ||
ASSERT_EQ(fooOp.getNameAttr(), "foo"); | ||
ASSERT_EQ(barOp.getNameAttr(), "bar"); | ||
|
||
// Call test function that does symbol replacement. | ||
LogicalResult res = replaceFn(symbolTable, module.get(), fooOp, barOp); | ||
ASSERT_TRUE(succeeded(res)); | ||
ASSERT_TRUE(succeeded(verify(module.get()))); | ||
|
||
// Check that it got renamed. | ||
bool calleeFound = false; | ||
fooOp->walk([&](CallOpInterface callOp) { | ||
StringAttr callee = callOp.getCallableForCallee() | ||
.dyn_cast<SymbolRefAttr>() | ||
.getLeafReference(); | ||
EXPECT_EQ(callee, "baz"); | ||
calleeFound = true; | ||
}); | ||
EXPECT_TRUE(calleeFound); | ||
}; | ||
|
||
// Symbol as `Operation *`, rename within module. | ||
testReplaceAllSymbolUses( | ||
ftynse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[&](auto symbolTable, auto module, auto fooOp, auto barOp) { | ||
return symbolTable.replaceAllSymbolUses( | ||
barOp, StringAttr::get(&context, "baz"), module); | ||
}); | ||
|
||
// Symbol as `StringAttr`, rename within module. | ||
testReplaceAllSymbolUses([&](auto symbolTable, auto module, auto fooOp, | ||
auto barOp) { | ||
return symbolTable.replaceAllSymbolUses(StringAttr::get(&context, "bar"), | ||
StringAttr::get(&context, "baz"), | ||
module); | ||
}); | ||
|
||
// Symbol as `Operation *`, rename within module body. | ||
testReplaceAllSymbolUses( | ||
[&](auto symbolTable, auto module, auto fooOp, auto barOp) { | ||
return symbolTable.replaceAllSymbolUses( | ||
barOp, StringAttr::get(&context, "baz"), &module->getRegion(0)); | ||
}); | ||
|
||
// Symbol as `StringAttr`, rename within module body. | ||
testReplaceAllSymbolUses([&](auto symbolTable, auto module, auto fooOp, | ||
auto barOp) { | ||
return symbolTable.replaceAllSymbolUses(StringAttr::get(&context, "bar"), | ||
StringAttr::get(&context, "baz"), | ||
&module->getRegion(0)); | ||
}); | ||
|
||
// Symbol as `Operation *`, rename within function. | ||
testReplaceAllSymbolUses( | ||
[&](auto symbolTable, auto module, auto fooOp, auto barOp) { | ||
return symbolTable.replaceAllSymbolUses( | ||
barOp, StringAttr::get(&context, "baz"), fooOp); | ||
}); | ||
|
||
// Symbol as `StringAttr`, rename within function. | ||
testReplaceAllSymbolUses([&](auto symbolTable, auto module, auto fooOp, | ||
auto barOp) { | ||
return symbolTable.replaceAllSymbolUses(StringAttr::get(&context, "bar"), | ||
StringAttr::get(&context, "baz"), | ||
fooOp); | ||
}); | ||
} | ||
|
||
} // namespace |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.