Skip to content

Commit 9d1b5e0

Browse files
youngyoung01CHAIYD-2025ikushare
authored
[HINT] [Shared_Memory] Support hint the use of shared memory for aipu (#29)
* [CONVERSION] Support the use of shared memory for AIPU. * When using shared memory, the annotation "@hint: shared_memory" is required. * MemorySpace value 8 of shared memory is only for AIPU backend. * [CONVERSION] Fix shared memory for aipu. test_02_fused_softmax.py requires support later. * [Conversion] Add out-of-bounds offset control for shared memory At present in the use of Shared memory, need to comment out mlir/lib/the Dialect/Affine/Analysis/LoopAnalysis.cpp in the following two lines, and recompile LLVM: //if (! memRefType.getLayout().isIdentity()) // return memoryOp.emitError("NYI: non-trivial layout map"), false; * [Conversion] Fix the Cases of Softmax Kernel and One-Dimensional Non-Mask. * Supports two-dimensional matrix addition using shared memory ; Fixed the bug that caused errors in the operation of 2D matrix addition. * [Conversion] Support multi-dimensional vector add and add the "shared" attribute. * The extension supports multi-dimensional vector add. * Add the "shared" attribute to the memref.subview carrying offset of shared memory to solve the problem of non-trivial layout. * [Conversion] Downward propagation of the 'shared_memory' hint in memref.alloc * [Conversion] Add the no_bufferize attribute to memref ops with shared memory address space --------- Co-authored-by: CHAIYD-2025 <chaiyd_z@163.com> Co-authored-by: duyb <2697533527@qq.com>
1 parent fb04f5a commit 9d1b5e0

13 files changed

Lines changed: 350 additions & 20 deletions

File tree

include/triton-shared/Conversion/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ add_subdirectory(TritonToUnstructured)
77
add_subdirectory(StructuredToMemref)
88
add_subdirectory(UnstructuredToMemref)
99
add_subdirectory(MemrefCopyToDMA_FlagTree)
10+
add_subdirectory(NoBufferize_FlagTree)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(LLVM_TARGET_DEFINITIONS Passes.td)
2+
mlir_tablegen(Passes.h.inc -gen-pass-decls --name NoBufferizeFlagTree)
3+
add_public_tablegen_target(NoBufferizeFlagTreeConversionPassIncGen)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef TRITON_CONVERSION_NOBUFFERIZEFLAGTREE_NOBUFFERIZEFLAGTREE_H
2+
#define TRITON_CONVERSION_NOBUFFERIZEFLAGTREE_NOBUFFERIZEFLAGTREE_H
3+
4+
#include "mlir/Pass/Pass.h"
5+
#include "mlir/Transforms/DialectConversion.h"
6+
7+
#include "triton/Dialect/Triton/IR/Dialect.h"
8+
9+
namespace mlir {
10+
class TypeConverter;
11+
namespace triton {
12+
13+
#define GEN_PASS_DECL
14+
#include "triton-shared/Conversion/NoBufferize_FlagTree/Passes.h.inc"
15+
16+
void populateNoBufferizeFlagTreeConversionPatterns(
17+
RewritePatternSet &patterns, TypeConverter &typeConverter);
18+
19+
std::unique_ptr<OperationPass<ModuleOp>> createNoBufferizeFlagTreePass();
20+
21+
} // namespace triton
22+
} // namespace mlir
23+
24+
#endif // TRITON_CONVERSION_NOBUFFERIZEFLAGTREE_NOBUFFERIZEFLAGTREE_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef TRITON_NO_BUFFERIZE_FLAGTREE_CONVERSION_PASSES_H
2+
#define TRITON_NO_BUFFERIZE_FLAGTREE_CONVERSION_PASSES_H
3+
4+
#include "triton-shared/Conversion/NoBufferize_FlagTree/NoBufferizeFlagTree.h"
5+
6+
namespace mlir {
7+
namespace triton {
8+
9+
#define GEN_PASS_REGISTRATION
10+
#include "triton-shared/Conversion/NoBufferize_FlagTree/Passes.h.inc"
11+
12+
} // namespace triton
13+
} // namespace mlir
14+
15+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef NO_BUFFERIZE_FLAGTREE_CONVERSION_PASSES
2+
#define NO_BUFFERIZE_FLAGTREE_CONVERSION_PASSES
3+
4+
include "mlir/Pass/PassBase.td"
5+
6+
def NoBufferizeFlagTree : Pass<"no-bufferize-flagtree", "mlir::ModuleOp"> {
7+
let summary = "Add no_bufferize to memref ops in shared memory space";
8+
}
9+
10+
#endif

lib/AnalysisStructured/PtrAnalysis.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,10 @@ LogicalResult PtrAnalysis::rewriteLoadOp(triton::LoadOp op,
11601160
loadOp->setAttr("flagtree_hints", strAttr);
11611161
}
11621162

1163+
if (op->getAttr("flagtree_hints")) {
1164+
loadOp->setAttr("flagtree_hints", op->getAttr("flagtree_hints"));
1165+
}
1166+
11631167
LLVM_DEBUG({
11641168
llvm::dbgs() << "creating tts::load:\n";
11651169
loadOp->dump();

lib/Conversion/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ add_subdirectory(StructuredToMemref)
77
add_subdirectory(TritonPtrToMemref)
88
add_subdirectory(UnstructuredToMemref)
99
add_subdirectory(MemrefCopyToDMA_FlagTree)
10+
add_subdirectory(NoBufferize_FlagTree)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
add_triton_library(NoBufferizeFlagTree
2+
NoBufferizeFlagTree.cpp
3+
NoBufferizeFlagTreePass.cpp
4+
5+
DEPENDS
6+
NoBufferizeFlagTreeConversionPassIncGen
7+
Registrar
8+
Common
9+
10+
LINK_LIBS PUBLIC
11+
MLIRSCFTransforms
12+
MLIRArithDialect
13+
MLIRDialectUtils
14+
MLIRIR
15+
MLIRMathDialect
16+
MLIRPass
17+
MLIRTensorDialect
18+
MLIRTransforms
19+
MLIRSupport
20+
TritonIR
21+
TritonTransforms
22+
TritonTilingExtIR
23+
TritonStructuredIR
24+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "flagtree/Common/UnifiedHardware.h"
2+
3+
#include "triton/Dialect/Triton/IR/Types.h"
4+
5+
#include "triton-shared/Analysis/OpFoldResultUtils.h"
6+
#include "triton-shared/Conversion/NoBufferize_FlagTree/NoBufferizeFlagTree.h"
7+
#include "triton-shared/Dialect/TritonStructured/IR/TritonStructuredDialect.h"
8+
9+
#include "mlir/Dialect/Arith/IR/Arith.h"
10+
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
11+
#include "mlir/Dialect/Linalg/IR/Linalg.h"
12+
#include "mlir/Dialect/MemRef/IR/MemRef.h"
13+
#include "mlir/Dialect/SCF/IR/SCF.h"
14+
#include "mlir/Dialect/Utils/StaticValueUtils.h"
15+
16+
#include "llvm/ADT/ArrayRef.h"
17+
#include "llvm/ADT/STLExtras.h"
18+
#include "llvm/ADT/SmallVector.h"
19+
20+
#include <algorithm>
21+
#include <cassert>
22+
#include <cstdint>
23+
#include <iostream>
24+
25+
#define DEBUG_TYPE "no-bufferize-flagtree"
26+
27+
using namespace mlir;
28+
#define GEN_PASS_CLASSES
29+
#include "triton-shared/Conversion/NoBufferize_FlagTree/Passes.h.inc"
30+
#include "triton-shared/Conversion/TritonArithToLinalg/Passes.h.inc"
31+
32+
namespace {
33+
struct NoBufferizeConverter : public RewritePattern {
34+
NoBufferizeConverter(MLIRContext *ctx)
35+
: RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, ctx) {}
36+
37+
LogicalResult matchAndRewrite(Operation *op,
38+
PatternRewriter &rewriter) const override {
39+
auto memrefNoBufferize = [](Type t) -> bool {
40+
Attribute msAttr;
41+
if (auto memTy = dyn_cast<MemRefType>(t))
42+
msAttr = memTy.getMemorySpace();
43+
else if (auto unranked = dyn_cast<UnrankedMemRefType>(t))
44+
msAttr = unranked.getMemorySpace();
45+
else
46+
return false;
47+
if (auto intAttr = dyn_cast_or_null<IntegerAttr>(msAttr))
48+
return intAttr.getValue().getZExtValue() == 8;
49+
return false;
50+
};
51+
52+
bool noBuf = llvm::any_of(op->getResultTypes(), memrefNoBufferize);
53+
if (!noBuf)
54+
return failure();
55+
56+
op->setAttr("no_bufferize", BoolAttr::get(op->getContext(), true));
57+
return success();
58+
}
59+
};
60+
} // namespace
61+
62+
void mlir::triton::populateNoBufferizeFlagTreeConversionPatterns(
63+
RewritePatternSet &patterns, TypeConverter &typeConverter) {
64+
patterns.add<NoBufferizeConverter>(patterns.getContext());
65+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "triton/Dialect/Triton/IR/Dialect.h"
2+
3+
#include "triton-shared/Conversion/NoBufferize_FlagTree/NoBufferizeFlagTree.h"
4+
#include "triton-shared/Dialect/TritonStructured/IR/TritonStructuredDialect.h"
5+
#include "triton-shared/Dialect/TritonTilingExt/IR/TritonTilingExtDialect.h"
6+
7+
#include "mlir/Dialect/Arith/IR/Arith.h"
8+
#include "mlir/Dialect/MemRef/IR/MemRef.h"
9+
#include "mlir/IR/Builders.h"
10+
#include "mlir/IR/BuiltinAttributes.h"
11+
#include "mlir/IR/BuiltinOps.h"
12+
#include "mlir/IR/BuiltinTypes.h"
13+
#include "mlir/IR/MLIRContext.h"
14+
#include "mlir/Support/LogicalResult.h"
15+
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
16+
17+
#include "mlir/Dialect/Affine/IR/AffineOps.h"
18+
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
19+
#include "mlir/Dialect/Func/IR/FuncOps.h"
20+
#include "mlir/Dialect/Linalg/IR/Linalg.h"
21+
#include "mlir/Dialect/SCF/Transforms/Patterns.h"
22+
#include "mlir/Pass/PassManager.h"
23+
#include "triton/Dialect/Triton/IR/Types.h"
24+
#include "llvm/ADT/STLExtras.h"
25+
#include "llvm/Support/Casting.h"
26+
#include <iostream>
27+
#include <optional>
28+
29+
#define DEBUG_TYPE "no-bufferize-flagtree"
30+
using namespace mlir;
31+
using namespace triton;
32+
33+
namespace mlir {
34+
namespace triton {
35+
#define GEN_PASS_DEF_NOBUFFERIZEFLAGTREE
36+
#include "triton-shared/Conversion/NoBufferize_FlagTree/Passes.h.inc"
37+
} // namespace triton
38+
} // namespace mlir
39+
40+
namespace {
41+
42+
class NoBufferizeFlagTreePass
43+
: public triton::impl::NoBufferizeFlagTreeBase<NoBufferizeFlagTreePass> {
44+
using NoBufferizeFlagTreeBase<
45+
NoBufferizeFlagTreePass>::NoBufferizeFlagTreeBase;
46+
47+
public:
48+
void runOnOperation() override {
49+
ModuleOp module = getOperation();
50+
RewritePatternSet localPatterns(&getContext());
51+
{
52+
TypeConverter typeConverter;
53+
triton::populateNoBufferizeFlagTreeConversionPatterns(localPatterns,
54+
typeConverter);
55+
}
56+
(void)applyPatternsGreedily(module, std::move(localPatterns));
57+
}
58+
};
59+
} // namespace
60+
61+
std::unique_ptr<OperationPass<ModuleOp>>
62+
triton::createNoBufferizeFlagTreePass() {
63+
return std::make_unique<NoBufferizeFlagTreePass>();
64+
}

0 commit comments

Comments
 (0)