Skip to content

Conversation

gdehame
Copy link
Contributor

@gdehame gdehame commented Aug 16, 2025

The operand emission needed the operand to be in scope which lead to failure when the emitc.call_opaque is in an emitc.expression's body

@llvmbot
Copy link
Member

llvmbot commented Aug 16, 2025

@llvm/pr-subscribers-mlir-emitc

@llvm/pr-subscribers-mlir

Author: Gabriel Dehame (gdehame)

Changes

The operand emission needed the operand to be in scope which lead to failure when the emitc.call_opaque is in an emitc.expression's body


Full diff: https://github.com/llvm/llvm-project/pull/153980.diff

1 Files Affected:

  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+2-4)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 8e83e455d1a7f..510bf7b72b00c 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -749,10 +749,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
       if (t.getType().isIndex()) {
         int64_t idx = t.getInt();
         Value operand = op.getOperand(idx);
-        if (!emitter.hasValueInScope(operand))
-          return op.emitOpError("operand ")
-                 << idx << "'s value not defined in scope";
-        os << emitter.getOrCreateName(operand);
+        if (failed(emitter.emitOperand(operand)))
+          return failure();
         return success();
       }
     }

@gdehame
Copy link
Contributor Author

gdehame commented Aug 16, 2025

Example MLIR leading to an error before fix:

module {
  emitc.func @f(%0 : i32, %1 : i32) {
    %2 = expression : i1 {
      %3 = cmp lt, %0, %1 : (i32, i32) -> i1
      %4 = emitc.call_opaque "g"(%3) {"args" = [0: index, 0 : i32]} : (i1) -> i1
      yield %4 : i1
    }
    return
  }
}

@gdehame gdehame changed the title Bugfix in emitc.call_opaque operand emission [MLIR][EmitC] Bugfix in emitc.call_opaque operand emission Aug 16, 2025
@EtoAndruwa
Copy link
Contributor

I think it would be good to add a test for this.

@aniragil
Copy link
Contributor

@marbre, @simon-camp, @mgehre-amd - this LGTM, but I'm fixing a very similar bug in #155171 so I'm biased. Could you take a look?

Copy link
Contributor

@simon-camp simon-camp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, Just two nits. Thanks you forbthe fix!

return op.emitOpError("operand ")
<< idx << "'s value not defined in scope";
os << emitter.getOrCreateName(operand);
if (failed(emitter.emitOperand(operand)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can return emitter.emitOperand directly.

yield %4 : i1
}
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line

@gdehame gdehame force-pushed the call-opaque-operand-emission-fix branch from 41b704a to c03d58f Compare August 24, 2025 16:02
The operand emission needed the operand to be in scope which lead to failure when the emitc.call_opaque is in an emitc.expression's body
@gdehame gdehame force-pushed the call-opaque-operand-emission-fix branch from c03d58f to 4168756 Compare August 24, 2025 16:06
@gdehame
Copy link
Contributor Author

gdehame commented Aug 24, 2025

Fixed format and squashed commits to merge a single commit

Copy link
Member

@marbre marbre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Do you want me to merge on your behalf @gdehame?

@gdehame
Copy link
Contributor Author

gdehame commented Aug 26, 2025

LGTM, thanks! Do you want me to merge on your behalf @gdehame?

Yes please

@marbre marbre merged commit 5e5fc64 into llvm:main Aug 26, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants