From 675b4bf945e5921dc7d942358b84e0f231aa5b09 Mon Sep 17 00:00:00 2001 From: Michael Melesse Date: Mon, 27 Mar 2023 12:21:01 -0500 Subject: [PATCH 1/3] Clean up Strjoin and add Comments to GCNBuilder This is a combination of 7 commits. ignore scripts and logs use different string join add scripts remove old string implementation remove scripts match main add comment to GCNBuilder class --- .../Conversion/TritonGPUToLLVM/AsmFormat.h | 10 +-- .../Conversion/TritonGPUToLLVM/GCNAsmFormat.h | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h b/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h index 3808d96cb230..1f68458ef839 100644 --- a/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h +++ b/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h @@ -5,6 +5,7 @@ #include "triton/Dialect/Triton/IR/Dialect.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringExtras.h" #include #include @@ -17,14 +18,7 @@ using llvm::StringRef; inline std::string strJoin(llvm::ArrayRef strs, llvm::StringRef delimiter) { - std::string osStr; - llvm::raw_string_ostream os(osStr); - for (size_t i = 0; !strs.empty() && i < strs.size() - 1; ++i) - os << strs[i] << delimiter; - if (!strs.empty()) - os << strs.back(); - os.flush(); - return osStr; + return llvm::join(strs.begin(),strs.end(), delimiter); } } // namespace triton diff --git a/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h b/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h index 3dd3ddc0972d..df8432d2287f 100644 --- a/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h +++ b/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h @@ -19,6 +19,67 @@ class GCNInstr; class GCNInstrCommon; class GCNInstrExecution; +// GCNBuilder helps to manage a GCN asm program consists of one or multiple +// instructions. +// +// A helper for building an ASM program, the objective of GCNBuilder is to give +// a thin encapsulation and make the ASM code for MLIR LLVM Dialect more clear. +// Currently, several factors are introduced to reduce the need for mixing +// string and C++ if-else code. +// +// Usage: +// To create a multiplcation operation +// +// +// GCNBuilder gcnBuilder; +// unsigned bitwidth = elemTy.getIntOrFloatBitWidth(); +// +// const std::string readConstraint = "v"; +// const std::string writeConstraint = "=v"; +// auto res = gcnBuilder.newOperand(writeConstraint); +// auto lhs = gcnBuilder.newOperand(operands[0], readConstraint); +// auto rhs = gcnBuilder.newOperand(operands[1], readConstraint); +// +// create inst +// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); +// +// launch insts +// mul_inst(res, lhs, rhs); +// +// return result +// Value ret = gcnBuilder.launch(rewriter, loc, elemTy, false); +// return ret; +// To get the asm code: +// builder.dump() +// +// To get all the mlir::Value used in the GCN code, +// +// builder.getAllMlirArgs() // get {pVal, iVal, jVal, kVal} +// +// To get the string containing all the constraints with "," separated, +// builder.getConstraints() // get "=v,v,v" +// +// GCNBuilder can build a GCN asm with multiple instructions, sample code: +// +// GCNBuilder builder; +// auto &rcp = gcnBuilder.create("v_rcp")->float_op_type(bitwidth); +// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); +// +// rcp(...); +// mul_inst(...); +// This will get a GCN code with two instructions. +// +// Similar to a C function, a declared GCNInstr instance can be launched +// multiple times with different operands, e.g. +// +// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); +// mul_inst(... some operands ...); +// mul_inst(... some different operands ...); +// +// Finally, we will get a GCN code with two mov instructions. +// +// There are several derived instruction type for typical instructions, for +// example, the GCNIOInstr for ld and st instructions. struct GCNBuilder { struct Operand { std::string constraint; From 116a7d4eef7e4feaaece4f7ccde9267b9b501ea0 Mon Sep 17 00:00:00 2001 From: Rahul Batra Date: Tue, 28 Mar 2023 18:16:39 -0500 Subject: [PATCH 2/3] clang format fix --- include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h b/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h index 1f68458ef839..00ec88089037 100644 --- a/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h +++ b/include/triton/Conversion/TritonGPUToLLVM/AsmFormat.h @@ -4,8 +4,8 @@ #include "mlir/IR/Value.h" #include "triton/Dialect/Triton/IR/Dialect.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" #include #include @@ -18,7 +18,7 @@ using llvm::StringRef; inline std::string strJoin(llvm::ArrayRef strs, llvm::StringRef delimiter) { - return llvm::join(strs.begin(),strs.end(), delimiter); + return llvm::join(strs.begin(), strs.end(), delimiter); } } // namespace triton From ca53705371583311de3f8809582866046c120b35 Mon Sep 17 00:00:00 2001 From: Rahul Batra Date: Tue, 28 Mar 2023 18:26:52 -0500 Subject: [PATCH 3/3] clang format fix 2 --- .../triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h b/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h index df8432d2287f..8532a15f0891 100644 --- a/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h +++ b/include/triton/Conversion/TritonGPUToLLVM/GCNAsmFormat.h @@ -41,7 +41,8 @@ class GCNInstrExecution; // auto rhs = gcnBuilder.newOperand(operands[1], readConstraint); // // create inst -// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); +// auto &mul_inst = +// gcnBuilder.create("v_mul")->float_op_type(bitwidth); // // launch insts // mul_inst(res, lhs, rhs); @@ -63,7 +64,8 @@ class GCNInstrExecution; // // GCNBuilder builder; // auto &rcp = gcnBuilder.create("v_rcp")->float_op_type(bitwidth); -// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); +// auto &mul_inst = +// gcnBuilder.create("v_mul")->float_op_type(bitwidth); // // rcp(...); // mul_inst(...); @@ -72,9 +74,9 @@ class GCNInstrExecution; // Similar to a C function, a declared GCNInstr instance can be launched // multiple times with different operands, e.g. // -// auto &mul_inst = gcnBuilder.create("v_mul")->float_op_type(bitwidth); -// mul_inst(... some operands ...); -// mul_inst(... some different operands ...); +// auto &mul_inst = +// gcnBuilder.create("v_mul")->float_op_type(bitwidth); mul_inst(... +// some operands ...); mul_inst(... some different operands ...); // // Finally, we will get a GCN code with two mov instructions. //