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

[AMDAIEControlCodeLowering] Separate DmaCpyNdToHalfDmaCpyNd to a new pass #945

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This pipeline is obtained by going into Passes.cpp, and dumping the pass pipeline (at the end of addAMDAIEObjectFifoLoweringPasses) using `passManager.dump()`. This test is included, as it can be useful to have a reference in IR of all the passes that are run.

// RUN: iree-opt --pass-pipeline="builtin.module(fold-memref-alias-ops,iree-amdaie-distribute-l1-allocations,iree-amdaie-convert-to-dma,iree-amdaie-normalize-loop-bounds,iree-amdaie-insert-cores,iree-amdaie-localize-logicalobjectfifo,cse,iree-amdaie-distribute-cores-and-objectfifos,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-split-logical-objectfifos-for-connection-reuse,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-assign-tiles,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-to-circular-dma,func.func(iree-amdaie-create-aie-workgroup),cse,iree-amdaie-dma-cse,iree-amdaie-hoist-logical-objectfifo,iree-amdaie-canonicalize-doubly-strided-op{fold-single-dims=false},iree-amdaie-flatten-logicalobjectfifo,iree-amdaie-assign-logical-objectfifo-depth{l1-buffer-depth=2 l2-buffer-depth=2 l3-buffer-depth=1},iree-amdaie-access-to-acquire-release,iree-amdaie-none-access-to-temporary-buffer,iree-amdaie-assign-connection-types,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-composition{only-zero-stride-on-outer-dim=true},cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-cse,iree-amdaie-assign-npu-dma-bd-ids,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-controlcode-loop-unroll,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-cse,iree-amdaie-canonicalize-doubly-strided-op{fold-single-dims=false},canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-convert-core-forall-to-for,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-assign-channels,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-objfifo-bufferization,iree-amdaie-connection-to-flow,iree-amdaie-assign-packet-ids,iree-amdaie-controlcode-lowering,iree-amdaie-controlcode-to-transaction,iree-amdaie-acquire-release-to-use-lock,iree-amdaie-canonicalize-npu-dma-cpy-nd{nb-dimensions=4},canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-sink-into-core,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-lower-to-aie,iree-amdaie-remove-memoryspace)" --split-input-file %s | FileCheck %s
// RUN: iree-opt --pass-pipeline="builtin.module(fold-memref-alias-ops,iree-amdaie-distribute-l1-allocations,iree-amdaie-convert-to-dma,iree-amdaie-normalize-loop-bounds,iree-amdaie-insert-cores,iree-amdaie-localize-logicalobjectfifo,cse,iree-amdaie-distribute-cores-and-objectfifos,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-split-logical-objectfifos-for-connection-reuse,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-assign-tiles,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-to-circular-dma,func.func(iree-amdaie-create-aie-workgroup),cse,iree-amdaie-dma-cse,iree-amdaie-hoist-logical-objectfifo,iree-amdaie-canonicalize-doubly-strided-op{fold-single-dims=false},iree-amdaie-flatten-logicalobjectfifo,iree-amdaie-assign-logical-objectfifo-depth{l1-buffer-depth=2 l2-buffer-depth=2 l3-buffer-depth=1},iree-amdaie-access-to-acquire-release,iree-amdaie-none-access-to-temporary-buffer,iree-amdaie-assign-connection-types,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-composition{only-zero-stride-on-outer-dim=true},cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-cse,iree-amdaie-assign-npu-dma-bd-ids,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-controlcode-loop-unroll,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-dma-cse,iree-amdaie-canonicalize-doubly-strided-op{fold-single-dims=false},canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-convert-core-forall-to-for,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-assign-channels,cse,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-objfifo-bufferization,iree-amdaie-connection-to-flow,iree-amdaie-assign-packet-ids,iree-amdaie-npu-dma-to-half-dma-cpy-nd,iree-amdaie-controlcode-lowering,iree-amdaie-controlcode-to-transaction,iree-amdaie-acquire-release-to-use-lock,iree-amdaie-canonicalize-npu-dma-cpy-nd{nb-dimensions=4},canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-sink-into-core,canonicalize{ max-iterations=10 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true},iree-amdaie-lower-to-aie,iree-amdaie-remove-memoryspace)" --split-input-file %s | FileCheck %s



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,6 @@

namespace mlir::iree_compiler::AMDAIE {

struct DmaCpyNdToHalfDmaCpyNdConverter final
: OpConversionPattern<AMDAIE::NpuDmaCpyNdOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult matchAndRewrite(
AMDAIE::NpuDmaCpyNdOp dmaOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
LLVM_DEBUG(llvm::dbgs() << "matchAndRewrite[AMDAIE::NpuDmaCpyNdOp]\n");
AMDAIE::ConnectionOp connectionOp = dmaOp.getConnectionOp();
if (!connectionOp) {
return dmaOp.emitOpError()
<< "should operate on an `amdaie.connection` op";
}
// Convert source half.
Value source =
dmaOp.getSource() ? dmaOp.getSource() : connectionOp.getSource();
if (connectionOp.getSourceChannels().size() != 1)
return connectionOp.emitOpError() << "expected a single source channel";
auto sourceChannelOp = dyn_cast<AMDAIE::ChannelOp>(
connectionOp.getSourceChannels()[0].getDefiningOp());
bool hasAsyncSourceToken =
llvm::any_of(dmaOp.getAsyncTokens(), [](Value token) {
return isa<AMDAIE::AsyncSourceTokenType>(token.getType());
});
SmallVector<Type> resultTypes = {
rewriter.getType<AMDAIE::AsyncTokenType>()};
TypeRange sourceResultTypes =
hasAsyncSourceToken ? TypeRange{resultTypes} : TypeRange{};
rewriter.setInsertionPoint(dmaOp);
auto sourceDma = rewriter.create<AMDAIE::NpuHalfDmaCpyNdOp>(
dmaOp.getLoc(), sourceResultTypes, connectionOp, source,
dmaOp.getSourceMixedOffsets(), dmaOp.getSourceMixedSizes(),
dmaOp.getSourceMixedStrides(), dmaOp.getSourceBdId(), sourceChannelOp);

// Convert target half.
Value target =
dmaOp.getTarget() ? dmaOp.getTarget() : connectionOp.getTarget();
if (connectionOp.getTargetChannels().size() != 1)
return connectionOp.emitOpError() << "expected a single target channel";
auto targetChannelOp = dyn_cast<AMDAIE::ChannelOp>(
connectionOp.getTargetChannels()[0].getDefiningOp());
bool hasAsyncTargetToken =
llvm::any_of(dmaOp.getAsyncTokens(), [](Value token) {
return isa<AMDAIE::AsyncTargetTokenType>(token.getType());
});
TypeRange targetResultTypes =
hasAsyncTargetToken ? TypeRange{resultTypes} : TypeRange{};
auto targetDma = rewriter.create<AMDAIE::NpuHalfDmaCpyNdOp>(
dmaOp.getLoc(), targetResultTypes, connectionOp, target,
dmaOp.getTargetMixedOffsets(), dmaOp.getTargetMixedSizes(),
dmaOp.getTargetMixedStrides(), dmaOp.getTargetBdId(), targetChannelOp);
if (dmaOp.getNumResults() == 1) {
if (sourceDma.getNumResults() == 1) {
rewriter.replaceUsesWithIf(
dmaOp.getResult(0), sourceDma.getResult(0), [&](OpOperand &use) {
return isa<AMDAIE::AsyncSourceTokenType>(use.get().getType()) &&
isa<AMDAIE::NpuDmaWaitOp>(use.getOwner());
});
}
if (targetDma.getNumResults() == 1) {
rewriter.replaceUsesWithIf(
dmaOp.getResult(0), targetDma.getResult(0), [&](OpOperand &use) {
return isa<AMDAIE::AsyncTargetTokenType>(use.get().getType()) &&
isa<AMDAIE::NpuDmaWaitOp>(use.getOwner());
});
}
if (!dmaOp.getResult(0).use_empty())
return dmaOp.emitOpError() << "should not have any uses anymore";
}
rewriter.eraseOp(dmaOp);
return success();
}
};

struct HalfDmaCpyNdToNpuConverter final
: OpConversionPattern<AMDAIE::NpuHalfDmaCpyNdOp> {
using OpConversionPattern::OpConversionPattern;
Expand Down Expand Up @@ -159,22 +85,18 @@ struct HalfDmaCpyNdToNpuConverter final
if (stride == 0) {
repeatCount = size;
} else {
iterationStride =
std::max(stride * elemWidthInBits / minStrideBitWidth,
(int64_t)1);
iterationStride = std::max(
stride * elemWidthInBits / minStrideBitWidth, (int64_t)1);
iterationSize = size;
if (stride == 1)
size = (size * elemWidthInBits) / minStrideBitWidth;
if (stride == 1) size = (size * elemWidthInBits) / minStrideBitWidth;
repeatCount = iterationSize;
}
} else {
staticStrides.push_back(
std::max(stride * elemWidthInBits / minStrideBitWidth,
(int64_t)1));
std::max(stride * elemWidthInBits / minStrideBitWidth, (int64_t)1));
// Innermost size needs to account for addressing granularity.
if (iter.index() == (sizes.size() - 1)) {
staticSizes.push_back(size * elemWidthInBits /
minStrideBitWidth);
staticSizes.push_back(size * elemWidthInBits / minStrideBitWidth);
} else {
staticSizes.push_back(size);
}
Expand Down Expand Up @@ -323,9 +245,7 @@ void AMDAIEControlCodeLoweringPass::runOnOperation() {
RewritePatternSet patterns(context);
ConversionTarget conversionTarget(*context);
conversionTarget.addLegalDialect<AMDAIEDialect>();
conversionTarget
.addIllegalOp<AMDAIE::NpuHalfDmaCpyNdOp, AMDAIE::NpuDmaCpyNdOp>();
patterns.insert<DmaCpyNdToHalfDmaCpyNdConverter>(context);
conversionTarget.addIllegalOp<AMDAIE::NpuHalfDmaCpyNdOp>();
patterns.insert<HalfDmaCpyNdToNpuConverter>(context, deviceModel);
if (failed(applyPartialConversion(parentOp, conversionTarget,
std::move(patterns)))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2024 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>

#include "iree-amd-aie/IR/AMDAIEOps.h"
#include "iree-amd-aie/Transforms/AMDAIEDmaUtils.h"
#include "iree-amd-aie/Transforms/AMDAIEUtils.h"
#include "iree-amd-aie/Transforms/Passes.h"
#include "iree-amd-aie/Transforms/Transforms.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

#define DEBUG_TYPE "iree-amdaie-npu-dma-to-half-dma-cpy-nd"

namespace mlir::iree_compiler::AMDAIE {

struct NpuDmaToHalfDmaCpyNdConverter final
: OpConversionPattern<AMDAIE::NpuDmaCpyNdOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult matchAndRewrite(
AMDAIE::NpuDmaCpyNdOp dmaOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
LLVM_DEBUG(llvm::dbgs() << "matchAndRewrite[AMDAIE::NpuDmaCpyNdOp]\n");
AMDAIE::ConnectionOp connectionOp = dmaOp.getConnectionOp();
if (!connectionOp) {
return dmaOp.emitOpError()
<< "should operate on an `amdaie.connection` op";
}
// Convert source half.
Value source =
dmaOp.getSource() ? dmaOp.getSource() : connectionOp.getSource();
if (connectionOp.getSourceChannels().size() != 1)
return connectionOp.emitOpError() << "expected a single source channel";
auto sourceChannelOp = dyn_cast<AMDAIE::ChannelOp>(
connectionOp.getSourceChannels()[0].getDefiningOp());
bool hasAsyncSourceToken =
llvm::any_of(dmaOp.getAsyncTokens(), [](Value token) {
return isa<AMDAIE::AsyncSourceTokenType>(token.getType());
});
SmallVector<Type> resultTypes = {
rewriter.getType<AMDAIE::AsyncTokenType>()};
TypeRange sourceResultTypes =
hasAsyncSourceToken ? TypeRange{resultTypes} : TypeRange{};
rewriter.setInsertionPoint(dmaOp);
auto sourceDma = rewriter.create<AMDAIE::NpuHalfDmaCpyNdOp>(
dmaOp.getLoc(), sourceResultTypes, connectionOp, source,
dmaOp.getSourceMixedOffsets(), dmaOp.getSourceMixedSizes(),
dmaOp.getSourceMixedStrides(), dmaOp.getSourceBdId(), sourceChannelOp);

// Convert target half.
Value target =
dmaOp.getTarget() ? dmaOp.getTarget() : connectionOp.getTarget();
if (connectionOp.getTargetChannels().size() != 1)
return connectionOp.emitOpError() << "expected a single target channel";
auto targetChannelOp = dyn_cast<AMDAIE::ChannelOp>(
connectionOp.getTargetChannels()[0].getDefiningOp());
bool hasAsyncTargetToken =
llvm::any_of(dmaOp.getAsyncTokens(), [](Value token) {
return isa<AMDAIE::AsyncTargetTokenType>(token.getType());
});
TypeRange targetResultTypes =
hasAsyncTargetToken ? TypeRange{resultTypes} : TypeRange{};
auto targetDma = rewriter.create<AMDAIE::NpuHalfDmaCpyNdOp>(
dmaOp.getLoc(), targetResultTypes, connectionOp, target,
dmaOp.getTargetMixedOffsets(), dmaOp.getTargetMixedSizes(),
dmaOp.getTargetMixedStrides(), dmaOp.getTargetBdId(), targetChannelOp);
if (dmaOp.getNumResults() == 1) {
if (sourceDma.getNumResults() == 1) {
rewriter.replaceUsesWithIf(
dmaOp.getResult(0), sourceDma.getResult(0), [&](OpOperand &use) {
return isa<AMDAIE::AsyncSourceTokenType>(use.get().getType()) &&
isa<AMDAIE::NpuDmaWaitOp>(use.getOwner());
});
}
if (targetDma.getNumResults() == 1) {
rewriter.replaceUsesWithIf(
dmaOp.getResult(0), targetDma.getResult(0), [&](OpOperand &use) {
return isa<AMDAIE::AsyncTargetTokenType>(use.get().getType()) &&
isa<AMDAIE::NpuDmaWaitOp>(use.getOwner());
});
}
if (!dmaOp.getResult(0).use_empty())
return dmaOp.emitOpError() << "should not have any uses anymore";
}
rewriter.eraseOp(dmaOp);
return success();
}
};

namespace {
class AMDAIENpuDmaToHalfDmaCpyNdPass
: public impl::AMDAIENpuDmaToHalfDmaCpyNdBase<
AMDAIENpuDmaToHalfDmaCpyNdPass> {
public:
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<AMDAIEDialect>();
}
void runOnOperation() override;
};

void AMDAIENpuDmaToHalfDmaCpyNdPass::runOnOperation() {
Operation *parentOp = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
ConversionTarget conversionTarget(*context);
conversionTarget.addLegalDialect<AMDAIEDialect>();
conversionTarget.addIllegalOp<AMDAIE::NpuDmaCpyNdOp>();
patterns.insert<NpuDmaToHalfDmaCpyNdConverter>(context);
if (failed(applyPartialConversion(parentOp, conversionTarget,
std::move(patterns)))) {
return signalPassFailure();
}
}

} // namespace

std::unique_ptr<Pass> createAMDAIENpuDmaToHalfDmaCpyNdPass() {
return std::make_unique<AMDAIENpuDmaToHalfDmaCpyNdPass>();
}

} // namespace mlir::iree_compiler::AMDAIE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ iree_cc_library(
"AMDAIEMapForallToCores.cpp"
"AMDAIENoneAccessToTemporaryBuffer.cpp"
"AMDAIENormalizeLoopBounds.cpp"
"AMDAIENpuDmaToHalfDmaCpyNd.cpp"
"AMDAIEObjFifoBufferization.cpp"
"AMDAIEPackAndTranspose.cpp"
"AMDAIEPad.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace mlir::iree_compiler::AMDAIE {
#define GEN_PASS_DEF_AMDAIEMAPFORALLTOCORES
#define GEN_PASS_DEF_AMDAIENONEACCESSTOTEMPORARYBUFFER
#define GEN_PASS_DEF_AMDAIENORMALIZELOOPBOUNDS
#define GEN_PASS_DEF_AMDAIENPUDMATOHALFDMACPYND
#define GEN_PASS_DEF_AMDAIEOBJFIFOBUFFERIZATION
#define GEN_PASS_DEF_AMDAIEPACKANDTRANSPOSE
#define GEN_PASS_DEF_AMDAIECONVERTTODMA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ void addAMDAIEObjectFifoLoweringPasses(
passManager.addPass(createAMDAIEConnectionToFlowPass());
passManager.addPass(createAMDAIEAssignPacketIdsPass());

passManager.addPass(createAMDAIENpuDmaToHalfDmaCpyNdPass());
passManager.addPass(createAMDAIEControlCodeLoweringPass());
passManager.addPass(createAMDAIEControlCodeToTransactionPass());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ std::unique_ptr<Pass> createAMDAIEControlCodeForallToForPass();
/// Pass to unroll the loops within the control code regions.
std::unique_ptr<Pass> createAMDAIEControlCodeLoopUnrollPass();

/// Pass to convert control code DMA operations into NPU writes and syncs.
/// Pass to convert control code HalfDmaCpyNd into NPU WriteBd, AddressPatch,
/// PushToQueue operations.
std::unique_ptr<Pass> createAMDAIEControlCodeLoweringPass();

/// Pass to convert control code into a transaction binary.
Expand Down Expand Up @@ -253,6 +254,9 @@ std::unique_ptr<Pass> createAMDAIENoneAccessToTemporaryBufferPass();
/// Normalize the loop bounds of `scf.for` and `scf.forall`.
std::unique_ptr<Pass> createAMDAIENormalizeLoopBoundsPass();

/// Pass to convert control code DMA operations into HalfDmaCpyNd.
std::unique_ptr<Pass> createAMDAIENpuDmaToHalfDmaCpyNdPass();

/// Create a pass to bufferize logical objectFifos.
std::unique_ptr<Pass> createAMDAIEObjFifoBufferizationPass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ def AMDAIENormalizeLoopBounds :
let constructor = "mlir::iree_compiler::AMDAIE::createAMDAIENormalizeLoopBoundsPass()";
}

def AMDAIENpuDmaToHalfDmaCpyNd :
Pass<"iree-amdaie-npu-dma-to-half-dma-cpy-nd", ""> {
let summary = "Lower DmaCpNd ops to HalfDmaCpyNd ops";
let constructor = "mlir::iree_compiler::AMDAIE::createAMDAIENpuDmaToHalfDmaCpyNdPass()";
}

def AMDAIEObjFifoBufferization :
Pass<"iree-amdaie-objfifo-bufferization", ""> {
let summary = "Bufferize logical objectFifos.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ iree_lit_test_suite(
"map_forall_to_cores.mlir"
"none_access_to_temporary_buffer.mlir"
"normalize_loop_bounds.mlir"
"npu_dma_to_half_dma_cpy_nd.mlir"
"obj_fifo_bufferization.mlir"
"pack_and_transpose_level1.mlir"
"pack_and_transpose_level2.mlir"
Expand Down
Loading
Loading