Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++ bridge] Switch from using arith ops to cc.cast #1978

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/cudaq/Optimizer/Builder/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ bool isAArch64(mlir::ModuleOp);
bool structUsesTwoArguments(mlir::Type ty);

std::optional<std::int64_t> getIntIfConstant(mlir::Value value);
std::optional<llvm::APFloat> getDoubleIfConstant(mlir::Value value);

/// Create a `cc.cast` operation, if it is needed.
mlir::Value createCast(mlir::OpBuilder &builder, mlir::Location loc,
Expand Down
8 changes: 5 additions & 3 deletions lib/Frontend/nvqpp/ConvertDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,16 @@ bool QuakeBridgeVisitor::VisitVarDecl(clang::VarDecl *x) {
if (initValue.getType().getIntOrFloatBitWidth() <
type.getIntOrFloatBitWidth()) {
// FIXME: Use zero-extend if this is unsigned!
initValue = builder.create<arith::ExtSIOp>(loc, type, initValue);
initValue = builder.create<cudaq::cc::CastOp>(
loc, type, initValue, cudaq::cc::CastOpMode::Signed);
} else if (initValue.getType().getIntOrFloatBitWidth() >
type.getIntOrFloatBitWidth()) {
initValue = builder.create<arith::TruncIOp>(loc, type, initValue);
initValue = builder.create<cudaq::cc::CastOp>(loc, type, initValue);
}
} else if (isa<IntegerType>(initValue.getType()) && isa<FloatType>(type)) {
// FIXME: Use UIToFP if this is unsigned!
initValue = builder.create<arith::SIToFPOp>(loc, type, initValue);
initValue = builder.create<cudaq::cc::CastOp>(
loc, type, initValue, cudaq::cc::CastOpMode::Signed);
}

if (auto initObject = initValue.getDefiningOp<cc::AllocaOp>()) {
Expand Down
97 changes: 45 additions & 52 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ static Value toIntegerImpl(OpBuilder &builder, Location loc, Value bitVec) {
Value bitElement = builder.create<cudaq::cc::LoadOp>(loc, eleAddr);

// -bits[k]
bitElement = builder.create<arith::ExtUIOp>(loc, builder.getI32Type(),
bitElement);
bitElement = builder.create<cudaq::cc::CastOp>(
loc, builder.getI32Type(), bitElement,
cudaq::cc::CastOpMode::Unsigned);
bitElement = builder.create<arith::MulIOp>(loc, negOne, bitElement);

// -bits[k] ^ i
Expand Down Expand Up @@ -326,38 +327,38 @@ static void castToSameType(OpBuilder builder, Location loc,
auto rhsTy = rhs.getType();
if (lhsTy.isa<IntegerType>() && rhsTy.isa<IntegerType>()) {
if (lhsTy.getIntOrFloatBitWidth() < rhsTy.getIntOrFloatBitWidth()) {
if (lhsType && lhsType->isUnsignedIntegerOrEnumerationType())
lhs = builder.create<arith::ExtUIOp>(loc, rhs.getType(), lhs);
else
lhs = builder.create<arith::ExtSIOp>(loc, rhs.getType(), lhs);
auto mode = (lhsType && lhsType->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
lhs = builder.create<cudaq::cc::CastOp>(loc, rhs.getType(), lhs, mode);
return;
}
if (rhsType && rhsType->isUnsignedIntegerOrEnumerationType())
rhs = builder.create<arith::ExtUIOp>(loc, lhs.getType(), rhs);
else
rhs = builder.create<arith::ExtSIOp>(loc, lhs.getType(), rhs);
auto mode = (rhsType && rhsType->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
rhs = builder.create<cudaq::cc::CastOp>(loc, lhs.getType(), rhs, mode);
return;
}
if (lhsTy.isa<FloatType>() && rhsTy.isa<FloatType>()) {
if (lhsTy.getIntOrFloatBitWidth() < rhsTy.getIntOrFloatBitWidth()) {
lhs = builder.create<arith::ExtFOp>(loc, rhs.getType(), lhs);
lhs = builder.create<cudaq::cc::CastOp>(loc, rhs.getType(), lhs);
return;
}
rhs = builder.create<arith::ExtFOp>(loc, lhs.getType(), rhs);
rhs = builder.create<cudaq::cc::CastOp>(loc, lhs.getType(), rhs);
return;
}
if (lhsTy.isa<FloatType>() && rhsTy.isa<IntegerType>()) {
if (rhsType && rhsType->isUnsignedIntegerOrEnumerationType())
rhs = builder.create<arith::UIToFPOp>(loc, lhs.getType(), rhs);
else
rhs = builder.create<arith::SIToFPOp>(loc, lhs.getType(), rhs);
auto mode = (rhsType && rhsType->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
rhs = builder.create<cudaq::cc::CastOp>(loc, lhs.getType(), rhs, mode);
return;
}
if (lhsTy.isa<IntegerType>() && rhsTy.isa<FloatType>()) {
if (lhsType && lhsType->isUnsignedIntegerOrEnumerationType())
lhs = builder.create<arith::UIToFPOp>(loc, rhs.getType(), lhs);
else
lhs = builder.create<arith::SIToFPOp>(loc, rhs.getType(), lhs);
auto mode = (lhsType && lhsType->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
lhs = builder.create<cudaq::cc::CastOp>(loc, rhs.getType(), lhs, mode);
return;
}
TODO_loc(loc, "conversion of operands in binary expression");
Expand Down Expand Up @@ -579,14 +580,8 @@ Value QuakeBridgeVisitor::floatingPointCoercion(Location loc, Type toType,
auto fromType = value.getType();
if (toType == fromType)
return value;
if (fromType.isa<IntegerType>() && toType.isa<IntegerType>()) {
if (fromType.getIntOrFloatBitWidth() < toType.getIntOrFloatBitWidth())
return builder.create<arith::ExtFOp>(loc, toType, value);
if (fromType.getIntOrFloatBitWidth() > toType.getIntOrFloatBitWidth())
return builder.create<arith::TruncFOp>(loc, toType, value);
TODO_loc(loc, "floating point types are distinct and same size");
}
TODO_loc(loc, "Float conversion but not floating point types");
assert(fromType.isa<FloatType>() && toType.isa<FloatType>());
return builder.create<cudaq::cc::CastOp>(loc, toType, value);
}

Value QuakeBridgeVisitor::integerCoercion(Location loc,
Expand All @@ -596,17 +591,15 @@ Value QuakeBridgeVisitor::integerCoercion(Location loc,
if (dstTy == fromTy)
return srcVal;

if (fromTy.isa<IntegerType>() && dstTy.isa<IntegerType>()) {
if (fromTy.getIntOrFloatBitWidth() < dstTy.getIntOrFloatBitWidth()) {
if (clangTy->isUnsignedIntegerOrEnumerationType())
return builder.create<arith::ExtUIOp>(loc, dstTy, srcVal);
return builder.create<arith::ExtSIOp>(loc, dstTy, srcVal);
}
if (fromTy.getIntOrFloatBitWidth() > dstTy.getIntOrFloatBitWidth())
return builder.create<arith::TruncIOp>(loc, dstTy, srcVal);
TODO_loc(loc, "Types are not the same but have the same length");
assert(fromTy.isa<IntegerType>() && dstTy.isa<IntegerType>());
if (fromTy.getIntOrFloatBitWidth() < dstTy.getIntOrFloatBitWidth()) {
auto mode = (clangTy->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
return builder.create<cudaq::cc::CastOp>(loc, dstTy, srcVal, mode);
}
TODO_loc(loc, "Integer conversion but not integer types");
assert(fromTy.getIntOrFloatBitWidth() > dstTy.getIntOrFloatBitWidth());
return builder.create<cudaq::cc::CastOp>(loc, dstTy, srcVal);
}

bool QuakeBridgeVisitor::TraverseCastExpr(clang::CastExpr *x,
Expand Down Expand Up @@ -654,9 +647,7 @@ bool QuakeBridgeVisitor::VisitCastExpr(clang::CastExpr *x) {
assert(toType && fromType);
if (toType == fromType)
return pushValue(value);
if (fromType.getIntOrFloatBitWidth() < toType.getIntOrFloatBitWidth())
return pushValue(builder.create<arith::ExtFOp>(loc, toType, value));
return pushValue(builder.create<arith::TruncFOp>(loc, toType, value));
return pushValue(builder.create<cudaq::cc::CastOp>(loc, toType, value));
}
case clang::CastKind::CK_IntegralCast: {
auto locSub = toLocation(x->getSubExpr());
Expand All @@ -672,18 +663,19 @@ bool QuakeBridgeVisitor::VisitCastExpr(clang::CastExpr *x) {
return true;
case clang::CastKind::CK_FloatingToIntegral: {
auto qualTy = x->getType();
if (qualTy->isUnsignedIntegerOrEnumerationType())
return pushValue(
builder.create<arith::FPToUIOp>(loc, castToTy, popValue()));
auto mode = qualTy->isUnsignedIntegerOrEnumerationType()
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
return pushValue(
builder.create<arith::FPToSIOp>(loc, castToTy, popValue()));
builder.create<cudaq::cc::CastOp>(loc, castToTy, popValue(), mode));
}
case clang::CastKind::CK_IntegralToFloating: {
if (x->getSubExpr()->getType()->isUnsignedIntegerOrEnumerationType())
return pushValue(
builder.create<arith::UIToFPOp>(loc, castToTy, popValue()));
auto mode =
(x->getSubExpr()->getType()->isUnsignedIntegerOrEnumerationType())
? cudaq::cc::CastOpMode::Unsigned
: cudaq::cc::CastOpMode::Signed;
return pushValue(
builder.create<arith::SIToFPOp>(loc, castToTy, popValue()));
builder.create<cudaq::cc::CastOp>(loc, castToTy, popValue(), mode));
}
case clang::CastKind::CK_IntegralToBoolean: {
auto last = popValue();
Expand Down Expand Up @@ -1153,7 +1145,7 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {

// Get the values involved
auto peelIntToFloat = [&](Value v) {
if (auto op = v.getDefiningOp<arith::SIToFPOp>())
if (auto op = v.getDefiningOp<cudaq::cc::CastOp>())
return op.getOperand();
return v;
};
Expand All @@ -1171,7 +1163,8 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {
base, x->getArg(1)->getType().getTypePtrOrNull(), power);
auto ipow = builder.create<math::IPowIOp>(loc, base, power);
if (isa<FloatType>(resTy))
return pushValue(builder.create<arith::SIToFPOp>(loc, resTy, ipow));
return pushValue(builder.create<cudaq::cc::CastOp>(
loc, resTy, ipow, cudaq::cc::CastOpMode::Signed));
assert(resTy == ipow.getType());
return pushValue(ipow);
}
Expand Down Expand Up @@ -2228,7 +2221,7 @@ bool QuakeBridgeVisitor::VisitCXXOperatorCallExpr(
auto eleAddr = builder.create<cc::ComputePtrOp>(loc, elePtrTy, vecPtr,
ValueRange{indexVar});
auto i1PtrTy = cc::PointerType::get(builder.getI1Type());
auto i1Cast = builder.create<arith::TruncIOp>(loc, i1PtrTy, eleAddr);
auto i1Cast = builder.create<cudaq::cc::CastOp>(loc, i1PtrTy, eleAddr);
return replaceTOSValue(i1Cast);
}
TODO_loc(loc, "unhandled operator call for quake conversion");
Expand Down
25 changes: 24 additions & 1 deletion lib/Optimizer/CodeGen/QuakeToCodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Dialect/Complex/IR/Complex.h"

using namespace mlir;

Expand Down Expand Up @@ -39,10 +40,32 @@ class CodeGenRAIIPattern : public OpRewritePattern<quake::InitializeStateOp> {
return success();
}
};

class ExpandComplexCast : public OpRewritePattern<cudaq::cc::CastOp> {
public:
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(cudaq::cc::CastOp castOp,
PatternRewriter &rewriter) const override {
auto complexTy = dyn_cast<ComplexType>(castOp.getType());
if (!complexTy)
return failure();
auto loc = castOp.getLoc();
auto ty = cast<ComplexType>(castOp.getValue().getType()).getElementType();
Value rePart = rewriter.create<complex::ReOp>(loc, ty, castOp.getValue());
Value imPart = rewriter.create<complex::ImOp>(loc, ty, castOp.getValue());
auto eleTy = complexTy.getElementType();
auto reCast = rewriter.create<cudaq::cc::CastOp>(loc, eleTy, rePart);
auto imCast = rewriter.create<cudaq::cc::CastOp>(loc, eleTy, imPart);
rewriter.replaceOpWithNewOp<complex::CreateOp>(castOp, complexTy, reCast,
imCast);
return success();
}
};
} // namespace

void cudaq::codegen::populateQuakeToCodegenPatterns(
mlir::RewritePatternSet &patterns) {
auto *ctx = patterns.getContext();
patterns.insert<CodeGenRAIIPattern>(ctx);
patterns.insert<CodeGenRAIIPattern, ExpandComplexCast>(ctx);
}
Loading
Loading