Skip to content

Commit 1f26809

Browse files
author
Simon Camphausen
authored
[mlir][EmitC] Add support for pointer and opaque types to subscript op (#86266)
For pointer types the indices are restricted to one integer-like operand. For opaque types no further restrictions are made.
1 parent d0dcf06 commit 1f26809

File tree

9 files changed

+175
-31
lines changed

9 files changed

+175
-31
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
namespace mlir {
3131
namespace emitc {
3232
void buildTerminatedBody(OpBuilder &builder, Location loc);
33+
3334
/// Determines whether \p type is a valid integer type in EmitC.
3435
bool isSupportedIntegerType(mlir::Type type);
36+
37+
/// Determines whether \p type is integer like, i.e. it's a supported integer,
38+
/// an index or opaque type.
39+
bool isIntegerIndexOrOpaqueType(Type type);
40+
3541
/// Determines whether \p type is a valid floating-point type in EmitC.
3642
bool isSupportedFloatType(mlir::Type type);
3743
} // namespace emitc

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,35 +1155,41 @@ def EmitC_IfOp : EmitC_Op<"if",
11551155
let hasCustomAssemblyFormat = 1;
11561156
}
11571157

1158-
def EmitC_SubscriptOp : EmitC_Op<"subscript",
1159-
[TypesMatchWith<"result type matches element type of 'array'",
1160-
"array", "result",
1161-
"::llvm::cast<ArrayType>($_self).getElementType()">]> {
1162-
let summary = "Array subscript operation";
1158+
def EmitC_SubscriptOp : EmitC_Op<"subscript", []> {
1159+
let summary = "Subscript operation";
11631160
let description = [{
11641161
With the `subscript` operation the subscript operator `[]` can be applied
1165-
to variables or arguments of array type.
1162+
to variables or arguments of array, pointer and opaque type.
11661163

11671164
Example:
11681165

11691166
```mlir
11701167
%i = index.constant 1
11711168
%j = index.constant 7
1172-
%0 = emitc.subscript %arg0[%i, %j] : <4x8xf32>, index, index
1169+
%0 = emitc.subscript %arg0[%i, %j] : !emitc.array<4x8xf32>, index, index
1170+
%1 = emitc.subscript %arg1[%i] : !emitc.ptr<i32>, index
11731171
```
11741172
}];
1175-
let arguments = (ins Arg<EmitC_ArrayType, "the reference to load from">:$array,
1176-
Variadic<IntegerIndexOrOpaqueType>:$indices);
1173+
let arguments = (ins Arg<AnyTypeOf<[
1174+
EmitC_ArrayType,
1175+
EmitC_OpaqueType,
1176+
EmitC_PointerType]>,
1177+
"the value to subscript">:$value,
1178+
Variadic<AnyType>:$indices);
11771179
let results = (outs AnyType:$result);
11781180

11791181
let builders = [
1180-
OpBuilder<(ins "Value":$array, "ValueRange":$indices), [{
1181-
build($_builder, $_state, cast<ArrayType>(array.getType()).getElementType(), array, indices);
1182+
OpBuilder<(ins "TypedValue<ArrayType>":$array, "ValueRange":$indices), [{
1183+
build($_builder, $_state, array.getType().getElementType(), array, indices);
1184+
}]>,
1185+
OpBuilder<(ins "TypedValue<PointerType>":$pointer, "Value":$index), [{
1186+
build($_builder, $_state, pointer.getType().getPointee(), pointer,
1187+
ValueRange{index});
11821188
}]>
11831189
];
11841190

11851191
let hasVerifier = 1;
1186-
let assemblyFormat = "$array `[` $indices `]` attr-dict `:` type($array) `,` type($indices)";
1192+
let assemblyFormat = "$value `[` $indices `]` attr-dict `:` functional-type(operands, results)";
11871193
}
11881194

11891195

mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ struct ConvertLoad final : public OpConversionPattern<memref::LoadOp> {
6262
return rewriter.notifyMatchFailure(op.getLoc(), "cannot convert type");
6363
}
6464

65+
auto arrayValue =
66+
dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
67+
if (!arrayValue) {
68+
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
69+
}
70+
6571
auto subscript = rewriter.create<emitc::SubscriptOp>(
66-
op.getLoc(), operands.getMemref(), operands.getIndices());
72+
op.getLoc(), arrayValue, operands.getIndices());
6773

6874
auto noInit = emitc::OpaqueAttr::get(getContext(), "");
6975
auto var =
@@ -81,9 +87,14 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
8187
LogicalResult
8288
matchAndRewrite(memref::StoreOp op, OpAdaptor operands,
8389
ConversionPatternRewriter &rewriter) const override {
90+
auto arrayValue =
91+
dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
92+
if (!arrayValue) {
93+
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
94+
}
8495

8596
auto subscript = rewriter.create<emitc::SubscriptOp>(
86-
op.getLoc(), operands.getMemref(), operands.getIndices());
97+
op.getLoc(), arrayValue, operands.getIndices());
8798
rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, subscript,
8899
operands.getValue());
89100
return success();

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ bool mlir::emitc::isSupportedIntegerType(Type type) {
7070
return false;
7171
}
7272

73+
bool mlir::emitc::isIntegerIndexOrOpaqueType(Type type) {
74+
return llvm::isa<IndexType, emitc::OpaqueType>(type) ||
75+
isSupportedIntegerType(type);
76+
}
77+
7378
bool mlir::emitc::isSupportedFloatType(Type type) {
7479
if (auto floatType = llvm::dyn_cast<FloatType>(type)) {
7580
switch (floatType.getWidth()) {
@@ -780,12 +785,61 @@ LogicalResult emitc::YieldOp::verify() {
780785
//===----------------------------------------------------------------------===//
781786

782787
LogicalResult emitc::SubscriptOp::verify() {
783-
if (getIndices().size() != (size_t)getArray().getType().getRank()) {
784-
return emitOpError() << "requires number of indices ("
785-
<< getIndices().size()
786-
<< ") to match the rank of the array type ("
787-
<< getArray().getType().getRank() << ")";
788+
// Checks for array operand.
789+
if (auto arrayType = llvm::dyn_cast<emitc::ArrayType>(getValue().getType())) {
790+
// Check number of indices.
791+
if (getIndices().size() != (size_t)arrayType.getRank()) {
792+
return emitOpError() << "on array operand requires number of indices ("
793+
<< getIndices().size()
794+
<< ") to match the rank of the array type ("
795+
<< arrayType.getRank() << ")";
796+
}
797+
// Check types of index operands.
798+
for (unsigned i = 0, e = getIndices().size(); i != e; ++i) {
799+
Type type = getIndices()[i].getType();
800+
if (!isIntegerIndexOrOpaqueType(type)) {
801+
return emitOpError() << "on array operand requires index operand " << i
802+
<< " to be integer-like, but got " << type;
803+
}
804+
}
805+
// Check element type.
806+
Type elementType = arrayType.getElementType();
807+
if (elementType != getType()) {
808+
return emitOpError() << "on array operand requires element type ("
809+
<< elementType << ") and result type (" << getType()
810+
<< ") to match";
811+
}
812+
return success();
788813
}
814+
815+
// Checks for pointer operand.
816+
if (auto pointerType =
817+
llvm::dyn_cast<emitc::PointerType>(getValue().getType())) {
818+
// Check number of indices.
819+
if (getIndices().size() != 1) {
820+
return emitOpError()
821+
<< "on pointer operand requires one index operand, but got "
822+
<< getIndices().size();
823+
}
824+
// Check types of index operand.
825+
Type type = getIndices()[0].getType();
826+
if (!isIntegerIndexOrOpaqueType(type)) {
827+
return emitOpError() << "on pointer operand requires index operand to be "
828+
"integer-like, but got "
829+
<< type;
830+
}
831+
// Check pointee type.
832+
Type pointeeType = pointerType.getPointee();
833+
if (pointeeType != getType()) {
834+
return emitOpError() << "on pointer operand requires pointee type ("
835+
<< pointeeType << ") and result type (" << getType()
836+
<< ") to match";
837+
}
838+
return success();
839+
}
840+
841+
// The operand has opaque type, so we can't assume anything about the number
842+
// or types of index operands.
789843
return success();
790844
}
791845

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop)
11041104
std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
11051105
std::string out;
11061106
llvm::raw_string_ostream ss(out);
1107-
ss << getOrCreateName(op.getArray());
1107+
ss << getOrCreateName(op.getValue());
11081108
for (auto index : op.getIndices()) {
11091109
ss << "[" << getOrCreateName(index) << "]";
11101110
}

mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ func.func @memref_store(%v : f32, %i: index, %j: index) {
66
// CHECK: %[[ALLOCA:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<4x8xf32>
77
%0 = memref.alloca() : memref<4x8xf32>
88

9-
// CHECK: %[[SUBSCRIPT:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : <4x8xf32>
9+
// CHECK: %[[SUBSCRIPT:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> f32
1010
// CHECK: emitc.assign %[[v]] : f32 to %[[SUBSCRIPT:.*]] : f32
1111
memref.store %v, %0[%i, %j] : memref<4x8xf32>
1212
return
@@ -19,7 +19,7 @@ func.func @memref_load(%i: index, %j: index) -> f32 {
1919
// CHECK: %[[ALLOCA:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<4x8xf32>
2020
%0 = memref.alloca() : memref<4x8xf32>
2121

22-
// CHECK: %[[LOAD:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : <4x8xf32>
22+
// CHECK: %[[LOAD:.*]] = emitc.subscript %[[ALLOCA]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> f32
2323
// CHECK: %[[VAR:.*]] = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> f32
2424
// CHECK: emitc.assign %[[LOAD]] : f32 to %[[VAR]] : f32
2525
%1 = memref.load %0[%i, %j] : memref<4x8xf32>

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,48 @@ func.func @logical_or_resulterror(%arg0: i32, %arg1: i32) {
390390

391391
// -----
392392

393-
func.func @test_subscript_indices_mismatch(%arg0: !emitc.array<4x8xf32>, %arg2: index) {
394-
// expected-error @+1 {{'emitc.subscript' op requires number of indices (1) to match the rank of the array type (2)}}
395-
%0 = emitc.subscript %arg0[%arg2] : <4x8xf32>, index
393+
func.func @test_subscript_array_indices_mismatch(%arg0: !emitc.array<4x8xf32>, %arg1: index) {
394+
// expected-error @+1 {{'emitc.subscript' op on array operand requires number of indices (1) to match the rank of the array type (2)}}
395+
%0 = emitc.subscript %arg0[%arg1] : (!emitc.array<4x8xf32>, index) -> f32
396+
return
397+
}
398+
399+
// -----
400+
401+
func.func @test_subscript_array_index_type_mismatch(%arg0: !emitc.array<4x8xf32>, %arg1: index, %arg2: f32) {
402+
// expected-error @+1 {{'emitc.subscript' op on array operand requires index operand 1 to be integer-like, but got 'f32'}}
403+
%0 = emitc.subscript %arg0[%arg1, %arg2] : (!emitc.array<4x8xf32>, index, f32) -> f32
404+
return
405+
}
406+
407+
// -----
408+
409+
func.func @test_subscript_array_type_mismatch(%arg0: !emitc.array<4x8xf32>, %arg1: index, %arg2: index) {
410+
// expected-error @+1 {{'emitc.subscript' op on array operand requires element type ('f32') and result type ('i32') to match}}
411+
%0 = emitc.subscript %arg0[%arg1, %arg2] : (!emitc.array<4x8xf32>, index, index) -> i32
412+
return
413+
}
414+
415+
// -----
416+
417+
func.func @test_subscript_ptr_indices_mismatch(%arg0: !emitc.ptr<f32>, %arg1: index) {
418+
// expected-error @+1 {{'emitc.subscript' op on pointer operand requires one index operand, but got 2}}
419+
%0 = emitc.subscript %arg0[%arg1, %arg1] : (!emitc.ptr<f32>, index, index) -> f32
420+
return
421+
}
422+
423+
// -----
424+
425+
func.func @test_subscript_ptr_index_type_mismatch(%arg0: !emitc.ptr<f32>, %arg1: f64) {
426+
// expected-error @+1 {{'emitc.subscript' op on pointer operand requires index operand to be integer-like, but got 'f64'}}
427+
%0 = emitc.subscript %arg0[%arg1] : (!emitc.ptr<f32>, f64) -> f32
428+
return
429+
}
430+
431+
// -----
432+
433+
func.func @test_subscript_ptr_type_mismatch(%arg0: !emitc.ptr<f32>, %arg1: index) {
434+
// expected-error @+1 {{'emitc.subscript' op on pointer operand requires pointee type ('f32') and result type ('f64') to match}}
435+
%0 = emitc.subscript %arg0[%arg1] : (!emitc.ptr<f32>, index) -> f64
396436
return
397437
}

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ func.func @test_for_not_index_induction(%arg0 : i16, %arg1 : i16, %arg2 : i16) {
214214
return
215215
}
216216

217+
func.func @test_subscript(%arg0 : !emitc.array<2x3xf32>, %arg1 : !emitc.ptr<i32>, %arg2 : !emitc.opaque<"std::map<char, int>">, %idx0 : index, %idx1 : i32, %idx2 : !emitc.opaque<"char">) {
218+
%0 = emitc.subscript %arg0[%idx0, %idx1] : (!emitc.array<2x3xf32>, index, i32) -> f32
219+
%1 = emitc.subscript %arg1[%idx0] : (!emitc.ptr<i32>, index) -> i32
220+
%2 = emitc.subscript %arg2[%idx2] : (!emitc.opaque<"std::map<char, int>">, !emitc.opaque<"char">) -> !emitc.opaque<"int">
221+
return
222+
}
223+
217224
emitc.verbatim "#ifdef __cplusplus"
218225
emitc.verbatim "extern \"C\" {"
219226
emitc.verbatim "#endif // __cplusplus"

mlir/test/Target/Cpp/subscript.mlir

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
22
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s
33

4-
func.func @load_store(%arg0: !emitc.array<4x8xf32>, %arg1: !emitc.array<3x5xf32>, %arg2: index, %arg3: index) {
5-
%0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index
6-
%1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>, index, index
4+
func.func @load_store_array(%arg0: !emitc.array<4x8xf32>, %arg1: !emitc.array<3x5xf32>, %arg2: index, %arg3: index) {
5+
%0 = emitc.subscript %arg0[%arg2, %arg3] : (!emitc.array<4x8xf32>, index, index) -> f32
6+
%1 = emitc.subscript %arg1[%arg2, %arg3] : (!emitc.array<3x5xf32>, index, index) -> f32
77
emitc.assign %0 : f32 to %1 : f32
88
return
99
}
10-
// CHECK: void load_store(float [[ARR1:[^ ]*]][4][8], float [[ARR2:[^ ]*]][3][5],
10+
// CHECK: void load_store_array(float [[ARR1:[^ ]*]][4][8], float [[ARR2:[^ ]*]][3][5],
1111
// CHECK-SAME: size_t [[I:[^ ]*]], size_t [[J:[^ ]*]])
1212
// CHECK-NEXT: [[ARR2]][[[I]]][[[J]]] = [[ARR1]][[[I]]][[[J]]];
1313

14+
func.func @load_store_pointer(%arg0: !emitc.ptr<f32>, %arg1: !emitc.ptr<f32>, %arg2: index, %arg3: index) {
15+
%0 = emitc.subscript %arg0[%arg2] : (!emitc.ptr<f32>, index) -> f32
16+
%1 = emitc.subscript %arg1[%arg3] : (!emitc.ptr<f32>, index) -> f32
17+
emitc.assign %0 : f32 to %1 : f32
18+
return
19+
}
20+
// CHECK: void load_store_pointer(float* [[PTR1:[^ ]*]], float* [[PTR2:[^ ]*]],
21+
// CHECK-SAME: size_t [[I:[^ ]*]], size_t [[J:[^ ]*]])
22+
// CHECK-NEXT: [[PTR2]][[[J]]] = [[PTR1]][[[I]]];
23+
24+
func.func @load_store_opaque(%arg0: !emitc.opaque<"std::map<char, int>">, %arg1: !emitc.opaque<"std::map<char, int>">, %arg2: !emitc.opaque<"char">, %arg3: !emitc.opaque<"char">) {
25+
%0 = emitc.subscript %arg0[%arg2] : (!emitc.opaque<"std::map<char, int>">, !emitc.opaque<"char">) -> !emitc.opaque<"int">
26+
%1 = emitc.subscript %arg1[%arg3] : (!emitc.opaque<"std::map<char, int>">, !emitc.opaque<"char">) -> !emitc.opaque<"int">
27+
emitc.assign %0 : !emitc.opaque<"int"> to %1 : !emitc.opaque<"int">
28+
return
29+
}
30+
// CHECK: void load_store_opaque(std::map<char, int> [[MAP1:[^ ]*]], std::map<char, int> [[MAP2:[^ ]*]],
31+
// CHECK-SAME: char [[I:[^ ]*]], char [[J:[^ ]*]])
32+
// CHECK-NEXT: [[MAP2]][[[J]]] = [[MAP1]][[[I]]];
33+
1434
emitc.func @func1(%arg0 : f32) {
1535
emitc.return
1636
}
1737

1838
emitc.func @call_arg(%arg0: !emitc.array<4x8xf32>, %i: i32, %j: i16,
1939
%k: i8) {
20-
%0 = emitc.subscript %arg0[%i, %j] : <4x8xf32>, i32, i16
21-
%1 = emitc.subscript %arg0[%j, %k] : <4x8xf32>, i16, i8
40+
%0 = emitc.subscript %arg0[%i, %j] : (!emitc.array<4x8xf32>, i32, i16) -> f32
41+
%1 = emitc.subscript %arg0[%j, %k] : (!emitc.array<4x8xf32>, i16, i8) -> f32
2242

2343
emitc.call @func1 (%0) : (f32) -> ()
2444
emitc.call_opaque "func2" (%1) : (f32) -> ()

0 commit comments

Comments
 (0)