Skip to content

Commit

Permalink
camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
bcdonovan committed May 23, 2024
1 parent f0381bb commit 183c8b0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 72 deletions.
4 changes: 2 additions & 2 deletions include/Dialect/QCS/Utils/ParameterInitialValueAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ using InitialValueType = llvm::StringMap<ParameterType>;

class ParameterInitialValueAnalysis {
private:
InitialValueType initial_values_;
InitialValueType initialValues_;
bool invalid_{true};

public:
ParameterInitialValueAnalysis(mlir::Operation *op);
InitialValueType &getNames() { return initial_values_; }
InitialValueType &getNames() { return initialValues_; }
void invalidate() { invalid_ = true; }
bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) {
return invalid_;
Expand Down
57 changes: 2 additions & 55 deletions lib/Dialect/QCS/IR/QCSOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,6 @@ using namespace mlir::qcs;
// NOLINTNEXTLINE(misc-include-cleaner): Required for MLIR registrations
#include "Dialect/QCS/IR/QCSOps.cpp.inc"

namespace {
LogicalResult
verifyQCSParameterOpSymbolUses(SymbolTableCollection &symbolTable,
mlir::Operation *op,
bool operandMustMatchSymbolType = false) {
assert(op);
return success();
// Check that op has attribute variable_name
auto paramRefAttr = op->getAttrOfType<FlatSymbolRefAttr>("parameter_name");
if (!paramRefAttr)
return op->emitOpError(
"requires a symbol reference attribute 'parameter_name'");

// Check that symbol reference resolves to a parameter declaration
auto declOp =
symbolTable.lookupNearestSymbolFrom<DeclareParameterOp>(op, paramRefAttr);

// check higher level modules
if (!declOp) {
auto targetModuleOp = op->getParentOfType<mlir::ModuleOp>();
if (targetModuleOp) {
auto topLevelModuleOp = targetModuleOp->getParentOfType<mlir::ModuleOp>();
if (!declOp && topLevelModuleOp)
declOp = symbolTable.lookupNearestSymbolFrom<DeclareParameterOp>(
topLevelModuleOp, paramRefAttr);
}
}

if (!declOp)
return op->emitOpError() << "no valid reference to a parameter '"
<< paramRefAttr.getValue() << "'";

assert(op->getNumResults() <= 1 && "assume none or single result");

// Check that type of variables matches result type of this Op
if (op->getNumResults() == 1) {
if (op->getResult(0).getType() != declOp.getType())
return op->emitOpError(
"type mismatch between variable declaration and variable use");
}

if (op->getNumOperands() > 0 && operandMustMatchSymbolType) {
assert(op->getNumOperands() == 1 &&
"type check only supported for a single operand");
if (op->getOperand(0).getType() != declOp.getType())
return op->emitOpError(
"type mismatch between variable declaration and variable assignment");
}
return success();
}

} // anonymous namespace

//===----------------------------------------------------------------------===//
// ParameterLoadOp
//===----------------------------------------------------------------------===//
Expand All @@ -100,8 +47,8 @@ verifyQCSParameterOpSymbolUses(SymbolTableCollection &symbolTable,
ParameterType ParameterLoadOp::getInitialValue() {
auto *op = getOperation();
double retVal = 0.0;
if (op->hasAttr("initial_value")) {
auto initAttr = op->getAttr("initial_value").dyn_cast<FloatAttr>();
if (op->hasAttr("initialValue")) {
auto initAttr = op->getAttr("initialValue").dyn_cast<FloatAttr>();
if (initAttr)
retVal = initAttr.getValue().convertToDouble();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Dialect/QCS/Utils/ParameterInitialValueAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ ParameterInitialValueAnalysis::ParameterInitialValueAnalysis(
if (!parameterLoadOp)
continue;

double initial_value =
double initialValue =
std::get<double>(parameterLoadOp.getInitialValue());
initial_values_[parameterLoadOp.getParameterName()] = initial_value;
initialValues_[parameterLoadOp.getParameterName()] = initialValue;
foundParameters = true;
}
if (!foundParameters) {
Expand All @@ -80,9 +80,9 @@ ParameterInitialValueAnalysis::ParameterInitialValueAnalysis(

// debugging / test print out
if (printAnalysisEntries) {
for (auto &initial_value : initial_values_) {
llvm::outs() << initial_value.first() << " = "
<< std::get<double>(initial_value.second) << "\n";
for (auto &initialValue : initialValues_) {
llvm::outs() << initialValue.first() << " = "
<< std::get<double>(initialValue.second) << "\n";
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Frontend/OpenQASM3/QUIRVariableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ QUIRVariableBuilder::generateParameterLoad(mlir::Location location,

mlir::FloatAttr const floatAttr =
getClassicalBuilder().getF64FloatAttr(initialValue);
op->setAttr("initial_value", floatAttr);
op->setAttr("initialValue", floatAttr);
return op;
}

Expand All @@ -157,7 +157,7 @@ QUIRVariableBuilder::generateParameterLoad(mlir::Location location,
initialValue = constAttr.getValueAsDouble();
mlir::FloatAttr const floatAttr =
getClassicalBuilder().getF64FloatAttr(initialValue);
loadOp->setAttr("initial_value", floatAttr);
loadOp->setAttr("initialValue", floatAttr);
return loadOp;
}

Expand Down
8 changes: 4 additions & 4 deletions test/Dialect/QCS/Utils/parameter-initial-value-analysis.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
module {
// without nested pass manager should only find
// alpha and beta
qcs.declare_parameter @alpha : !quir.angle<64> = #quir.angle<1.000000e+00> : !quir.angle<64>
qcs.declare_parameter @beta : f64 = 2.000000e+00 : f64
qcs.parameter_load "alpha" : !quir.angle<64> {initialValue = 1.000000e+00 :f64}
qcs.parameter_load "beta" : f64 {initialValue = 2.000000e+00 :f64}
module @first {
// nested test should find alpha and beta
qcs.declare_parameter @theta : !quir.angle<64> = #quir.angle<3.140000e+00> : !quir.angle<64>
qcs.declare_parameter @phi : f64 = 1.500000e+00 : f64
qcs.parameter_load "theta" : !quir.angle<64> {initialValue = 3.140000e+00 :f64}
qcs.parameter_load "phi" : f64 {initialValue = 1.500000e+00 :f64}
}
module @second {
// test module without declare_parameter
Expand Down
2 changes: 1 addition & 1 deletion test/Frontend/OpenQASM3/input-parameters-if.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ is_excited = measure $2;
// CHECK: [[QUBIT2:%.*]] = quir.declare_qubit {id = 2 : i32} : !quir.qubit<1>
// CHECK: [[QUBIT3:%.*]] = quir.declare_qubit {id = 3 : i32} : !quir.qubit<1>

// CHECK: [[PARAM:%.*]] = qcs.parameter_load "theta" : !quir.angle<64> {initial_value = 3.141000e+00 : f64}
// CHECK: [[PARAM:%.*]] = qcs.parameter_load "theta" : !quir.angle<64> {initialValue = 3.141000e+00 : f64}
// CHECK: oq3.variable_assign @theta : !quir.angle<64> = [[PARAM]]

// CHECK: [[EXCITED:%.*]] = oq3.variable_load @is_excited : !quir.cbit<1>
Expand Down
2 changes: 1 addition & 1 deletion test/Frontend/OpenQASM3/input-parameters-while.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bit is_excited;

// CHECK: func.func @main() -> i32 {
// CHECK: scf.for %arg0 = %c0 to %c1000 step %c1 {
// CHECK: {{.*}} = qcs.parameter_load "theta" : !quir.angle<64> {initial_value = 3.141000e+00 : f64}
// CHECK: {{.*}} = qcs.parameter_load "theta" : !quir.angle<64> {initialValue = 3.141000e+00 : f64}
// CHECK: [[QUBIT:%.*]] = quir.declare_qubit {id = 0 : i32} : !quir.qubit<1>
// CHECK: scf.while : () -> () {
// CHECK: [[N:%.*]] = oq3.variable_load @n : i32
Expand Down
4 changes: 2 additions & 2 deletions test/Frontend/OpenQASM3/input-parameters.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ c = measure $0;
// CHECK: %0 = quir.declare_qubit {id = 0 : i32} : !quir.qubit<1>
// CHECK: %1 = quir.declare_qubit {id = 2 : i32} : !quir.qubit<1>

// CHECK: %2 = qcs.parameter_load "theta" : !quir.angle<64> {initial_value = 3.141000e+00 : f64}
// CHECK: %2 = qcs.parameter_load "theta" : !quir.angle<64> {initialValue = 3.141000e+00 : f64}
// CHECK: oq3.variable_assign @theta : !quir.angle<64> = %2
// CHECK: %3 = qcs.parameter_load "theta2" : f64 {initial_value = 1.560000e+00 : f64}
// CHECK: %3 = qcs.parameter_load "theta2" : f64 {initialValue = 1.560000e+00 : f64}
// CHECK: oq3.variable_assign @theta2 : f64 = %3
// CHECK-XX: quir.reset %0 : !quir.qubit<1>
// CHECK-NOT: oq3.variable_assign @theta : !quir.angle<64> = %angle
Expand Down

0 comments on commit 183c8b0

Please sign in to comment.