Skip to content

Commit 6c59f0e

Browse files
authored
[mlir][emitc] Fix literal translation (#71296)
- Do not emit variables-at-top for literals - Do not emit an error for a missing name for literals used as call operands.
1 parent 4263068 commit 6c59f0e

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
637637
// regions.
638638
WalkResult result =
639639
functionOp.walk<WalkOrder::PreOrder>([&](Operation *op) -> WalkResult {
640+
if (isa<emitc::LiteralOp>(op))
641+
return WalkResult::skip();
640642
for (OpResult result : op->getResults()) {
641643
if (failed(emitter.emitVariableDeclaration(
642644
result, /*trailingSemicolon=*/true))) {
@@ -839,7 +841,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
839841

840842
LogicalResult CppEmitter::emitOperands(Operation &op) {
841843
auto emitOperandName = [&](Value result) -> LogicalResult {
842-
if (!hasValueInScope(result))
844+
auto literalDef = dyn_cast_if_present<LiteralOp>(result.getDefiningOp());
845+
if (!literalDef && !hasValueInScope(result))
843846
return op.emitOpError() << "operand value not in scope";
844847
os << getOrCreateName(result);
845848
return success();

mlir/test/Target/Cpp/literal.mlir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ func.func @emitc_literal(%arg0: f32) {
1010
// CPP-DEFAULT: float [[V2:[^ ]*]] = [[V0:[^ ]*]] + M_PI
1111

1212
// CPP-DECLTOP: void emitc_literal(float [[V0:[^ ]*]]) {
13-
// CPP-DECLTOP: float M_PI;
1413
// CPP-DECLTOP: float [[V1:[^ ]*]];
1514
// CPP-DECLTOP: [[V1]] = [[V0:[^ ]*]] + M_PI

mlir/test/Target/Cpp/literal_call_operand.mlir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
22
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
3-
// XFAIL: *
43

54
func.func @emitc_call_operand() {
65
%p0 = emitc.literal "M_PI" : f32
@@ -11,6 +10,5 @@ func.func @emitc_call_operand() {
1110
// CPP-DEFAULT-NEXT: float v1 = foo(M_PI);
1211

1312
// CPP-DECLTOP: void emitc_call_operand() {
14-
// CPP-DECLTOP-NEXT: float M_PI;
1513
// CPP-DECLTOP-NEXT: float v1;
1614
// CPP-DECLTOP-NEXT: v1 = foo(M_PI);

0 commit comments

Comments
 (0)