Skip to content

Commit

Permalink
Use .inc files instead of splitting patterns out
Browse files Browse the repository at this point in the history
  • Loading branch information
annagrin committed Jan 31, 2025
1 parent 718d9ed commit f33a3b9
Show file tree
Hide file tree
Showing 14 changed files with 568 additions and 680 deletions.
3 changes: 0 additions & 3 deletions lib/Optimizer/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ add_cudaq_library(OptTransforms
ArgumentSynthesis.cpp
BasisConversion.cpp
CollapseStores.cpp
CollapseStoresPatterns.cpp
CombineMeasurements.cpp
CombineQuantumAlloc.cpp
ConstPropComplex.cpp
Expand All @@ -36,11 +35,9 @@ add_cudaq_library(OptTransforms
GlobalizeArrayValues.cpp
LambdaLifting.cpp
LiftArrayAlloc.cpp
LiftArrayAllocPatterns.cpp
LinearCtrlRelations.cpp
LoopAnalysis.cpp
LoopNormalize.cpp
LoopNormalizePatterns.cpp
LoopPeeling.cpp
LoopUnroll.cpp
LowerToCFG.cpp
Expand Down
3 changes: 2 additions & 1 deletion lib/Optimizer/Transforms/CollapseStores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

#include "CollapseStoresPatterns.h"
#include "PassDetails.h"
#include "cudaq/Optimizer/Builder/Intrinsics.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
Expand All @@ -28,6 +27,8 @@ namespace cudaq::opt {

using namespace mlir;

#include "CollapseStores.inc"

namespace {
class CollapseStoresPass
: public cudaq::opt::impl::CollapseStoresBase<CollapseStoresPass> {
Expand Down
123 changes: 123 additions & 0 deletions lib/Optimizer/Transforms/CollapseStores.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

// These patterns are used by the collapse-stores and cc-loop-unroll passes.

// This file must be included after a `using namespace mlir;` as it uses bare
// identifiers from that namespace.

namespace {
/// Remove stores followed by a store to the same pointer
/// if the pointer is not used in between.
/// ```
/// cc.store %c0_i64, %5 : !cc.ptr<i64>
/// // no use of %5 until next line
/// cc.store %3, %5 : !cc.ptr<i64>
/// ───────────────────────────────────────────
/// cc.store %3, %5 : !cc.ptr<i64>
/// ```
class RemoveUselessStorePattern : public OpRewritePattern<cudaq::cc::StoreOp> {
public:
using OpRewritePattern::OpRewritePattern;

explicit RemoveUselessStorePattern(MLIRContext *ctx)
: OpRewritePattern(ctx) {}

/// Remove stores followed by a store to the same pointer
/// if the pointer is not used in between.
/// ```
/// cc.store %c0_i64, %5 : !cc.ptr<i64>
/// // no use of %5 until next line
/// cc.store %3, %5 : !cc.ptr<i64>
/// ───────────────────────────────────────────
/// cc.store %3, %5 : !cc.ptr<i64>
/// ```
LogicalResult matchAndRewrite(cudaq::cc::StoreOp store,
PatternRewriter &rewriter) const override {
if (isUselessStore(store)) {
rewriter.eraseOp(store);
return success();
}
return failure();
}

private:
/// Detect if the current store can be removed.
static bool isUselessStore(cudaq::cc::StoreOp store) {
Value currentPtr;

if (!isStoreToStack(store))
return false;

auto block = store.getOperation()->getBlock();
for (auto &op : *block) {
if (auto s = dyn_cast<cudaq::cc::StoreOp>(&op)) {
auto nextPtr = s.getPtrvalue();
if (store == s) {
// Start searching from the current store
currentPtr = nextPtr;
} else {
// Found an overriding store, the current store is useless
if (currentPtr == nextPtr)
return true;

// Found a use for a current ptr before the overriding store
if (currentPtr && isUsed(currentPtr, &op))
return false;
}
} else {
// Found a use for a current ptr before the overriding store
if (currentPtr && isUsed(currentPtr, &op))
return false;
}
}
// No multiple stores to the same location found
return false;
}

/// Detect stores to stack locations
/// ```
/// %1 = cc.alloca !cc.array<i64 x 2>
///
/// %2 = cc.cast %1 : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64>
/// cc.store %c0_i64, %2 : !cc.ptr<i64>
///
/// %3 = cc.compute_ptr %1[1] : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64>
/// cc.store %c0_i64, %3 : !cc.ptr<i64>
/// ```
static bool isStoreToStack(cudaq::cc::StoreOp store) {
auto ptrOp = store.getPtrvalue();
if (auto cast = ptrOp.getDefiningOp<cudaq::cc::CastOp>())
ptrOp = cast.getOperand();

if (auto computePtr = ptrOp.getDefiningOp<cudaq::cc::ComputePtrOp>())
ptrOp = computePtr.getBase();

if (auto alloca = ptrOp.getDefiningOp<cudaq::cc::AllocaOp>())
return true;

return false;
}

/// Detect if value is used in the op or its nested blocks.
static bool isUsed(Value v, Operation *op) {
for (auto opnd : op->getOperands())
if (opnd == v)
return true;

for (auto &region : op->getRegions())
for (auto &b : region)
for (auto &innerOp : b)
if (isUsed(v, &innerOp))
return true;

return false;
}
};

} // namespace
115 changes: 0 additions & 115 deletions lib/Optimizer/Transforms/CollapseStoresPatterns.cpp

This file was deleted.

60 changes: 0 additions & 60 deletions lib/Optimizer/Transforms/CollapseStoresPatterns.h

This file was deleted.

3 changes: 2 additions & 1 deletion lib/Optimizer/Transforms/LiftArrayAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

#include "LiftArrayAllocPatterns.h"
#include "PassDetails.h"
#include "cudaq/Optimizer/Builder/Intrinsics.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
Expand All @@ -28,6 +27,8 @@ namespace cudaq::opt {

using namespace mlir;

#include "LiftArrayAlloc.inc"

namespace {
class LiftArrayAllocPass
: public cudaq::opt::impl::LiftArrayAllocBase<LiftArrayAllocPass> {
Expand Down
Loading

0 comments on commit f33a3b9

Please sign in to comment.