Skip to content

Commit 6cbb67f

Browse files
[mlir][emitc] Fix the emitc::ExpressionOp (#143894)
Fix the lack of verification that the definingOp of the return value belongs to emitc::ExpressionOp.
1 parent 2bf3cca commit 6cbb67f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,7 @@ OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
386386
Operation *ExpressionOp::getRootOp() {
387387
auto yieldOp = cast<YieldOp>(getBody()->getTerminator());
388388
Value yieldedValue = yieldOp.getResult();
389-
Operation *rootOp = yieldedValue.getDefiningOp();
390-
assert(rootOp && "Yielded value not defined within expression");
391-
return rootOp;
389+
return yieldedValue.getDefiningOp();
392390
}
393391

394392
LogicalResult ExpressionOp::verify() {
@@ -406,6 +404,14 @@ LogicalResult ExpressionOp::verify() {
406404
if (!yieldResult)
407405
return emitOpError("must yield a value at termination");
408406

407+
Operation *rootOp = yieldResult.getDefiningOp();
408+
409+
if (!rootOp)
410+
return emitOpError("yielded value has no defining op");
411+
412+
if (rootOp->getParentOp() != getOperation())
413+
return emitOpError("yielded value not defined within expression");
414+
409415
Type yieldType = yieldResult.getType();
410416

411417
if (resultType != yieldType)

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,28 @@ func.func @test_expression_multiple_results(%arg0: i32) -> i32 {
346346

347347
// -----
348348

349+
emitc.func @test_expression_no_defining_op(%a : i32) {
350+
// expected-error @+1 {{'emitc.expression' op yielded value has no defining op}}
351+
%res = emitc.expression : i32 {
352+
emitc.yield %a : i32
353+
}
354+
355+
return
356+
}
357+
358+
// -----
359+
360+
emitc.func @test_expression_op_outside_expression() {
361+
%cond = literal "true" : i1
362+
// expected-error @+1 {{'emitc.expression' op yielded value not defined within expression}}
363+
%res = emitc.expression : i1 {
364+
emitc.yield %cond : i1
365+
}
366+
return
367+
}
368+
369+
// -----
370+
349371
// expected-error @+1 {{'emitc.func' op requires zero or exactly one result, but has 2}}
350372
emitc.func @multiple_results(%0: i32) -> (i32, i32) {
351373
emitc.return %0 : i32

0 commit comments

Comments
 (0)