Skip to content

Commit 39c4494

Browse files
committed
feat: improve CSE by fusing locations when replacing one op for the other.
1 parent 2f0e627 commit 39c4494

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

mlir/lib/Transforms/CSE.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ void CSEDriver::replaceUsesAndDelete(ScopedMapTy &knownValues, Operation *op,
171171
// current op.
172172
if (isa<UnknownLoc>(existing->getLoc()) && !isa<UnknownLoc>(op->getLoc()))
173173
existing->setLoc(op->getLoc());
174+
else {
175+
// Otherwise, fuse both locations.
176+
existing->setLoc(mlir::FusedLoc::get(existing->getContext(),
177+
{existing->getLoc(), op->getLoc()}));
178+
}
174179

175180
++numCSE;
176181
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline='builtin.module(func.func(cse))' -mlir-print-debuginfo | FileCheck %s
2+
3+
// CHECK-LABEL: @many
4+
func.func @many(f32, f32) -> (f32, f32) {
5+
^bb0(%a : f32, %b : f32):
6+
// All operations have locations. Must have locations of Add0, Add1, Add2, Add3.
7+
%c = arith.addf %a, %b : f32 loc(#loc0)
8+
%d = arith.addf %a, %b : f32 loc(#loc1)
9+
%e = arith.addf %a, %b : f32 loc(#loc2)
10+
%f = arith.addf %a, %b : f32 loc(#loc3)
11+
// CHECK-NEXT: %[[VAR_0:[0-9a-zA-Z_]+]] = arith.addf %{{.*}}, %{{.*}} : f32 loc([[LOC_ABCD:.*]])
12+
13+
// First operation has unknown location. Must have locations of Add0, Add1, Add2.
14+
%g = arith.addf %c, %d : f32 loc(#loc)
15+
%h = arith.addf %e, %f : f32 loc(#loc0)
16+
%i = arith.addf %c, %e : f32 loc(#fused_loc0)
17+
// CHECK-NEXT: %[[VAR_1:[0-9a-zA-Z_]+]] = arith.addf %[[VAR_0]], %[[VAR_0]] : f32 loc([[LOC_ABC:.*]])
18+
19+
// Last operation has unknown location. Must have locations of Add2, Add3.
20+
%j = arith.addf %g, %h : f32 loc(#fused_loc1)
21+
%k = arith.addf %h, %i : f32 loc(#loc)
22+
// CHECK-NEXT: %[[VAR_2:[0-9a-zA-Z_]+]] = arith.addf %[[VAR_1]], %[[VAR_1]] : f32 loc([[LOC_CD:.*]])
23+
24+
// Two operations with fused locations. Must have locations of Add1, Add2, Add3.
25+
%l = arith.addf %j, %k : f32 loc(#fused_loc0)
26+
%m = arith.addf %j, %k : f32 loc(#fused_loc1)
27+
// CHECK-NEXT: %[[VAR_3:[0-9a-zA-Z_]+]] = arith.addf %[[VAR_2]], %[[VAR_2]] : f32 loc([[LOC_BCD:.*]])
28+
29+
// CHECK-NEXT: return %[[VAR_3]], %[[VAR_3]] : f32, f32
30+
return %l, %m : f32, f32
31+
}
32+
#loc = loc(unknown)
33+
#loc0 = loc("Add0")
34+
#loc1 = loc("Add1")
35+
#loc2 = loc("Add2")
36+
#loc3 = loc("Add3")
37+
38+
#fused_loc0 = loc(fused[#loc1, #loc2])
39+
#fused_loc1 = loc(fused[#loc2, #loc3])
40+
41+
// CHECK-DAG: #[[LOC_A:.*]] = loc("Add0")
42+
// CHECK-DAG: #[[LOC_B:.*]] = loc("Add1")
43+
// CHECK-DAG: #[[LOC_C:.*]] = loc("Add2")
44+
// CHECK-DAG: #[[LOC_D:.*]] = loc("Add3")
45+
// CHECK-DAG: [[LOC_ABCD]] = loc(fused[#[[LOC_A]], #[[LOC_B]], #[[LOC_C]], #[[LOC_D]]])
46+
// CHECK-DAG: [[LOC_ABC]] = loc(fused[#[[LOC_A]], #[[LOC_B]], #[[LOC_C]]])
47+
// CHECK-DAG: [[LOC_BCD]] = loc(fused[#[[LOC_B]], #[[LOC_C]], #[[LOC_D]]])
48+
// CHECK-DAG: [[LOC_CD]] = loc(fused[#[[LOC_C]], #[[LOC_D]]])

0 commit comments

Comments
 (0)