Skip to content

Commit

Permalink
[nvq++] Removes MLIR's scf dialect
Browse files Browse the repository at this point in the history
We don't use this dialect and its presence is a historical artifact.
This change triggered the removal of two tests:

* `test/Quake/ghz.qke`
* `test/Quake/iqft.qke`

Both tests are a reminder of a past when we had to write quantum kernels
directly in MLIR because of a lack of frontend. Both no longer test
aything useful.

The commit modifies `test/Quake/canonical-2.qke`, which was only testing
the canonicalization of `cc.scope` operations. The new form is removes
the clutter, making the test more precise.

`test/Translate/ghz.qke` had to be modified because it uses MLIR's
`affined.for` and its conversion to LLVMDialect requires `scf.for`.

Signed-off-by: boschmitt <[email protected]>
  • Loading branch information
boschmitt committed Nov 28, 2024
1 parent c9d0e4c commit 3500632
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 276 deletions.
2 changes: 0 additions & 2 deletions include/cudaq/Optimizer/InitAllDialects.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"

namespace cudaq {

Expand All @@ -35,7 +34,6 @@ inline void registerAllDialects(mlir::DialectRegistry &registry) {
mlir::LLVM::LLVMDialect,
mlir::math::MathDialect,
mlir::memref::MemRefDialect,
mlir::scf::SCFDialect,

// NVQ++ dialects
cudaq::cc::CCDialect,
Expand Down
1 change: 0 additions & 1 deletion lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "llvm/Support/Debug.h"
#include "mlir/Dialect/SCF/IR/SCF.h"

#define DEBUG_TYPE "lower-ast-expr"

Expand Down
1 change: 0 additions & 1 deletion lib/Optimizer/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ add_cudaq_library(OptCodeGen
MLIRFuncToLLVM
MLIRMathToFuncs
MLIRMathToLLVM
MLIRSCFToControlFlow

# Translation
MLIRTargetLLVMIRExport
Expand Down
2 changes: 0 additions & 2 deletions lib/Optimizer/CodeGen/ConvertCCToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/Arith/Transforms/Passes.h"
#include "mlir/Target/LLVMIR/TypeToLLVM.h"

Expand Down Expand Up @@ -121,7 +120,6 @@ struct CCToLLVM : public cudaq::opt::impl::CCToLLVMBase<CCToLLVM> {
arith::populateArithToLLVMConversionPatterns(ccTypeConverter, patterns);
populateMathToLLVMConversionPatterns(ccTypeConverter, patterns);

populateSCFToControlFlowConversionPatterns(patterns);
cf::populateControlFlowToLLVMConversionPatterns(ccTypeConverter, patterns);
populateFuncToLLVMConversionPatterns(ccTypeConverter, patterns);
cudaq::opt::populateCCToLLVMPatterns(ccTypeConverter, patterns);
Expand Down
2 changes: 0 additions & 2 deletions lib/Optimizer/CodeGen/ConvertToQIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/Arith/Transforms/Passes.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
Expand Down Expand Up @@ -172,7 +171,6 @@ class ConvertToQIR : public cudaq::opt::impl::ConvertToQIRBase<ConvertToQIR> {
arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
populateMathToLLVMConversionPatterns(typeConverter, patterns);

populateSCFToControlFlowConversionPatterns(patterns);
cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns);
populateFuncToLLVMConversionPatterns(typeConverter, patterns);
cudaq::opt::populateCCToLLVMPatterns(typeConverter, patterns);
Expand Down
6 changes: 0 additions & 6 deletions lib/Optimizer/Transforms/ApplyOpSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "cudaq/Todo.h"
#include "llvm/Support/Debug.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/Dominance.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/Transforms/DialectConversion.h"
Expand Down Expand Up @@ -558,11 +557,6 @@ class ApplySpecializationPass
invert(newIfOp.getElseRegion());
continue;
}
if (auto forOp = dyn_cast<scf::ForOp>(op)) {
LLVM_DEBUG(llvm::dbgs() << "moving for: " << forOp << ".\n");
TODO_loc(loc, "cannot make adjoint of kernel with scf.for");
// should we convert to cc.loop and use code below?
}
if (auto loopOp = dyn_cast<cudaq::cc::LoopOp>(op)) {
LLVM_DEBUG(llvm::dbgs() << "moving loop: " << loopOp << ".\n");
auto newLoopOp = cloneReversedLoop(builder, loopOp);
Expand Down
65 changes: 20 additions & 45 deletions test/Quake/canonical-2.qke
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,29 @@

// RUN: cudaq-opt -canonicalize %s | FileCheck %s

func.func @__nvqpp__mlirgen__reflect_about_uniform(%arg0: !quake.veq<?>) attributes {"cudaq-kernel"} {
%0 = quake.veq_size %arg0 : (!quake.veq<?>) -> i64
%c1_i32 = arith.constant 1 : i32
%1 = arith.extsi %c1_i32 : i32 to i64
%2 = arith.subi %0, %1 : i64
%c0_i64 = arith.constant 0 : i64
%c1_i64 = arith.constant 1 : i64
%3 = arith.subi %2, %c1_i64 : i64
%4 = quake.subveq %arg0, %c0_i64, %3 : (!quake.veq<?>, i64, i64) -> !quake.veq<?>
%5 = quake.veq_size %arg0 : (!quake.veq<?>) -> i64
%c1_i64_0 = arith.constant 1 : i64
%6 = arith.subi %5, %c1_i64_0 : i64
%7 = quake.extract_ref %arg0[%6] : (!quake.veq<?>,i64) -> !quake.ref
%8 = cc.create_lambda {
cc.scope {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%10 = quake.veq_size %arg0 : (!quake.veq<?>) -> i64
%11 = arith.index_cast %10 : i64 to index
scf.for %arg1 = %c0 to %11 step %c1 {
%12 = quake.extract_ref %arg0[%arg1] : (!quake.veq<?>,index) -> !quake.ref
quake.h %12 : (!quake.ref) -> ()
}
}
} : !cc.callable<() -> ()>
%9 = cc.create_lambda {
cc.scope {
quake.z [%4] %7 : (!quake.veq<?>, !quake.ref) -> ()
}
} : !cc.callable<() -> ()>
quake.compute_action %8, %9 : !cc.callable<() -> ()>, !cc.callable<() -> ()>
return
}
func.func @canonicalize_scope(%arg0: !quake.ref) attributes {"cudaq-kernel"} {
%0 = cc.create_lambda {
cc.scope {
quake.h %arg0 : (!quake.ref) -> ()
}
} : !cc.callable<() -> ()>
%1 = cc.create_lambda {
cc.scope {
quake.z %arg0 : (!quake.ref) -> ()
}
} : !cc.callable<() -> ()>
quake.compute_action %0, %1 : !cc.callable<() -> ()>, !cc.callable<() -> ()>
return
}

// CHECK-LABEL: func.func @__nvqpp__mlirgen__reflect_about_uniform(
// CHECK: %[[VAL_12:.*]] = cc.create_lambda {
// CHECK-LABEL: func.func @canonicalize_scope(
// CHECK: %[[VAL_0:.*]] = cc.create_lambda {
// CHECK-NOT: cc.scope
// CHECK: %[[VAL_13:.*]] = quake.veq_size %{{.*}} : (!quake.veq<?>) -> i64
// CHECK: %[[VAL_14:.*]] = arith.index_cast %[[VAL_13]] : i64 to index
// CHECK: scf.for %[[VAL_15:.*]] = %{{.*}} to %[[VAL_14]] step %
// CHECK: %[[VAL_16:.*]] = quake.extract_ref
// CHECK: quake.h %[[VAL_16]]
// CHECK: }
// CHECK: quake.h %{{.*}} :
// CHECK: } : !cc.callable<() -> ()>
// CHECK: %[[VAL_17:.*]] = cc.create_lambda {
// CHECK: %[[VAL_1:.*]] = cc.create_lambda {
// CHECK-NOT: cc.scope
// CHECK: quake.z [%{{.*}}] %{{.*}} :
// CHECK: quake.z %{{.*}} :
// CHECK: } : !cc.callable<() -> ()>
// CHECK: quake.compute_action
// CHECK: return
// CHECK: quake.compute_action %[[VAL_0]], %[[VAL_1]]

45 changes: 0 additions & 45 deletions test/Quake/ghz.qke

This file was deleted.

138 changes: 0 additions & 138 deletions test/Quake/iqft.qke

This file was deleted.

Loading

0 comments on commit 3500632

Please sign in to comment.