Skip to content

Commit 44b6f17

Browse files
committed
[mlir][SymbolDCE] Check SSA uses for symbols with results
1 parent e70e9ec commit 44b6f17

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

mlir/lib/Transforms/SymbolDCE.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ LogicalResult SymbolDCE::computeLiveness(Operation *symbolTableOp,
106106
continue;
107107
}
108108
bool isDiscardable = (symbolTableIsHidden || symbol.isPrivate()) &&
109-
symbol.canDiscardOnUseEmpty();
109+
symbol.canDiscardOnUseEmpty() && op.use_empty();
110110
if (!isDiscardable && liveSymbols.insert(&op).second)
111111
worklist.push_back(&op);
112112
}

mlir/test/Transforms/test-symbol-dce.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,23 @@ module attributes { test.nested_nosymboltable_region_notcalled } {
133133
"test.finish"() : () -> ()
134134
}) : () -> ()
135135
}
136+
137+
// -----
138+
139+
// Check that symbols with SSA results are not DCE'd if their result is used.
140+
// CHECK-LABEL: module attributes {test.symbol_with_ssa_result}
141+
module attributes {test.symbol_with_ssa_result} {
142+
// CHECK: "test.symbol_with_result"() <{sym_name = "used_symbol", sym_visibility = "private"}> : () -> i32
143+
%0 = "test.symbol_with_result"() <{sym_name = "used_symbol", sym_visibility = "private"}> : () -> i32
144+
145+
// CHECK-NOT: test.symbol_with_result
146+
// CHECK-NOT: unused_symbol
147+
%1 = "test.symbol_with_result"() <{sym_name = "unused_symbol", sym_visibility = "private"}> : () -> i32
148+
149+
// CHECK-NOT: test.symbol
150+
// CHECK-NOT: another_unused_symbol
151+
"test.symbol"() <{sym_name = "another_unused_symbol", sym_visibility = "private"}> : () -> ()
152+
153+
// Use %0, keeping @used_symbol alive via SSA
154+
"test.use"(%0) : (i32) -> ()
155+
}

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ def SymbolOp : TEST_Op<"symbol", [NoMemoryEffect, Symbol]> {
120120
OptionalAttr<StrAttr>:$sym_visibility);
121121
}
122122

123+
def SymbolWithResultOp : TEST_Op<"symbol_with_result", [NoMemoryEffect, Symbol]> {
124+
let summary = "symbol operation that produces an SSA result";
125+
let arguments = (ins StrAttr:$sym_name,
126+
OptionalAttr<StrAttr>:$sym_visibility);
127+
let results = (outs AnyType:$result);
128+
}
129+
123130
def OverriddenSymbolVisibilityOp : TEST_Op<"overridden_symbol_visibility", [
124131
DeclareOpInterfaceMethods<Symbol, ["getVisibility", "setVisibility"]>,
125132
]> {

0 commit comments

Comments
 (0)