Skip to content
Open
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
22 changes: 22 additions & 0 deletions include/PTO/Transforms/CppPostprocess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2026 Huawei Technologies Co., Ltd.
// This program is free software, you can redistribute it and/or modify it under the terms and conditions of
// CANN Open Software License Agreement Version 2.0 (the "License").
// Please refer to the License for details. You may not use this file except in compliance with the License.
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
// See LICENSE in the root of the software repository for the full text of the License.

#ifndef MLIR_DIALECT_PTO_TRANSFORMS_CPPPOSTPROCESS_H
#define MLIR_DIALECT_PTO_TRANSFORMS_CPPPOSTPROCESS_H

#include <string>

namespace mlir {
namespace pto {

bool rewriteLastUseMarkersInCpp(std::string &cpp);

} // namespace pto
} // namespace mlir

#endif // MLIR_DIALECT_PTO_TRANSFORMS_CPPPOSTPROCESS_H
3 changes: 3 additions & 0 deletions include/PTO/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ std::unique_ptr<Pass> createPTOViewToMemrefPass();
std::unique_ptr<Pass> createPTOMaterializeTileHandlesPass();
std::unique_ptr<Pass> createInferPTOLayoutPass();
std::unique_ptr<Pass> createPTOA5NormalizeTMovPass();
std::unique_ptr<Pass> createFusionPlanPass();
std::unique_ptr<Pass> createOpSchedulingPass();
std::unique_ptr<Pass> createPTOMarkLastUsePass();

//===----------------------------------------------------------------------===//
// Registration
Expand Down
36 changes: 36 additions & 0 deletions include/PTO/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ def PTOLoweringSyncToPipe : Pass<"pto-lowering-sync-to-pipe", "func::FuncOp"> {
];
}

def FusionPlan : Pass<"pto-fusion-plan", "func::FuncOp"> {
let summary = "Build conservative tile-fusion planning groups from block-local analysis";
let description = [{
Consumes PreFusionAnalysis results, forms conservative block-local fusion
groups for currently supported tile-native compute ops, and annotates
accepted group members with:
- pto.fusion.group_id
- pto.fusion.order
}];
let constructor = "mlir::pto::createFusionPlanPass()";
let dependentDialects = ["mlir::pto::PTODialect"];
}

def OpScheduling : Pass<"pto-op-scheduling", "func::FuncOp"> {
let summary = "Compact planned fusion groups into block-local contiguous spans";
let description = [{
Consumes fusion planning metadata emitted by FusionPlanPass and performs
block-local instruction scheduling to make each accepted fusion group a
contiguous span without redefining group membership.
}];
let constructor = "mlir::pto::createOpSchedulingPass()";
let dependentDialects = ["mlir::pto::PTODialect"];
}

def PTOMarkLastUse : Pass<"pto-mark-last-use", "func::FuncOp"> {
let summary = "Mark scheduled tile-fusion operand last-use slots";
let description = [{
Walks scheduled tile-fusion spans identified by pto.fusion.group_id /
pto.fusion.order and annotates each op with a stable per-input last-use
bit mask. The analysis considers both later in-span uses and later
post-span uses in the enclosing block.
}];
let constructor = "mlir::pto::createPTOMarkLastUsePass()";
let dependentDialects = ["mlir::pto::PTODialect"];
}

def PTOWrapFunctionsInSections : Pass<"pto-wrap-functions-in-sections", "func::FuncOp"> {
let summary = "Wrap attributed single-core functions in PTO cube/vector sections";
let description = [{
Expand Down
135 changes: 135 additions & 0 deletions include/PTO/Transforms/TileFusion/FusionAnalysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) 2026 Huawei Technologies Co., Ltd.
// This program is free software, you can redistribute it and/or modify it under the terms and conditions of
// CANN Open Software License Agreement Version 2.0 (the "License").
// Please refer to the License for details. You may not use this file except in compliance with the License.
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
// See LICENSE in the root of the software repository for the full text of the License.

#ifndef PTO_TRANSFORMS_TILEFUSION_FUSIONANALYSIS_H
#define PTO_TRANSFORMS_TILEFUSION_FUSIONANALYSIS_H

#include "PTO/Transforms/TileFusion/FusionOpSemantics.h"

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LLVM.h"

#include <cassert>
#include <optional>

namespace mlir {
namespace pto {

enum class IterationDomainProof {
Proven,
Unproven,
};

enum class IterationDomainUnprovenReason {
None,
MissingTileDomain,
DynamicShape,
InconsistentShape,
};

struct IterationDomainInfo {
int64_t vRow = ShapedType::kDynamic;
int64_t vCol = ShapedType::kDynamic;
IterationDomainProof proof = IterationDomainProof::Unproven;
IterationDomainUnprovenReason unprovenReason =
IterationDomainUnprovenReason::MissingTileDomain;
};

struct IterationDomainClass {
unsigned id = 0;
IterationDomainInfo info;
SmallVector<unsigned, 4> members;
};

struct FusionDFGEdge {
unsigned producerNode = 0;
unsigned consumerNode = 0;
Value value;
};

struct FusionValueLiveness {
Value value;
std::optional<unsigned> producerNode;
SmallVector<unsigned, 4> consumerNodes;
SmallVector<unsigned, 2> writeInstances;
std::optional<unsigned> lastLocalConsumer;
bool hasExternalUsers = false;
bool escapesBlock = false;
bool hasLocalBoundaryUsers = false;
bool hasLocalHardBoundaryUsers = false;
};

enum class FusionWriteInstanceEscapeClass {
Internal,
LocalBoundaryExternal,
HardExternal,
};

struct FusionWriteInstanceLiveness {
unsigned id = 0;
Value value;
Value storageValue;
std::optional<unsigned> producerNode;
SmallVector<unsigned, 4> consumerNodes;
std::optional<unsigned> lastLocalConsumer;
FusionWriteInstanceEscapeClass escapeClass =
FusionWriteInstanceEscapeClass::Internal;
bool hasExternalUsers = false;
bool escapesBlock = false;
bool hasLocalBoundaryUsers = false;
bool hasLocalHardBoundaryUsers = false;
};

struct FusionComputeNode {
unsigned id = 0;
unsigned blockOrder = 0;
Operation *op = nullptr;
FusionOpSemantics semantics;
unsigned iterationDomainClass = 0;
SmallVector<unsigned, 4> incomingEdges;
SmallVector<unsigned, 4> outgoingEdges;
};

struct FusionBlockAnalysis {
Block *block = nullptr;
SmallVector<FusionComputeNode, 8> computeNodes;
SmallVector<IterationDomainClass, 4> iterationDomainClasses;
SmallVector<FusionDFGEdge, 8> edges;
SmallVector<FusionValueLiveness, 8> liveness;
SmallVector<FusionWriteInstanceLiveness, 8> writeInstances;
};

struct PreFusionAnalysisResult {
SmallVector<FusionBlockAnalysis, 8> blocks;
};

FailureOr<PreFusionAnalysisResult> buildPreFusionAnalysis(func::FuncOp func);

class PreFusionAnalysis {
public:
explicit PreFusionAnalysis(func::FuncOp func) {
FailureOr<PreFusionAnalysisResult> resultOr = buildPreFusionAnalysis(func);
if (succeeded(resultOr))
result = std::move(*resultOr);
}

bool isValid() const { return result.has_value(); }

const PreFusionAnalysisResult &getResult() const {
assert(result && "expected valid pre-fusion analysis result");
return *result;
}

private:
std::optional<PreFusionAnalysisResult> result;
};

} // namespace pto
} // namespace mlir

#endif // PTO_TRANSFORMS_TILEFUSION_FUSIONANALYSIS_H
52 changes: 52 additions & 0 deletions include/PTO/Transforms/TileFusion/FusionOpSemantics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2026 Huawei Technologies Co., Ltd.
// This program is free software, you can redistribute it and/or modify it under the terms and conditions of
// CANN Open Software License Agreement Version 2.0 (the "License").
// Please refer to the License for details. You may not use this file except in compliance with the License.
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
// See LICENSE in the root of the software repository for the full text of the License.

#ifndef PTO_TRANSFORMS_TILEFUSION_FUSIONOPSEMANTICS_H
#define PTO_TRANSFORMS_TILEFUSION_FUSIONOPSEMANTICS_H

#include "PTO/IR/PTO.h"

#include "mlir/Support/LLVM.h"

#include <string>

namespace mlir {
namespace pto {

enum class FusionOpKind {
Compute,
LocalBoundary,
HardBoundary,
};

enum class FusionComputeFamily {
Unknown,
Elementwise,
ScalarExpand,
RowBroadcastBinary,
ReduceRow,
ReduceCol,
};

struct FusionOpSemantics {
FusionOpKind kind = FusionOpKind::HardBoundary;
FusionComputeFamily computeFamily = FusionComputeFamily::Unknown;
Operation *op = nullptr;
std::string opName;
SmallVector<Value, 4> tileInputs;
SmallVector<Value, 2> tileOutputs;
SmallVector<Value, 2> scalarInputs;
};

bool isSupportedPreFusionComputeOp(StringRef opName);
FailureOr<FusionOpSemantics> getFusionOpSemantics(Operation *op);

} // namespace pto
} // namespace mlir

#endif // PTO_TRANSFORMS_TILEFUSION_FUSIONOPSEMANTICS_H
6 changes: 6 additions & 0 deletions lib/PTO/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
# See LICENSE in the root of the software repository for the full text of the License.

add_mlir_dialect_library(PTOTransforms
TileFusion/FusionAnalysis.cpp
TileFusion/FusionOpSemantics.cpp
TileFusion/PTOFusionPlan.cpp
TileFusion/PTOOpScheduling.cpp
TileFusion/PTOMarkLastUse.cpp
InsertSync/PTOInsertSync.cpp
PTOInjectBarrierAllSync.cpp
InsertSync/InsertSyncDebug.cpp
PTOViewToMemref.cpp
PTOToEmitC.cpp
CppPostprocess.cpp
Utils.cpp
OptMemPlanForPipeline.cpp
AllocToPointerCast.cpp
Expand Down
Loading
Loading