Skip to content

Commit

Permalink
Add another new executable test. Not fully enabled until bugs in
Browse files Browse the repository at this point in the history
the thunk unpacking code are fixed.

Move functions to static functions.

Add handling of std::vector<bool>. std::vector<bool> is a class that
is distinct from all other std::vector<T> and it needs to be handled
with special code on the host side. (On the device side, it is forced
to look like any other span.)

Signed-off-by: Eric Schweitz <[email protected]>
  • Loading branch information
schweitzpgi committed Nov 6, 2024
1 parent 06c57c7 commit c69b593
Show file tree
Hide file tree
Showing 11 changed files with 1,297 additions and 829 deletions.
5 changes: 5 additions & 0 deletions include/cudaq/Optimizer/Builder/Intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ static constexpr const char getCudaqSizeFromTriple[] =
// typically specialized to be bit packed).
static constexpr const char stdvecBoolCtorFromInitList[] =
"__nvqpp_initializer_list_to_vector_bool";

// Convert a (likely packed) std::vector<bool> into a sequence of bytes, each
// holding a boolean value.
static constexpr const char stdvecBoolUnpackToInitList[] =
"__nvqpp_vector_bool_to_initializer_list";

// Free any temporary buffers used to hold std::vector<bool> data.
static constexpr const char stdvecBoolFreeTemporaryLists[] =
"__nvqpp_vector_bool_free_temporary_initlists";

// The internal data of the cudaq::state object must be `2**n` in length. This
// function returns the value `n`.
static constexpr const char getNumQubitsFromCudaqState[] =
Expand Down
19 changes: 17 additions & 2 deletions lib/Optimizer/Builder/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ cc::StructType factory::stlVectorType(Type eleTy) {
return cc::StructType::get(ctx, ArrayRef<Type>{ptrTy, ptrTy, ptrTy});
}

// Note that this is the raw host type, where std::vector<bool> is distinct.
// When converting to the device side, the distinction is deliberately removed
// making std::vector<bool> the same format as std::vector<char>.
static cc::StructType stlHostVectorType(Type eleTy) {
MLIRContext *ctx = eleTy.getContext();
if (eleTy != IntegerType::get(ctx, 1)) {
// std::vector<T> where T != bool.
return factory::stlVectorType(eleTy);
}
// std::vector<bool> is a different type than std::vector<T>.
auto ptrTy = cc::PointerType::get(eleTy);
auto i8Ty = IntegerType::get(ctx, 8);
auto padout = cc::ArrayType::get(ctx, i8Ty, 32);
return cc::StructType::get(ctx, ArrayRef<Type>{ptrTy, padout});
}

// FIXME: Give these front-end names so we can disambiguate more types.
cc::StructType factory::getDynamicBufferType(MLIRContext *ctx) {
auto ptrTy = cc::PointerType::get(IntegerType::get(ctx, 8));
Expand All @@ -344,8 +360,7 @@ Type factory::getSRetElementType(FunctionType funcTy) {

Type factory::convertToHostSideType(Type ty) {
if (auto memrefTy = dyn_cast<cc::StdvecType>(ty))
return factory::stlVectorType(
convertToHostSideType(memrefTy.getElementType()));
return stlHostVectorType(convertToHostSideType(memrefTy.getElementType()));
if (isa<cc::IndirectCallableType>(ty))
return cc::PointerType::get(IntegerType::get(ty.getContext(), 8));
if (isa<cc::CharspanType>(ty))
Expand Down
8 changes: 7 additions & 1 deletion lib/Optimizer/Builder/Intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,17 @@ static constexpr IntrinsicCode intrinsicTable[] = {
return %0 : !cc.ptr<i8>
})#"},

// __nvqpp_vector_bool_free_temporary_lists
{cudaq::stdvecBoolFreeTemporaryLists,
{},
R"#(
func.func private @__nvqpp_vector_bool_free_temporary_initlists(!cc.ptr<i8>) -> ())#"},

// __nvqpp_vector_bool_to_initializer_list
{cudaq::stdvecBoolUnpackToInitList,
{},
R"#(
func.func private @__nvqpp_vector_bool_to_initializer_list(!cc.ptr<!cc.struct<{!cc.ptr<i1>, !cc.ptr<i1>, !cc.ptr<i1>}>>, !cc.ptr<!cc.struct<{!cc.ptr<i1>, !cc.ptr<i1>, !cc.ptr<i1>}>>) -> ())#"},
func.func private @__nvqpp_vector_bool_to_initializer_list(!cc.ptr<!cc.struct<{!cc.ptr<i1>, !cc.ptr<i1>, !cc.ptr<i1>}>>, !cc.ptr<!cc.struct<{!cc.ptr<i1>, !cc.array<i8 x 32>}>>, !cc.ptr<!cc.ptr<i8>>) -> ())#"},

{"__nvqpp_zeroDynamicResult", {}, R"#(
func.func private @__nvqpp_zeroDynamicResult() -> !cc.struct<{!cc.ptr<i8>, i64}> {
Expand Down
Loading

0 comments on commit c69b593

Please sign in to comment.