Skip to content

Commit 5ab6589

Browse files
committed
[mlir:bytecode] Fix bytecode lazy loading for ops with multiple regions
We currently encode each region as a separate section, but the reader expects all of the regions to be in the same section. This updates the writer to match the behavior that the reader expects. Differential Revision: https://reviews.llvm.org/D156198
1 parent a809720 commit 5ab6589

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ class BytecodeWriter {
587587
LogicalResult writeRegion(EncodingEmitter &emitter, Region *region);
588588
LogicalResult writeIRSection(EncodingEmitter &emitter, Operation *op);
589589

590+
LogicalResult writeRegions(EncodingEmitter &emitter,
591+
MutableArrayRef<Region> regions) {
592+
return success(llvm::all_of(regions, [&](Region &region) {
593+
return succeeded(writeRegion(emitter, &region));
594+
}));
595+
}
596+
590597
//===--------------------------------------------------------------------===//
591598
// Resources
592599

@@ -938,20 +945,17 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
938945
bool isIsolatedFromAbove = op->hasTrait<OpTrait::IsIsolatedFromAbove>();
939946
emitter.emitVarIntWithFlag(numRegions, isIsolatedFromAbove);
940947

941-
for (Region &region : op->getRegions()) {
942-
// If the region is not isolated from above, or we are emitting bytecode
943-
// targeting version <kLazyLoading, we don't use a section.
944-
if (!isIsolatedFromAbove ||
945-
config.bytecodeVersion < bytecode::kLazyLoading) {
946-
if (failed(writeRegion(emitter, &region)))
947-
return failure();
948-
continue;
949-
}
950-
948+
// If the region is not isolated from above, or we are emitting bytecode
949+
// targeting version <kLazyLoading, we don't use a section.
950+
if (isIsolatedFromAbove &&
951+
config.bytecodeVersion >= bytecode::kLazyLoading) {
951952
EncodingEmitter regionEmitter;
952-
if (failed(writeRegion(regionEmitter, &region)))
953+
if (failed(writeRegions(regionEmitter, op->getRegions())))
953954
return failure();
954955
emitter.emitSection(bytecode::Section::kIR, std::move(regionEmitter));
956+
957+
} else if (failed(writeRegions(emitter, op->getRegions()))) {
958+
return failure();
955959
}
956960
}
957961
return success();

mlir/test/Bytecode/bytecode-lazy-loading.mlir

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33

44

55
func.func @op_with_passthrough_region_args() {
6+
%0 = arith.constant 10 : index
7+
68
// Ensure we can handle nested non-isolated/non-lazy regions.
7-
"test.one_region_op"() ({}) : () -> ()
9+
"test.one_region_op"() ({
10+
"test.consumer"(%0) : (index) -> ()
11+
}) : () -> ()
812

9-
%0 = arith.constant 10 : index
1013
test.isolated_region %0 {
1114
"test.consumer"(%0) : (index) -> ()
1215
}
1316
%result:2 = "test.op"() : () -> (index, index)
1417
test.isolated_region %result#1 {
1518
"test.consumer"(%result#1) : (index) -> ()
1619
}
20+
21+
test.isolated_regions {
22+
"test.unknown_op"() : () -> ()
23+
}, {
24+
"test.unknown_op"() : () -> ()
25+
}
1726
return
1827
}
1928

@@ -39,11 +48,12 @@ func.func @op_with_passthrough_region_args() {
3948
// CHECK-NOT: arith
4049
// CHECK: Materializing...
4150
// CHECK: "func.func"() <{function_type = () -> (), sym_name = "op_with_passthrough_region_args"}> ({
42-
// CHECK: one_region_op
4351
// CHECK: arith
52+
// CHECK: one_region_op
53+
// CHECK: test.consumer
4454
// CHECK: isolated_region
4555
// CHECK-NOT: test.consumer
46-
// CHECK: Has 2 ops to materialize
56+
// CHECK: Has 3 ops to materialize
4757

4858
// CHECK: Before Materializing...
4959
// CHECK: test.isolated_region
@@ -52,12 +62,21 @@ func.func @op_with_passthrough_region_args() {
5262
// CHECK: test.isolated_region
5363
// CHECK: ^bb0(%arg0: index):
5464
// CHECK: test.consumer
55-
// CHECK: Has 1 ops to materialize
65+
// CHECK: Has 2 ops to materialize
5666

5767
// CHECK: Before Materializing...
5868
// CHECK: test.isolated_region
5969
// CHECK-NOT: test.consumer
6070
// CHECK: Materializing...
6171
// CHECK: test.isolated_region
6272
// CHECK: test.consumer
73+
// CHECK: Has 1 ops to materialize
74+
75+
// CHECK: Before Materializing...
76+
// CHECK: test.isolated_regions
77+
// CHECK-NOT: test.unknown_op
78+
// CHECK: Materializing...
79+
// CHECK: test.isolated_regions
80+
// CHECK: test.unknown_op
81+
// CHECK: test.unknown_op
6382
// CHECK: Has 0 ops to materialize

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ def IsolatedOneRegionOp : TEST_Op<"isolated_one_region_op", [IsolatedFromAbove]>
451451
}];
452452
}
453453

454+
def IsolatedRegionsOp : TEST_Op<"isolated_regions", [IsolatedFromAbove]> {
455+
let regions = (region VariadicRegion<AnyRegion>:$regions);
456+
let assemblyFormat = "attr-dict-with-keyword $regions";
457+
}
458+
454459
//===----------------------------------------------------------------------===//
455460
// NoTerminator Operation
456461
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)