Skip to content

Commit

Permalink
Add the ability to pass unrecognized command line options from the
Browse files Browse the repository at this point in the history
driver into cudaq-quake. This change will enable addressing a number of
bugs relating to features that the driver script isn't enabled for yet.

Add code to handle target-specific options.

Fix #535: add a version flag for the nvq++ version.

clang-format

Review comments.

Fix infinite loop bug.
  • Loading branch information
schweitzpgi committed Sep 13, 2023
1 parent d8e1f21 commit 5499ace
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,4 @@ if (CUDAQ_BUILD_SELFCONTAINED)
add_target_libs_to_wheel(${LIB_SO_FILE} ${LIB_CONFIG_FILE})
endwhile()
endif()
endif()
endif()
1 change: 1 addition & 0 deletions include/cudaq/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
# ============================================================================ #

add_subdirectory(Optimizer)
add_subdirectory(Support)
13 changes: 13 additions & 0 deletions include/cudaq/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ============================================================================ #
# Copyright (c) 2022 - 2023 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. #
# ============================================================================ #

execute_process(COMMAND git describe --tags --abbrev=0 --dirty=-developer
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE CUDA_QUANTUM_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)

configure_file("Version.h.in" "Version.h" @ONLY)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

namespace cudaq {

// TODO: const char *getVersion();
namespace internal {
constexpr const char version[] = "@CUDA_QUANTUM_VERSION@";
}

/// Get the CUDA Quantum version.
const char *getVersion() { return internal::version; }

/// A generic bug report message.
constexpr const char *bugReportMsg =
Expand Down
10 changes: 6 additions & 4 deletions runtime/cudaq/platform/default/rest/helpers/ionq/ionq.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ IONQ_MACHINE="simulator"
IONQ_NOISE_MODEL=""

PLATFORM_EXTRA_ARGS=""
while [ $# -ne 0 ]; do
# NB: extra arguments always take the form:
# --<target>-<option> <value>
# as in
# --ionq-machine foo
while [ $# -gt 1 ]; do
case "$1" in
--ionq-machine)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;qpu;$2"
IONQ_MACHINE="$2"
shift
;;
--ionq-noise-model)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;noise;$2"
IONQ_NOISE_MODEL="$2"
shift
;;
esac
shift
shift 2
done

# Validate the settings
Expand Down
10 changes: 6 additions & 4 deletions runtime/cudaq/platform/default/rest/helpers/iqm/iqm.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ CODEGEN_EMISSION=iqm
LIBRARY_MODE=false

PLATFORM_EXTRA_ARGS=""
while [ $# -ne 0 ]; do
# NB: extra arguments always take the form:
# --<target>-<option> <value>
# as in
# --iqm-machine foo
while [ $# -gt 1 ]; do
case "$1" in
--iqm-server-url)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;url;$2"
shift
;;
--iqm-machine)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;qpu-architecture;$2"
shift
;;
esac
shift
shift 2
done
5 changes: 5 additions & 0 deletions runtime/cudaq/platform/default/rest/helpers/oqc/oqc.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ CODEGEN_EMISSION=qir
# and it is the default, physical backends must
# turn this off
LIBRARY_MODE=false

# NB: extra arguments always take the form:
# --<target>-<option> <value>
# as in
# --oqc-machine foo
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ CODEGEN_EMISSION=qir
LIBRARY_MODE=false

PLATFORM_EXTRA_ARGS=""
while [ $# -ne 0 ]; do
# NB: extra arguments always take the form:
# --<target>-<option> <value>
# as in
# --quantinuum-machine foo
while [ $# -gt 1 ]; do
case "$1" in
--quantinuum-url)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;url;$2"
shift
;;
--quantinuum-machine)
PLATFORM_EXTRA_ARGS="$PLATFORM_EXTRA_ARGS;machine;$2"
shift
;;
esac
shift
shift 2
done
20 changes: 20 additions & 0 deletions test/AST-error/wall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2022 - 2023 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. *
******************************************************************************/

// RUN: cudaq-quake -Xcudaq -Wall -verify %s -o /dev/null

#include <cudaq.h>

void foo();

struct S {
void operator()() __qpu__ {
int unused = 42; // expected-warning{{unused variable}}
foo();
}
};
14 changes: 14 additions & 0 deletions tools/cudaq-quake/cudaq-quake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ static cl::opt<bool> noSimplify(
static cl::opt<bool> astDump("ast-dump", cl::desc("Dump the ast."),
cl::init(false));

static cl::opt<bool> showVersion("nvqpp-version",
cl::desc("Print the version."),
cl::init(false));

static cl::opt<bool> verboseClang("v",
cl::desc("Add -v to clang tool arguments."),
cl::init(false));
Expand All @@ -104,6 +108,9 @@ static cl::list<std::string> includePath("I", cl::desc("Include file path."));
static cl::list<std::string>
systemIncludePath("J", cl::desc("System include file path."));

static cl::list<std::string>
extraClangArgs("Xcudaq", cl::desc("Extra options to pass to clang++"));

inline bool isStdinInput(StringRef str) { return str == "-"; }

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -334,6 +341,10 @@ int main(int argc, char **argv) {
<< '\n';
});

if (showVersion) {
llvm::errs() << "nvq++ Version " << cudaq::getVersion() << '\n';
}

// Process arguments.
std::vector<std::string> clArgs = {"-std=c++20", "-resource-dir",
resourceDirPath.string()};
Expand Down Expand Up @@ -407,6 +418,9 @@ int main(int argc, char **argv) {
clArgs.push_back("-ast-dump");
}

for (auto &xtra : extraClangArgs)
clArgs.push_back(xtra);

// Allow a user to specify extra args for clang via
// an environment variable.
if (auto extraArgs = std::getenv("CUDAQ_CLANG_EXTRA_ARGS")) {
Expand Down
43 changes: 34 additions & 9 deletions tools/nvqpp/nvq++.in
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ OUTPUTFILE=
OBJS=
SRCS=
ARGS=
CUDAQ_QUAKE_ARGS=
DO_LINK=true
ENABLE_DEVICE_CODE_LOADERS=true
ENABLE_KERNEL_EXECUTION=true
Expand All @@ -232,7 +233,7 @@ EMIT_QIR=false
PREPROCESSOR_DEFINES=
CUDAQ_QUAKE_DEBUG=
SHOW_HELP=false
LIST_TARGETS=false
LIST_TARGETS=false

# We default to LIBRARY_MODE, physical
# Quantum Targets can override this to turn on
Expand All @@ -242,13 +243,32 @@ LIBRARY_MODE=true
CXX=${LLVMBIN}clang++${llvm_suffix}
LINKER_CXX=${CXX}
CMAKE_FALLBACK_HOST_CXX=${CXX}
COPY_INPUT_ARGS=$@
TARGET_ARGS=$@

if [[ $# -eq 0 ]]; then
SHOW_HELP=true
fi

# First, find the target option. The name is needed since it is the prefix for
# any target specific options.
N=$#
for (( i=1; i<=$N; i++ )); do
if [[ ${@:$i:1} == --target ]] || [[ ${@:$i:1} == -target ]]; then
TARGET_CONFIG="${@:$i+1:1}"
break
fi
done

while [ $# -ne 0 ]; do
# Filter out any target-specific options.
if [[-n "${TARGET_CONFIG}"]] && [[ "$1" == --${TARGET_CONFIG}-* ]]; then
# Assume always has an additional value
TARGET_ARGS="${TARGET_ARGS} $1 $2"
shift 2
continue
fi

# Otherwise, process the argument.
case "$1" in
--llvm-version)
llvm_suffix="$2"
Expand All @@ -259,7 +279,6 @@ while [ $# -ne 0 ]; do
shift
;;
--target | -target)
TARGET_CONFIG="$2"
shift
;;
--emit-qir | -emit-qir)
Expand Down Expand Up @@ -364,19 +383,25 @@ while [ $# -ne 0 ]; do
-l*)
LINKLIBS="${LINKLIBS} $1"
;;
--version)
CUDAQ_QUAKE_ARGS="--nvqpp-version ${CUDAQ_QUAKE_ARGS}"
;;
-v)
ECHO=true
;;
*.o)
*.o | *.so | *.bundle)
OBJS="${OBJS} $1"
;;
*.cpp | *.cc)
SRCS="${SRCS} $1"
;;
*.a | *.dylib)
LINKLIBS="${LINKLIBS} $1"
;;
*)
#FIXME: issue #258
#echo "argument $1 is ignored" >&2
# Pass any unrecognized options on to the clang++ tool.
ARGS="${ARGS} $1"
CUDAQ_QUAKE_ARGS="${CUDAQ_QUAKE_ARGS} --Xcudaq $1"
;;
esac
shift
Expand Down Expand Up @@ -408,7 +433,7 @@ if [ -n "${TARGET_CONFIG}" ]; then
TARGET_CONFIG_FILE="${install_dir}/targets/${TARGET_CONFIG}.config"
if [ -f "${TARGET_CONFIG_FILE}" ]; then
GEN_TARGET_BACKEND=false
. "${TARGET_CONFIG_FILE}" ${COPY_INPUT_ARGS}
. "${TARGET_CONFIG_FILE}" ${TARGET_ARGS}

if ${GEN_TARGET_BACKEND}; then
# Add a function that will run before main
Expand Down Expand Up @@ -486,7 +511,7 @@ for i in ${SRCS}; do
# If LIBRARY_MODE explicitly requested, then
# simply compile with the classical compiler.
if ${LIBRARY_MODE}; then
run ${CMAKE_FALLBACK_HOST_CXX} ${COMPILER_FLAGS} -DCUDAQ_LIBRARY_MODE ${PREPROCESSOR_DEFINES} ${INCLUDES} -o ${file}.o -c $i
run ${CMAKE_FALLBACK_HOST_CXX} ${COMPILER_FLAGS} -DCUDAQ_LIBRARY_MODE ${PREPROCESSOR_DEFINES} ${INCLUDES} ${ARGS} -o ${file}.o -c $i
OBJS="${OBJS} ${file}.o"
# Go to the next iteration, maybe there
# will be cudaq kernels there
Expand All @@ -495,7 +520,7 @@ for i in ${SRCS}; do

# If we make it here, we have CUDA Quantum kernels, need
# to map to MLIR and output an LLVM file for the classical code
run ${TOOLBIN}cudaq-quake ${CUDAQ_QUAKE_DEBUG} ${CLANG_VERBOSE} ${CLANG_RESOURCE_DIR} ${PREPROCESSOR_DEFINES} ${INCLUDES} --emit-llvm-file $i -o ${file}.qke
run ${TOOLBIN}cudaq-quake ${CUDAQ_QUAKE_DEBUG} ${CLANG_VERBOSE} ${CLANG_RESOURCE_DIR} ${PREPROCESSOR_DEFINES} ${INCLUDES} ${CUDAQ_QUAKE_ARGS} --emit-llvm-file $i -o ${file}.qke
TMPFILES="${TMPFILES} ${file}.ll ${file}.qke"

# Run the MLIR passes
Expand Down

0 comments on commit 5499ace

Please sign in to comment.