Skip to content

Commit ed6cd8f

Browse files
lforg37Luc Forget
andauthored
[MLIR][WASM] Custom assembly format for if memory and table ops (#152668)
Co-authored-by: Luc Forget <[email protected]>
1 parent c23b4fb commit ed6cd8f

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

mlir/include/mlir/Dialect/WasmSSA/IR/WasmSSAOps.td

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ def WasmSSA_IfOp : WasmSSA_Op<"if", [Terminator,
329329

330330
```mlir
331331
// Runs the if clause is %a is non-zero
332-
"wasmssa.if"(%a)[^bb1] ({
332+
wasmssa.if %a {
333333
// Execute if %a is non-zero
334-
},{
334+
} else {
335335
// else clause
336-
}) : (i32) -> ()
336+
}
337337
```
338338
}];
339339
let arguments = (ins I32:$condition, Variadic<WasmSSA_ValType>: $inputs);
@@ -359,6 +359,7 @@ def WasmSSA_IfOp : WasmSSA_Op<"if", [Terminator,
359359
return createBlock(getElse());
360360
}
361361
}];
362+
let assemblyFormat = "$condition (`(`$inputs^`)` `:` type($inputs))? attr-dict `:` $if custom<ElseRegion>($else) `>` $target";
362363
}
363364

364365
def WasmSSA_LocalOp : WasmSSA_Op<"local", [
@@ -445,7 +446,7 @@ def WasmSSA_MemOp : WasmSSA_Op<"memory", [Symbol]> {
445446

446447
```mlir
447448
// Define the `mem_0` memory with defined bounds of 0 -> 65536
448-
"wasmssa.memory"() <{limits = !wasmssa<limit[0:65536]>, sym_name = "mem_0"}> : () -> ()
449+
wasmssa.memory @mem_0 !wasmssa<limit[0:65536]>
449450
```
450451
}];
451452
let arguments = (ins SymbolNameAttr: $sym_name,
@@ -456,6 +457,8 @@ def WasmSSA_MemOp : WasmSSA_Op<"memory", [Symbol]> {
456457
"::llvm::StringRef":$symbol,
457458
"wasmssa::LimitType":$limit)>
458459
];
460+
461+
let assemblyFormat = "$sym_name custom<WasmVisibility>($sym_visibility) $limits attr-dict";
459462
}
460463

461464
def WasmSSA_MemImportOp : WasmSSA_Op<"import_mem", [Symbol, ImportOpInterface]> {
@@ -494,6 +497,7 @@ def WasmSSA_TableOp : WasmSSA_Op<"table", [Symbol]> {
494497
let builders = [OpBuilder<(ins
495498
"::llvm::StringRef":$symbol,
496499
"wasmssa::TableType":$type)>];
500+
let assemblyFormat = "$sym_name custom<WasmVisibility>($sym_visibility) $type attr-dict";
497501
}
498502

499503
def WasmSSA_TableImportOp : WasmSSA_Op<"import_table", [Symbol, ImportOpInterface]> {

mlir/lib/Dialect/WasmSSA/IR/WasmSSAOps.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,54 @@
2222
// TableGen'd op method definitions
2323
//===----------------------------------------------------------------------===//
2424

25+
using namespace mlir;
26+
namespace {
27+
ParseResult parseElseRegion(OpAsmParser &opParser, Region &elseRegion) {
28+
std::string keyword;
29+
std::ignore = opParser.parseOptionalKeywordOrString(&keyword);
30+
if (keyword == "else")
31+
return opParser.parseRegion(elseRegion);
32+
return ParseResult::success();
33+
}
34+
35+
void printElseRegion(OpAsmPrinter &opPrinter, Operation *op,
36+
Region &elseRegion) {
37+
if (elseRegion.empty())
38+
return;
39+
opPrinter.printKeywordOrString("else ");
40+
opPrinter.printRegion(elseRegion);
41+
}
42+
43+
ParseResult parseWasmVisibility(OpAsmParser &opParser, StringAttr &visibility) {
44+
std::string keyword;
45+
auto initLocation = opParser.getCurrentLocation();
46+
std::ignore = opParser.parseOptionalKeywordOrString(&keyword);
47+
if (keyword == "nested" or keyword == "") {
48+
visibility = StringAttr::get(opParser.getContext(), "nested");
49+
return ParseResult::success();
50+
}
51+
52+
if (keyword == "public" || keyword == "private") {
53+
visibility = StringAttr::get(opParser.getContext(), keyword);
54+
return ParseResult::success();
55+
}
56+
opParser.emitError(initLocation, "expecting symbol visibility");
57+
return ParseResult::failure();
58+
}
59+
60+
void printWasmVisibility(OpAsmPrinter &opPrinter, Operation *op,
61+
Attribute visibility) {
62+
opPrinter.printKeywordOrString(cast<StringAttr>(visibility).strref());
63+
}
64+
} // namespace
65+
2566
#define GET_OP_CLASSES
2667
#include "mlir/Dialect/WasmSSA/IR/WasmSSAOps.cpp.inc"
2768

2869
#include "mlir/IR/OpImplementation.h"
2970
#include "mlir/IR/Types.h"
3071
#include "llvm/Support/LogicalResult.h"
3172

32-
using namespace mlir;
3373
using namespace wasmssa;
3474

3575
namespace {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: mlir-opt %s | FileCheck %s
2+
3+
// CHECK-LABEL: wasmssa.func nested @func_0(
4+
// CHECK-SAME: %[[ARG0:.*]]: !wasmssa<local ref to i32>) -> i32 {
5+
// CHECK: %[[VAL_0:.*]] = wasmssa.local_get %[[ARG0]] : ref to i32
6+
// CHECK: wasmssa.if %[[VAL_0]] : {
7+
// CHECK: %[[VAL_1:.*]] = wasmssa.const 5.000000e-01 : f32
8+
// CHECK: wasmssa.block_return %[[VAL_1]] : f32
9+
// CHECK: } "else "{
10+
// CHECK: %[[VAL_2:.*]] = wasmssa.const 2.500000e-01 : f32
11+
// CHECK: wasmssa.block_return %[[VAL_2]] : f32
12+
// CHECK: }> ^bb1
13+
// CHECK: ^bb1(%[[VAL_3:.*]]: f32):
14+
// CHECK: wasmssa.return %[[VAL_3]] : f32
15+
wasmssa.func nested @func_0(%arg0 : !wasmssa<local ref to i32>) -> i32 {
16+
%cond = wasmssa.local_get %arg0 : ref to i32
17+
wasmssa.if %cond : {
18+
%c0 = wasmssa.const 0.5 : f32
19+
wasmssa.block_return %c0 : f32
20+
} else {
21+
%c1 = wasmssa.const 0.25 : f32
22+
wasmssa.block_return %c1 : f32
23+
} >^bb1
24+
^bb1(%retVal: f32):
25+
wasmssa.return %retVal : f32
26+
}
27+
28+
// CHECK-LABEL: wasmssa.func nested @func_1(
29+
// CHECK-SAME: %[[ARG0:.*]]: !wasmssa<local ref to i32>) -> i32 {
30+
// CHECK: %[[VAL_0:.*]] = wasmssa.local_get %[[ARG0]] : ref to i32
31+
// CHECK: %[[VAL_1:.*]] = wasmssa.local of type i32
32+
// CHECK: %[[VAL_2:.*]] = wasmssa.const 0 : i64
33+
// CHECK: wasmssa.if %[[VAL_0]] : {
34+
// CHECK: %[[VAL_3:.*]] = wasmssa.const 1 : i32
35+
// CHECK: wasmssa.local_set %[[VAL_1]] : ref to i32 to %[[VAL_3]] : i32
36+
// CHECK: wasmssa.block_return
37+
// CHECK: } > ^bb1
38+
// CHECK: ^bb1:
39+
// CHECK: %[[VAL_4:.*]] = wasmssa.local_get %[[VAL_1]] : ref to i32
40+
// CHECK: wasmssa.return %[[VAL_4]] : i32
41+
wasmssa.func nested @func_1(%arg0 : !wasmssa<local ref to i32>) -> i32 {
42+
%cond = wasmssa.local_get %arg0 : ref to i32
43+
%var = wasmssa.local of type i32
44+
%zero = wasmssa.const 0
45+
wasmssa.if %cond : {
46+
%c1 = wasmssa.const 1 : i32
47+
wasmssa.local_set %var : ref to i32 to %c1 : i32
48+
wasmssa.block_return
49+
} >^bb1
50+
^bb1:
51+
%res = wasmssa.local_get %var : ref to i32
52+
wasmssa.return %res : i32
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: mlir-opt %s | FileCheck %s
2+
3+
// CHECK: wasmssa.memory @mem0 public !wasmssa<limit[0: 65536]>
4+
wasmssa.memory @mem0 public !wasmssa<limit[0:65536]>
5+
6+
// CHECK: wasmssa.memory @mem1 nested !wasmssa<limit[512:]>
7+
wasmssa.memory @mem1 !wasmssa<limit[512:]>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: mlir-opt %s | FileCheck %s
2+
3+
// CHECK: wasmssa.table @tab0 public !wasmssa<tabletype !wasmssa.externref [0: 65536]>
4+
wasmssa.table @tab0 public !wasmssa<tabletype !wasmssa.externref [0:65536]>
5+
6+
// CHECK: wasmssa.table @tab1 nested !wasmssa<tabletype !wasmssa.funcref [348:]>
7+
wasmssa.table @tab1 !wasmssa<tabletype !wasmssa.funcref [348:]>

0 commit comments

Comments
 (0)