Skip to content

Commit

Permalink
Merge branch 'main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
khalatepradnya authored Aug 21, 2024
2 parents 54adacb + 18c305b commit 23ac086
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/cudaq/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def GenerateKernelExecution : Pass<"kernel-execution", "mlir::ModuleOp"> {
/*default=*/"\"-\"", "Name of output file.">,
Option<"startingArgIdx", "starting-arg-idx", "std::size_t", /*default=*/"0",
"The starting argument index for the argsCreator.">,
Option<"codegenKind", "codegen", "std::size_t", /*default=*/"1",
Option<"codegenKind", "codegen", "std::size_t", /*default=*/"0",
"Set the kind of code to generate for the launches.">
];
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Optimizer/Transforms/LowerToCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ class RewriteIf : public OpRewritePattern<cudaq::cc::IfOp> {
rewriter.inlineRegionBefore(ifOp.getElseRegion(), endBlock);
rewriter.setInsertionPointToEnd(initBlock);
rewriter.create<cf::CondBranchOp>(loc, ifOp.getCondition(), thenBlock,
ValueRange{}, elseBlock, ValueRange{});
ifOp.getLinearArgs(), elseBlock,
ifOp.getLinearArgs());
rewriter.replaceOp(ifOp, endBlock->getArguments());
return success();
}
Expand Down
5 changes: 0 additions & 5 deletions runtime/cudaq/platform/default/DefaultQuantumPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class DefaultQPU : public cudaq::QPU {
kernelFunc(args);
}

void launchKernel(const std::string &name,
const std::vector<void *> &) override {
throw std::runtime_error("Wrong kernel launch point.");
}

/// Overrides setExecutionContext to forward it to the ExecutionManager
void setExecutionContext(cudaq::ExecutionContext *context) override {
ScopedTraceWithContext("DefaultPlatform::setExecutionContext",
Expand Down
5 changes: 0 additions & 5 deletions runtime/cudaq/platform/mqpu/custatevec/GPUEmulatedQPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class GPUEmulatedQPU : public cudaq::QPU {
kernelFunc(args);
}

void launchKernel(const std::string &name,
const std::vector<void *> &rawArgs) override {
throw std::runtime_error("not implemented");
}

/// Overrides setExecutionContext to forward it to the ExecutionManager
void setExecutionContext(cudaq::ExecutionContext *context) override {
cudaSetDevice(qpu_id);
Expand Down
11 changes: 10 additions & 1 deletion runtime/cudaq/platform/qpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,17 @@ class QPU : public registry::RegisteredType<QPU> {
/// as a struct-packed void pointer and its corresponding size.
virtual void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t, std::uint64_t) = 0;

/// Launch the kernel with given name and argument arrays.
// This is intended for remote QPUs whereby we need to JIT-compile the kernel
// with argument synthesis. Remote QPU implementation to override this.
virtual void launchKernel(const std::string &name,
const std::vector<void *> &rawArgs) = 0;
const std::vector<void *> &rawArgs) {
if (!isRemote())
throw std::runtime_error("Wrong kernel launch point: Attempt to launch "
"kernel in streamlined for JIT mode on local "
"simulated QPU. This is not supported.");
}

/// Launch serialized code for remote execution. Subtypes that support this
/// should override this function.
Expand Down
13 changes: 13 additions & 0 deletions runtime/cudaq/platform/quantum_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,16 @@ void cudaq::streamlinedLaunchKernel(const char *kernelName,
std::string kernName = kernelName;
platform.launchKernel(kernName, rawArgs);
}

void cudaq::hybridLaunchKernel(const char *kernelName, void (*kernel)(void *),
void *args, std::uint64_t argsSize,
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) {
ScopedTraceWithContext("hybridLaunchKernel", kernelName);
auto &platform = *cudaq::getQuantumPlatformInternal();
const std::string kernName = kernelName;
if (platform.is_remote(platform.get_current_qpu()))
platform.launchKernel(kernName, rawArgs);
else
platform.launchKernel(kernName, kernel, args, argsSize, resultOffset);
}
2 changes: 1 addition & 1 deletion test/Quake-QIR/argument.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: cudaq-translate --convert-to=qir | FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down
2 changes: 1 addition & 1 deletion test/Quake-QIR/return_values.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --add-dealloc --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --add-dealloc --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: cudaq-translate --convert-to=qir | FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down
4 changes: 2 additions & 2 deletions test/Quake/kernel_exec-1.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=1 %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=2 %s | FileCheck --check-prefix=STREAM %s
// RUN: cudaq-opt --kernel-execution=codegen=0 %s | FileCheck --check-prefix=HYBRID %s
// RUN: cudaq-opt --kernel-execution %s | FileCheck --check-prefix=HYBRID %s

module attributes {quake.mangled_name_map = {
__nvqpp__mlirgen__ghz = "_ZN3ghzclEi"}} {
Expand Down
2 changes: 1 addition & 1 deletion test/Quake/kernel_exec-2.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=1 %s | FileCheck %s

module attributes {quake.mangled_name_map = {
__nvqpp__mlirgen__function_hawaiian = "shirt",
Expand Down
2 changes: 1 addition & 1 deletion test/Quake/return_vector.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --add-dealloc --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --add-dealloc --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down
89 changes: 87 additions & 2 deletions test/Quake/to_cfg.qke
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

// RUN: cudaq-opt --lower-to-cfg %s | cudaq-opt | FileCheck %s

module {

func.func private @f1()
func.func private @f2()

Expand Down Expand Up @@ -300,4 +298,91 @@ func.func @scope_with_cf2() {
// CHECK: return
// CHECK: }


func.func @test_wired_through_if() {
%0 = quake.null_wire
%1 = quake.null_wire
%3 = cc.undef i1
%2:2 = cc.if (%3) ((%4 = %0, %5 = %1)) -> (!quake.wire, !quake.wire) {
%6:2 = quake.x [%4] %5 : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
cc.continue %6#1, %6#0 : !quake.wire, !quake.wire
} else {
%6:2 = quake.x [%4] %5 : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
cc.continue %6#0, %6#1 : !quake.wire, !quake.wire
}
quake.sink %2#0 : !quake.wire
quake.sink %2#1 : !quake.wire
return
}

// CHECK-LABEL: func.func @test_wired_through_if() {
// CHECK: %[[VAL_0:.*]] = quake.null_wire
// CHECK: %[[VAL_1:.*]] = quake.null_wire
// CHECK: %[[VAL_2:.*]] = cc.undef i1
// CHECK: cf.cond_br %[[VAL_2]], ^bb1(%[[VAL_0]], %[[VAL_1]] : !quake.wire, !quake.wire), ^bb2(%[[VAL_0]], %[[VAL_1]] : !quake.wire, !quake.wire)
// CHECK: ^bb1(%[[VAL_3:.*]]: !quake.wire, %[[VAL_4:.*]]: !quake.wire):
// CHECK: %[[VAL_5:.*]]:2 = quake.x {{\[}}%[[VAL_3]]] %[[VAL_4]] : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
// CHECK: cf.br ^bb3(%[[VAL_5]]#1, %[[VAL_5]]#0 : !quake.wire, !quake.wire)
// CHECK: ^bb2(%[[VAL_6:.*]]: !quake.wire, %[[VAL_7:.*]]: !quake.wire):
// CHECK: %[[VAL_8:.*]]:2 = quake.x {{\[}}%[[VAL_6]]] %[[VAL_7]] : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
// CHECK: cf.br ^bb3(%[[VAL_8]]#0, %[[VAL_8]]#1 : !quake.wire, !quake.wire)
// CHECK: ^bb3(%[[VAL_9:.*]]: !quake.wire, %[[VAL_10:.*]]: !quake.wire):
// CHECK: cf.br ^bb4
// CHECK: ^bb4:
// CHECK: quake.sink %[[VAL_9]] : !quake.wire
// CHECK: quake.sink %[[VAL_10]] : !quake.wire
// CHECK: return
// CHECK: }

func.func @test_wired_a_bit_loopy() {
%0 = quake.null_wire
%1 = quake.null_wire
%2:2 = cc.loop while ((%4 = %0, %5 = %1) -> (!quake.wire, !quake.wire)) {
%3 = cc.undef i1
cc.condition %3 (%4, %5 : !quake.wire, !quake.wire)
} do {
^bb1(%4: !quake.wire, %5: !quake.wire):
%7 = cc.undef i1
cf.cond_br %7, ^bb2(%4, %5 : !quake.wire, !quake.wire), ^bb3(%5, %4 : !quake.wire, !quake.wire)
^bb2(%14: !quake.wire, %15: !quake.wire):
%16:2 = quake.x [%14] %15 : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
cc.continue %16#1, %16#0 : !quake.wire, !quake.wire
^bb3(%24: !quake.wire, %25: !quake.wire):
%26:2 = quake.y [%24] %25 : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
cc.continue %26#1, %26#0 : !quake.wire, !quake.wire
} step {
^bb1(%4: !quake.wire, %5: !quake.wire):
%6:2 = quake.z [%4] %5 : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
cc.continue %6#0, %6#1 : !quake.wire, !quake.wire
}
quake.sink %2#0 : !quake.wire
quake.sink %2#1 : !quake.wire
return
}

// CHECK-LABEL: func.func @test_wired_a_bit_loopy() {
// CHECK: %[[VAL_0:.*]] = quake.null_wire
// CHECK: %[[VAL_1:.*]] = quake.null_wire
// CHECK: cf.br ^bb1(%[[VAL_0]], %[[VAL_1]] : !quake.wire, !quake.wire)
// CHECK: ^bb1(%[[VAL_2:.*]]: !quake.wire, %[[VAL_3:.*]]: !quake.wire):
// CHECK: %[[VAL_4:.*]] = cc.undef i1
// CHECK: cf.cond_br %[[VAL_4]], ^bb2(%[[VAL_2]], %[[VAL_3]] : !quake.wire, !quake.wire), ^bb6(%[[VAL_2]], %[[VAL_3]] : !quake.wire, !quake.wire)
// CHECK: ^bb2(%[[VAL_5:.*]]: !quake.wire, %[[VAL_6:.*]]: !quake.wire):
// CHECK: %[[VAL_7:.*]] = cc.undef i1
// CHECK: cf.cond_br %[[VAL_7]], ^bb3(%[[VAL_5]], %[[VAL_6]] : !quake.wire, !quake.wire), ^bb4(%[[VAL_6]], %[[VAL_5]] : !quake.wire, !quake.wire)
// CHECK: ^bb3(%[[VAL_8:.*]]: !quake.wire, %[[VAL_9:.*]]: !quake.wire):
// CHECK: %[[VAL_10:.*]]:2 = quake.x {{\[}}%[[VAL_8]]] %[[VAL_9]] : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
// CHECK: cf.br ^bb5(%[[VAL_10]]#1, %[[VAL_10]]#0 : !quake.wire, !quake.wire)
// CHECK: ^bb4(%[[VAL_11:.*]]: !quake.wire, %[[VAL_12:.*]]: !quake.wire):
// CHECK: %[[VAL_13:.*]]:2 = quake.y {{\[}}%[[VAL_11]]] %[[VAL_12]] : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
// CHECK: cf.br ^bb5(%[[VAL_13]]#1, %[[VAL_13]]#0 : !quake.wire, !quake.wire)
// CHECK: ^bb5(%[[VAL_14:.*]]: !quake.wire, %[[VAL_15:.*]]: !quake.wire):
// CHECK: %[[VAL_16:.*]]:2 = quake.z {{\[}}%[[VAL_14]]] %[[VAL_15]] : (!quake.wire, !quake.wire) -> (!quake.wire, !quake.wire)
// CHECK: cf.br ^bb1(%[[VAL_16]]#0, %[[VAL_16]]#1 : !quake.wire, !quake.wire)
// CHECK: ^bb6(%[[VAL_17:.*]]: !quake.wire, %[[VAL_18:.*]]: !quake.wire):
// CHECK: cf.br ^bb7
// CHECK: ^bb7:
// CHECK: quake.sink %[[VAL_17]] : !quake.wire
// CHECK: quake.sink %[[VAL_18]] : !quake.wire
// CHECK: return
// CHECK: }

0 comments on commit 23ac086

Please sign in to comment.