Skip to content

Commit

Permalink
[core] Move canonical patterns out of tablegen.
Browse files Browse the repository at this point in the history
Fix #476. Eliminate the file Canonical.td and move/rewrite the patterns
from that file.

Signed-off-by: Eric Schweitz <[email protected]>
  • Loading branch information
schweitzpgi committed Dec 11, 2024
1 parent 1a3ffcb commit 8e251c6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 75 deletions.
4 changes: 0 additions & 4 deletions include/cudaq/Optimizer/Dialect/Quake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@
add_cudaq_dialect(Quake quake)
add_cudaq_interface(QuakeInterfaces)
add_cudaq_dialect_doc(QuakeDialect quake)

set(LLVM_TARGET_DEFINITIONS Canonical.td)
mlir_tablegen(Canonical.inc -gen-rewriters)
add_public_tablegen_target(CanonicalIncGen)
67 changes: 0 additions & 67 deletions include/cudaq/Optimizer/Dialect/Quake/Canonical.td

This file was deleted.

1 change: 0 additions & 1 deletion lib/Optimizer/Dialect/Quake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ add_cudaq_dialect_library(QuakeDialect
QuakeDialectIncGen
QuakeOpsIncGen
QuakeTypesIncGen
CanonicalIncGen

LINK_LIBS
CCDialect
Expand Down
90 changes: 90 additions & 0 deletions lib/Optimizer/Dialect/Quake/CanonicalPatterns.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2024 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 canonicalization patterns are used by the canonicalize pass and not
// shared for other uses. Generally speaking, these patterns should be trivial
// peephole optimizations that reduce the size and complexity of the input IR.

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

namespace {

// %4 = quake.veq_size %3 : (!quake.veq<10>) -> 164
// ────────────────────────────────────────────────
// %4 = constant 10 : i64
struct ForwardConstantVeqSizePattern
: public OpRewritePattern<quake::VeqSizeOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(quake::VeqSizeOp veqSize,
PatternRewriter &rewriter) const override {
auto veqTy = dyn_cast<quake::VeqType>(veqSize.getVeq().getType());
if (!veqTy)
return failure();
if (!veqTy.hasSpecifiedSize())
return failure();
auto resTy = veqSize.getType();
rewriter.replaceOpWithNewOp<arith::ConstantIntOp>(veqSize, veqTy.getSize(),
resTy);
return success();
}
};

// %2 = constant 10 : i32
// %3 = quake.alloca !quake.veq<?>[%2 : i32]
// ───────────────────────────────────────────
// %3 = quake.alloca !quake.veq<10>
struct FuseConstantToAllocaPattern : public OpRewritePattern<quake::AllocaOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(quake::AllocaOp alloc,
PatternRewriter &rewriter) const override {
auto size = alloc.getSize();
if (!size)
return failure();
auto intCon = cudaq::opt::factory::getIntIfConstant(size);
if (!intCon)
return failure();
auto veqTy = dyn_cast<quake::VeqType>(alloc.getType());
if (!veqTy)
return failure();
if (veqTy.hasSpecifiedSize())
return failure();
auto loc = alloc.getLoc();
auto resTy = alloc.getType();
auto newAlloc = rewriter.create<quake::AllocaOp>(
loc, static_cast<std::size_t>(*intCon));
rewriter.replaceOpWithNewOp<quake::RelaxSizeOp>(alloc, resTy, newAlloc);
return success();
}
};

// %2 = constant 10 : i32
// %3 = quake.extract_ref %1[%2] : (!quake.veq<?>, i32) -> !quake.ref
// ───────────────────────────────────────────
// %3 = quake.extract_ref %1[10] : (!quake.veq<?>) -> !quake.ref
struct FuseConstantToExtractRefPattern
: public OpRewritePattern<quake::ExtractRefOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(quake::ExtractRefOp extract,
PatternRewriter &rewriter) const override {
auto index = extract.getIndex();
if (!index)
return failure();
auto intCon = cudaq::opt::factory::getIntIfConstant(index);
if (!intCon)
return failure();
rewriter.replaceOpWithNewOp<quake::ExtractRefOp>(
extract, extract.getVeq(), static_cast<std::size_t>(*intCon));
return success();
}
};

} // namespace
10 changes: 7 additions & 3 deletions lib/Optimizer/Dialect/Quake/QuakeOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

using namespace mlir;

namespace {
#include "cudaq/Optimizer/Dialect/Quake/Canonical.inc"
} // namespace
#include "CanonicalPatterns.inc"

static LogicalResult verifyWireResultsAreLinear(Operation *op) {
for (Value v : op->getOpResults())
Expand Down Expand Up @@ -344,6 +342,12 @@ struct ConcatNoOpPattern : public OpRewritePattern<quake::ConcatOp> {
}
};

// %8 = quake.concat %4, %5, %6 : (!quake.ref, !quake.veq<4>,
// !quake.veq<2>) -> !quake.veq<?>
// ───────────────────────────────────────────────────────────
// %.8 = quake.concat %4, %5, %6 : (!quake.ref, !quake.veq<4>,
// !quake.veq<2>) -> !quake.veq<7>
// %8 = quake.relax_size %.8 : (!quake.veq<7>) -> !quake.veq<?>
struct ConcatSizePattern : public OpRewritePattern<quake::ConcatOp> {
using OpRewritePattern::OpRewritePattern;

Expand Down

0 comments on commit 8e251c6

Please sign in to comment.