Skip to content

Commit 5d385bb

Browse files
authored
[Conversion] TritonArithToLinalg argmin & argmax supports cmpi, and get_structured_state supports I16Tensor (#22)
1 parent f4b735c commit 5d385bb

2 files changed

Lines changed: 50 additions & 18 deletions

File tree

include/triton-shared/Conversion/TritonArithToLinalg/ConversionPatterns.hpp

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ class ArgMinMaxBaseConverter : public OpConversionPattern<triton::ReduceOp> {
17831783
Value &tileBreakValue) const {
17841784
// Match the following (section 1. of the above)
17851785
//
1786-
// %11 = arith.cmpf oeq, %arg9, %arg11 : f32
1786+
// %11 = arith.cmpf/i oeq, %arg9, %arg11 : f32
17871787
// %12 = arith.cmpi slt, %arg10, %arg12 : i32
17881788
// %13 = arith.andi %11, %12 : i1
17891789
//
@@ -1793,17 +1793,27 @@ class ArgMinMaxBaseConverter : public OpConversionPattern<triton::ReduceOp> {
17931793

17941794
// matching: %11 = arith.cmpf oeq, %arg9, %arg11 : f32
17951795
LLVM_DEBUG(llvm::dbgs() << "Matching: " << *it << "\n");
1796-
auto eqCmpOp = dyn_cast<arith::CmpFOp>(*it++);
1797-
if (eqCmpOp) {
1798-
if (eqCmpOp.getPredicate() != arith::CmpFPredicate::OEQ) {
1796+
Value eqCmpOp;
1797+
if (auto cmpOp = dyn_cast<arith::CmpFOp>(*it)) {
1798+
if (cmpOp.getPredicate() != arith::CmpFPredicate::OEQ) {
17991799
return failure();
18001800
}
1801-
if (currValue != eqCmpOp.getLhs() || reduceValue != eqCmpOp.getRhs()) {
1801+
if (currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
18021802
return failure();
18031803
}
1804+
eqCmpOp = cmpOp;
1805+
} else if (auto cmpOp = dyn_cast<arith::CmpIOp>(*it)) {
1806+
if (cmpOp.getPredicate() != arith::CmpIPredicate::eq) {
1807+
return failure();
1808+
}
1809+
if (currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
1810+
return failure();
1811+
}
1812+
eqCmpOp = cmpOp;
18041813
} else {
18051814
return failure();
18061815
}
1816+
it++;
18071817

18081818
// matching: %12 = arith.cmpi slt, %arg10, %arg12 : i32
18091819
LLVM_DEBUG(llvm::dbgs() << "Matching: " << *it << "\n");
@@ -1949,9 +1959,16 @@ class ArgMinMaxBaseConverter : public OpConversionPattern<triton::ReduceOp> {
19491959
// the result value to either -inf or +inf depending on
19501960
// whether we're dealing with argmax or argmin
19511961
auto valueType = elemTypes[0];
1952-
auto valuesAccBaseVal = rewriter.create<arith::ConstantOp>(
1953-
loc, valueType,
1954-
rewriter.getFloatAttr(valueType, T::getBaseReductionValue()));
1962+
Value valuesAccBaseVal;
1963+
if (mlir::isa<FloatType>(valueType)) {
1964+
valuesAccBaseVal = rewriter.create<arith::ConstantOp>(
1965+
loc, valueType,
1966+
rewriter.getFloatAttr(valueType, T::getBaseReductionValue()));
1967+
} else {
1968+
valuesAccBaseVal = rewriter.create<arith::ConstantOp>(
1969+
loc, valueType,
1970+
rewriter.getIntegerAttr(valueType, T::getBaseReductionValue()));
1971+
}
19551972

19561973
// Set the initial value of the rank-0 tensor containing the index of the
19571974
// min or max value to -1
@@ -2014,20 +2031,27 @@ struct ArgMaxConverter : public ArgMinMaxBaseConverter<ArgMaxConverter> {
20142031
Value reduceIndex,
20152032
mlir::Block::iterator &it,
20162033
Value &comparisonResult) {
2017-
// %14 = arith.cmpf ogt, %arg9, %arg11 : f32
2034+
// %14 = arith.cmpf/i ogt, %arg9, %arg11 : f32
20182035
// This corresponds to section 2. of the sample snippet in
20192036
// ArgMinMaxBaseConverter
2020-
auto cmpOp = dyn_cast<arith::CmpFOp>(*it++);
2021-
if (cmpOp) {
2037+
if (auto cmpOp = dyn_cast<arith::CmpFOp>(*it)) {
20222038
if (cmpOp.getPredicate() != arith::CmpFPredicate::OGT ||
20232039
currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
20242040
return failure();
20252041
}
2042+
comparisonResult = cmpOp;
2043+
} else if (auto cmpOp = dyn_cast<arith::CmpIOp>(*it)) {
2044+
auto predicate = cmpOp.getPredicate();
2045+
if ((predicate != arith::CmpIPredicate::sgt && predicate != arith::CmpIPredicate::ugt) ||
2046+
currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
2047+
return failure();
2048+
}
2049+
comparisonResult = cmpOp;
20262050
} else {
20272051
return failure();
20282052
}
2053+
it++;
20292054

2030-
comparisonResult = cmpOp;
20312055
return success();
20322056
}
20332057

@@ -2044,21 +2068,29 @@ struct ArgMinConverter : public ArgMinMaxBaseConverter<ArgMinConverter> {
20442068
Value reduceIndex,
20452069
mlir::Block::iterator &it,
20462070
Value &comparisonResult) {
2047-
// %14 = arith.cmpf olt, %arg9, %arg11 : f32
2071+
// %14 = arith.cmpf/i olt, %arg9, %arg11 : f32
20482072
// This corresponds to section 2. of the sample snippet in
20492073
// ArgMinMaxBaseConverter
20502074
LLVM_DEBUG(llvm::dbgs() << "Matching: " << *it << "\n");
2051-
auto cmpOp = dyn_cast<arith::CmpFOp>(*it++);
2052-
if (cmpOp) {
2075+
2076+
if (auto cmpOp = dyn_cast<arith::CmpFOp>(*it)) {
20532077
if (cmpOp.getPredicate() != arith::CmpFPredicate::OLT ||
20542078
currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
20552079
return failure();
20562080
}
2081+
comparisonResult = cmpOp;
2082+
} else if (auto cmpOp = dyn_cast<arith::CmpIOp>(*it)) {
2083+
auto predicate = cmpOp.getPredicate();
2084+
if ((predicate != arith::CmpIPredicate::slt && predicate != arith::CmpIPredicate::ult) ||
2085+
currValue != cmpOp.getLhs() || reduceValue != cmpOp.getRhs()) {
2086+
return failure();
2087+
}
2088+
comparisonResult = cmpOp;
20572089
} else {
20582090
return failure();
20592091
}
2092+
it++;
20602093

2061-
comparisonResult = cmpOp;
20622094
return success();
20632095
}
20642096

include/triton-shared/Dialect/TritonStructured/IR/TritonStructuredDialect.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ def TTS_GetStructuredStateOp : TTS_Op<"get_structured_state", [AttrSizedResultSe
125125
let description = "Used to pass the offsets and strides to scf.for op to simplify IR rewrites.";
126126

127127
let arguments = (
128-
ins AnyTypeOf<[TT_PtrLike, I1Tensor, I32Tensor, I64Tensor]>:$input
128+
ins AnyTypeOf<[TT_PtrLike, I1Tensor, I16Tensor, I32Tensor, I64Tensor]>:$input
129129
);
130130
let results = (
131-
outs AnyTypeOf<[TT_PtrLike, I1Tensor, I32Tensor, I64Tensor]>:$structured,
131+
outs AnyTypeOf<[TT_PtrLike, I1Tensor, I16Tensor, I32Tensor, I64Tensor]>:$structured,
132132
Variadic<Index>:$offsets,
133133
Variadic<Index>:$strides
134134
);

0 commit comments

Comments
 (0)