Skip to content

#1851: Add options to ttmlir-translate to output C++ from TTKernel dialect #1602

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 28 commits into from
Jan 17, 2025

Conversation

TT-billteng
Copy link
Contributor

@TT-billteng TT-billteng commented Dec 15, 2024

Shardon requires emitting C++ from python kernels. To help with the testing and debug process, I've added a TTKernel to C++ target and options to ttmlir-translate to directly dump C++ from TTKernel dialect.

There are separate Noc and Tensix kernel dumping options because shardon users need to explicitly control the definition of Noc and Tensix kernels separately.

@TT-billteng TT-billteng force-pushed the bteng/shardon_emitc branch 7 times, most recently from d77935c to 3a50482 Compare December 20, 2024 23:56
@TT-billteng TT-billteng changed the title initial check-in Add options to dump noc and tensix kernels directly to C++ Dec 28, 2024
emitKernelAsCpp(mlir::ModuleOp op, llvm::raw_ostream &os, const ttkernel::ThreadType &threadType )
{
std::vector<func::FuncOp> ops;
op->walk([&](func::FuncOp entry) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a better way to extract the nested FuncOp from ModuleOp?

//TODO: Should generalize this to read kernel type from Attribute?
void registerTensixKernelToCpp() {
TranslateFromMLIRRegistration reg(
"tensixkernel-to-cpp", "translate tensix kernel to C++",
Copy link
Contributor Author

@TT-billteng TT-billteng Dec 28, 2024

Choose a reason for hiding this comment

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

Feel like I need to change the option to ttkernel-to-cpp=noc and ttkernel-to-cpp=tensix

Is this possible? If so, where can I find an example?

Copy link
Contributor

Choose a reason for hiding this comment

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

I only know how to do this using the -opt tool, the translate MLIR tool seems much less featureful in terms of CLI. Maybe we could do --ttkernel-to-cpp-tensix and --ttkernel-to-cpp-noc? It's probably OK the way you have it too.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am more for what @nsmithtt suggested, it aligns better with the rest of the codebase:
--ttkernel-to-cpp-tensix
--ttkernel-to-cpp-noc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@TT-billteng TT-billteng changed the title Add options to dump noc and tensix kernels directly to C++ Add TTKernel to C++ target and new options to ttmlir-translate Dec 28, 2024
@TT-billteng TT-billteng changed the title Add TTKernel to C++ target and new options to ttmlir-translate Add new options to ttmlir-translate to output C++ from TTKernel dialect Dec 28, 2024
@TT-billteng TT-billteng changed the title Add new options to ttmlir-translate to output C++ from TTKernel dialect Add options to ttmlir-translate to output C++ from TTKernel dialect Dec 28, 2024
@TT-billteng TT-billteng marked this pull request as ready for review December 28, 2024 08:26
Copy link
Contributor

@nsmithtt nsmithtt left a comment

Choose a reason for hiding this comment

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

Hey Bill, sorry I was on vacation, still catching up on reviews. This looks great a couple of minor nits and comments inline, but all optional.


namespace mlir::tt::ttkernel {

enum class ThreadType : uint32_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Should we include "ttmlir/Dialect/TTKernel/IR/TTKernelOpsTypes.h" instead of doing a forward declare? Would also prevent having an enum passed by reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK let's do it since compile times aren't a concern here most likely

//TODO: Should generalize this to read kernel type from Attribute?
void registerTensixKernelToCpp() {
TranslateFromMLIRRegistration reg(
"tensixkernel-to-cpp", "translate tensix kernel to C++",
Copy link
Contributor

Choose a reason for hiding this comment

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

I only know how to do this using the -opt tool, the translate MLIR tool seems much less featureful in terms of CLI. Maybe we could do --ttkernel-to-cpp-tensix and --ttkernel-to-cpp-noc? It's probably OK the way you have it too.


namespace mlir::tt::ttkernel {

// TODO: Should generalize this to read kernel type from Attribute?
Copy link
Contributor

Choose a reason for hiding this comment

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

This is true too, we could attach an attribute to the func op via op->setDiscardableAttr.

I think the IR would like:

func.func @ttkernel_noc() -> () attributes { ttkernel.thread_type = #ttkernel.thread_type<tensix> } {

Copy link
Contributor

@sdjordjevicTT sdjordjevicTT left a comment

Choose a reason for hiding this comment

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

Just a few minor comments that I would like your follow up.

//TODO: Should generalize this to read kernel type from Attribute?
void registerTensixKernelToCpp() {
TranslateFromMLIRRegistration reg(
"tensixkernel-to-cpp", "translate tensix kernel to C++",
Copy link
Contributor

Choose a reason for hiding this comment

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

I am more for what @nsmithtt suggested, it aligns better with the rest of the codebase:
--ttkernel-to-cpp-tensix
--ttkernel-to-cpp-noc


// Translates a TTKernel operation to C++ and writes it to the given
// stream.

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 5 to 14
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Tools/mlir-translate/Translation.h"

#include "mlir/Dialect/SCF/IR/SCF.h"
#include "ttmlir/Dialect/TT/IR/TT.h"
#include "ttmlir/Dialect/TTKernel/IR/TTKernel.h"
#include "ttmlir/Dialect/TTKernel/IR/TTKernelOpsTypes.h"
#include "ttmlir/Target/TTKernel/TTKernelToCpp.h"
#include <mlir/Dialect/MemRef/IR/MemRef.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok done

@TT-billteng TT-billteng enabled auto-merge (squash) January 16, 2025 23:09
@TT-billteng TT-billteng changed the title Add options to ttmlir-translate to output C++ from TTKernel dialect #1851: Add options to ttmlir-translate to output C++ from TTKernel dialect Jan 16, 2025
@TT-billteng TT-billteng merged commit 5a4f508 into main Jan 17, 2025
19 checks passed
@TT-billteng TT-billteng deleted the bteng/shardon_emitc branch January 17, 2025 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants