Skip to content

[MLIR] Fix mlirExecutionEngineLookup throwing assert on lookup fail #123924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025

Conversation

edg-l
Copy link
Contributor

@edg-l edg-l commented Jan 22, 2025

Apparently trying to lookup a function pointer using the C api mlirExecutionEngineLookup will throw an assert instead of just returning a nullptr on builds with asserts.

The docs itself says it returns a nullptr when no function is found so it should be sensible to not throw an assert in this case.

@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-execution-engine

Author: Edgar (edg-l)

Changes

Apparently trying to lookup a function pointer using the C api mlirExecutionEngineLookup will throw an assert instead of just returning a nullptr on builds with asserts.

The docs itself says it returns a nullptr when no function is found so it should be sensible to not throw an assert in this case.


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

1 Files Affected:

  • (modified) mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp (+9-6)
diff --git a/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp b/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp
index 507be9171d328d..8480a5409a8159 100644
--- a/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp
+++ b/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp
@@ -15,6 +15,7 @@
 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
 #include "llvm/ExecutionEngine/Orc/Mangling.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/TargetSelect.h"
 
 using namespace mlir;
@@ -85,18 +86,20 @@ mlirExecutionEngineInvokePacked(MlirExecutionEngine jit, MlirStringRef name,
 
 extern "C" void *mlirExecutionEngineLookupPacked(MlirExecutionEngine jit,
                                                  MlirStringRef name) {
-  auto expectedFPtr = unwrap(jit)->lookupPacked(unwrap(name));
-  if (!expectedFPtr)
+  auto optionalFPtr =
+      llvm::expectedToOptional(unwrap(jit)->lookup(unwrap(name)));
+  if (!optionalFPtr.has_value())
     return nullptr;
-  return reinterpret_cast<void *>(*expectedFPtr);
+  return reinterpret_cast<void *>(*optionalFPtr);
 }
 
 extern "C" void *mlirExecutionEngineLookup(MlirExecutionEngine jit,
                                            MlirStringRef name) {
-  auto expectedFPtr = unwrap(jit)->lookup(unwrap(name));
-  if (!expectedFPtr)
+  auto optionalFPtr =
+      llvm::expectedToOptional(unwrap(jit)->lookup(unwrap(name)));
+  if (!optionalFPtr.has_value())
     return nullptr;
-  return reinterpret_cast<void *>(*expectedFPtr);
+  return reinterpret_cast<void *>(*optionalFPtr);
 }
 
 extern "C" void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit,

@edg-l edg-l force-pushed the mlir_c_exec_engine_lookup branch from 06f7955 to 7198fb2 Compare January 22, 2025 10:36
Copy link
Member

@Lewuathe Lewuathe left a comment

Choose a reason for hiding this comment

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

It appears that a couple of tests of execution engine fails. This Python code is likely to use the CAPI as well. Could you check whether it's related to this change or not?

https://buildkite.com/llvm-project/github-pull-requests/builds/139352

# RUN: at line 1
env MLIR_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_runner_utils.so MLIR_C_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_c_runner_utils.so /usr/bin/python3.10 /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py 2>&1 | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# executed command: env MLIR_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_runner_utils.so MLIR_C_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_c_runner_utils.so /usr/bin/python3.10 /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# note: command had no output on stdout or stderr
# error: command failed with exit status: -11
# executed command: /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# .---command stderr------------
# | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py:129:11: error: CHECK: expected string not found in input
# |  # CHECK: 42.0 + 2.0 = 44.0
# |           ^
# | <stdin>:12:25: note: scanning from here
# | TEST: testInvokeFloatAdd

@edg-l
Copy link
Contributor Author

edg-l commented Jan 24, 2025

It appears that a couple of tests of execution engine fails. This Python code is likely to use the CAPI as well. Could you check whether it's related to this change or not?

https://buildkite.com/llvm-project/github-pull-requests/builds/139352

# RUN: at line 1
env MLIR_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_runner_utils.so MLIR_C_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_c_runner_utils.so /usr/bin/python3.10 /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py 2>&1 | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# executed command: env MLIR_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_runner_utils.so MLIR_C_RUNNER_UTILS=/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/lib/libmlir_c_runner_utils.so /usr/bin/python3.10 /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# note: command had no output on stdout or stderr
# error: command failed with exit status: -11
# executed command: /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py
# .---command stderr------------
# | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-gz48w-1/llvm-project/github-pull-requests/mlir/test/python/execution_engine.py:129:11: error: CHECK: expected string not found in input
# |  # CHECK: 42.0 + 2.0 = 44.0
# |           ^
# | <stdin>:12:25: note: scanning from here
# | TEST: testInvokeFloatAdd

To my knowledge, invoke should be a call to invoke not lookup, so i'm not sure if its related

Nvm, looks like invoke calls lookup so maybe its related

@edg-l
Copy link
Contributor Author

edg-l commented Jan 24, 2025

I found the issue, copy pasted the lookup into lookuppacked and forgot to change lookup to lookuppacked.

@Lewuathe should be fixed now

Copy link
Member

@Lewuathe Lewuathe left a comment

Choose a reason for hiding this comment

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

Thank you for fixing the failure. It looks good to me.

@edg-l
Copy link
Contributor Author

edg-l commented Jan 30, 2025

I guess its ready to be merged? Would be cool to get it in before 20 (i guess its too late)

@edg-l edg-l force-pushed the mlir_c_exec_engine_lookup branch from 0a52b59 to 9b579b3 Compare February 15, 2025 08:29
@edg-l edg-l force-pushed the mlir_c_exec_engine_lookup branch from 9b579b3 to abd362d Compare February 15, 2025 08:30
@edg-l
Copy link
Contributor Author

edg-l commented Feb 15, 2025

I rebased and all, this has been ready for quite a while. Can you look into merging this?

@joker-eph joker-eph merged commit 2db2628 into llvm:main Feb 15, 2025
8 checks passed
@edg-l edg-l deleted the mlir_c_exec_engine_lookup branch February 15, 2025 13:53
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…lvm#123924)

Apparently trying to lookup a function pointer using the C api
`mlirExecutionEngineLookup` will throw an assert instead of just
returning a nullptr on builds with asserts.

The docs itself says it returns a nullptr when no function is found so
it should be sensible to not throw an assert in this case.
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.

5 participants