From 9db38a54fe16d0a8b3089a93c87207df1261f3ef Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Wed, 21 Aug 2024 18:01:19 +0000 Subject: [PATCH 1/8] Add compiler optimization test target --- include/cudaq/Support/TargetConfig.h | 2 ++ lib/Support/Config/TargetConfig.cpp | 5 +++++ runtime/cudaq/platform/default/CMakeLists.txt | 2 ++ runtime/cudaq/platform/default/opt-test.yml | 16 ++++++++++++++++ tools/nvqpp/nvq++.in | 8 ++++++++ 5 files changed, 33 insertions(+) create mode 100644 runtime/cudaq/platform/default/opt-test.yml diff --git a/include/cudaq/Support/TargetConfig.h b/include/cudaq/Support/TargetConfig.h index dc2097912e..30f0b00ecc 100644 --- a/include/cudaq/Support/TargetConfig.h +++ b/include/cudaq/Support/TargetConfig.h @@ -69,6 +69,8 @@ struct BackendEndConfigEntry { std::optional LibraryMode; /// IR lowering configuration (hardware REST QPU) std::string PlatformLoweringConfig; + /// Exact cudaq-opt passes for pseudo-targets + std::string TargetPassPipeline; /// Codegen emission configuration (hardware REST QPU) std::string CodegenEmission; /// Post code generation IR passes configuration (hardware REST QPU) diff --git a/lib/Support/Config/TargetConfig.cpp b/lib/Support/Config/TargetConfig.cpp index a75595b655..f4116abb2c 100644 --- a/lib/Support/Config/TargetConfig.cpp +++ b/lib/Support/Config/TargetConfig.cpp @@ -52,6 +52,10 @@ static std::string processSimBackendConfig( output << "PLATFORM_LOWERING_CONFIG=\"" << configValue.PlatformLoweringConfig << "\"\n"; + if (!configValue.TargetPassPipeline.empty()) + output << "TARGET_PASS_PIPELINE=\"" + << configValue.TargetPassPipeline << "\"\n"; + if (!configValue.CodegenEmission.empty()) output << "CODEGEN_EMISSION=" << configValue.CodegenEmission << "\n"; @@ -302,6 +306,7 @@ void MappingTraits::mapping( io.mapOptional("gen-target-backend", info.GenTargetBackend); io.mapOptional("library-mode", info.LibraryMode); io.mapOptional("platform-lowering-config", info.PlatformLoweringConfig); + io.mapOptional("target-pass-pipeline", info.TargetPassPipeline); io.mapOptional("codegen-emission", info.CodegenEmission); io.mapOptional("post-codegen-passes", info.PostCodeGenPasses); io.mapOptional("platform-library", info.PlatformLibrary); diff --git a/runtime/cudaq/platform/default/CMakeLists.txt b/runtime/cudaq/platform/default/CMakeLists.txt index 39ff674cb9..98bfd4be74 100644 --- a/runtime/cudaq/platform/default/CMakeLists.txt +++ b/runtime/cudaq/platform/default/CMakeLists.txt @@ -42,6 +42,8 @@ if (OPENSSL_FOUND AND CUDAQ_ENABLE_REST) add_subdirectory(rest) add_subdirectory(rest_server) endif() + +add_target_config(opt-test) if (CUSTATEVEC_ROOT AND CUDA_FOUND) add_target_config(nvidia) diff --git a/runtime/cudaq/platform/default/opt-test.yml b/runtime/cudaq/platform/default/opt-test.yml new file mode 100644 index 0000000000..d7483aa40f --- /dev/null +++ b/runtime/cudaq/platform/default/opt-test.yml @@ -0,0 +1,16 @@ +# ============================================================================ # +# Copyright (c) 2022 - 2024 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. # +# ============================================================================ # + +name: opt-test +description: "Compiler Optimization Test Target" +gpu-requirements: true +config: + nvqir-simulation-backend: cusvsim-fp32, custatevec-fp32 + preprocessor-defines: ["-D CUDAQ_SIMULATION_SCALAR_FP32"] + target-pass-pipeline: "func.func(unwind-lowering),canonicalize,lambda-lifting,func.func(memtoreg{quantum=0}),canonicalize,apply-op-specialization,kernel-execution,aggressive-early-inlining,func.func(quake-add-metadata),const-prop-complex,lift-array-value,func.func(get-concrete-matrix),device-code-loader{use-quake=1},canonicalize,cse,func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce" + library-mode: false diff --git a/tools/nvqpp/nvq++.in b/tools/nvqpp/nvq++.in index 98368cdf8e..9b00359a98 100644 --- a/tools/nvqpp/nvq++.in +++ b/tools/nvqpp/nvq++.in @@ -707,6 +707,14 @@ if ${RUN_OPT}; then OPT_PASSES=$(add_pass_to_pipeline "${OPT_PASSES}" "canonicalize,cse") fi +# If TARGET_PASS_PIPELINE is set, then use that exact pipeline (while still +# allowing command-line additions like normal). +if [ ! -z "$TARGET_PASS_PIPELINE" ]; then + RUN_OPT=true + # Don't use add_pass_to_pipeline here. + OPT_PASSES="$TARGET_PASS_PIPELINE" +fi + if [ ! -z "$CUDAQ_OPT_EXTRA_PASSES" ]; then OPT_PASSES=$(add_pass_to_pipeline "${OPT_PASSES}" "$CUDAQ_OPT_EXTRA_PASSES") fi From 81d9699ad709ff0ae2a795a72f0ae6034e637836 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Wed, 21 Aug 2024 21:14:43 +0000 Subject: [PATCH 2/8] Update opt-test to support multiple simulators --- include/cudaq/Support/TargetConfig.h | 2 ++ lib/Support/Config/TargetConfig.cpp | 4 ++- runtime/cudaq/platform/default/opt-test.yml | 35 ++++++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/cudaq/Support/TargetConfig.h b/include/cudaq/Support/TargetConfig.h index 30f0b00ecc..d49a661e99 100644 --- a/include/cudaq/Support/TargetConfig.h +++ b/include/cudaq/Support/TargetConfig.h @@ -24,6 +24,8 @@ enum TargetFeatureFlag : unsigned { flagsFP64 = 0x0002, flagsMgpu = 0x0004, flagsMqpu = 0x0008, + flagsDepAnalysis = 0x0010, + flagsQPP = 0x0020, }; /// @brief Configuration argument type annotation diff --git a/lib/Support/Config/TargetConfig.cpp b/lib/Support/Config/TargetConfig.cpp index f4116abb2c..797b1eecce 100644 --- a/lib/Support/Config/TargetConfig.cpp +++ b/lib/Support/Config/TargetConfig.cpp @@ -32,7 +32,9 @@ static std::unordered_map stringToFeatureFlag{{"fp32", cudaq::config::flagsFP32}, {"fp64", cudaq::config::flagsFP64}, {"mgpu", cudaq::config::flagsMgpu}, - {"mqpu", cudaq::config::flagsMqpu}}; + {"mqpu", cudaq::config::flagsMqpu}, + {"dep-analysis", cudaq::config::flagsDepAnalysis}, + {"qpp", cudaq::config::flagsQPP}}; } /// @brief Convert the backend config entry into nvq++ compatible script. diff --git a/runtime/cudaq/platform/default/opt-test.yml b/runtime/cudaq/platform/default/opt-test.yml index d7483aa40f..d5bd0e347b 100644 --- a/runtime/cudaq/platform/default/opt-test.yml +++ b/runtime/cudaq/platform/default/opt-test.yml @@ -9,8 +9,33 @@ name: opt-test description: "Compiler Optimization Test Target" gpu-requirements: true -config: - nvqir-simulation-backend: cusvsim-fp32, custatevec-fp32 - preprocessor-defines: ["-D CUDAQ_SIMULATION_SCALAR_FP32"] - target-pass-pipeline: "func.func(unwind-lowering),canonicalize,lambda-lifting,func.func(memtoreg{quantum=0}),canonicalize,apply-op-specialization,kernel-execution,aggressive-early-inlining,func.func(quake-add-metadata),const-prop-complex,lift-array-value,func.func(get-concrete-matrix),device-code-loader{use-quake=1},canonicalize,cse,func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce" - library-mode: false + +target-arguments: + - key: option + required: false + type: option-flags + help-string: "Specify the target options as a comma-separated list.\nSupported options are 'dep-analysis, 'fp32', 'fp64', 'qpp'.\nFor example, the 'dep-analysis,fp32' option combination will activate single-precision simulation with the dep-analysis passes. Not all option combinations are supported." + +configuration-matrix: + - name: dep-analysis-fp32 + option-flags: [dep-analysis, fp32] + default: true + config: + nvqir-simulation-backend: cusvsim-fp32, custatevec-fp32 + preprocessor-defines: ["-D CUDAQ_SIMULATION_SCALAR_FP32"] + target-pass-pipeline: "func.func(unwind-lowering),canonicalize,lambda-lifting,func.func(memtoreg{quantum=0}),canonicalize,apply-op-specialization,kernel-execution,aggressive-early-inlining,func.func(quake-add-metadata),const-prop-complex,lift-array-value,func.func(get-concrete-matrix),device-code-loader{use-quake=1},canonicalize,cse,func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce" + library-mode: false + - name: dep-analysis-fp64 + option-flags: [dep-analysis, fp64] + config: + nvqir-simulation-backend: cusvsim-fp64, custatevec-fp64 + preprocessor-defines: ["-D CUDAQ_SIMULATION_SCALAR_FP64"] + target-pass-pipeline: "func.func(unwind-lowering),canonicalize,lambda-lifting,func.func(memtoreg{quantum=0}),canonicalize,apply-op-specialization,kernel-execution,aggressive-early-inlining,func.func(quake-add-metadata),const-prop-complex,lift-array-value,func.func(get-concrete-matrix),device-code-loader{use-quake=1},canonicalize,cse,func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce" + library-mode: false + - name: dep-analysis-qpp + option-flags: [dep-analysis, qpp] + config: + nvqir-simulation-backend: qpp + preprocessor-defines: ["-D CUDAQ_SIMULATION_SCALAR_FP64"] + target-pass-pipeline: "func.func(unwind-lowering),canonicalize,lambda-lifting,func.func(memtoreg{quantum=0}),canonicalize,apply-op-specialization,kernel-execution,aggressive-early-inlining,func.func(quake-add-metadata),const-prop-complex,lift-array-value,func.func(get-concrete-matrix),device-code-loader{use-quake=1},canonicalize,cse,func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce" + library-mode: false From ebed9cdfb060054ed345b9301aad4eef43a35043 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Thu, 22 Aug 2024 19:54:27 +0000 Subject: [PATCH 3/8] Use opt-test in C++ tests --- targettests/execution/qubit_management/if-1.cpp | 2 +- targettests/execution/qubit_management/if-2.cpp | 5 +++-- targettests/execution/qubit_management/if-3.cpp | 2 +- targettests/execution/qubit_management/if-classical.cpp | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/targettests/execution/qubit_management/if-1.cpp b/targettests/execution/qubit_management/if-1.cpp index e8bee89daa..7062d1d8e6 100644 --- a/targettests/execution/qubit_management/if-1.cpp +++ b/targettests/execution/qubit_management/if-1.cpp @@ -11,7 +11,7 @@ // TODO: filecheck with statistics // TODO: should work properly with regtomem fixes -// RUN: nvq++ --enable-mlir -fno-lower-to-cfg --opt-pass 'func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce' %s -o %t && %t +// RUN: nvq++ --target opt-test %s -o %t && %t // XFAIL: * // Simple test, shouldn't affect anything diff --git a/targettests/execution/qubit_management/if-2.cpp b/targettests/execution/qubit_management/if-2.cpp index b2b9501e1d..01667260f7 100644 --- a/targettests/execution/qubit_management/if-2.cpp +++ b/targettests/execution/qubit_management/if-2.cpp @@ -8,8 +8,9 @@ #include -// RUN: nvq++ --enable-mlir -fno-lower-to-cfg --opt-pass 'func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce' %s -o %t && %t -// XFAIL: * +// RUN: nvq++ --target opt-test %s -o %t && %t +// This passes but should x-fail (FIXME!) +// X_FAIL: * struct run_test { __qpu__ auto operator()() { diff --git a/targettests/execution/qubit_management/if-3.cpp b/targettests/execution/qubit_management/if-3.cpp index b61235bc5a..1e6e0adcdb 100644 --- a/targettests/execution/qubit_management/if-3.cpp +++ b/targettests/execution/qubit_management/if-3.cpp @@ -8,7 +8,7 @@ #include -// RUN: nvq++ --enable-mlir -fno-lower-to-cfg --opt-pass 'func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce' %s -o %t && %t +// RUN: nvq++ --target opt-test %s -o %t && %t // XFAIL: * struct run_test { diff --git a/targettests/execution/qubit_management/if-classical.cpp b/targettests/execution/qubit_management/if-classical.cpp index fc17ea030d..9d8972a009 100644 --- a/targettests/execution/qubit_management/if-classical.cpp +++ b/targettests/execution/qubit_management/if-classical.cpp @@ -8,7 +8,7 @@ #include -// RUN: nvq++ --enable-mlir -fno-lower-to-cfg --opt-pass 'func.func(add-dealloc,combine-quantum-alloc,canonicalize,factor-quantum-alloc,memtoreg),canonicalize,cse,add-wireset,func.func(assign-wire-indices),dep-analysis,func.func(regtomem),symbol-dce' %s -o %t && %t +// RUN: nvq++ --target opt-test %s -o %t && %t // XFAIL: * struct run_test { From b45c8bea2308bee2d0d50eeb7e0bccf183849773 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Thu, 22 Aug 2024 21:37:21 +0000 Subject: [PATCH 4/8] Use --target-option dep-analysis,qpp --- targettests/execution/qubit_management/if-1.cpp | 2 +- targettests/execution/qubit_management/if-2.cpp | 2 +- targettests/execution/qubit_management/if-3.cpp | 2 +- targettests/execution/qubit_management/if-classical.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/targettests/execution/qubit_management/if-1.cpp b/targettests/execution/qubit_management/if-1.cpp index 7062d1d8e6..28772af830 100644 --- a/targettests/execution/qubit_management/if-1.cpp +++ b/targettests/execution/qubit_management/if-1.cpp @@ -11,7 +11,7 @@ // TODO: filecheck with statistics // TODO: should work properly with regtomem fixes -// RUN: nvq++ --target opt-test %s -o %t && %t +// RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t // XFAIL: * // Simple test, shouldn't affect anything diff --git a/targettests/execution/qubit_management/if-2.cpp b/targettests/execution/qubit_management/if-2.cpp index 01667260f7..db8455330f 100644 --- a/targettests/execution/qubit_management/if-2.cpp +++ b/targettests/execution/qubit_management/if-2.cpp @@ -8,7 +8,7 @@ #include -// RUN: nvq++ --target opt-test %s -o %t && %t +// RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t // This passes but should x-fail (FIXME!) // X_FAIL: * diff --git a/targettests/execution/qubit_management/if-3.cpp b/targettests/execution/qubit_management/if-3.cpp index 1e6e0adcdb..b9ffe84567 100644 --- a/targettests/execution/qubit_management/if-3.cpp +++ b/targettests/execution/qubit_management/if-3.cpp @@ -8,7 +8,7 @@ #include -// RUN: nvq++ --target opt-test %s -o %t && %t +// RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t // XFAIL: * struct run_test { diff --git a/targettests/execution/qubit_management/if-classical.cpp b/targettests/execution/qubit_management/if-classical.cpp index 9d8972a009..59ade181c1 100644 --- a/targettests/execution/qubit_management/if-classical.cpp +++ b/targettests/execution/qubit_management/if-classical.cpp @@ -8,7 +8,7 @@ #include -// RUN: nvq++ --target opt-test %s -o %t && %t +// RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t // XFAIL: * struct run_test { From 4d13b5611296cbc545e6fd7b1dd65665005d372b Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Fri, 23 Aug 2024 13:05:35 +0000 Subject: [PATCH 5/8] Remove gpu-requirements from test target --- runtime/cudaq/platform/default/opt-test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/cudaq/platform/default/opt-test.yml b/runtime/cudaq/platform/default/opt-test.yml index d5bd0e347b..caa1532c53 100644 --- a/runtime/cudaq/platform/default/opt-test.yml +++ b/runtime/cudaq/platform/default/opt-test.yml @@ -8,7 +8,6 @@ name: opt-test description: "Compiler Optimization Test Target" -gpu-requirements: true target-arguments: - key: option From 71ace1d356841e2a8d9f3383265bae8ba2b4a0f6 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Fri, 23 Aug 2024 13:10:27 +0000 Subject: [PATCH 6/8] clang-format --- lib/Support/Config/TargetConfig.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Support/Config/TargetConfig.cpp b/lib/Support/Config/TargetConfig.cpp index 797b1eecce..0814f3f846 100644 --- a/lib/Support/Config/TargetConfig.cpp +++ b/lib/Support/Config/TargetConfig.cpp @@ -55,8 +55,8 @@ static std::string processSimBackendConfig( << configValue.PlatformLoweringConfig << "\"\n"; if (!configValue.TargetPassPipeline.empty()) - output << "TARGET_PASS_PIPELINE=\"" - << configValue.TargetPassPipeline << "\"\n"; + output << "TARGET_PASS_PIPELINE=\"" << configValue.TargetPassPipeline + << "\"\n"; if (!configValue.CodegenEmission.empty()) output << "CODEGEN_EMISSION=" << configValue.CodegenEmission << "\n"; From 48cb2b7f6431edde2206aa731156f7781c431b8a Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Fri, 23 Aug 2024 16:38:02 +0000 Subject: [PATCH 7/8] Update validate_container.sh script to skip opt-test target --- scripts/validate_container.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/validate_container.sh b/scripts/validate_container.sh index 2f5e17241c..32945e1130 100644 --- a/scripts/validate_container.sh +++ b/scripts/validate_container.sh @@ -70,6 +70,10 @@ available_backends=`\ if grep -q "library-mode-execution-manager: photonics" $file ; then continue fi + # Skip optimization test targets + if [[ $file == *"opt-test.yml" ]]; then + continue + fi platform=$(cat $file | grep "platform-qpu:") qpu=${platform##* } requirements=$(cat $file | grep "gpu-requirements:") From b4eefbd7b1c700a11942cd9d03cdc5cef6682915 Mon Sep 17 00:00:00 2001 From: Ben Howe Date: Mon, 26 Aug 2024 18:47:18 +0000 Subject: [PATCH 8/8] Update test pass/fail conditions after regtomem updates --- targettests/execution/qubit_management/if-1.cpp | 2 -- targettests/execution/qubit_management/if-2.cpp | 2 -- 2 files changed, 4 deletions(-) diff --git a/targettests/execution/qubit_management/if-1.cpp b/targettests/execution/qubit_management/if-1.cpp index 28772af830..df83599c18 100644 --- a/targettests/execution/qubit_management/if-1.cpp +++ b/targettests/execution/qubit_management/if-1.cpp @@ -9,10 +9,8 @@ #include // TODO: filecheck with statistics -// TODO: should work properly with regtomem fixes // RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t -// XFAIL: * // Simple test, shouldn't affect anything struct run_test { diff --git a/targettests/execution/qubit_management/if-2.cpp b/targettests/execution/qubit_management/if-2.cpp index db8455330f..b9824b1c46 100644 --- a/targettests/execution/qubit_management/if-2.cpp +++ b/targettests/execution/qubit_management/if-2.cpp @@ -9,8 +9,6 @@ #include // RUN: nvq++ --target opt-test --target-option dep-analysis,qpp %s -o %t && %t -// This passes but should x-fail (FIXME!) -// X_FAIL: * struct run_test { __qpu__ auto operator()() {