Skip to content

Commit a50b096

Browse files
authored
[mlir][emitc] Only mark operator with fundamental type have no side effect (#144990)
1 parent 64b9896 commit a50b096

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ bool isPointerWideType(mlir::Type type);
5353
struct Placeholder {};
5454
using ReplacementItem = std::variant<StringRef, Placeholder>;
5555

56+
/// Determines whether \p type is a valid fundamental C++ type in EmitC.
57+
bool isFundamentalType(mlir::Type type);
58+
5659
} // namespace emitc
5760
} // namespace mlir
5861

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class EmitC_UnaryOp<string mnemonic, list<Trait> traits = []> :
4343

4444
let extraClassDeclaration = [{
4545
bool hasSideEffects() {
46-
return false;
46+
// If operand is fundamental type, the operation is pure.
47+
return !isFundamentalType(getOperand().getType());
4748
}
4849
}];
4950
}
@@ -57,7 +58,9 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
5758

5859
let extraClassDeclaration = [{
5960
bool hasSideEffects() {
60-
return false;
61+
// If both operands are fundamental types, the operation is pure.
62+
return !isFundamentalType(getOperand(0).getType()) ||
63+
!isFundamentalType(getOperand(1).getType());
6164
}
6265
}];
6366
}

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ bool mlir::emitc::isPointerWideType(Type type) {
131131
type);
132132
}
133133

134+
bool mlir::emitc::isFundamentalType(Type type) {
135+
return llvm::isa<IndexType>(type) || isPointerWideType(type) ||
136+
isSupportedIntegerType(type) || isSupportedFloatType(type) ||
137+
isa<emitc::PointerType>(type);
138+
}
139+
134140
/// Check that the type of the initial value is compatible with the operations
135141
/// result type.
136142
static LogicalResult verifyInitializationAttribute(Operation *op,

mlir/test/Dialect/EmitC/form-expressions.mlir

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,26 @@ func.func @expression_with_load(%arg0: i32, %arg1: !emitc.ptr<i32>) -> i1 {
160160
%c = emitc.cmp lt, %b, %arg0 :(i32, i32) -> i1
161161
return %c : i1
162162
}
163+
164+
// CHECK-LABEL: func.func @opaque_type_expression(%arg0: i32, %arg1: !emitc.opaque<"T0">, %arg2: i32) -> i1 {
165+
// CHECK: %0 = "emitc.constant"() <{value = 42 : i32}> : () -> i32
166+
// CHECK: %1 = emitc.expression : i32 {
167+
// CHECK: %3 = mul %arg0, %0 : (i32, i32) -> i32
168+
// CHECK: %4 = sub %3, %arg1 : (i32, !emitc.opaque<"T0">) -> i32
169+
// CHECK: yield %4 : i32
170+
// CHECK: }
171+
// CHECK: %2 = emitc.expression : i1 {
172+
// CHECK: %3 = cmp lt, %1, %arg2 : (i32, i32) -> i1
173+
// CHECK: yield %3 : i1
174+
// CHECK: }
175+
// CHECK: return %2 : i1
176+
// CHECK: }
177+
178+
179+
func.func @opaque_type_expression(%arg0: i32, %arg1: !emitc.opaque<"T0">, %arg2: i32) -> i1 {
180+
%c42 = "emitc.constant"(){value = 42 : i32} : () -> i32
181+
%a = emitc.mul %arg0, %c42 : (i32, i32) -> i32
182+
%b = emitc.sub %a, %arg1 : (i32, !emitc.opaque<"T0">) -> i32
183+
%c = emitc.cmp lt, %b, %arg2 :(i32, i32) -> i1
184+
return %c : i1
185+
}

0 commit comments

Comments
 (0)