Skip to content

Commit

Permalink
Merge branch 'main' into workflows/ci
Browse files Browse the repository at this point in the history
  • Loading branch information
bettinaheim authored Jul 27, 2023
2 parents a0a6179 + 914560e commit 28eb45f
Show file tree
Hide file tree
Showing 29 changed files with 159 additions and 156 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ header:
- 'LICENSE'
- 'NOTICE'
- 'build'
- 'examples'
- 'tpls'
- 'docs/sphinx/_templates'
- 'runtime/cudaq/algorithms/optimizers/nlopt/nlopt-src'
Expand Down
1 change: 1 addition & 0 deletions examples
12 changes: 6 additions & 6 deletions include/cudaq/Optimizer/Dialect/CC/CCOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include "cudaq/Optimizer/Dialect/CC/CCTypes.td"
def AnyPointerType : Type<CPred<"$_self.isa<mlir::LLVM::LLVMPointerType,"
"cudaq::cc::PointerType>()">, "any pointer type">;

def AnyCallableType : Type<CPred<"$_self.isa<cudaq::cc::LambdaType, "
def AnyCallableType : Type<CPred<"$_self.isa<cudaq::cc::CallableType, "
"mlir::FunctionType>()">, "any callable type">;

def AnyAggregateType : Type<CPred<"$_self.isa<cudaq::cc::StructType, "
Expand Down Expand Up @@ -1125,13 +1125,13 @@ def cc_CreateLambdaOp : CCOp<"create_lambda",
```
}];

let results = (outs cc_LambdaType:$signature);
let results = (outs cc_CallableType:$signature);
let regions = (region SizedRegion<1>:$initRegion);
let hasCustomAssemblyFormat = 1;

let builders = [
OpBuilder<(ins
"cudaq::cc::LambdaType":$signature,
"cudaq::cc::CallableType":$signature,
CArg<"llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>",
"nullptr">)>];

Expand Down Expand Up @@ -1278,7 +1278,7 @@ def cc_InstantiateCallableOp : CCOp<"instantiate_callable", [Pure]> {
SymbolRefAttr:$callee,
Variadic<AnyType>:$closure_data
);
let results = (outs cc_LambdaType:$signature);
let results = (outs cc_CallableType:$signature);

let assemblyFormat = [{
$callee `(` $closure_data `)`
Expand All @@ -1305,7 +1305,7 @@ def cc_CallableFuncOp : CCOp<"callable_func", [Pure]> {
See also InstantiateCallableOp, CallableClosureOp.
}];

let arguments = (ins cc_LambdaType:$callable);
let arguments = (ins cc_CallableType:$callable);
let results = (outs FunctionType:$function);

let assemblyFormat = [{
Expand Down Expand Up @@ -1343,7 +1343,7 @@ def cc_CallableClosureOp : CCOp<"callable_closure", [Pure]> {
See also CallableFuncOp, InstantiateCallableOp.
}];

let arguments = (ins cc_LambdaType:$callable);
let arguments = (ins cc_CallableType:$callable);
let results = (outs Variadic<AnyType>:$closure_data);

let assemblyFormat = [{
Expand Down
18 changes: 9 additions & 9 deletions include/cudaq/Optimizer/Dialect/CC/CCTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ def cc_ArrayType : CCType<"Array", "array"> {
}

//===----------------------------------------------------------------------===//
// LambdaType
// CallableType
//===----------------------------------------------------------------------===//

def cc_LambdaType : CCType<"Lambda", "lambda"> {
let summary = "Proxy for a C++ lambda instance in cc";
def cc_CallableType : CCType<"Callable", "callable"> {
let summary = "Proxy for a C++ callable instance in cc";
let description = [{
A kernel may be a template function that takes a lambda as an argument.
The type of these lambda instances is this proxy lambda type. The
instantiated template instance will resolve the lambda instance, and this
resolved instance of the lambda will be lowered in the specialized template
A kernel may be a template function that takes a callable as an argument.
The type of these callable instances is this proxy callable type. The
instantiated template instance will resolve the callable instance, and this
resolved instance of the callable will be lowered in the specialized template
function.
}];

Expand All @@ -162,8 +162,8 @@ def cc_LambdaType : CCType<"Lambda", "lambda"> {
];

let extraClassDeclaration = [{
/// Create a lambda type without a signature. This is for prototyping only.
[[deprecated]] static LambdaType getNoSignature(mlir::MLIRContext *ctx);
/// Create a callable type without a signature. This is for prototyping only.
[[deprecated]] static CallableType getNoSignature(mlir::MLIRContext *ctx);
}];
}

Expand Down
4 changes: 2 additions & 2 deletions include/cudaq/Optimizer/Dialect/Quake/QuakeOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def quake_ComputeActionOp : QuakeOp<"compute_action"> {

let arguments = (ins
UnitAttr:$is_dagger,
cc_LambdaType:$compute,
cc_LambdaType:$action
cc_CallableType:$compute,
cc_CallableType:$action
);

let assemblyFormat = [{
Expand Down
13 changes: 7 additions & 6 deletions lib/Frontend/nvqpp/ConvertDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void QuakeBridgeVisitor::addArgumentSymbols(
// Transform pass-by-value arguments to stack slots.
auto loc = toLocation(argVal);
auto parmTy = entryBlock->getArgument(index).getType();
if (isa<cc::LambdaType, cc::StdvecType, cc::ArrayType, cc::StructType,
if (isa<cc::CallableType, cc::StdvecType, cc::ArrayType, cc::StructType,
LLVM::LLVMStructType, FunctionType, quake::RefType,
quake::VeqType>(parmTy)) {
symbolTable.insert(name, entryBlock->getArgument(index));
Expand Down Expand Up @@ -336,11 +336,12 @@ bool QuakeBridgeVisitor::VisitVarDecl(clang::VarDecl *x) {
}
}

if (auto lambdaTy = dyn_cast<cc::LambdaType>(type)) {
// Variable is of !cc.lambda type. Lambdas are always in the value domain.
auto lambda = popValue();
symbolTable.insert(name, lambda);
return pushValue(lambda);
if (auto callableTy = dyn_cast<cc::CallableType>(type)) {
// Variable is of !cc.callable type. Callables are always in the value
// domain.
auto callable = popValue();
symbolTable.insert(name, callable);
return pushValue(callable);
}

// Variable is of some basic type not already handled. Create a local stack
Expand Down
22 changes: 11 additions & 11 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,12 @@ bool QuakeBridgeVisitor::VisitImplicitCastExpr(clang::ImplicitCastExpr *x) {
}
}
// Enable implicit conversion of lambda -> std::function, which are both
// cc::LambdaType.
if (toTy.isa<cc::LambdaType>()) {
// cc::CallableType.
if (toTy.isa<cc::CallableType>()) {
auto subExpr = x->getSubExpr();
if (auto cxxExpr = dyn_cast<clang::CXXConstructExpr>(subExpr);
cxxExpr->getNumArgs() == 1 &&
genType(cxxExpr->getArg(0)->getType()).isa<cc::LambdaType>()) {
genType(cxxExpr->getArg(0)->getType()).isa<cc::CallableType>()) {
return true;
}
// Enable implicit conversion of callable -> std::function.
Expand Down Expand Up @@ -995,8 +995,8 @@ bool QuakeBridgeVisitor::VisitMaterializeTemporaryExpr(
// The following cases are λ expressions and quantum data. In those cases,
// there is nothing to materialize, so we can just pass the Value on the top
// of the stack.
if (isa<cc::LambdaType>(ty)) {
assert(isa<cc::LambdaType>(peekValue().getType()));
if (isa<cc::CallableType>(ty)) {
assert(isa<cc::CallableType>(peekValue().getType()));
return true;
}
if (isa<quake::VeqType>(ty)) {
Expand Down Expand Up @@ -1037,7 +1037,7 @@ bool QuakeBridgeVisitor::TraverseLambdaExpr(clang::LambdaExpr *x,
TODO_x(loc, x, mangler, "lambda expression with explicit captures");
}
auto lambdaInstance = builder.create<cc::CreateLambdaOp>(
loc, cc::LambdaType::get(ctx, getFunctionType(x->getCallOperator())),
loc, cc::CallableType::get(ctx, getFunctionType(x->getCallOperator())),
[&](OpBuilder &builder, Location loc) {
// FIXME: the capture list, etc. should be visited in an appropriate
// context here, not as part of lowering the body of the lambda.
Expand Down Expand Up @@ -1417,7 +1417,7 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {
kernelArgs);
return inlinedFinishControlNegations();
}
if (auto ty = dyn_cast<cc::LambdaType>(calleeValue.getType())) {
if (auto ty = dyn_cast<cc::CallableType>(calleeValue.getType())) {
// In order to autogenerate the control form of the called kernel, we
// have to be able to determine precisely which kernel is being called
// at this point. If this is a local lambda expression, it is handled
Expand Down Expand Up @@ -1513,7 +1513,7 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {
loc, funcTy.getResults(), calleeSym,
/*isAdjoint=*/true, ValueRange{}, kernelArgs);
}
if (auto ty = dyn_cast<cc::LambdaType>(calleeValue.getType())) {
if (auto ty = dyn_cast<cc::CallableType>(calleeValue.getType())) {
// In order to autogenerate the control form of the called kernel, we
// have to be able to determine precisely which kernel is being called
// at this point. If this is a local lambda expression, it is handled
Expand Down Expand Up @@ -1692,7 +1692,7 @@ bool QuakeBridgeVisitor::VisitCXXOperatorCallExpr(
auto funcArity = func->getNumParams();
SmallVector<Value> args = lastValues(funcArity);
auto indirect = popValue();
auto indirectTy = indirect.getType().cast<cc::LambdaType>();
auto indirectTy = indirect.getType().cast<cc::CallableType>();
auto callInd = builder.create<cc::CallCallableOp>(
loc, indirectTy.getSignature().getResults(), indirect, args);
return pushValue(callInd.getResult(0));
Expand Down Expand Up @@ -1800,7 +1800,7 @@ bool QuakeBridgeVisitor::VisitCXXConstructExpr(clang::CXXConstructExpr *x) {
if (ctorName == "function") {
// Are we converting a lambda expr to a std::function?
auto backTy = peekValue().getType();
if (backTy.isa<cc::LambdaType>()) {
if (backTy.isa<cc::CallableType>()) {
// Skip this constructor (for now).
return true;
}
Expand Down Expand Up @@ -1833,7 +1833,7 @@ bool QuakeBridgeVisitor::VisitCXXConstructExpr(clang::CXXConstructExpr *x) {
auto kernelCallTy = getFunctionType(callOperDecl);
auto kernelName = generateCudaqKernelName(callOperDecl);
return pushValue(builder.create<cc::CreateLambdaOp>(
loc, cc::LambdaType::get(kernelCallTy),
loc, cc::CallableType::get(kernelCallTy),
[&](OpBuilder &builder, Location loc) {
auto args = builder.getBlock()->getArguments();
auto call = builder.create<func::CallOp>(
Expand Down
12 changes: 6 additions & 6 deletions lib/Frontend/nvqpp/ConvertType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static bool isArithmeticSequenceType(Type t) {
static bool isKernelSignatureType(FunctionType t);

static bool isKernelCallable(Type t) {
if (auto lambdaTy = dyn_cast<cudaq::cc::LambdaType>(t))
if (auto lambdaTy = dyn_cast<cudaq::cc::CallableType>(t))
return isKernelSignatureType(lambdaTy.getSignature());
return false;
}
Expand Down Expand Up @@ -195,12 +195,12 @@ bool QuakeBridgeVisitor::TraverseRecordType(clang::RecordType *t) {

bool QuakeBridgeVisitor::convertToLambda() {
// Should be called from a Visit...() method.
if (isa<cudaq::cc::LambdaType>(peekType()))
if (isa<cudaq::cc::CallableType>(peekType()))
return true;
auto funcTy = dyn_cast<FunctionType>(popType());
assert(funcTy &&
"lambda expression should be convertible from function type");
return pushType(cudaq::cc::LambdaType::get(builder.getContext(), funcTy));
return pushType(cudaq::cc::CallableType::get(builder.getContext(), funcTy));
}

bool QuakeBridgeVisitor::VisitCXXRecordDecl(clang::CXXRecordDecl *x) {
Expand Down Expand Up @@ -296,7 +296,7 @@ bool QuakeBridgeVisitor::VisitElaboratedType(clang::ElaboratedType *t) {
// expedience, pretend Callables and lambdas are "the same".
auto fnTy = dyn_cast<FunctionType>(popType());
assert(fnTy);
return pushType(cudaq::cc::LambdaType::get(context, fnTy));
return pushType(cudaq::cc::CallableType::get(context, fnTy));
}
if (matchTypeName("cudaq", "qubit", name, /*toEnd=*/true) ||
matchTypeName("cudaq", "qudit<2UL>", name, /*toEnd=*/true))
Expand Down Expand Up @@ -408,7 +408,7 @@ bool QuakeBridgeVisitor::VisitPointerType(clang::PointerType *t) {
bool QuakeBridgeVisitor::VisitLValueReferenceType(
clang::LValueReferenceType *t) {
auto eleTy = popType();
if (isa<cc::LambdaType, cc::StdvecType, quake::VeqType, quake::RefType>(
if (isa<cc::CallableType, cc::StdvecType, quake::VeqType, quake::RefType>(
eleTy))
return pushType(eleTy);
return pushType(cc::PointerType::get(eleTy));
Expand All @@ -418,7 +418,7 @@ bool QuakeBridgeVisitor::VisitRValueReferenceType(
clang::RValueReferenceType *t) {
auto eleTy = popType();
// FIXME: LLVMStructType is promoted as a temporary workaround.
if (isa<cc::LambdaType, cc::StdvecType, cc::ArrayType, cc::StructType,
if (isa<cc::CallableType, cc::StdvecType, cc::ArrayType, cc::StructType,
quake::VeqType, quake::RefType, LLVM::LLVMStructType>(eleTy))
return pushType(eleTy);
return pushType(cc::PointerType::get(eleTy));
Expand Down
2 changes: 1 addition & 1 deletion lib/Optimizer/CodeGen/LowerToQIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ class QuakeToQIRRewrite : public cudaq::opt::QuakeToQIRBase<QuakeToQIRRewrite> {
[&](quake::VeqType type) { return cudaq::opt::getArrayType(context); });
typeConverter.addConversion(
[&](quake::RefType type) { return cudaq::opt::getQubitType(context); });
typeConverter.addConversion([&](cudaq::cc::LambdaType type) {
typeConverter.addConversion([&](cudaq::cc::CallableType type) {
return lambdaAsPairOfPointers(type.getContext());
});
typeConverter.addConversion([&](cudaq::cc::StdvecType type) {
Expand Down
10 changes: 5 additions & 5 deletions lib/Optimizer/Dialect/CC/CCOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ void cudaq::cc::IfOp::getSuccessorRegions(

void cudaq::cc::CreateLambdaOp::build(OpBuilder &builder,
OperationState &result,
cudaq::cc::LambdaType lambdaTy,
cudaq::cc::CallableType lambdaTy,
BodyBuilderFn bodyBuilder) {
auto *bodyRegion = result.addRegion();
bodyRegion->push_back(new Block);
Expand All @@ -1083,7 +1083,7 @@ void cudaq::cc::CreateLambdaOp::print(OpAsmPrinter &p) {
p << ' ';
bool hasArgs = getRegion().getNumArguments() != 0;
bool hasRes =
getType().cast<cudaq::cc::LambdaType>().getSignature().getNumResults();
getType().cast<cudaq::cc::CallableType>().getSignature().getNumResults();
p.printRegion(getRegion(), /*printEntryBlockArgs=*/hasArgs,
/*printBlockTerminators=*/hasRes);
p << " : " << getType();
Expand Down Expand Up @@ -1111,7 +1111,7 @@ ParseResult cudaq::cc::CreateLambdaOp::parse(OpAsmParser &parser,
LogicalResult cudaq::cc::CallCallableOp::verify() {
FunctionType funcTy;
auto ty = getCallee().getType();
if (auto lambdaTy = dyn_cast<cudaq::cc::LambdaType>(ty))
if (auto lambdaTy = dyn_cast<cudaq::cc::CallableType>(ty))
funcTy = lambdaTy.getSignature();
else if (auto fTy = dyn_cast<FunctionType>(ty))
funcTy = fTy;
Expand Down Expand Up @@ -1144,7 +1144,7 @@ LogicalResult cudaq::cc::ReturnOp::verify() {
auto *op = getOperation();
auto resultTypes = [&]() {
if (auto func = op->getParentOfType<CreateLambdaOp>()) {
auto lambdaTy = cast<LambdaType>(func->getResult(0).getType());
auto lambdaTy = cast<CallableType>(func->getResult(0).getType());
return SmallVector<Type>(lambdaTy.getSignature().getResults());
}
if (auto func = op->getParentOfType<func::FuncOp>())
Expand Down Expand Up @@ -1304,7 +1304,7 @@ LogicalResult cudaq::cc::UnwindReturnOp::verify() {
auto *op = getOperation();
auto resultTypes = [&]() {
if (auto func = op->getParentOfType<CreateLambdaOp>()) {
auto lambdaTy = cast<LambdaType>(func->getResult(0).getType());
auto lambdaTy = cast<CallableType>(func->getResult(0).getType());
return SmallVector<Type>(lambdaTy.getSignature().getResults());
}
if (auto func = op->getParentOfType<func::FuncOp>())
Expand Down
6 changes: 3 additions & 3 deletions lib/Optimizer/Dialect/CC/CCTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ void cc::ArrayType::print(AsmPrinter &printer) const {

namespace cudaq {

cc::LambdaType cc::LambdaType::getNoSignature(MLIRContext *ctx) {
return LambdaType::get(ctx, FunctionType::get(ctx, {}, {}));
cc::CallableType cc::CallableType::getNoSignature(MLIRContext *ctx) {
return CallableType::get(ctx, FunctionType::get(ctx, {}, {}));
}

//===----------------------------------------------------------------------===//

void cc::CCDialect::registerTypes() {
addTypes<ArrayType, LambdaType, PointerType, StdvecType, StructType>();
addTypes<ArrayType, CallableType, PointerType, StdvecType, StructType>();
}

} // namespace cudaq
8 changes: 4 additions & 4 deletions lib/Optimizer/Transforms/GenKernelExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class GenerateKernelExecution
SmallVector<Type> eleTys;
// Add all argument types, translating std::vector to a length.
for (auto inTy : funcTy.getInputs()) {
if (inTy.isa<cudaq::cc::LambdaType, cudaq::cc::StructType>())
if (inTy.isa<cudaq::cc::CallableType, cudaq::cc::StructType>())
eleTys.push_back(IntegerType::get(ctx, 64));
else if (inTy.isa<cudaq::cc::StdvecType, quake::VeqType>())
eleTys.push_back(IntegerType::get(ctx, 64));
Expand All @@ -73,7 +73,7 @@ class GenerateKernelExecution
}
// Add all result types, translating std::vector to a length.
for (auto outTy : funcTy.getResults()) {
if (outTy.isa<cudaq::cc::LambdaType, cudaq::cc::StructType>()) {
if (outTy.isa<cudaq::cc::CallableType, cudaq::cc::StructType>()) {
eleTys.push_back(IntegerType::get(ctx, 64));
} else if (auto vecTy = dyn_cast<cudaq::cc::StdvecType>(outTy)) {
eleTys.push_back(cudaq::cc::PointerType::get(vecTy.getElementType()));
Expand Down Expand Up @@ -357,7 +357,7 @@ class GenerateKernelExecution
Type inTy = inp.value();
std::int64_t idx = inp.index();
auto off = DenseI64ArrayAttr::get(ctx, ArrayRef<std::int64_t>{idx});
if (inTy.isa<cudaq::cc::LambdaType, cudaq::cc::StructType>()) {
if (inTy.isa<cudaq::cc::CallableType, cudaq::cc::StructType>()) {
auto undef = builder.create<cudaq::cc::UndefOp>(loc, inTy);
args.push_back(undef);
} else if (inTy.isa<cudaq::cc::StdvecType, quake::VeqType>()) {
Expand Down Expand Up @@ -520,7 +520,7 @@ class GenerateKernelExecution
Type inTy = arg.getType();
std::int64_t idx = inp.index();
auto off = DenseI64ArrayAttr::get(ctx, ArrayRef<std::int64_t>{idx});
if (inTy.isa<cudaq::cc::LambdaType, cudaq::cc::StructType>()) {
if (inTy.isa<cudaq::cc::CallableType, cudaq::cc::StructType>()) {
/* do nothing */
} else if (cudaq::opt::factory::isStdVecArg(inTy)) {
auto ptrTy = dyn_cast<cudaq::cc::PointerType>(inTy);
Expand Down
4 changes: 2 additions & 2 deletions lib/Optimizer/Transforms/LambdaLifting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct CallCallableOpPattern
auto closureTy = closure.getType();

// For a callable, call the trampoline with the closure data.
if (auto lambTy = dyn_cast<cudaq::cc::LambdaType>(closureTy)) {
if (auto lambTy = dyn_cast<cudaq::cc::CallableType>(closureTy)) {
auto dynFunc = rewriter.create<cudaq::cc::CallableFuncOp>(
loc, call.getFunctionType(), closure);
rewriter.replaceOpWithNewOp<func::CallIndirectOp>(call, dynFunc,
Expand Down Expand Up @@ -245,7 +245,7 @@ class LambdaLiftingPass
return;
}
rewriter.setInsertionPoint(lambda);
cudaq::cc::LambdaType lambdaTy = lambda.getType();
cudaq::cc::CallableType lambdaTy = lambda.getType();
auto sig = lambdaTy.getSignature();
auto counter = iter->second.counter;
ValueRange freeValues = iter->second.freeValues;
Expand Down
Loading

0 comments on commit 28eb45f

Please sign in to comment.