Skip to content

Commit

Permalink
Add a helper function for conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
schweitzpgi committed Mar 15, 2024
1 parent d774ba6 commit e77639a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 11 additions & 2 deletions include/cudaq/Optimizer/Builder/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ class PointerType;
class StructType;
} // namespace cc

namespace opt::factory {
namespace opt {

template <typename T>
requires std::integral<T>
T convertBitsToBytes(T bits) {
return (bits + 7) / 8;
}

namespace factory {

constexpr const char targetTripleAttrName[] = "llvm.triple";
constexpr const char targetDataLayoutAttrName[] = "llvm.data_layout";
Expand Down Expand Up @@ -225,5 +233,6 @@ bool isAArch64(mlir::ModuleOp);
/// the X86-64 ABI.) If \p ty is not a `struct`, this returns `false`.
bool structUsesTwoArguments(mlir::Type ty);

} // namespace opt::factory
} // namespace factory
} // namespace opt
} // namespace cudaq
13 changes: 8 additions & 5 deletions lib/Optimizer/Transforms/QuakeSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeTypes.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "cudaq/Todo.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
Expand Down Expand Up @@ -412,7 +411,8 @@ class QuakeSynthesizer
// Process scalar floating point types.
if (type == builder.getF32Type()) {
synthesizeRuntimeArgument<float>(
builder, argument, args, offset, type.getIntOrFloatBitWidth() / 8,
builder, argument, args, offset,
cudaq::opt::convertBitsToBytes(type.getIntOrFloatBitWidth()),
[=](OpBuilder &builder, float *concrete) {
llvm::APFloat f(*concrete);
return builder.create<arith::ConstantFloatOp>(
Expand All @@ -422,7 +422,8 @@ class QuakeSynthesizer
}
if (type == builder.getF64Type()) {
synthesizeRuntimeArgument<double>(
builder, argument, args, offset, type.getIntOrFloatBitWidth() / 8,
builder, argument, args, offset,
cudaq::opt::convertBitsToBytes(type.getIntOrFloatBitWidth()),
[=](OpBuilder &builder, double *concrete) {
llvm::APFloat f(*concrete);
return builder.create<arith::ConstantFloatOp>(
Expand All @@ -443,7 +444,8 @@ class QuakeSynthesizer
char *ptrToSizeInBuffer = static_cast<char *>(args) + offset;
auto sizeFromBuffer =
*reinterpret_cast<std::uint64_t *>(ptrToSizeInBuffer);
auto bytesInType = (eleTy.getIntOrFloatBitWidth() + 7) / 8;
auto bytesInType =
cudaq::opt::convertBitsToBytes(eleTy.getIntOrFloatBitWidth());
assert(bytesInType > 0 && "element must have a size");
auto vectorSize = sizeFromBuffer / bytesInType;
stdVecInfo.emplace_back(argNum, eleTy, vectorSize);
Expand Down Expand Up @@ -510,7 +512,8 @@ class QuakeSynthesizer
doVector(std::int64_t{});
break;
default:
bufferAppendix += vecLength * (ty.getIntOrFloatBitWidth() / 8);
bufferAppendix += vecLength * cudaq::opt::convertBitsToBytes(
ty.getIntOrFloatBitWidth());
funcOp.emitOpError("synthesis failed for vector<integral-type>.");
break;
}
Expand Down

0 comments on commit e77639a

Please sign in to comment.