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
10 changes: 10 additions & 0 deletions include/PTO/IR/PTOOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def AddPtrOp : PTO_Op<"addptr", [
//===----------------------------------------------------------------------===//

def LoadScalarOp : PTO_Op<"load_scalar", [
OpPipeInterface,
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
]> {
let summary = "Load a single scalar element from a pointer at offset.";
Expand All @@ -116,9 +117,14 @@ def LoadScalarOp : PTO_Op<"load_scalar", [
let assemblyFormat = [{
$ptr `[` $offset `]` attr-dict `:` type($ptr) `->` type($value)
}];

let extraClassDeclaration = [{
::mlir::pto::PIPE getPipe() { return ::mlir::pto::PIPE::PIPE_S; }
}];
}

def StoreScalarOp : PTO_Op<"store_scalar", [
OpPipeInterface,
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
]> {
let summary = "Store a single scalar element to a pointer at offset.";
Expand All @@ -136,6 +142,10 @@ def StoreScalarOp : PTO_Op<"store_scalar", [
let assemblyFormat = [{
$value `,` $ptr `[` $offset `]` attr-dict `:` type($ptr) `,` type($value)
}];

let extraClassDeclaration = [{
::mlir::pto::PIPE getPipe() { return ::mlir::pto::PIPE::PIPE_S; }
}];
}

def MakeTensorViewOp : PTO_Op<"make_tensor_view", [AttrSizedOperandSegments]> {
Expand Down
8 changes: 8 additions & 0 deletions include/PTO/Transforms/InsertSync/PTOIRTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/BuiltinOps.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>

namespace mlir {
namespace pto {
Expand Down Expand Up @@ -76,12 +77,19 @@ class PTOIRTranslator {

// 根据 Values 填充 Def/Use 列表
void UpdateDefUseVec(ValueRange values, SmallVector<const BaseMemInfo *> &vec);

// scalar 访问切片建模:按 ptr+offset 构建访问级依赖信息。
void UpdateScalarDefUseVec(Value ptr, Value offset, Type scalarType,
SmallVector<const BaseMemInfo *> &vec);

// 调试辅助
std::string getPipelineName(PipelineType pipe);
void printMemInfoList(llvm::raw_ostream &os,
const SmallVector<const BaseMemInfo *> &list,
AsmState &state);

// 持久化 scalar 访问切片,保证 def/use 指针在分析期间有效。
std::vector<std::unique_ptr<BaseMemInfo>> scalarAccessMemInfoPool_;
};

} // namespace pto
Expand Down
19 changes: 13 additions & 6 deletions include/PTO/Transforms/InsertSync/SyncCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ enum class TCoreType {
struct BaseMemInfo {
BaseMemInfo(
Value baseBuffer, Value rootBuffer, pto::AddressSpace scope,
SmallVector<uint64_t> baseAddresses, uint64_t allocateSize)
SmallVector<uint64_t> baseAddresses, uint64_t allocateSize,
bool unknownRange = false)
: baseBuffer(baseBuffer), rootBuffer(rootBuffer), scope(scope),
baseAddresses(std::move(baseAddresses)), allocateSize(allocateSize) {}
baseAddresses(std::move(baseAddresses)), allocateSize(allocateSize),
unknownRange(unknownRange) {}

/// baseBuffer: 当前操作直接使用的 Buffer (可能是 View 或 Alias)
Value baseBuffer;
Expand All @@ -85,6 +87,8 @@ struct BaseMemInfo {
pto::AddressSpace scope;
SmallVector<uint64_t> baseAddresses; // 用于 Offset 分析
uint64_t allocateSize;
// True means alias range is unknown and must be treated conservatively.
bool unknownRange{false};

bool areVectorEqual(const SmallVector<uint64_t>& vec1,
const SmallVector<uint64_t>& vec2) const {
Expand All @@ -99,6 +103,7 @@ struct BaseMemInfo {
if (!areVectorEqual(baseAddresses, other.baseAddresses)) return false;
if (rootBuffer != other.rootBuffer) return false;
if (scope != other.scope) return false;
if (unknownRange != other.unknownRange) return false;
// allocateSize 和 baseBuffer 的严格相等性在某些别名分析中可能太强了,
// 但为了保持原有逻辑,先保留。重点是 rootBuffer 必须一致。
if (allocateSize != other.allocateSize) return false;
Expand All @@ -108,12 +113,14 @@ struct BaseMemInfo {

std::unique_ptr<BaseMemInfo> clone() const {
return std::make_unique<BaseMemInfo>(
baseBuffer, rootBuffer, scope, baseAddresses, allocateSize);
baseBuffer, rootBuffer, scope, baseAddresses, allocateSize,
unknownRange);
}

std::unique_ptr<BaseMemInfo> clone(Value cloneBaseBuffer) const {
return std::make_unique<BaseMemInfo>(
cloneBaseBuffer, rootBuffer, scope, baseAddresses, allocateSize);
cloneBaseBuffer, rootBuffer, scope, baseAddresses, allocateSize,
unknownRange);
}
};

Expand Down Expand Up @@ -355,4 +362,4 @@ void checkCondition(bool condition, const std::string &message);
} // namespace pto
} // namespace mlir

#endif // MLIR_DIALECT_PTO_TRANSFORMS_SYNC_COMMON_H
#endif // MLIR_DIALECT_PTO_TRANSFORMS_SYNC_COMMON_H
5 changes: 3 additions & 2 deletions lib/PTO/Transforms/InsertSync/InsertSyncAnalysis.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "PTO/Transforms/InsertSync/InsertSyncAnalysis.h"
#include "PTO/IR/PTO.h"
#include "PTO/Transforms/InsertSync/SyncCommon.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand Down Expand Up @@ -121,9 +122,9 @@ bool InsertSyncAnalysis::IsNoNeedToInsertSync(
const PipelineType frontPipe = frontCompound->kPipeValue;
const PipelineType nowPipe = nowCompound->kPipeValue;

if (frontPipe == nowPipe && frontPipe == PipelineType::PIPE_S) {
// Scalar pipe is in-order on target hardware; skip same-pipe sync.
if (frontPipe == nowPipe && frontPipe == PipelineType::PIPE_S)
return true;
}

if (nowCompound->elementOp == frontCompound->elementOp && !isBackwardDep) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions lib/PTO/Transforms/InsertSync/MemoryDependentAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ bool MemoryDependentAnalyzer::MemAlias(const BaseMemInfo *a,
// 2. Local Memory (UB/L1)

if (a->rootBuffer == b->rootBuffer) {
if (a->unknownRange || b->unknownRange) return true;
if (a->baseAddresses.empty() || b->baseAddresses.empty()) return true;
if (a->allocateSize == 0 || b->allocateSize == 0) return true;
return isBufferAddressRangeOverlap(a, b);
}

Expand Down Expand Up @@ -182,6 +184,7 @@ bool MemoryDependentAnalyzer::isGMBufferOverlap(const BaseMemInfo *a,
return true;
}

if (a->unknownRange || b->unknownRange) return true;
if (a->baseAddresses.empty() || b->baseAddresses.empty()) return true;
if (a->allocateSize == 0 || b->allocateSize == 0) return true;

Expand Down
Loading
Loading