-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This is stll a kind of the boilerplate and basic lowering for the first milestone (compiling vector addition). This PR firstly lowers `tt.func` and `tt.return`. Test Plan: It can safely compile an empty kernel. ``` @triton.jit def add_kernel(x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr): return ``` > TRITON_ENABLE_LLVM_DEBUG=1 TRITON_CPU_BACKEND=1 python3 empty_kerne.py ``` //===-------------------------------------------===// Legalizing operation : 'tt.func'(0x73be2a0) { * Fold { } -> FAILURE : unable to fold * Pattern : 'tt.func -> ()' { Trying to match "(anonymous namespace)::FuncOpConversion" ** Insert : 'llvm.func'(0x6c04c70) ** Insert Block into : 'llvm.func'(0x6c04c70) ** Insert Block into : 'llvm.func'(0x6c04c70) ** Erase : 'tt.func'(0x73be2a0) "(anonymous namespace)::FuncOpConversion" result 1 //===-------------------------------------------===// Legalizing operation : 'llvm.func'(0x6c04c70) { } -> SUCCESS : operation marked legal by the target //===-------------------------------------------===// ... //===-------------------------------------------===// Legalizing operation : 'tt.return'(0x73efeb0) { "tt.return"() : () -> () * Fold { } -> FAILURE : unable to fold * Pattern : 'tt.return -> ()' { Trying to match "(anonymous namespace)::ReturnOpConversion" ** Insert : 'llvm.return'(0x73c0f00) ** Replace : 'tt.return'(0x73efeb0) "(anonymous namespace)::ReturnOpConversion" result 1 //===-------------------------------------------===// Legalizing operation : 'llvm.return'(0x73c0f00) { "llvm.return"() : () -> () } -> SUCCESS : operation marked legal by the target //===-------------------------------------------===// } -> SUCCESS : pattern applied successfully ```
- Loading branch information
Showing
9 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
include/triton/Conversion/TritonCPUToLLVM/PatternTritonCPUOpToLLVM.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef TRITON_CONVERSION_TRITONCPU_TO_LLVM_PATTERNS_TRITON_CPU_OP_TO_LLVM_H | ||
#define TRITON_CONVERSION_TRITONCPU_TO_LLVM_PATTERNS_TRITON_CPU_OP_TO_LLVM_H | ||
|
||
#include "mlir/Conversion/LLVMCommon/TypeConverter.h" | ||
#include "triton/Dialect/TritonCPU/IR/Dialect.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::triton; | ||
|
||
namespace mlir { | ||
namespace triton { | ||
// Some populate* functions have name collisions with the ones for GPUs. | ||
namespace cpu { | ||
|
||
constexpr int patternBenefitDefault = 1; | ||
constexpr int patternBenefitPrioritizeOverLLVMConversions = 10; | ||
constexpr int patternBenefitClampOptimizedPattern = 20; | ||
constexpr int patternBenefitConvertLayoutOptimizedPattern = 20; | ||
|
||
void populateControlFlowOpToLLVMPattern(LLVMTypeConverter &typeConverter, | ||
RewritePatternSet &patterns, | ||
PatternBenefit benefit); | ||
|
||
void populateFuncOpConversionPattern(LLVMTypeConverter &typeConverter, | ||
RewritePatternSet &patterns, | ||
PatternBenefit benefit); | ||
|
||
void populatePrintOpToLLVMPattern(LLVMTypeConverter &typeConverter, | ||
RewritePatternSet &patterns, | ||
PatternBenefit benefit); | ||
|
||
} // namespace cpu | ||
} // namespace triton | ||
} // namespace mlir | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef TRITON_CONVERSION_TRITONCPU_TO_LLVM_UTILITY_H | ||
#define TRITON_CONVERSION_TRITONCPU_TO_LLVM_UTILITY_H | ||
|
||
#include "mlir/Conversion/LLVMCommon/Pattern.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "triton/Analysis/Utility.h" | ||
#include "triton/Conversion/MLIRTypes.h" | ||
#include "triton/Dialect/Triton/IR/Utility.h" | ||
#include "triton/Dialect/TritonCPU/IR/Dialect.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::triton; | ||
|
||
namespace mlir { | ||
namespace LLVM { | ||
|
||
// TODO: Not sure we need this for CPU backends. | ||
inline bool isKernel(FunctionOpInterface funcOp) { | ||
return funcOp.getVisibility() == SymbolTable::Visibility::Public; | ||
} | ||
|
||
} // namespace LLVM | ||
} // namespace mlir | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "triton/Conversion/TritonCPUToLLVM/PatternTritonCPUOpToLLVM.h" | ||
#include "triton/Conversion/TritonCPUToLLVM/Utility.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
namespace { | ||
|
||
using namespace mlir; | ||
using namespace mlir::triton; | ||
|
||
struct ReturnOpConversion : public ConvertOpToLLVMPattern<triton::ReturnOp> { | ||
using ConvertOpToLLVMPattern<triton::ReturnOp>::ConvertOpToLLVMPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(triton::ReturnOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto funcOp = op->getParentOfType<LLVM::LLVMFuncOp>(); | ||
if (funcOp->hasAttr("cpu.kernel")) { | ||
if (op.getNumOperands() > 0) { | ||
return rewriter.notifyMatchFailure( | ||
op, "Kernel functions do not support return with operands"); | ||
} | ||
rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, TypeRange(), ValueRange(), | ||
op->getAttrs()); | ||
} else { | ||
llvm_unreachable("Not implemented"); | ||
} | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::triton::cpu::populateControlFlowOpToLLVMPattern( | ||
LLVMTypeConverter &typeConverter, RewritePatternSet &patterns, | ||
PatternBenefit benefit) { | ||
patterns.add<ReturnOpConversion>(typeConverter, benefit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "triton/Conversion/TritonCPUToLLVM/PatternTritonCPUOpToLLVM.h" | ||
#include "triton/Conversion/TritonCPUToLLVM/Utility.h" | ||
|
||
namespace mlir { | ||
FailureOr<LLVM::LLVMFuncOp> | ||
convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp, | ||
ConversionPatternRewriter &rewriter, | ||
const LLVMTypeConverter &converter); | ||
} | ||
|
||
namespace { | ||
|
||
using namespace mlir; | ||
using namespace mlir::triton; | ||
|
||
struct FuncOpConversion : public ConvertOpToLLVMPattern<triton::FuncOp> { | ||
FuncOpConversion(LLVMTypeConverter &converter, PatternBenefit benefit) | ||
: ConvertOpToLLVMPattern(converter, benefit) {} | ||
|
||
LogicalResult | ||
matchAndRewrite(triton::FuncOp funcOp, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
if (!LLVM::isKernel(funcOp)) { | ||
llvm_unreachable("Not implemented"); | ||
} | ||
|
||
LLVM::LLVMFuncOp newFuncOp = | ||
*mlir::convertFuncOpToLLVMFuncOp(funcOp, rewriter, *getTypeConverter()); | ||
if (!newFuncOp) { | ||
return failure(); | ||
} | ||
|
||
auto ctx = funcOp->getContext(); | ||
if (LLVM::isKernel(funcOp)) { | ||
// Set an attribute to indicate this function is a kernel entry. | ||
newFuncOp->setAttr("cpu.kernel", | ||
rewriter.getIntegerAttr(type::u1Ty(ctx), 1)); | ||
} else { | ||
llvm_unreachable("Not implemented"); | ||
} | ||
|
||
rewriter.eraseOp(funcOp); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::triton::cpu::populateFuncOpConversionPattern( | ||
LLVMTypeConverter &typeConverter, RewritePatternSet &patterns, | ||
PatternBenefit benefit) { | ||
patterns.add<FuncOpConversion>(typeConverter, benefit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters