Skip to content

Commit e36f02b

Browse files
authored
[FXML-4320][mlir][emitc] Restrict integer and float types (#143)
Restrict which integers types and floating point types are valid in EmitC. This should cover the types which are supported in C++ and is aligned with what the emitter currently supports.
1 parent 31b7dc7 commit e36f02b

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
namespace mlir {
3131
namespace emitc {
3232
void buildTerminatedBody(OpBuilder &builder, Location loc);
33+
/// Determines whether \p type is a valid integer type in EmitC.
34+
bool isValidEmitCIntegerType(mlir::Type type);
35+
/// Determines whether \p type is a valid floating-point type in EmitC.
36+
bool isValidEmitCFloatType(mlir::Type type);
3337
} // namespace emitc
3438
} // namespace mlir
3539

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
5151
def CExpression : NativeOpTrait<"emitc::CExpression">;
5252

5353
// Types only used in binary arithmetic operations.
54-
def IntegerIndexOrOpaqueType : AnyTypeOf<[AnyInteger, Index, EmitC_OpaqueType]>;
55-
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[AnyFloat, IntegerIndexOrOpaqueType]>;
54+
def IntegerIndexOrOpaqueType : AnyTypeOf<[Valid_EmitC_Integer_Type, Index, EmitC_OpaqueType]>;
55+
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[Valid_EmitC_Float_Type, IntegerIndexOrOpaqueType]>;
5656

5757
def EmitC_AddOp : EmitC_BinaryOp<"add", [CExpression]> {
5858
let summary = "Addition operation";

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ include "mlir/IR/BuiltinTypeInterfaces.td"
2222
// EmitC type definitions
2323
//===----------------------------------------------------------------------===//
2424

25+
def Valid_EmitC_Integer_Type : Type<CPred<"emitc::isValidEmitCIntegerType($_self)">,
26+
"EmitC integer type">;
27+
28+
def Valid_EmitC_Float_Type : Type<CPred<"emitc::isValidEmitCFloatType($_self)">,
29+
"EmitC floating-point type">;
30+
2531
class EmitC_Type<string name, string typeMnemonic, list<Trait> traits = []>
2632
: TypeDef<EmitC_Dialect, name, traits> {
2733
let mnemonic = typeMnemonic;

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@ void mlir::emitc::buildTerminatedBody(OpBuilder &builder, Location loc) {
5454
builder.create<emitc::YieldOp>(loc);
5555
}
5656

57+
bool mlir::emitc::isValidEmitCIntegerType(Type type) {
58+
if (auto intType = llvm::dyn_cast<IntegerType>(type)) {
59+
switch (intType.getWidth()) {
60+
case 1:
61+
case 8:
62+
case 16:
63+
case 32:
64+
case 64:
65+
return true;
66+
default:
67+
return false;
68+
}
69+
}
70+
return false;
71+
}
72+
73+
bool mlir::emitc::isValidEmitCFloatType(Type type) {
74+
if (auto floatType = llvm::dyn_cast<FloatType>(type)) {
75+
switch (floatType.getWidth()) {
76+
case 32:
77+
case 64:
78+
return true;
79+
default:
80+
return false;
81+
}
82+
}
83+
return false;
84+
}
85+
5786
/// Check that the type of the initial value is compatible with the operations
5887
/// result type.
5988
static LogicalResult verifyInitializationAttribute(Operation *op,

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,31 +170,31 @@ func.func @add_float_pointer(%arg0: f32, %arg1: !emitc.ptr<f32>) {
170170
// -----
171171

172172
func.func @div_tensor(%arg0: tensor<i32>, %arg1: tensor<i32>) {
173-
// expected-error @+1 {{'emitc.div' op operand #0 must be floating-point or integer or index or EmitC opaque type, but got 'tensor<i32>'}}
173+
// expected-error @+1 {{'emitc.div' op operand #0 must be EmitC floating-point type or EmitC integer type or index or EmitC opaque type, but got 'tensor<i32>'}}
174174
%1 = "emitc.div" (%arg0, %arg1) : (tensor<i32>, tensor<i32>) -> tensor<i32>
175175
return
176176
}
177177

178178
// -----
179179

180180
func.func @mul_tensor(%arg0: tensor<i32>, %arg1: tensor<i32>) {
181-
// expected-error @+1 {{'emitc.mul' op operand #0 must be floating-point or integer or index or EmitC opaque type, but got 'tensor<i32>'}}
181+
// expected-error @+1 {{'emitc.mul' op operand #0 must be EmitC floating-point type or EmitC integer type or index or EmitC opaque type, but got 'tensor<i32>'}}
182182
%1 = "emitc.mul" (%arg0, %arg1) : (tensor<i32>, tensor<i32>) -> tensor<i32>
183183
return
184184
}
185185

186186
// -----
187187

188188
func.func @rem_tensor(%arg0: tensor<i32>, %arg1: tensor<i32>) {
189-
// expected-error @+1 {{'emitc.rem' op operand #0 must be integer or index or EmitC opaque type, but got 'tensor<i32>'}}
189+
// expected-error @+1 {{'emitc.rem' op operand #0 must be EmitC integer type or index or EmitC opaque type, but got 'tensor<i32>'}}
190190
%1 = "emitc.rem" (%arg0, %arg1) : (tensor<i32>, tensor<i32>) -> tensor<i32>
191191
return
192192
}
193193

194194
// -----
195195

196196
func.func @rem_float(%arg0: f32, %arg1: f32) {
197-
// expected-error @+1 {{'emitc.rem' op operand #0 must be integer or index or EmitC opaque type, but got 'f32'}}
197+
// expected-error @+1 {{'emitc.rem' op operand #0 must be EmitC integer type or index or EmitC opaque type, but got 'f32'}}
198198
%1 = "emitc.rem" (%arg0, %arg1) : (f32, f32) -> f32
199199
return
200200
}

0 commit comments

Comments
 (0)