Skip to content

Commit 78cd1f6

Browse files
committed
Filter out functions with non-scalar results from CallCSE pass
1 parent 9d0b312 commit 78cd1f6

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/Dialect/BaseModelica/Transforms/CallCSE.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ void collectCallOps(ModelOp modelOp, llvm::SmallVectorImpl<CallOp> &callOps) {
4242
continue;
4343
}
4444
visitedTemplateOps.insert(templateOp);
45-
templateOp->walk([&](CallOp callOp) { callOps.push_back(callOp); });
45+
templateOp->walk([&](CallOp callOp) {
46+
// Skip functions with tensor results
47+
if (llvm::any_of(callOp.getResultTypes(), [](mlir::Type type) {
48+
return type.isa<mlir::TensorType>();
49+
})) {
50+
return;
51+
}
52+
callOps.push_back(callOp);
53+
});
4654
}
4755
}
4856

test/Dialect/BaseModelica/Transforms/CallCSE/idempotent-configurations.mlir

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,60 @@ module @InductionVariables {
9292
bmodelica.equation_instance %t1 {indices = #modeling<multidim_range [0, 1]>} : !bmodelica.equation
9393
}
9494
}
95+
}
96+
97+
// -----
98+
99+
module @ArrayResult {
100+
bmodelica.function @FuncWithArrayResult {
101+
bmodelica.variable @x : !bmodelica.variable<f64, input>
102+
bmodelica.variable @y : !bmodelica.variable<1xf64, output>
103+
bmodelica.algorithm {
104+
%0 = bmodelica.constant 0 : index
105+
%1 = bmodelica.variable_get @x : f64
106+
bmodelica.variable_set @y[%0], %1 : index, f64
107+
}
108+
}
109+
110+
bmodelica.model @M {
111+
bmodelica.variable @x : !bmodelica.variable<1xf64>
112+
bmodelica.variable @y : !bmodelica.variable<1xf64>
113+
114+
// CHECK: bmodelica.equation_template
115+
// CHECK-NEXT: %[[R0:.*]] = bmodelica.variable_get @x
116+
// CHECK-NEXT: %[[LHS:.*]] = bmodelica.equation_side %[[R0]]
117+
// CHECK-NEXT: %[[R1:.*]] = bmodelica.constant 1
118+
// CHECK-NEXT: %[[R2:.*]] = bmodelica.call @FuncWithArrayResult(%[[R1]])
119+
// CHECK-NEXT: %[[RHS:.*]] = bmodelica.equation_side %[[R2]]
120+
// CHECK-NEXT: bmodelica.equation_sides %[[LHS]], %[[RHS]]
121+
%t0 = bmodelica.equation_template inductions = [] {
122+
%0 = bmodelica.variable_get @x : tensor<1xf64>
123+
%1 = bmodelica.equation_side %0 : tuple<tensor<1xf64>>
124+
%2 = bmodelica.constant 1.0 : f64
125+
%3 = bmodelica.call @FuncWithArrayResult(%2) : (f64) -> tensor<1xf64>
126+
%4 = bmodelica.equation_side %3 : tuple<tensor<1xf64>>
127+
bmodelica.equation_sides %1, %4 : tuple<tensor<1xf64>>, tuple<tensor<1xf64>>
128+
}
129+
130+
// CHECK: bmodelica.equation_template
131+
// CHECK-NEXT: %[[R0:.*]] = bmodelica.variable_get @y
132+
// CHECK-NEXT: %[[LHS:.*]] = bmodelica.equation_side %[[R0]]
133+
// CHECK-NEXT: %[[R1:.*]] = bmodelica.constant 1
134+
// CHECK-NEXT: %[[R2:.*]] = bmodelica.call @FuncWithArrayResult(%[[R1]])
135+
// CHECK-NEXT: %[[RHS:.*]] = bmodelica.equation_side %[[R2]]
136+
// CHECK-NEXT: bmodelica.equation_sides %[[LHS]], %[[RHS]]
137+
%t1 = bmodelica.equation_template inductions = [] {
138+
%0 = bmodelica.variable_get @y : tensor<1xf64>
139+
%1 = bmodelica.equation_side %0 : tuple<tensor<1xf64>>
140+
%2 = bmodelica.constant 1.0 : f64
141+
%3 = bmodelica.call @FuncWithArrayResult(%2) : (f64) -> tensor<1xf64>
142+
%4 = bmodelica.equation_side %3 : tuple<tensor<1xf64>>
143+
bmodelica.equation_sides %1, %4 : tuple<tensor<1xf64>>, tuple<tensor<1xf64>>
144+
}
145+
146+
bmodelica.dynamic {
147+
bmodelica.equation_instance %t0 : !bmodelica.equation
148+
bmodelica.equation_instance %t1 : !bmodelica.equation
149+
}
150+
}
95151
}

0 commit comments

Comments
 (0)