Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions runtime/cudaq/builder/kernel_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cudaq/Optimizer/Dialect/Quake/QuakeDialect.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "cudaq/platform/nvqpp_interface.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/ExecutionEngine/ExecutionEngine.h"
Expand All @@ -30,16 +31,10 @@
#include "mlir/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "mlir/Transforms/Passes.h"

#include <numeric>

using namespace mlir;

extern "C" {
void altLaunchKernel(const char *kernelName, void (*kernelFunc)(void *),
void *kernelArgs, std::uint64_t argsSize);
}

namespace cudaq::details {

/// @brief Track unique measurement register names.
Expand Down Expand Up @@ -1105,9 +1100,20 @@ void invokeCode(ImplicitLocOpBuilder &builder, ExecutionEngine *jit,
}

// Invoke and free the args memory.
auto thunk = reinterpret_cast<void (*)(void *)>(*thunkPtr);
auto thunk = reinterpret_cast<KernelThunkType>(*thunkPtr);

// Extract the result offset, which we named.
auto roName = properName + ".returnOffset";
auto roPtr = jit->lookup(roName);
if (!roPtr)
throw std::runtime_error(
"cudaq::builder failed to get result offset function");

// Invoke and free the args memory.
auto resultOffset = reinterpret_cast<std::uint64_t>(*roPtr);

altLaunchKernel(properName.data(), thunk, rawArgs, size);
[[maybe_unused]] auto uncheckedResult =
altLaunchKernel(properName.data(), thunk, rawArgs, size, resultOffset);
std::free(rawArgs);
// TODO: any return values are dropped on the floor here.
}
Expand Down
40 changes: 40 additions & 0 deletions runtime/cudaq/platform/nvqpp_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/****************************************************************-*- C++ -*-****
* Copyright (c) 2025 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 <cstdint>
#include <vector>

namespace cudaq {

/// Entry point for the auto-generated kernel execution path. TODO: Needs to be
/// tied to the quantum platform instance somehow. Note that the compiler cannot
/// provide that information.
extern "C" {
// Client-server (legacy) interface.
[[nodiscard]] KernelThunkResultType
altLaunchKernel(const char *kernelName, KernelThunkType kernel, void *args,
std::uint64_t argsSize, std::uint64_t resultOffset);

// Streamlined interface for launching kernels. Argument synthesis and JIT
// compilation *must* happen on the local machine.
[[nodiscard]] KernelThunkResultType
streamlinedLaunchKernel(const char *kernelName,
const std::vector<void *> &rawArgs);

// Hybrid of the client-server and streamlined approaches. Letting JIT
// compilation happen either early or late and can handle return values from
// each kernel launch.
[[nodiscard]] KernelThunkResultType
hybridLaunchKernel(const char *kernelName, KernelThunkType kernel, void *args,
std::uint64_t argsSize, std::uint64_t resultOffset,
const std::vector<void *> &rawArgs);
} // extern "C"
} // namespace cudaq
2 changes: 1 addition & 1 deletion runtime/cudaq/platform/quantum_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
#include "common/ThunkInterface.h"
#include "cudaq/remote_capabilities.h"
#include "cudaq/utils/cudaq_utils.h"
#include "nvqpp_interface.h"
Copy link
Collaborator

@khalatepradnya khalatepradnya Oct 6, 2025

Choose a reason for hiding this comment

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

With the addition of this new header file, those declarations in this file can be removed? (lines 239-258)

#include <cstring>
#include <cxxabi.h>
#include <functional>
#include <future>
#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace cudaq {

Expand Down
Loading