Skip to content

Commit

Permalink
[IRSim] Remove early check from similarity matching such that commuta…
Browse files Browse the repository at this point in the history
…tive instructions are checked correctly when using the same value.

When the first commutative instruction in a region using the same value in both positions was compared to a corresponding instruction with two different values, there was an early check that determined that since the values were new, it was true that these values acted in the same way structurally. If this was not contradicted later in the program, the regions were marked as similar. This removes that check, so that it is clear that the same value cannot be mapped to two different values.

Reviewer: paquette

Differential Revision: https://reviews.llvm.org/D124775
  • Loading branch information
AndrewLitteken committed May 10, 2022
1 parent 784a5bc commit 96345f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
15 changes: 4 additions & 11 deletions llvm/lib/Analysis/IRSimilarityIdentifier.cpp
Expand Up @@ -526,19 +526,13 @@ static bool checkNumberingAndReplaceCommutative(
for (Value *V : SourceOperands) {
ArgVal = SourceValueToNumberMapping.find(V)->second;

// Instead of finding a current mapping, we attempt to insert a set.
std::tie(ValueMappingIt, WasInserted) = CurrentSrcTgtNumberMapping.insert(
std::make_pair(ArgVal, TargetValueNumbers));

// Instead of finding a current mapping, we inserted a set. This means a
// mapping did not exist for the source Instruction operand, it has no
// current constraints we need to check.
if (WasInserted)
continue;

// If a mapping already exists for the source operand to the values in the
// other IRSimilarityCandidate we need to iterate over the items in other
// IRSimilarityCandidate's Instruction to determine whether there is a valid
// mapping of Value to Value.
// We need to iterate over the items in other IRSimilarityCandidate's
// Instruction to determine whether there is a valid mapping of
// Value to Value.
DenseSet<unsigned> NewSet;
for (unsigned &Curr : ValueMappingIt->second)
// If we can find the value in the mapping, we add it to the new set.
Expand All @@ -558,7 +552,6 @@ static bool checkNumberingAndReplaceCommutative(
if (ValueMappingIt->second.size() != 1)
continue;


unsigned ValToRemove = *ValueMappingIt->second.begin();
// When there is only one item left in the mapping for and operand, remove
// the value from the other operands. If it results in there being no
Expand Down
23 changes: 23 additions & 0 deletions llvm/unittests/Analysis/IRSimilarityIdentifierTest.cpp
Expand Up @@ -2619,6 +2619,29 @@ TEST(IRSimilarityIdentifier, CommutativeSimilarity) {
}
}

// This test ensures that when the first instruction in a sequence is
// a commutative instruction with the same value (mcomm_inst_same_val), but the
// corresponding instruction (comm_inst_diff_val) is not, we mark the regions
// and not similar.
TEST(IRSimilarityIdentifier, CommutativeSameValueFirstMisMatch) {
StringRef ModuleString = R"(
define void @v_1_0(i64 %v_33) {
entry:
%comm_inst_same_val = mul i64 undef, undef
%add = add i64 %comm_inst_same_val, %v_33
%comm_inst_diff_val = mul i64 0, undef
%mul.i = add i64 %comm_inst_diff_val, %comm_inst_diff_val
unreachable
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
getSimilarities(*M, SimilarityCandidates);

ASSERT_TRUE(SimilarityCandidates.size() == 0);
}

// This test makes sure that intrinsic functions that are marked commutative
// are still treated as non-commutative since they are function calls.
TEST(IRSimilarityIdentifier, IntrinsicCommutative) {
Expand Down

0 comments on commit 96345f7

Please sign in to comment.