Skip to content

Commit 8104b4c

Browse files
committed
OpenXLA-specific changes
1 parent 632bfc3 commit 8104b4c

File tree

45 files changed

+3539
-979
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3539
-979
lines changed

BUILD

+933
Large diffs are not rendered by default.

lib/Analysis/AxisInfo.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ class ShROpAxisInfoVisitor final : public BinaryOpVisitorImpl<OpTy> {
937937
// Treat [2^n,2^n+1,...]'s divisibility as 1 instead of 2^n
938938
lhsDivisibility = 1;
939939
}
940-
return std::max<int64_t>(1, lhsDivisibility / (1 << shift));
940+
return std::max<int64_t>(1, lhsDivisibility / (int64_t(1) << shift));
941941
}
942942

943943
int64_t getConstancy(OpTy op, const AxisInfo &lhs, const AxisInfo &rhs,
@@ -1011,6 +1011,7 @@ AxisInfoAnalysis::AxisInfoAnalysis(DataFlowSolver &solver)
10111011
CastOpAxisInfoVisitor<arith::ExtUIOp>,
10121012
CastOpAxisInfoVisitor<arith::TruncIOp>,
10131013
CastOpAxisInfoVisitor<arith::IndexCastOp>,
1014+
CastOpAxisInfoVisitor<arith::IndexCastUIOp>,
10141015
CastOpAxisInfoVisitor<triton::gpu::ConvertLayoutOp>,
10151016
CastOpAxisInfoVisitor<mlir::UnrealizedConversionCastOp>,
10161017
CastOpAxisInfoVisitor<triton::BitcastOp>>();

lib/Conversion/TritonToTritonGPU/TritonGPUConversion.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ TritonGPUTypeConverter::TritonGPUTypeConverter(MLIRContext *context,
5757
addArgumentMaterialization([&](OpBuilder &builder,
5858
RankedTensorType tensorType, ValueRange inputs,
5959
Location loc) -> Value {
60+
// Allows partial TTIR to TTGIR conversion by materializing a conversion for
61+
// remaining arguments that have been converted to a new type.
62+
// We use this to rewrite triton_xla.sparse_dot in a separate pass after
63+
// 'convert-triton-to-tritongpu'.
64+
return builder.create<triton::gpu::ConvertLayoutOp>(loc, tensorType,
65+
inputs);
6066
llvm_unreachable("Argument rematerialization should not happen in Triton "
6167
"-> TritonGPU conversion");
6268
return {};
@@ -66,6 +72,12 @@ TritonGPUTypeConverter::TritonGPUTypeConverter(MLIRContext *context,
6672
// convert origValue to newValue
6773
addSourceMaterialization([&](OpBuilder &builder, RankedTensorType tensorType,
6874
ValueRange inputs, Location loc) -> Value {
75+
// Allows partial TTIR to TTGIR conversion by materializing a conversion for
76+
// remaining uses of values that have been converted to a new type.
77+
// We use this to rewrite triton_xla.sparse_dot in a separate pass after
78+
// 'convert-triton-to-tritongpu'.
79+
return builder.create<triton::gpu::ConvertLayoutOp>(loc, tensorType,
80+
inputs);
6981
llvm_unreachable("Source rematerialization should not happen in Triton -> "
7082
"TritonGPU Conversion");
7183
return {};

lib/Dialect/Triton/Transforms/Combine.td

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def CombineDotAddIPattern : Pat<
1717
[(Constraint<CPred<"isZero($0)">> $c),
1818
(Constraint<CPred<"res->hasOneUse()">, "dot result has a single use">)]>;
1919
def CombineDotAddFPattern : Pat<
20-
(Arith_AddFOp $d, (TT_DotOp:$res $a, $b, $c, $inputPrecision, $maxNumImpreciseAcc), $fastmath, $denorm),
20+
(Arith_AddFOp $d, (TT_DotOp:$res $a, $b, $c, $inputPrecision, $maxNumImpreciseAcc), $fastmath),
2121
(TT_DotOp $a, $b, $d, $inputPrecision, $maxNumImpreciseAcc, (location $res)),
2222
[(Constraint<CPred<"isZero($0)">> $c),
2323
(Constraint<CPred<"::llvm::cast<::mlir::IntegerAttr>($0).getInt() == 0">> $maxNumImpreciseAcc),
@@ -29,7 +29,7 @@ def CombineDotAddIRevPattern : Pat<
2929
[(Constraint<CPred<"isZero($0)">> $c),
3030
(Constraint<CPred<"res->hasOneUse()">, "dot result has a single use">)]>;
3131
def CombineDotAddFRevPattern : Pat<
32-
(Arith_AddFOp (TT_DotOp:$res $a, $b, $c, $inputPrecision, $maxNumImpreciseAcc), $d, $fastmath, $denorm),
32+
(Arith_AddFOp (TT_DotOp:$res $a, $b, $c, $inputPrecision, $maxNumImpreciseAcc), $d, $fastmath),
3333
(TT_DotOp $a, $b, $d, $inputPrecision, $maxNumImpreciseAcc, (location $res)),
3434
[(Constraint<CPred<"isZero($0)">> $c),
3535
(Constraint<CPred<"::llvm::cast<::mlir::IntegerAttr>($0).getInt() == 0">> $maxNumImpreciseAcc),

lib/Dialect/TritonGPU/IR/Ops.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ struct CanonicalizeConvertFromAlloc
118118
auto convert = op.getSrc().getDefiningOp<ConvertLayoutOp>();
119119
if (!convert)
120120
return failure();
121+
// LocalAllocOp lowering doesn't support going from DotOperandEncoding
122+
// to SharedEncoding, so we want to keep this layout conversion.
123+
if (mlir::isa<triton::gpu::DotOperandEncodingAttr>(
124+
convert.getSrc().getType().getEncoding()))
125+
return failure();
121126
rewriter.replaceOpWithNewOp<triton::gpu::LocalAllocOp>(
122127
op, op->getResult(0).getType(), convert.getSrc());
123128
return mlir::success();
@@ -180,13 +185,13 @@ struct CanonicalizeConvertFromConvert
180185
// heuristic to accommodate fused attention.
181186
auto srcType = op.getSrc().getType();
182187
auto dstType = op.getType();
183-
if (mlir::isa<DotOperandEncodingAttr>(dstType.getEncoding()) &&
184-
mlir::isa<NvidiaMmaEncodingAttr>(srcType.getEncoding()))
188+
if (mlir::isa_and_nonnull<DotOperandEncodingAttr>(dstType.getEncoding()) &&
189+
mlir::isa_and_nonnull<NvidiaMmaEncodingAttr>(srcType.getEncoding()))
185190
return failure();
186191

187192
// for hopper MMAv3
188-
if (mlir::isa<SharedEncodingAttr>(dstType.getEncoding()) &&
189-
mlir::isa<NvidiaMmaEncodingAttr>(srcType.getEncoding()) &&
193+
if (mlir::isa_and_nonnull<SharedEncodingAttr>(dstType.getEncoding()) &&
194+
mlir::isa_and_nonnull<NvidiaMmaEncodingAttr>(srcType.getEncoding()) &&
190195
llvm::any_of(op.getResult().getUsers(), [](Operation *dot) {
191196
return dot->hasTrait<OpTrait::DotLike>();
192197
})) {

lib/Dialect/TritonGPU/Transforms/AccelerateMatmul.cpp

+43-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace mlir {
2020
namespace triton {
2121
namespace gpu {
2222

23-
namespace {
24-
2523
// Get the highest version supported for the hardware and the dot.
2624
static int getMMAVersionSafe(int computeCapability, DotOp op) {
2725
// List supported mma version in order of preference.
@@ -44,8 +42,8 @@ static int getMMAVersionSafe(int computeCapability, DotOp op) {
4442
return 0;
4543
}
4644

47-
SmallVector<unsigned> warpsPerTileV2(DotOp dotOp, const ArrayRef<int64_t> shape,
48-
int numWarps) {
45+
SmallVector<unsigned>
46+
warpsPerTileV2(Operation *dotOp, const ArrayRef<int64_t> shape, int numWarps) {
4947
auto rank = shape.size();
5048
// Early exit for batched matmul
5149
if (rank == 3)
@@ -109,10 +107,10 @@ SmallVector<unsigned> warpsPerTileV2(DotOp dotOp, const ArrayRef<int64_t> shape,
109107
}
110108

111109
SmallVector<unsigned, 2>
112-
warpsPerTileV3(DotOp dotOp, const ArrayRef<int64_t> shape, int numWarps,
110+
warpsPerTileV3(Operation *dotOp, const ArrayRef<int64_t> shape, int numWarps,
113111
const SmallVector<unsigned, 3> &instrShape) {
114112
SetVector<Operation *> slices;
115-
mlir::getForwardSlice(dotOp.getResult(), &slices);
113+
mlir::getForwardSlice(dotOp->getResult(0), &slices);
116114
// Contains a chained dot. We prefer to assign warps to one axis
117115
// to facilitate use cases like flash attention, allowing reductions within
118116
// the same warp.
@@ -167,11 +165,26 @@ static Value getSharedMemoryMMAOperand(Value v, mlir::PatternRewriter &rewriter,
167165
auto newType = MemDescType::get(argType.getShape(), argType.getElementType(),
168166
newLayout, SharedMemorySpace);
169167
rewriter.setInsertionPointAfterValue(arg);
168+
169+
// LocalAllocOp lowering doesn't support going from DotOperandEncoding
170+
// to SharedEncoding.
171+
if (auto dotOpEnc = mlir::dyn_cast<DotOperandEncodingAttr>(
172+
argType.getEncoding())) {
173+
// Create a layout conversion from DotOperandEncoding to BlockedEncoding
174+
// then pass it to the LocalAllocOp.
175+
auto newArgType = RankedTensorType::get(
176+
argType.getShape(), argType.getElementType(), dotOpEnc.getParent());
177+
auto dotOperandToBlockedCvt =
178+
rewriter.create<ConvertLayoutOp>(arg.getLoc(), newArgType, arg);
179+
return rewriter.create<LocalAllocOp>(arg.getLoc(), newType,
180+
dotOperandToBlockedCvt);
181+
}
182+
170183
return rewriter.create<LocalAllocOp>(arg.getLoc(), newType, arg);
171184
}
172185

173186
SmallVector<unsigned, 3>
174-
getWarpsPerTile(DotOp dotOp, const ArrayRef<int64_t> shape, int version,
187+
getWarpsPerTile(Operation* dotOp, const ArrayRef<int64_t> shape, int version,
175188
int numWarps, const SmallVector<unsigned, 3> &instrShape) {
176189
switch (version) {
177190
case 2:
@@ -184,18 +197,32 @@ getWarpsPerTile(DotOp dotOp, const ArrayRef<int64_t> shape, int version,
184197
}
185198
}
186199

200+
// Move anonymous namespace down, so getWarpsPerTile is visible to the sparsity
201+
// extension.
202+
namespace {
203+
187204
class BlockedToMMA : public mlir::OpRewritePattern<DotOp> {
188205
int computeCapability;
189206
mutable llvm::DenseMap<Operation *, unsigned> dotOpInstNs;
190207

191208
static bool bwdFilter(Operation *op) {
209+
// Dot operand layout assignment to Predicates are not currently supported
210+
// during lowering from TritonGPU to LLVM in Triton for MMA cases. This
211+
// condition limits visibility of the original bit-width so that predicate
212+
// are not considered, hence, kwidth can never be = 32.
213+
if (isa<arith::UIToFPOp>(op)) {
214+
Type srcType = getElementTypeOrSelf(op->getOperand(0));
215+
if (srcType.isInteger(1))
216+
return false;
217+
}
192218
return op->getNumOperands() == 1 &&
193219
(isa<FpToFpOp, BitcastOp, ConvertLayoutOp>(op) ||
194220
isPureUnaryInlineAsm(op) ||
195221
op->getDialect()->getTypeID() ==
196222
mlir::TypeID::get<arith::ArithDialect>());
197223
}
198224

225+
public:
199226
// Finds the first different bitwidth in the chain of shape-preserving
200227
// unary ops that x depends on.
201228
// There are two primary scenarios:
@@ -806,6 +833,15 @@ class TritonGPUAccelerateMatmulPass
806833
}
807834
};
808835

836+
// Expose helper functions from BlockedToMMA to be reused for sparse matmul.
837+
int computeOrigBitWidth(Value x) {
838+
return BlockedToMMA::computeOrigBitWidth(x);
839+
}
840+
Value getSharedMemMMAOperand(Value v, mlir::PatternRewriter &rewriter,
841+
int opIdx, bool allowTranspose) {
842+
return getSharedMemoryMMAOperand(v, rewriter, opIdx, allowTranspose);
843+
}
844+
809845
} // namespace gpu
810846
} // namespace triton
811847
} // namespace mlir

lib/Dialect/TritonGPU/Transforms/Pipeliner/MatmulLoopPipeline.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static int createAsyncCopy(scf::ForOp forOp, tt::LoadOp loadOp, Value alloc,
131131

132132
Value zero = builder.createWithStage<arith::ConstantIntOp>(
133133
forOp.getLoc(), stage, clusterId, 0, 32);
134+
134135
// Replace the load with insert/extract slice.
135136
builder.setInsertionPoint(loadOp);
136137
Location loc = loadOp.getLoc();
@@ -487,7 +488,8 @@ assignMemoryLayouts(scf::ForOp &forOp,
487488
}
488489
});
489490

490-
loadsToPipeline.insert(&op);
491+
// TODO: b/381421713 - Uncomment this once pipelining is fixed.
492+
// loadsToPipeline.insert(&op);
491493
LoadInfo loadInfo;
492494
for (auto use : users) {
493495
if (use->hasTrait<OpTrait::DotLike>()) {
@@ -527,6 +529,11 @@ assignMemoryLayouts(scf::ForOp &forOp,
527529
getBlockedEncoding(loadOp, axisInfoAnalysis);
528530
}
529531
}
532+
533+
// TODO: b/381421713 - Remove this once pipelining is fixed.
534+
if (!loadInfo.sharedEncoding) continue;
535+
loadsToPipeline.insert(&op);
536+
530537
loadToInfo[&op] = loadInfo;
531538
}
532539
// Make sure all loads in loadsToPipeline are in loadToInfo.

lib/Dialect/TritonGPU/Transforms/Prefetch.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Value Prefetcher::generatePrefetch(Value v, unsigned opIdx, bool isPrologue,
116116
// opIdx: 0 => a, 1 => b
117117
auto type = cast<triton::gpu::MemDescType>(v.getType());
118118
SmallVector<int64_t> shape{type.getShape().begin(), type.getShape().end()};
119-
SmallVector<int64_t> offset{0, 0};
119+
SmallVector<int64_t> offset(shape.size(), 0);
120120
Type elementType = type.getElementType();
121121

122122
// k => (prefetchWidth, k - prefetchWidth)
@@ -141,8 +141,14 @@ Value Prefetcher::generatePrefetch(Value v, unsigned opIdx, bool isPrologue,
141141
type.getMutableMemory(), type.getAllocShape()),
142142
v, offsetsVal);
143143

144+
// We need to assign kwidth to zero in the case where the parent layout is
145+
// Blocked, otherwise the verifier emits a failure. The parent layout is
146+
// Blocked only when Tensor Cores are disabled.
147+
int kwidth = dyn_cast<triton::gpu::BlockedEncodingAttr>(dotEncoding)
148+
? 0
149+
: prefetchWidth / 8;
144150
auto dotOperandEnc = triton::gpu::DotOperandEncodingAttr::get(
145-
builder.getContext(), opIdx, dotEncoding, prefetchWidth / 8);
151+
builder.getContext(), opIdx, dotEncoding, kwidth);
146152
Value prefetchSlice = builder.create<triton::gpu::LocalLoadOp>(
147153
v.getLoc(), RankedTensorType::get(shape, elementType, dotOperandEnc),
148154
newSmem);
@@ -191,6 +197,22 @@ LogicalResult Prefetcher::initialize() {
191197
break;
192198
if (!op->getResult(0).hasOneUse())
193199
break;
200+
// Similar to issues faced in HoistLayoutConversion pattern in
201+
// OptimizeDotOperands.cpp, we can't propagate through type casts from
202+
// predicates as they aren't supported in Triton when encoded with dot_op
203+
// layout.
204+
if (isa<arith::UIToFPOp>(op)) {
205+
Type srcType = getElementTypeOrSelf(op->getOperand(0));
206+
if (srcType.isInteger(1))
207+
break;
208+
}
209+
// Propagation through ExpandDims is currently not supported. This blindly
210+
// replaces the encoding with dot encoding & but ExpandDims requires a
211+
// SliceEncoding. This could be rewritten to support it somehow, but I
212+
// don't think it's trivial & it's currently crashing.
213+
if (isa<ExpandDimsOp>(op)) {
214+
break;
215+
}
194216
rets.push_back(op->getOperand(0));
195217
if (auto cvt = dyn_cast<triton::gpu::LocalLoadOp>(op)) {
196218
foundConvertFromShared = true;

lib/Dialect/TritonGPU/Transforms/Utility.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -990,18 +990,26 @@ getSharedEncIfAllUsersAreDotEnc(Value val, bool &incompatible) {
990990
} else {
991991
if (!isa<ttg::LocalLoadOp, ttg::ConvertLayoutOp>(user))
992992
return std::nullopt;
993-
auto dotOpEnc = dyn_cast<ttg::DotOperandEncodingAttr>(
994-
cast<triton::gpu::TensorOrMemDesc>(user->getResult(0).getType())
995-
.getEncoding());
996-
if (!dotOpEnc)
993+
auto enc =
994+
cast<triton::gpu::TensorOrMemDesc>(user->getResult(0).getType()).getEncoding();
995+
if (isa<ttg::DotOperandEncodingAttr>(enc)) {
996+
auto srcTy = cast<triton::gpu::TensorOrMemDesc>(val.getType());
997+
auto CTALayout = ttg::getCTALayout(srcTy.getEncoding());
998+
auto order = ttg::getOrder(srcTy.getEncoding());
999+
unsigned bitWidth = srcTy.getElementType().getIntOrFloatBitWidth();
1000+
tempAttr = ttg::SharedEncodingAttr::get(
1001+
val.getContext(), cast<ttg::DotOperandEncodingAttr>(enc),
1002+
srcTy.getShape(), order, CTALayout, bitWidth, /*needTrans=*/false);
1003+
} else if (enc.getAbstractAttribute().getName().str() ==
1004+
"triton.gpu.sparse_dot_meta_encoding") {
1005+
auto srcTy = cast<triton::gpu::TensorOrMemDesc>(val.getType());
1006+
tempAttr = ttg::SharedEncodingAttr::get(
1007+
val.getContext(), /*vec=*/1, /*perPhase=*/1, /*maxPhase=*/1,
1008+
ttg::getOrder(srcTy.getEncoding()),
1009+
ttg::getCTALayout(srcTy.getEncoding()));
1010+
} else {
9971011
return std::nullopt;
998-
auto srcTy = cast<triton::gpu::TensorOrMemDesc>(val.getType());
999-
auto CTALayout = ttg::getCTALayout(srcTy.getEncoding());
1000-
auto order = ttg::getOrder(srcTy.getEncoding());
1001-
unsigned bitWidth = srcTy.getElementType().getIntOrFloatBitWidth();
1002-
tempAttr = ttg::SharedEncodingAttr::get(
1003-
val.getContext(), dotOpEnc, srcTy.getShape(), order, CTALayout,
1004-
bitWidth, /*needTrans=*/false);
1012+
}
10051013
}
10061014
// Check that the shared encodings needed by the users are compatible.
10071015
if (attr != nullptr && attr != tempAttr) {

lib/Dialect/TritonNvidiaGPU/Transforms/FenceInsertion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct FenceInsertionPass
4444
return;
4545
ModuleOp mod = getOperation();
4646
mod.walk([&](Operation *op) {
47-
if (!isa<ttng::WarpGroupDotOp>(op))
47+
if (!op->hasTrait<OpTrait::DotLike>())
4848
return WalkResult::advance();
4949
OpBuilder builder(op);
5050
auto a = op->getOperand(0);

0 commit comments

Comments
 (0)