Skip to content

[TritonNVIDIAGPU] Add dependency tokens to TMEM ops #6520

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

Merged
merged 25 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Expand Up @@ -17,11 +17,6 @@ namespace triton::nvidia_gpu {
// MMA Pipeline Analysis
//===----------------------------------------------------------------------===//

// Returns the TMEMAllocOp and TMEMLoadOp that are used to allocate and load the
// accumulator for the given MMA operation. The TMEMAllocOp and TMEMLoadOp must
// be in the same region as the MMA operation.
std::optional<std::pair<TMEMAllocOp, TMEMLoadOp>>
getTMemAllocAndLoad(MMAv5OpInterface mmaOp);
// Given an MMAv5 operation in a loop, determine if its accumulator can be
// multibuffered.
bool isAccMultibufferingPossible(MMAv5OpInterface mma, scf::ForOp forOp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ void hoistOpsBefore(Operation *refOp,
void hoistOpsBefore(Block *block, Block::iterator it,
const llvm::SetVector<Operation *> &toHoist);

//===----------------------------------------------------------------------===//
// Sinking Utilities
//===----------------------------------------------------------------------===//

// Sink a value redefinition into a block, provided that the block is dominated
// by `in` and postdominated by `out`.
Value sinkValueRedefinition(RewriterBase &rewriter, Value in, Value out,
Block *block);

//===----------------------------------------------------------------------===//
// Loop Pipelining Utilities
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 0 additions & 4 deletions include/triton/Dialect/TritonGPU/Transforms/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ SetVector<Value> getNestedOperands(Operation *op);
// Erase the given loop carried values from the loop, where `loop` is replaced
// with a new loop.
void eraseLoopCarriedValues(scf::ForOp &loop, llvm::BitVector indices);

// Return true if two value sets may refer to the same allocation.
bool mayAliasAllocations(const DenseSet<Value> &lhs,
const DenseSet<Value> &rhs);
} // namespace mlir

namespace mlir::triton {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def MMAv5OpInterface : OpInterface<"MMAv5OpInterface"> {
"void",
"setPredicate",
(ins "::mlir::Value":$pred)>,
InterfaceMethod<"Get the memory dependencies of the accumulator.",
"::mlir::Value",
"getAccDep">,
InterfaceMethod<"Get the mutable memory dependencies of the accumulator.",
"::mlir::MutableOperandRange",
"getAccDepMutable">,
InterfaceMethod<"Get the produced write dependency of the accumulator.",
"::mlir::Value",
"getToken">,
];
}
#endif // TRITON_NVIDIAGPU_OP_INTERFACES
86 changes: 68 additions & 18 deletions include/triton/Dialect/TritonNvidiaGPU/IR/TritonNvidiaGPUOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def TTNG_TCGen5MMAOp : TTNG_Op<"tc_gen5_mma", [
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
DeclareOpInterfaceMethods<DotOpInterface>,
DeclareOpInterfaceMethods<MMAv5OpInterface>,
SameVariadicOperandSize
AttrSizedOperandSegments
]> {
let summary = "block level op mapping to tensorcore gen5 mma";

Expand All @@ -427,29 +427,36 @@ def TTNG_TCGen5MMAOp : TTNG_Op<"tc_gen5_mma", [
If there is a barrier the result will be safe to read after a barrier wait.
If $two_ctas is set the op will execute a matmul across two contiguous CTAs, it will read the data distributed across the two CTAs.
and syncronize both CTAs if the op is synchronous.

This operation takes and produces an optional token to indicate TMEM read
and write on its accumulator operand. When the tokens are present, they can
be used to check aliasing and modref on the accumulator memory.
}];

let arguments = (ins
TTG_MemDescType:$a,
TTG_MemDescType:$b,
TTG_MemDescType:$d,
Optional<TTG_AsyncToken>:$acc_dep,
I1:$useD,
I1:$pred,
Variadic<TTG_MemDescType>:$barriers,
Variadic<I1>:$barrier_preds,
OptionalAttr<UnitAttr>:$two_ctas
);
let results = (outs Optional<TTG_AsyncToken>:$token);

let builders = [
OpBuilder<(ins
"Value":$a, "Value":$b, "Value":$d, "Value":$useD, "Value":$pred,
CArg<"bool", "false">:$two_ctas, CArg<"ValueRange", "{}">:$barriers,
OpBuilder<(ins "Type":$token,
"Value":$a, "Value":$b, "Value":$d, "Value":$acc_dep, "Value":$useD,
"Value":$pred, CArg<"bool", "false">:$two_ctas,
CArg<"ValueRange", "{}">:$barriers,
CArg<"ValueRange", "{}">:$barrier_preds)>
];

let assemblyFormat = [{
$a`,` $b`,` $d`,` $useD`,` $pred
`` custom<BarriersAndPreds>($barriers, $barrier_preds)
$a `,` $b `,` $d `` custom<Token>($acc_dep, type($token)) `,` $useD`,`
$pred `` custom<BarriersAndPreds>($barriers, $barrier_preds)
attr-dict `:` qualified(type($a)) `,` qualified(type($b)) `,`
qualified(type($d)) (`,` qualified(type($barriers))^)?
}];
Expand All @@ -459,20 +466,25 @@ def TTNG_TCGen5MMAScaledOp : TTNG_Op<"tc_gen5_mma_scaled", [
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
DeclareOpInterfaceMethods<DotOpInterface, ["verifyDims", "verifyOutputDims"]>,
DeclareOpInterfaceMethods<MMAv5OpInterface>,
SameVariadicOperandSize
AttrSizedOperandSegments
]> {
let summary = "block level op mapping to tensorcore gen5 mma";

let description = [{
$d += matrix_multiply(scale($lhs, $lhs_scale), scale(rlhs, $rhs_scale))
If no barrier is given the op is assumed to be synchronous otherwise the op will trigger a commit/arrive on the given barrier.
If there is a barrier the result will be safe to read after a barrier wait.

This operation takes and produces an optional token to indicate TMEM read
and write on its accumulator operand. When the tokens are present, they can
be used to check aliasing and modref on the accumulator memory.
}];

let arguments = (ins
TTG_MemDescType:$a,
TTG_MemDescType:$b,
TTG_MemDescType:$d,
Optional<TTG_AsyncToken>:$acc_dep,
TTG_MemDescType:$a_scale,
TTG_MemDescType:$b_scale,
TT_ScaleDotElemTypeAttr:$a_type,
Expand All @@ -482,6 +494,8 @@ def TTNG_TCGen5MMAScaledOp : TTNG_Op<"tc_gen5_mma_scaled", [
Variadic<TTG_MemDescType>:$barriers,
Variadic<I1>:$barrier_preds
);
let results = (outs Optional<TTG_AsyncToken>:$token);

let extraClassDeclaration = [{
int64_t getBlockM();
int64_t getBlockN();
Expand All @@ -491,19 +505,19 @@ def TTNG_TCGen5MMAScaledOp : TTNG_Op<"tc_gen5_mma_scaled", [
let builders = [
// Namespaces need to be prefixed so ODS prefers our
// custom builder signature over the default-generated one.
OpBuilder<(ins
OpBuilder<(ins "::mlir::Type":$token,
"::mlir::Value":$a, "::mlir::Value":$b, "::mlir::Value":$d,
"::mlir::Value":$a_scale, "::mlir::Value":$b_scale,
"::mlir::triton::ScaleDotElemType":$a_type,
"::mlir::Value":$acc_dep, "::mlir::Value":$a_scale,
"::mlir::Value":$b_scale, "::mlir::triton::ScaleDotElemType":$a_type,
"::mlir::triton::ScaleDotElemType":$b_type,
"::mlir::Value":$useD, "::mlir::Value":$pred,
CArg<"::mlir::ValueRange", "{}">:$barriers,
CArg<"::mlir::ValueRange", "{}">:$barrier_preds)>
];

let assemblyFormat = [{
$a `,` $b `,` $d `,` $a_scale `,` $b_scale `,` $useD`,` $pred
`lhs` `=` $a_type `rhs` `=` $b_type
$a `,` $b `,` $d `` custom<Token>($acc_dep, type($token)) `,` $a_scale `,`
$b_scale `,` $useD `,` $pred `lhs` `=` $a_type `rhs` `=` $b_type
`` custom<BarriersAndPreds>($barriers, $barrier_preds)
attr-dict `:` qualified(type($a)) `,` qualified(type($b)) `,`
qualified(type($d)) `,` qualified(type($a_scale)) `,`
Expand All @@ -517,27 +531,55 @@ def TTNG_TMEMLoadOp : TTNG_Op<"tmem_load"> {
let description = [{
This is similar to ttg.local_load except the result layout is restricted to only few possibility.
Therefore we cannot combine this op with any convert layout like local_load.

This operation takes and produces an optional token to indicate TMEM read
on its source operand. When the tokens are present, they can
be used to check aliasing and modref on the TMEM buffer.
}];
let arguments = (ins
Arg<TTG_MemDescType, "", [MemRead<TensorMemory>]>:$src,
Optional<TTG_AsyncToken>:$dep
);
let results = (outs
TT_Tensor:$result,
Optional<TTG_AsyncToken>:$token
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does load returns a token even though it doesn't modify TMem?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that if the ops have tokens, they could be marked Pure and the IR will still be semantically correct. This means that WAR dependencies have to be represented by passing the result token from the load as an operand to the next store.

For example, this token is how the load-store forwarding pattern in HoistTMEMAlloc is implemented: pattern matching tmem_store(tmem_load) where the compiler knows the store modifies the memory after the load due to the token.

);

let assemblyFormat = [{
$src `` custom<Token>($dep, type($token))
attr-dict `:` qualified(type($src)) `->` type($result)
}];
let arguments = (ins Arg<TTG_MemDescType, "", [MemRead<TensorMemory>]>:$src);

let assemblyFormat = [{$src attr-dict `:` qualified(type($src)) `->` type($result)}];
let results = (outs TT_Tensor:$result);
let hasVerifier = 1;

let extraClassDeclaration = [{
RankedTensorType getType() { return getResult().getType(); }
operator TypedValue<RankedTensorType>() { return getResult(); }
}];
}

def TTNG_TMEMStoreOp : TTNG_Op<"tmem_store"> {
let summary = "Store a distributed tensor into a buffer in tensor memory";

let description = [{
This is similar to ttg.local_local except the source layout is restricted to only few possibility.
This is similar to ttg.local_store except the source layout is restricted to only few possibility.

This operation takes and produces an optional token to indicate TMEM write
on its source operand. When the tokens are present, they can
be used to check aliasing and modref on the TMEM buffer.
}];
let arguments = (ins
Arg<TTG_MemDescType, "", [MemWrite<TensorMemory>]>:$dst,
Optional<TTG_AsyncToken>:$dep,
TT_Tensor:$src,
I1:$pred
);
let results = (outs Optional<TTG_AsyncToken>:$token);

let assemblyFormat = [{$src `,` $dst `,` $pred attr-dict `:` type($src) `->` qualified(type($dst))}];
let assemblyFormat = [{
$src `,` $dst `` custom<Token>($dep, type($token)) `,` $pred
attr-dict `:` type($src) `->` qualified(type($dst))
}];
let hasVerifier = 1;
}

Expand All @@ -551,13 +593,21 @@ def TTNG_TMEMAllocOp : TTNG_Op<"tmem_alloc", [DeclareOpInterfaceMethods<MemoryEf
Explicitly deallocating a buffer is optional; see local_dealloc.
}];
let arguments = (ins Optional<TT_Tensor>:$src);
let results = (outs
TTG_MemDescType:$result,
Optional<TTG_AsyncToken>:$token
);

let assemblyFormat = [{
($src^)? attr-dict `:` functional-type(operands, results)
}];

let results = (outs TTG_MemDescType:$result);
let hasVerifier = 1;

let extraClassDeclaration = [{
triton::gpu::MemDescType getType() { return getResult().getType(); }
operator TypedValue<triton::gpu::MemDescType>() { return getResult(); }
}];
}

def TTNG_TMEMSubSliceOp : TTNG_Op<"tmem_subslice", [Pure]> {
Expand Down
2 changes: 2 additions & 0 deletions include/triton/Dialect/TritonNvidiaGPU/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ std::unique_ptr<Pass> createTritonNvidiaGPUMMALoweringPass();

std::unique_ptr<Pass> createTritonNvidiaGPUPromoteLHSToTMemPass();

std::unique_ptr<Pass> createTritonNvidiaGPURemoveTMEMTokensPass();

std::unique_ptr<Pass> createTritonNvidiaGPUOptimizeDescriptorEncodingPass();

std::unique_ptr<Pass> createTritonNvidiaGPUOptimizeTMemSubtilingPass();
Expand Down
9 changes: 9 additions & 0 deletions include/triton/Dialect/TritonNvidiaGPU/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,13 @@ def TritonNvidiaGPUOptimizeTMemSubtilingPass : Pass<"triton-nvidia-optimize-tmem
"mlir::triton::TritonDialect"];
}

def TritonNvidiaGPURemoveTMEMTokensPass : Pass<"triton-nvidia-gpu-remove-tmem-tokens", "mlir::ModuleOp"> {
let summary = "remove TMEM tokens";

let description = [{
The `triton-nvidia-gpu-remove-tmem-tokens` pass removes TMEM memory
dependency tokens from the IR, after they are no longer needed.
}];
}

#endif
36 changes: 22 additions & 14 deletions lib/Dialect/TritonGPU/Transforms/AccelerateMatmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,17 @@ class BlockedToMMAv5 : public mlir::OpRewritePattern<DotOp> {
newDistributedEncoding);
Value cvtAcc =
rewriter.create<ConvertLayoutOp>(loc, newAccType, dotOp.getOperand(2));
auto tokType = rewriter.getType<AsyncTokenType>();
auto acc = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, accMemDescType, cvtAcc);
loc, accMemDescType, tokType, cvtAcc);
auto vTrue = rewriter.create<arith::ConstantIntOp>(dotOp.getLoc(), 1, 1);
auto mma = rewriter.create<triton::nvidia_gpu::TCGen5MMAOp>(
loc, a, b, acc, /*useD=*/vTrue, /*pred=*/vTrue);
loc, tokType, a, b, acc, acc.getToken(), /*useD=*/vTrue,
/*pred=*/vTrue);
mma.setTwoCtas(useTwoCTAs);

auto ld =
rewriter.create<triton::nvidia_gpu::TMEMLoadOp>(loc, newAccType, acc);
auto ld = rewriter.create<triton::nvidia_gpu::TMEMLoadOp>(
loc, newAccType, tokType, acc, /*dep=*/mma.getToken());
rewriter.replaceOpWithNewOp<ConvertLayoutOp>(dotOp, oldRetType, ld);
return success();
}
Expand Down Expand Up @@ -697,8 +699,9 @@ class ScaledBlockedToMMAv5
newDistributedEncoding);
Value cvtAcc =
rewriter.create<ConvertLayoutOp>(loc, newAccType, dotOp.getOperand(2));
auto tokType = rewriter.getType<AsyncTokenType>();
auto acc = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, accMemDescType, cvtAcc);
loc, accMemDescType, tokType, cvtAcc);

RankedTensorType oldScaleAType = dotOp.getAScale().getType();
RankedTensorType oldScaleBType = dotOp.getBScale().getType();
Expand Down Expand Up @@ -728,17 +731,22 @@ class ScaledBlockedToMMAv5
rewriter.create<ConvertLayoutOp>(loc, newScaleAType, lhsScale);
Value newScaleB =
rewriter.create<ConvertLayoutOp>(loc, newScaleBType, rhsScale);
Value scaleA = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, scaleAType, newScaleA);
Value scaleB = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, scaleBType, newScaleB);

// We don't need to track memory dependencies for the scale operands since
// they are not pipelined.
auto scaleA = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, scaleAType, /*token=*/Type(), newScaleA);
auto scaleB = rewriter.create<triton::nvidia_gpu::TMEMAllocOp>(
loc, scaleBType, /*token=*/Type(), newScaleB);

auto vTrue = rewriter.create<arith::ConstantIntOp>(dotOp.getLoc(), 1, 1);
rewriter.create<triton::nvidia_gpu::TCGen5MMAScaledOp>(
loc, a, b, acc, scaleA, scaleB, dotOp.getAElemType(),
dotOp.getBElemType(), /*useD=*/vTrue, /*pred=*/vTrue);
auto mmaOp = rewriter.create<triton::nvidia_gpu::TCGen5MMAScaledOp>(
loc, tokType, a, b, acc.getResult(), acc.getToken(), scaleA.getResult(),
scaleB.getResult(), dotOp.getAElemType(), dotOp.getBElemType(),
/*useD=*/vTrue, /*pred=*/vTrue);

auto ld =
rewriter.create<triton::nvidia_gpu::TMEMLoadOp>(loc, newAccType, acc);
auto ld = rewriter.create<triton::nvidia_gpu::TMEMLoadOp>(
loc, newAccType, tokType, acc, mmaOp.getToken());
rewriter.replaceOpWithNewOp<ConvertLayoutOp>(dotOp, oldRetType, ld);
return success();
}
Expand Down
Loading
Loading