-
Notifications
You must be signed in to change notification settings - Fork 348
[python] Unify specialize and launch in ModuleLauncher #4052
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||
| /******************************************************************************* | ||||
| * Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. * | ||||
lmondada marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| * All rights reserved. * | ||||
| * * | ||||
| * This source code and the accompanying materials are made available under * | ||||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||||
| ******************************************************************************/ | ||||
|
|
||||
| #include "CompiledKernel.h" | ||||
|
|
||||
| namespace cudaq { | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should move to the LLVM style so as to better catch bugs, etc.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood, I'm removing the |
||||
|
|
||||
| CompiledKernel::CompiledKernel(OpaquePtr<JitEngine> engine, | ||||
| std::string kernelName, void (*entryPoint)(), | ||||
| bool hasResult) | ||||
| : engine(std::move(engine)), name(std::move(kernelName)), | ||||
| entryPoint(entryPoint), hasResult(hasResult) {} | ||||
|
|
||||
| KernelThunkResultType | ||||
| CompiledKernel::execute(const std::vector<void *> &rawArgs) const { | ||||
| auto funcPtr = getEntryPoint(); | ||||
| if (hasResult) { | ||||
| void *buff = const_cast<void *>(rawArgs.back()); | ||||
| return reinterpret_cast<KernelThunkResultType (*)(void *, bool)>(funcPtr)( | ||||
| buff, /*client_server=*/false); | ||||
| } else { | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: else after return. https://clang.llvm.org/extra/clang-tidy/checks/readability/else-after-return.html
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, thanks. |
||||
| reinterpret_cast<void (*)()>(funcPtr)(); | ||||
| return {nullptr, 0}; | ||||
| } | ||||
| } | ||||
|
|
||||
| void (*CompiledKernel::getEntryPoint() const)() { return entryPoint; } | ||||
|
|
||||
| const JitEngine &CompiledKernel::getEngine() const { return *engine; } | ||||
|
|
||||
| } // namespace cudaq | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /****************************************************************-*- C++ -*-**** | ||
| * Copyright (c) 2022 - 2026 NVIDIA Corporation & Affiliates. * | ||
| * All rights reserved. * | ||
| * * | ||
| * This source code and the accompanying materials are made available under * | ||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||
| ******************************************************************************/ | ||
| #pragma once | ||
|
|
||
| #include "common/ThunkInterface.h" | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace cudaq { | ||
|
|
||
| class JitEngine; | ||
|
|
||
| /// A unique_ptr with a plain function pointer as destructor, allowing | ||
| /// type-erased ownership of forward-declared (incomplete) types. | ||
| template <typename T> | ||
| using OpaquePtr = std::unique_ptr<T, void (*)(T *)>; | ||
Renaud-K marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| template <typename T, typename... Args> | ||
| OpaquePtr<T> makeOpaquePtr(Args &&...args) { | ||
| return OpaquePtr<T>(new T(std::forward<Args>(args)...), | ||
| [](T *p) { delete p; }); | ||
| } | ||
|
|
||
| /// @brief A compiled, ready-to-execute kernel. | ||
| /// | ||
| /// This type does not have a dependency on MLIR (or LLVM) as it only keeps | ||
| /// type-erased pointers to JIT-related types. | ||
| /// | ||
| /// The constructor is private; use the factory function in | ||
| /// `runtime/common/JIT.h` to construct instances. | ||
| class CompiledKernel { | ||
| public: | ||
| /// @brief Execute the JIT-ed kernel. | ||
| /// | ||
| /// If the kernel has a return type, the caller must have appended a result | ||
| /// buffer as the last element of \p rawArgs. | ||
| KernelThunkResultType execute(const std::vector<void *> &rawArgs) const; | ||
|
|
||
| void (*getEntryPoint() const)(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm dubious of this. CUDA-Q does not define entry point kernels as always having |
||
|
|
||
| const JitEngine &getEngine() const; | ||
|
|
||
| private: | ||
| CompiledKernel(OpaquePtr<JitEngine> engine, std::string kernelName, | ||
| void (*entryPoint)(), bool hasResult); | ||
|
|
||
| // Use the following factory function in JIT.h to construct CompiledKernels. | ||
| friend CompiledKernel createCompiledKernel(JitEngine engine, | ||
| std::string kernelName, | ||
| bool hasResult); | ||
|
|
||
| OpaquePtr<JitEngine> engine; | ||
Renaud-K marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::string name; | ||
| void (*entryPoint)(); | ||
| bool hasResult; | ||
| }; | ||
|
|
||
| } // namespace cudaq | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -325,6 +325,16 @@ cudaq::JitEngine cudaq::createQIRJITEngine(mlir::ModuleOp &moduleOp, | |
| return JitEngine(std::move(jitOrError.get())); | ||
| } | ||
|
|
||
| cudaq::CompiledKernel cudaq::createCompiledKernel(JitEngine engine, | ||
| std::string kernelName, | ||
| bool hasResult) { | ||
| std::string fullName = cudaq::runtime::cudaqGenPrefixName + kernelName; | ||
| std::string entryName = hasResult ? kernelName + ".thunk" : fullName; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code looks like the efficiency hack used in Python. i.e., unrelated to the C++ implementation. But it is appearing here in what looks like common code? |
||
| void (*entryPoint)() = engine.lookupRawNameOrFail(entryName); | ||
| return cudaq::CompiledKernel(cudaq::makeOpaquePtr<JitEngine>(engine), | ||
| std::move(kernelName), entryPoint, hasResult); | ||
| } | ||
|
|
||
| namespace cudaq { | ||
| class JitEngine::Impl { | ||
| public: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.