Skip to content

Commit be54af5

Browse files
committed
[MLIR] EmitC: Add subscript operator (llvm#84783)
Introduces a SubscriptOp that allows to write IR like ``` func.func @load_store(%arg0: !emitc.array<4x8xf32>, %arg1: !emitc.array<3x5xf32>, %arg2: index, %arg3: index) { %0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index %1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>, index, index emitc.assign %0 : f32 to %1 : f32 return } ``` which gets translated into the C++ code ``` v1[v2][v3] = v0[v1][v2]; ``` To make this happen, this - adds the SubscriptOp - allows the subscript op as rhs of emitc.assign - updates the emitter to print SubscriptOps The emitter prints emitc.subscript in a delayed fashing to allow it being used as lvalue. I.e. while processing ``` %0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index ``` it will not emit any text, but record in the `valueMapper` that the name for `%0` is `v0[v1][v2]`, see `CppEmitter::getSubscriptName`. Only when that result is then used (here in `emitc.assign`), that name is inserted into the text.
1 parent 4e66654 commit be54af5

File tree

6 files changed

+48
-69
lines changed

6 files changed

+48
-69
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,11 +1169,11 @@ def EmitC_SubscriptOp : EmitC_Op<"subscript",
11691169
```mlir
11701170
%i = index.constant 1
11711171
%j = index.constant 7
1172-
%0 = emitc.subscript %arg0[%i][%j] : (!emitc.array<4x8xf32>) -> f32
1172+
%0 = emitc.subscript %arg0[%i, %j] : <4x8xf32>, index, index
11731173
```
11741174
}];
11751175
let arguments = (ins Arg<EmitC_ArrayType, "the reference to load from">:$array,
1176-
Variadic<Index>:$indices);
1176+
Variadic<IntegerIndexOrOpaqueType>:$indices);
11771177
let results = (outs AnyType:$result);
11781178

11791179
let builders = [
@@ -1183,7 +1183,7 @@ def EmitC_SubscriptOp : EmitC_Op<"subscript",
11831183
];
11841184

11851185
let hasVerifier = 1;
1186-
let assemblyFormat = "$array `[` $indices `]` attr-dict `:` type($array)";
1186+
let assemblyFormat = "$array `[` $indices `]` attr-dict `:` type($array) `,` type($indices)";
11871187
}
11881188

11891189

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -759,20 +759,6 @@ LogicalResult emitc::VariableOp::verify() {
759759
return verifyInitializationAttribute(getOperation(), getValueAttr());
760760
}
761761

762-
//===----------------------------------------------------------------------===//
763-
// SubscriptOp
764-
//===----------------------------------------------------------------------===//
765-
766-
LogicalResult emitc::SubscriptOp::verify() {
767-
if (getIndices().size() != (size_t)getArray().getType().getRank()) {
768-
return emitOpError() << "requires number of indices ("
769-
<< getIndices().size()
770-
<< ") to match the rank of the array type ("
771-
<< getArray().getType().getRank() << ")";
772-
}
773-
return success();
774-
}
775-
776762
//===----------------------------------------------------------------------===//
777763
// YieldOp
778764
//===----------------------------------------------------------------------===//
@@ -790,6 +776,20 @@ LogicalResult emitc::YieldOp::verify() {
790776
return success();
791777
}
792778

779+
//===----------------------------------------------------------------------===//
780+
// SubscriptOp
781+
//===----------------------------------------------------------------------===//
782+
783+
LogicalResult emitc::SubscriptOp::verify() {
784+
if (getIndices().size() != (size_t)getArray().getType().getRank()) {
785+
return emitOpError() << "requires number of indices ("
786+
<< getIndices().size()
787+
<< ") to match the rank of the array type ("
788+
<< getArray().getType().getRank() << ")";
789+
}
790+
return success();
791+
}
792+
793793
//===----------------------------------------------------------------------===//
794794
// TableGen'd op method definitions
795795
//===----------------------------------------------------------------------===//

mlir/test/Conversion/MemRefToEmitC/memref-to-emit-failed.mlir

Lines changed: 0 additions & 41 deletions
This file was deleted.

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func.func @test_misplaced_yield() {
235235
// -----
236236

237237
func.func @test_assign_to_non_variable(%arg1: f32, %arg2: f32) {
238-
// expected-error @+1 {{'emitc.assign' op requires first operand (<block argument> of type 'f32' at index: 1) to be a Variable}}
238+
// expected-error @+1 {{'emitc.assign' op requires first operand (<block argument> of type 'f32' at index: 1) to be a Variable or subscript}}
239239
emitc.assign %arg1 : f32 to %arg2 : f32
240240
return
241241
}
@@ -387,3 +387,11 @@ func.func @logical_or_resulterror(%arg0: i32, %arg1: i32) {
387387
%0 = "emitc.logical_or"(%arg0, %arg1) : (i32, i32) -> i32
388388
return
389389
}
390+
391+
// -----
392+
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
396+
return
397+
}

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,6 @@ 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<4x8xf32>, %arg1: !emitc.array<3x5xf32>,
218-
%arg2: index, %arg3: index) {
219-
%0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>
220-
%1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>
221-
emitc.assign %0 : f32 to %1 : f32
222-
return
223-
}
224-
225217
emitc.verbatim "#ifdef __cplusplus"
226218
emitc.verbatim "extern \"C\" {"
227219
emitc.verbatim "#endif // __cplusplus"

mlir/test/Target/Cpp/subscript.mlir

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

44
func.func @load_store(%arg0: !emitc.array<4x8xf32>, %arg1: !emitc.array<3x5xf32>, %arg2: index, %arg3: index) {
5-
%0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>
6-
%1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>
5+
%0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index
6+
%1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>, index, index
77
emitc.assign %0 : f32 to %1 : f32
88
return
99
}
1010
// CHECK: void load_store(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]]];
13+
14+
emitc.func @func1(%arg0 : f32) {
15+
emitc.return
16+
}
17+
18+
emitc.func @call_arg(%arg0: !emitc.array<4x8xf32>, %i: i32, %j: i16,
19+
%k: i8) {
20+
%0 = emitc.subscript %arg0[%i, %j] : <4x8xf32>, i32, i16
21+
%1 = emitc.subscript %arg0[%j, %k] : <4x8xf32>, i16, i8
22+
23+
emitc.call @func1 (%0) : (f32) -> ()
24+
emitc.call_opaque "func2" (%1) : (f32) -> ()
25+
emitc.call_opaque "func3" (%0, %1) { args = [1 : index, 0 : index] } : (f32, f32) -> ()
26+
emitc.return
27+
}
28+
// CHECK: void call_arg(float [[ARR1:[^ ]*]][4][8], int32_t [[I:[^ ]*]],
29+
// CHECK-SAME: int16_t [[J:[^ ]*]], int8_t [[K:[^ ]*]])
30+
// CHECK-NEXT: func1([[ARR1]][[[I]]][[[J]]]);
31+
// CHECK-NEXT: func2([[ARR1]][[[J]]][[[K]]]);
32+
// CHECK-NEXT: func3([[ARR1]][[[J]]][[[K]]], [[ARR1]][[[I]]][[[J]]]);

0 commit comments

Comments
 (0)